diff options
author | slewis | 2010-02-18 19:22:36 +0000 |
---|---|---|
committer | slewis | 2010-02-18 19:22:36 +0000 |
commit | 3d37c16e3cad918e398bd85c23a622d80fe283d4 (patch) | |
tree | e5fff18b87e2d870fb13f21bf94602bf7fb321c4 /examples/bundles/org.eclipse.ecf.example.clients | |
parent | 0d2209ecf0f6f26728e99a575dcba87928c13357 (diff) | |
download | org.eclipse.ecf-3d37c16e3cad918e398bd85c23a622d80fe283d4.tar.gz org.eclipse.ecf-3d37c16e3cad918e398bd85c23a622d80fe283d4.tar.xz org.eclipse.ecf-3d37c16e3cad918e398bd85c23a622d80fe283d4.zip |
Modifications for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=303215
Diffstat (limited to 'examples/bundles/org.eclipse.ecf.example.clients')
9 files changed, 298 insertions, 318 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.clients/META-INF/MANIFEST.MF b/examples/bundles/org.eclipse.ecf.example.clients/META-INF/MANIFEST.MF index 7c20b4099..61cac0fd7 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/META-INF/MANIFEST.MF +++ b/examples/bundles/org.eclipse.ecf.example.clients/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %plugin.name Bundle-SymbolicName: org.eclipse.ecf.example.clients;singleton:=true -Bundle-Version: 1.4.0.qualifier +Bundle-Version: 2.0.0.qualifier Bundle-Vendor: %plugin.provider Bundle-Localization: plugin Export-Package: org.eclipse.ecf.example.clients, diff --git a/examples/bundles/org.eclipse.ecf.example.clients/build.properties b/examples/bundles/org.eclipse.ecf.example.clients/build.properties index 26aa06896..9330a1a0a 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/build.properties +++ b/examples/bundles/org.eclipse.ecf.example.clients/build.properties @@ -2,10 +2,12 @@ bin.includes = .,\ META-INF/,\ plugin.xml,\ about.html,\ - plugin.properties + plugin.properties,\ + products/ jars.compile.order = clients.jar,\ . -src.includes = about.html +src.includes = about.html,\ + products/ source.. = src/ output.. = bin/ jre.compilation.profile = J2SE-1.4 diff --git a/examples/bundles/org.eclipse.ecf.example.clients/products/ChatRobot.product b/examples/bundles/org.eclipse.ecf.example.clients/products/ChatRobot.product new file mode 100644 index 000000000..38fdda515 --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.example.clients/products/ChatRobot.product @@ -0,0 +1,73 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?pde version="3.5"?> + +<product application="org.eclipse.ecf.example.clients.chatrobot" useFeatures="false" includeLaunchers="true"> + + <configIni use="default"> + </configIni> + + <launcherArgs> + <programArgs>-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog sender@gmail.com senderPassword receiver@gmail.com "Hi, ECF Robot is sending you a message"</programArgs> + <vmArgs>-Xms40m -Xmx384m</vmArgs> + <vmArgsMac>-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts</vmArgsMac> + </launcherArgs> + + <launcher> + <solaris/> + <win useIco="false"> + <bmp/> + </win> + </launcher> + + <vm> + </vm> + + <plugins> + <plugin id="com.ibm.icu"/> + <plugin id="org.eclipse.core.commands"/> + <plugin id="org.eclipse.core.contenttype"/> + <plugin id="org.eclipse.core.databinding"/> + <plugin id="org.eclipse.core.databinding.observable"/> + <plugin id="org.eclipse.core.databinding.property"/> + <plugin id="org.eclipse.core.expressions"/> + <plugin id="org.eclipse.core.jobs"/> + <plugin id="org.eclipse.core.runtime"/> + <plugin id="org.eclipse.core.runtime.compatibility.registry" fragment="true"/> + <plugin id="org.eclipse.ecf"/> + <plugin id="org.eclipse.ecf.datashare"/> + <plugin id="org.eclipse.ecf.discovery"/> + <plugin id="org.eclipse.ecf.example.clients"/> + <plugin id="org.eclipse.ecf.filetransfer"/> + <plugin id="org.eclipse.ecf.identity"/> + <plugin id="org.eclipse.ecf.presence"/> + <plugin id="org.eclipse.ecf.presence.ui"/> + <plugin id="org.eclipse.ecf.provider"/> + <plugin id="org.eclipse.ecf.provider.datashare"/> + <plugin id="org.eclipse.ecf.provider.remoteservice"/> + <plugin id="org.eclipse.ecf.provider.xmpp"/> + <plugin id="org.eclipse.ecf.remoteservice"/> + <plugin id="org.eclipse.ecf.sharedobject"/> + <plugin id="org.eclipse.ecf.ssl" fragment="true"/> + <plugin id="org.eclipse.ecf.ui"/> + <plugin id="org.eclipse.equinox.app"/> + <plugin id="org.eclipse.equinox.common"/> + <plugin id="org.eclipse.equinox.concurrent"/> + <plugin id="org.eclipse.equinox.preferences"/> + <plugin id="org.eclipse.equinox.registry"/> + <plugin id="org.eclipse.help"/> + <plugin id="org.eclipse.jface"/> + <plugin id="org.eclipse.jface.databinding"/> + <plugin id="org.eclipse.jface.text"/> + <plugin id="org.eclipse.osgi"/> + <plugin id="org.eclipse.osgi.services"/> + <plugin id="org.eclipse.swt"/> + <plugin id="org.eclipse.swt.win32.win32.x86" fragment="true"/> + <plugin id="org.eclipse.text"/> + <plugin id="org.eclipse.ui"/> + <plugin id="org.eclipse.ui.forms"/> + <plugin id="org.eclipse.ui.workbench"/> + <plugin id="org.jivesoftware.smack"/> + </plugins> + + +</product> 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 deleted file mode 100644 index 494c0a452..000000000 --- a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/TestSharedObject.java +++ /dev/null @@ -1,89 +0,0 @@ -/**************************************************************************** - * 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/XMPPChatClient.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatClient.java index c550f1002..266ced44b 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatClient.java +++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatClient.java @@ -13,9 +13,7 @@ import java.util.Map; import org.eclipse.ecf.core.ContainerFactory; import org.eclipse.ecf.core.IContainer; import org.eclipse.ecf.core.identity.ID; -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.util.ECFException; import org.eclipse.ecf.presence.IIMMessageEvent; @@ -28,9 +26,6 @@ import org.eclipse.ecf.presence.im.IChatMessageSender; public class XMPPChatClient { - protected static String CONTAINER_TYPE = "ecf.xmpp.smack"; - - Namespace namespace = null; IContainer container = null; IPresenceContainerAdapter presence = null; IChatMessageSender sender = null; @@ -46,28 +41,19 @@ public class XMPPChatClient { public XMPPChatClient(IMessageReceiver receiver) { super(); - setMessageReceiver(receiver); + this.receiver = receiver; } public XMPPChatClient(IMessageReceiver receiver, IPresenceListener presenceListener) { this(receiver); - setPresenceListener(presenceListener); - } - - protected void setMessageReceiver(IMessageReceiver receiver) { - this.receiver = receiver; - } - - protected void setPresenceListener(IPresenceListener listener) { - this.presenceListener = listener; + this.presenceListener = presenceListener; } public IContainer setupContainer() throws ECFException { if (container == null) { container = ContainerFactory.getDefault().createContainer( - CONTAINER_TYPE); - namespace = container.getConnectNamespace(); + "ecf.xmpp.smack"); } return container; } @@ -76,10 +62,6 @@ public class XMPPChatClient { return container; } - protected Namespace getConnectNamespace() { - return namespace; - } - public void setupPresence() throws ECFException { if (presence == null) { presence = (IPresenceContainerAdapter) container @@ -115,19 +97,13 @@ public class XMPPChatClient { public void doConnect(String account, String password) throws ECFException { // Now connect - ID targetID = IDFactory.getDefault().createID(namespace, account); - container.connect(targetID, ConnectContextFactory - .createPasswordConnectContext(password)); userID = createID(account); + container.connect(userID, ConnectContextFactory + .createPasswordConnectContext(password)); } public ID createID(String name) { - try { - return IDFactory.getDefault().createID(namespace, name); - } catch (IDCreateException e) { - e.printStackTrace(); - return null; - } + return IDFactory.getDefault().createID(container.getConnectNamespace(), name); } public void sendChat(String jid, String msg) { @@ -135,7 +111,19 @@ public class XMPPChatClient { try { sender.sendChatMessage(createID(jid), msg); } catch (ECFException e) { - // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + /** + * @since 2.0 + */ + public void sendChat(ID targetID, String msg) { + if (sender != null) { + try { + sender.sendChatMessage(targetID, msg); + } catch (ECFException e) { e.printStackTrace(); } } @@ -146,7 +134,6 @@ public class XMPPChatClient { try { sender.sendChatMessage(createID(jid), null, IChatMessage.Type.CHAT, null, null, properties); } catch (ECFException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } 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 27571796b..1f2db9ac2 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,7 +15,6 @@ 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; @@ -99,18 +98,6 @@ public class XMPPChatRoomClient { userID = getID(account); } - /** - * @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); diff --git a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRobotApplication.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRobotApplication.java index ca3131ffe..840275d23 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRobotApplication.java +++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatRobotApplication.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2007 Composent, Inc. and others. + * Copyright (c) 2010 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 @@ -10,50 +10,70 @@ ******************************************************************************/ package org.eclipse.ecf.example.clients.applications; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import org.eclipse.ecf.core.util.ECFException; +import org.eclipse.ecf.core.identity.ID; import org.eclipse.ecf.example.clients.IMessageReceiver; import org.eclipse.ecf.example.clients.XMPPChatClient; +import org.eclipse.ecf.presence.IPresence; +import org.eclipse.ecf.presence.IPresenceListener; +import org.eclipse.ecf.presence.im.IChatID; import org.eclipse.ecf.presence.im.IChatMessage; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; -public class ChatRobotApplication implements IApplication, IMessageReceiver { - - public static final int WAIT_TIME = 10000; - public static final int WAIT_COUNT = 10; - - private boolean running = false; - private String userName; - private XMPPChatClient client; +public class ChatRobotApplication implements IApplication, IMessageReceiver, IPresenceListener { + // this map contains the account -> XMPPID. Items are added to it via the IPresenceListener.handlePresence method + private Map rosterUsers = Collections.synchronizedMap(new HashMap()); + /* * (non-Javadoc) * - * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext) + * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app. + * IApplicationContext) */ public Object start(IApplicationContext context) throws Exception { - 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) { - userName = (String) arguments[l - 4]; - String hostName = (String) arguments[l - 3]; - String password = (String) arguments[l - 2]; - String targetName = (String) arguments[l - 1]; - runRobot(hostName, password, targetName); + // process program arguments + String[] originalArgs = (String[]) context.getArguments().get( + "application.args"); + if (originalArgs.length < 3) { + System.out + .println("Parameters: <senderAccount> <senderPassword> <targetAccount> [<message>]. e.g. sender@gmail.com senderpassword receiver@gmail.com \"Hello there\""); + return new Integer(-1); + } + String message = null; + if (originalArgs.length > 3) message = originalArgs[3]; + + // Create client + XMPPChatClient client = new XMPPChatClient(this,this); + // connect + client.connect(originalArgs[0], originalArgs[1]); + + // Wait for 5s for the roster/presence information to be received + final Object lock = new Object(); + synchronized (lock) { + lock.wait(5000); + } + + // Get desired user ID from rosterUsers map. This is just looking for a user that's active and on our contacts list + ID targetID = (ID) rosterUsers.get(originalArgs[2]); + if (targetID == null) { + System.out.println("target user="+originalArgs[2]+" is not on active on your contacts list. Cannot send message to this user"); return new Integer(0); } - System.out - .println("Usage: pass in four arguments (username, hostname, password, targetIMUser)"); - return new Integer(-1); + // Construct message + String msgToSend = (message==null)?"Hi, I'm an ECF chat robot.":message; + System.out.println("ECF chat robot example sending to targetAccount=" + originalArgs[2] + " message="+msgToSend); + + // Send message to targetID + client.sendChat(targetID, msgToSend); + + // Close up nicely and return + client.close(); + return IApplication.EXIT_OK; } /* @@ -64,36 +84,19 @@ public class ChatRobotApplication implements IApplication, IMessageReceiver { public void stop() { } - private void runRobot(String hostName, String password, String targetIMUser) - throws ECFException, Exception, InterruptedException { - // Create client - client = new XMPPChatClient(this); - client.setupContainer(); - client.setupPresence(); - - // Then connect - String connectTarget = userName + "@" + hostName; - - client.doConnect(connectTarget, password); - - System.out.println("ECF chat robot (" + connectTarget + ")"); - // Send initial message to target user - client.sendChat(targetIMUser, "Hi, I'm an ECF chat robot."); - - running = true; - int count = 0; - // Loop ten times and send ten 'hello there' messages to targetIMUser - // out-of-band via XMPP message properties - while (running && count++ < WAIT_COUNT) { - wait(WAIT_TIME); - Map properties = new HashMap(); - properties.put("message", "howdy"); - client.sendProperties(targetIMUser, properties); - } + public void handleMessage(IChatMessage chatMessage) { + System.out.println("handleMessage(" + chatMessage + ")"); } - public synchronized void handleMessage(IChatMessage chatMessage) { - System.out.println("handleMessage(" + chatMessage + ")"); + /** + * @since 2.0 + */ + public void handlePresence(ID fromID, IPresence presence) { + System.out.println("handlePresence fromID="+fromID+" presence="+presence); + IChatID fromChatID = (IChatID) fromID.getAdapter(IChatID.class); + if (fromChatID != null) { + rosterUsers.put(fromChatID.getUsername() + "@" + fromChatID.getHostname(), fromID); + } } } 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 5004f25dd..4072c0003 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004 Composent, Inc. All rights reserved. This + * Copyright (c) 2010 Composent, Inc. 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 @@ -31,71 +31,67 @@ import org.eclipse.equinox.app.IApplicationContext; */ public class ChatRoomRobotApplication implements IApplication, IMessageReceiver, IIMMessageListener { + private String senderAccount; + private Object lock = new Object(); + private boolean done = false; private IChatRoomMessageSender sender; - - private boolean running = false; - - private String userName; - + public Object start(IApplicationContext context) throws Exception { - Object[] args = context.getArguments().values().toArray(); - while (args[0] instanceof Object[]) - args = (Object[]) args[0]; - 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]; - 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); + // process program arguments + String[] originalArgs = (String[]) context.getArguments().get( + "application.args"); + if (originalArgs.length < 4) { + System.out + .println("Parameters: <senderAccount> <senderPassword> <chatroomname>. e.g. sender@gmail.com senderpassword mychatroom"); + return new Integer(-1); } - System.out.println("Usage: pass in four arguments (username, hostname, password, roomname)"); - return new Integer(-1); - } - - public void stop() { - } - - private synchronized void runRobot(String hostName, String password, String roomName) throws ECFException, Exception, InterruptedException { + senderAccount = originalArgs[0]; + // Create client final XMPPChatRoomClient client = new XMPPChatRoomClient(this); - - // Then connect - final String connectTarget = userName + "@" + hostName; - - client.connect(connectTarget, password); - - final IChatRoomContainer room = client.createChatRoom(roomName); - room.connect(client.getChatRoomInfo().getRoomID(), null); - - 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; + // connect to senderAccount using senderPassword + client.connect(senderAccount, originalArgs[1]); + // get chat room + final IChatRoomContainer chatRoomContainer = client.createChatRoom(originalArgs[2]); + // join/connect to chat room + chatRoomContainer.connect(client.getChatRoomInfo().getRoomID(), null); + + System.out.println("ECF chat room robot sender=" + senderAccount + " Connected to room: " + client.getChatRoomInfo().getRoomID().getName()); + // Add message listener to chat room + chatRoomContainer.addMessageListener(this); + // Get chat room message sender + sender = chatRoomContainer.getChatRoomMessageSender(); sender.sendMessage("Hi, I'm a robot. To get rid of me, send me a direct message."); - while (running) { - wait(); + synchronized (lock) { + while (!done) { + lock.wait(); + } } + return IApplication.EXIT_OK; } - public synchronized void handleMessage(IChatMessage chatMessage) { + public void handleMessage(IChatMessage chatMessage) { // direct message try { sender.sendMessage("gotta run"); } catch (final ECFException e) { e.printStackTrace(); } - running = false; - notifyAll(); + synchronized (lock) { + done = true; + lock.notify(); + } + } + + public void handleMessageEvent(IIMMessageEvent messageEvent) { + if (messageEvent instanceof IChatRoomMessageEvent) { + final IChatRoomMessage m = ((IChatRoomMessageEvent) messageEvent).getChatRoomMessage(); + handleChatRoomMessage(m.getFromID(), m.getMessage()); + } } - public void handleChatRoomMessage(ID fromID, String messageBody) { + private void handleChatRoomMessage(ID fromID, String messageBody) { // message in chat room - if (fromID.getName().startsWith(userName + "@")) { + if (fromID.getName().indexOf(senderAccount) != -1) { // my own message, don't respond return; } @@ -112,10 +108,10 @@ public class ChatRoomRobotApplication implements IApplication, IMessageReceiver, } } - public void handleMessageEvent(IIMMessageEvent messageEvent) { - if (messageEvent instanceof IChatRoomMessageEvent) { - final IChatRoomMessage m = ((IChatRoomMessageEvent) messageEvent).getChatRoomMessage(); - handleChatRoomMessage(m.getFromID(), m.getMessage()); + public void stop() { + synchronized (lock) { + done = true; + lock.notify(); } } diff --git a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatSORobotApplication.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatSORobotApplication.java index 0c1d099ef..2abd1c08b 100644 --- a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatSORobotApplication.java +++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/applications/ChatSORobotApplication.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2007 Composent, Inc. and others. + * Copyright (c) 2010 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 @@ -10,108 +10,129 @@ ******************************************************************************/ package org.eclipse.ecf.example.clients.applications; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.ecf.core.identity.ID; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.sharedobject.ISharedObjectContainer; -import org.eclipse.ecf.core.util.ECFException; import org.eclipse.ecf.example.clients.IMessageReceiver; import org.eclipse.ecf.example.clients.XMPPChatClient; +import org.eclipse.ecf.presence.IPresence; +import org.eclipse.ecf.presence.IPresenceListener; +import org.eclipse.ecf.presence.im.IChatID; import org.eclipse.ecf.presence.im.IChatMessage; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; -public class ChatSORobotApplication implements IApplication, IMessageReceiver { - - public static final int WAIT_TIME = 10000; - public static final int WAIT_COUNT = 10; +public class ChatSORobotApplication implements IApplication, IMessageReceiver, + IPresenceListener { - private boolean running = false; - private String userName; - private XMPPChatClient client; - private TrivialSharedObject sharedObject = null; + // this map contains the account -> XMPPID. Items are added to it via the + // IPresenceListener.handlePresence method + private Map rosterUsers = Collections.synchronizedMap(new HashMap()); /* * (non-Javadoc) * - * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext) + * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app. + * IApplicationContext) */ public Object start(IApplicationContext context) throws Exception { - 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) { - userName = (String) arguments[l - 4]; - String hostName = (String) arguments[l - 3]; - String password = (String) arguments[l - 2]; - String targetName = (String) arguments[l - 1]; - runRobot(hostName, password, targetName); - return new Integer(0); + // process program arguments + String[] originalArgs = (String[]) context.getArguments().get( + "application.args"); + if (originalArgs.length < 3) { + System.out + .println("Parameters: <senderAccount> <senderPassword> <targetAccount> [<message>]. e.g. sender@gmail.com senderpassword receiver@gmail.com \"Hello there\""); + return new Integer(-1); } + String message = null; + if (originalArgs.length > 3) + message = originalArgs[3]; - System.out - .println("Usage: pass in four arguments (username, hostname, password, targetIMUser)"); - return new Integer(-1); - } + // Create client + XMPPChatClient client = new XMPPChatClient(this, this); - /* - * (non-Javadoc) - * - * @see org.eclipse.equinox.app.IApplication#stop() - */ - public void stop() { - } - - private void runRobot(String hostName, String password, String targetIMUser) - throws ECFException, Exception, InterruptedException { - // Create client and connect to host - client = new XMPPChatClient(this); - // Setup container - client.setupContainer(); - // Setup presence adapter - client.setupPresence(); - // Create and add shared object - createSharedObject(); - - // Then connect - String connectTarget = userName + "@" + hostName; - - client.doConnect(connectTarget, password); - - System.out.println("ECF so chat robot (" + connectTarget + ")"); + // connect + client.connect(originalArgs[0], originalArgs[1]); - // Send initial message to target user - client.sendChat(targetIMUser, "Hi, I'm an IM robot"); + // Wait for 5s for the roster/presence information to be received + final Object lock = new Object(); + synchronized (lock) { + lock.wait(5000); + } - running = true; - int count = 0; - // Loop ten times and send ten 'hello there' messages to targetIMUser - // out-of-band via shared object - while (running && count++ < WAIT_COUNT) { - // Send shared object message - sharedObject.sendMessageTo(client.createID(targetIMUser), - "hello from " + userName); - wait(WAIT_TIME); + // Get desired user ID from rosterUsers map. This is just looking for a + // user that's active and on our contacts list + ID targetID = (ID) rosterUsers.get(originalArgs[2]); + if (targetID == null) { + System.out + .println("target user=" + + originalArgs[2] + + " is not on active on your contacts list. Cannot send message to this user"); + return new Integer(0); } - } + // Construct message + String msgToSend = (message == null) ? "Hi, I'm an ECF chat robot." + : message; + System.out.println("ECF chat robot example sending to targetAccount=" + + originalArgs[2] + " message=" + msgToSend); - protected void createSharedObject() throws ECFException { + // Send chat message to targetID + client.sendChat(targetID, msgToSend); + + // Get shared object container adapter ISharedObjectContainer socontainer = (ISharedObjectContainer) client .getContainer().getAdapter(ISharedObjectContainer.class); - // Create TrivialSharedObject - sharedObject = new TrivialSharedObject(); - // Add shared object to container + // Create and add shared object to container + TrivialSharedObject sharedObject = new TrivialSharedObject(); socontainer.getSharedObjectManager().addSharedObject( IDFactory.getDefault().createStringID( TrivialSharedObject.class.getName()), sharedObject, null); + + // Send messages via shared object...and wait a short while before sending the next one + int count = 0; + synchronized (lock) { + while (count++ < 5) { + // Send shared object message + sharedObject.sendMessageTo(targetID, "hello from " + + originalArgs[0]+" via shared object"); + lock.wait(5000); + } + + } + + // Close up nicely + client.close(); + return IApplication.EXIT_OK; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.equinox.app.IApplication#stop() + */ + public void stop() { } - public synchronized void handleMessage(IChatMessage chatMessage) { + public void handleMessage(IChatMessage chatMessage) { System.out.println("handleMessage(" + chatMessage + ")"); } + /** + * @since 2.0 + */ + public void handlePresence(ID fromID, IPresence presence) { + System.out.println("handlePresence fromID=" + fromID + " presence=" + + presence); + IChatID fromChatID = (IChatID) fromID.getAdapter(IChatID.class); + if (fromChatID != null) { + rosterUsers.put(fromChatID.getUsername() + "@" + + fromChatID.getHostname(), fromID); + } + } + } |