diff options
author | slewis | 2008-01-11 07:57:19 +0000 |
---|---|---|
committer | slewis | 2008-01-11 07:57:19 +0000 |
commit | 1890ded39fb01834301d540b383180e3b65438e3 (patch) | |
tree | bb9b78dfb715d0ea9fc2f2048c3fd15460aa8781 /examples/bundles | |
parent | e4961ca5b9f8d29de2b23a3e89884dbc31e4e4f3 (diff) | |
download | org.eclipse.ecf-1890ded39fb01834301d540b383180e3b65438e3.tar.gz org.eclipse.ecf-1890ded39fb01834301d540b383180e3b65438e3.tar.xz org.eclipse.ecf-1890ded39fb01834301d540b383180e3b65438e3.zip |
Fixed startup logic
Diffstat (limited to 'examples/bundles')
3 files changed, 131 insertions, 42 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/TestSharedObject.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/TestSharedObject.java new file mode 100644 index 000000000..494c0a452 --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/TestSharedObject.java @@ -0,0 +1,89 @@ +/**************************************************************************** + * Copyright (c) 2007 Composent, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Composent, Inc. - initial API and implementation + *****************************************************************************/ + +package org.eclipse.ecf.example.clients; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.sharedobject.BaseSharedObject; +import org.eclipse.ecf.core.sharedobject.ReplicaSharedObjectDescription; +import org.eclipse.ecf.core.sharedobject.SharedObjectInitException; +import org.eclipse.ecf.core.sharedobject.events.ISharedObjectActivatedEvent; +import org.eclipse.ecf.core.util.Event; +import org.eclipse.ecf.core.util.IEventProcessor; + +/** + * + */ +public class TestSharedObject extends BaseSharedObject { + + public static final String NAME_PROPERTY = "name"; + + String name; + + /** + * Primary constructor + * @param name the name to say hello to + */ + public TestSharedObject(String name) { + this.name = name; + Assert.isNotNull(name); + } + + /** + * Replica constructor (null constructor) + */ + public TestSharedObject() { + super(); + } + + /* (non-Javadoc) + * @see org.eclipse.ecf.core.sharedobject.BaseSharedObject#initialize() + */ + protected void initialize() throws SharedObjectInitException { + super.initialize(); + if (isPrimary()) { + // If primary, then add an event processor that handles activated + // event by replicating to all current remote containers + addEventProcessor(new IEventProcessor() { + public boolean processEvent(Event event) { + if (event instanceof ISharedObjectActivatedEvent) { + // If we've been activated, are primary and are connected + // then replicate to all remotes + // This calls the getReplicaDescription method below + if (isPrimary() && isConnected()) { + TestSharedObject.this.replicateToRemoteContainers(null); + } + } + return false; + } + }); + System.out.println("Primary(" + getContext().getLocalContainerID() + ") says Hello " + name); + } else { + // This is a replica, so initialize the name from property + name = (String) getConfig().getProperties().get(NAME_PROPERTY); + System.out.println("Replica(" + getContext().getLocalContainerID() + ") says Hello " + name); + } + } + + /* (non-Javadoc) + * @see org.eclipse.ecf.core.sharedobject.BaseSharedObject#getReplicaDescription(org.eclipse.ecf.core.identity.ID) + */ + protected ReplicaSharedObjectDescription getReplicaDescription(ID receiver) { + // Put primary state into properties and include in replica description + final Map properties = new HashMap(); + properties.put(NAME_PROPERTY, name); + return new ReplicaSharedObjectDescription(this.getClass(), getConfig().getSharedObjectID(), getConfig().getHomeContainerID(), properties); + } +} diff --git a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatRoomClient.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatRoomClient.java index 4f033ccce..27571796b 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatRoomClient.java +++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatRoomClient.java @@ -15,6 +15,7 @@ import org.eclipse.ecf.core.identity.IDCreateException; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.identity.Namespace; import org.eclipse.ecf.core.security.ConnectContextFactory; +import org.eclipse.ecf.core.sharedobject.ISharedObjectContainer; import org.eclipse.ecf.core.util.ECFException; import org.eclipse.ecf.presence.IIMMessageEvent; import org.eclipse.ecf.presence.IIMMessageListener; @@ -53,8 +54,7 @@ public class XMPPChatRoomClient { protected IContainer createContainer() throws ECFException { // Create container - container = ContainerFactory.getDefault().createContainer( - CONTAINER_TYPE); + container = ContainerFactory.getDefault().createContainer(CONTAINER_TYPE); namespace = container.getConnectNamespace(); return container; } @@ -69,18 +69,15 @@ public class XMPPChatRoomClient { protected void setupPresenceAdapter() { // Get presence adapter off of container - presence = (IPresenceContainerAdapter) container - .getAdapter(IPresenceContainerAdapter.class); + presence = (IPresenceContainerAdapter) container.getAdapter(IPresenceContainerAdapter.class); // Get sender interface sender = presence.getChatManager().getChatMessageSender(); // Setup message requestListener to handle incoming messages presence.getChatManager().addMessageListener(new IIMMessageListener() { public void handleMessageEvent(IIMMessageEvent messageEvent) { if (messageEvent instanceof IChatMessageEvent) { - IChatMessage m = ((IChatMessageEvent) messageEvent) - .getChatMessage(); - receiver - .handleMessage(m); + final IChatMessage m = ((IChatMessageEvent) messageEvent).getChatMessage(); + receiver.handleMessage(m); } } }); @@ -94,16 +91,27 @@ public class XMPPChatRoomClient { createContainer(); setupPresenceAdapter(); // create target id - ID targetID = IDFactory.getDefault().createID(getNamespace(), account); + final ID targetID = IDFactory.getDefault().createID(getNamespace(), account); // Now connect - getContainer().connect(targetID, - ConnectContextFactory.createPasswordConnectContext(password)); + getContainer().connect(targetID, ConnectContextFactory.createPasswordConnectContext(password)); + // Get a local ID for user account userID = getID(account); } - public IChatRoomContainer createChatRoom(String chatRoomName) - throws Exception { + /** + * @throws ECFException + * + */ + public void createSharedObject() throws ECFException { + final ISharedObjectContainer socontainer = (ISharedObjectContainer) chatroom.getAdapter(ISharedObjectContainer.class); + final ID sharedObjectID = IDFactory.getDefault().createGUID(); + if (socontainer != null) { + socontainer.getSharedObjectManager().addSharedObject(sharedObjectID, new TestSharedObject("testsharedobject"), null); + } + } + + public IChatRoomContainer createChatRoom(String chatRoomName) throws Exception { // Create chat room container from manager roomInfo = presence.getChatRoomManager().getChatRoomInfo(chatRoomName); chatroom = roomInfo.createChatRoomContainer(); @@ -117,7 +125,7 @@ public class XMPPChatRoomClient { private ID getID(String name) { try { return IDFactory.getDefault().createID(namespace, name); - } catch (IDCreateException e) { + } catch (final IDCreateException e) { e.printStackTrace(); return null; } @@ -127,7 +135,7 @@ public class XMPPChatRoomClient { if (sender != null) { try { sender.sendChatMessage(getID(jid), msg); - } catch (ECFException e) { + } catch (final ECFException e) { e.printStackTrace(); } } diff --git a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRoomRobotApplication.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRoomRobotApplication.java index 56683e048..5004f25dd 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRoomRobotApplication.java +++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRoomRobotApplication.java @@ -29,8 +29,7 @@ import org.eclipse.equinox.app.IApplicationContext; * and make sure you have all required plug-ins. * */ -public class ChatRoomRobotApplication implements IApplication, - IMessageReceiver, IIMMessageListener { +public class ChatRoomRobotApplication implements IApplication, IMessageReceiver, IIMMessageListener { private IChatRoomMessageSender sender; @@ -42,48 +41,42 @@ public class ChatRoomRobotApplication implements IApplication, Object[] args = context.getArguments().values().toArray(); while (args[0] instanceof Object[]) args = (Object[]) args[0]; - Object[] arguments = (Object[]) args; - int l = arguments.length; - if (arguments[l - 1] instanceof String - && arguments[l - 2] instanceof String - && arguments[l - 3] instanceof String - && arguments[l - 4] instanceof String) { + final Object[] arguments = args; + final int l = arguments.length; + if (arguments[l - 1] instanceof String && arguments[l - 2] instanceof String && arguments[l - 3] instanceof String && arguments[l - 4] instanceof String) { userName = (String) arguments[l - 4]; - String hostName = (String) arguments[l - 3]; - String password = (String) arguments[l - 2]; - String roomName = (String) arguments[l - 1]; + final String hostName = (String) arguments[l - 3]; + final String password = (String) arguments[l - 2]; + final String roomName = (String) arguments[l - 1]; runRobot(hostName, password, roomName); return new Integer(0); } - System.out - .println("Usage: pass in four arguments (username, hostname, password, roomname)"); + System.out.println("Usage: pass in four arguments (username, hostname, password, roomname)"); return new Integer(-1); } public void stop() { } - private void runRobot(String hostName, String password, String roomName) - throws ECFException, Exception, InterruptedException { - XMPPChatRoomClient client = new XMPPChatRoomClient(this); + private synchronized void runRobot(String hostName, String password, String roomName) throws ECFException, Exception, InterruptedException { + final XMPPChatRoomClient client = new XMPPChatRoomClient(this); // Then connect - String connectTarget = userName + "@" + hostName; + final String connectTarget = userName + "@" + hostName; client.connect(connectTarget, password); - IChatRoomContainer room = client.createChatRoom(roomName); + final IChatRoomContainer room = client.createChatRoom(roomName); room.connect(client.getChatRoomInfo().getRoomID(), null); - System.out.println("ECF chat room robot (" + connectTarget - + "). Connected to room: " - + client.getChatRoomInfo().getRoomID().getName()); + client.createSharedObject(); + + System.out.println("ECF chat room robot (" + connectTarget + "). Connected to room: " + client.getChatRoomInfo().getRoomID().getName()); room.addMessageListener(this); sender = room.getChatRoomMessageSender(); running = true; - sender - .sendMessage("Hi, I'm a robot. To get rid of me, send me a direct message."); + sender.sendMessage("Hi, I'm a robot. To get rid of me, send me a direct message."); while (running) { wait(); } @@ -93,7 +86,7 @@ public class ChatRoomRobotApplication implements IApplication, // direct message try { sender.sendMessage("gotta run"); - } catch (ECFException e) { + } catch (final ECFException e) { e.printStackTrace(); } running = false; @@ -114,15 +107,14 @@ public class ChatRoomRobotApplication implements IApplication, } else { sender.sendMessage("'s up?"); } - } catch (ECFException e) { + } catch (final ECFException e) { e.printStackTrace(); } } public void handleMessageEvent(IIMMessageEvent messageEvent) { if (messageEvent instanceof IChatRoomMessageEvent) { - IChatRoomMessage m = ((IChatRoomMessageEvent) messageEvent) - .getChatRoomMessage(); + final IChatRoomMessage m = ((IChatRoomMessageEvent) messageEvent).getChatRoomMessage(); handleChatRoomMessage(m.getFromID(), m.getMessage()); } } |