Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbbokowski2006-02-03 06:30:55 +0000
committerbbokowski2006-02-03 06:30:55 +0000
commitc53188039e01e344ce8a7a62687f9c232819329f (patch)
tree2890b0fa09b18373f8749bd57f194e2fe374659f /examples
parentf17cbe0afc373c0597b74ac467901dea27f7b8be (diff)
downloadorg.eclipse.ecf-c53188039e01e344ce8a7a62687f9c232819329f.tar.gz
org.eclipse.ecf-c53188039e01e344ce8a7a62687f9c232819329f.tar.xz
org.eclipse.ecf-c53188039e01e344ce8a7a62687f9c232819329f.zip
Added a simple robot application that connects to an XMPP chat room.
Diffstat (limited to 'examples')
-rw-r--r--examples/bundles/org.eclipse.ecf.example.clients/plugin.xml8
-rw-r--r--examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/RobotApplication.java103
-rw-r--r--examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/XMPPChatClient.java6
3 files changed, 115 insertions, 2 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.clients/plugin.xml b/examples/bundles/org.eclipse.ecf.example.clients/plugin.xml
index 8e49b5212..627e3f6c2 100644
--- a/examples/bundles/org.eclipse.ecf.example.clients/plugin.xml
+++ b/examples/bundles/org.eclipse.ecf.example.clients/plugin.xml
@@ -1,5 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
+ <extension
+ id="robot"
+ name="Example Robot"
+ point="org.eclipse.core.runtime.applications">
+ <application>
+ <run class="org.eclipse.ecf.example.clients.RobotApplication"/>
+ </application>
+ </extension>
</plugin>
diff --git a/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/RobotApplication.java b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/RobotApplication.java
new file mode 100644
index 000000000..0f382acd9
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.clients/src/org/eclipse/ecf/example/clients/RobotApplication.java
@@ -0,0 +1,103 @@
+package org.eclipse.ecf.example.clients;
+
+import java.io.IOException;
+
+import org.eclipse.core.runtime.IPlatformRunnable;
+import org.eclipse.ecf.core.identity.ID;
+import org.eclipse.ecf.core.util.ECFException;
+import org.eclipse.ecf.presence.IMessageListener;
+import org.eclipse.ecf.presence.chat.IChatMessageSender;
+import org.eclipse.ecf.presence.chat.IChatRoomContainer;
+
+/**
+ * To be started as an application. Go to Run->Run..., create a new Eclipse
+ * Application, select org.eclipse.ecf.example.clients.robot as the application
+ * and make sure you have all required plug-ins.
+ *
+ */
+public class RobotApplication implements IPlatformRunnable, IMessageReceiver,
+ IMessageListener {
+
+ private IChatMessageSender sender;
+
+ private boolean running = false;
+
+ private String userName;
+
+ public synchronized Object run(Object args) throws Exception {
+ if (args instanceof Object[]) {
+ Object[] arguments = (Object[]) args;
+ while (arguments.length > 0 && arguments[0] instanceof String
+ && ((String) arguments[0]).startsWith("-")) {
+ System.arraycopy(arguments, 1,
+ arguments = new Object[arguments.length - 1], 0,
+ arguments.length);
+ }
+ if (arguments.length == 4) {
+ if (arguments[0] instanceof String
+ && arguments[1] instanceof String
+ && arguments[2] instanceof String
+ && arguments[3] instanceof String) {
+ userName = (String) arguments[0];
+ String hostName = (String) arguments[1];
+ String password = (String) arguments[2];
+ String roomName = (String) arguments[3];
+ runRobot(hostName, password, roomName);
+ return new Integer(0);
+ }
+ }
+ }
+ System.out
+ .println("Usage: pass in four arguments (username, hostname, password, roomname)");
+ return new Integer(-1);
+ }
+
+ private void runRobot(String hostName, String password, String roomName)
+ throws ECFException, Exception, InterruptedException {
+ XMPPChatClient client = new XMPPChatClient(this);
+ client.connect(userName + "@" + hostName, password);
+ IChatRoomContainer room = client.connectChatRoom(userName, hostName,
+ roomName);
+ System.out.println(room.getConnectedID().getName());
+ room.addMessageListener(this);
+ sender = room.getChatMessageSender();
+ running = true;
+ sender
+ .sendMessage("Hi, I'm a robot. To get rid of me, send me a direct message.");
+ while (running) {
+ wait();
+ }
+ }
+
+ public synchronized void handleMessage(String from, String msg) {
+ // direct message
+ try {
+ sender.sendMessage("gotta run");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ running = false;
+ notifyAll();
+ }
+
+ public void handleMessage(ID fromID, ID toID, Type type, String subject,
+ String messageBody) {
+ // message in chat room
+ if (fromID.getName().startsWith(userName + "@")) {
+ // my own message, don't respond
+ return;
+ }
+ try {
+ if (messageBody.indexOf("e") != -1) {
+ sender.sendMessage("kewl");
+ } else if (messageBody.indexOf("s") != -1) {
+ sender.sendMessage(";-)");
+ } else {
+ sender.sendMessage("'s up?");
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
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 4a78d7c4a..54a48794e 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
@@ -59,13 +59,15 @@ public class XMPPChatClient {
userID = getID(account);
}
- protected void connectChatRoom(String chatRoomID) throws Exception {
+ public IChatRoomContainer connectChatRoom(String username, String hostname, String chatRoomID) throws Exception {
chatmanager = presence.getChatRoomManager();
chatroom = chatmanager.createChatRoomContainer();
socontainer = (ISharedObjectContainer) chatroom.getAdapter(ISharedObjectContainer.class);
- ID targetChatID = IDFactory.getDefault().createID(chatroom.getConnectNamespace(),chatRoomID);
+ ID targetChatID = IDFactory.getDefault().createID(chatroom.getConnectNamespace(), new Object[] {username,hostname,null,chatRoomID,username});
chatroom.connect(targetChatID, null);
+ return chatroom;
}
+
private ID getID(String name) {
try {
return IDFactory.getDefault().createID(namespace, name);

Back to the top