Search in sources :

Example 1 with XMPPChatClient

use of org.eclipse.ecf.example.clients.XMPPChatClient in project ecf by eclipse.

the class ChatSORobotApplication method start.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
	 * IApplicationContext)
	 */
public Object start(IApplicationContext context) throws Exception {
    // 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);
    }
    // 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 chat message to targetID
    client.sendChat(targetID, msgToSend);
    // Get shared object container adapter
    ISharedObjectContainer socontainer = (ISharedObjectContainer) client.getContainer().getAdapter(ISharedObjectContainer.class);
    // 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;
}
Also used : ISharedObjectContainer(org.eclipse.ecf.core.sharedobject.ISharedObjectContainer) ID(org.eclipse.ecf.core.identity.ID) IChatID(org.eclipse.ecf.presence.im.IChatID) XMPPChatClient(org.eclipse.ecf.example.clients.XMPPChatClient)

Example 2 with XMPPChatClient

use of org.eclipse.ecf.example.clients.XMPPChatClient in project ecf by eclipse.

the class ChatRobotApplication method start.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
	 * IApplicationContext)
	 */
public Object start(IApplicationContext context) throws Exception {
    // 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);
    }
    // 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;
}
Also used : ID(org.eclipse.ecf.core.identity.ID) IChatID(org.eclipse.ecf.presence.im.IChatID) XMPPChatClient(org.eclipse.ecf.example.clients.XMPPChatClient)

Aggregations

ID (org.eclipse.ecf.core.identity.ID)2 XMPPChatClient (org.eclipse.ecf.example.clients.XMPPChatClient)2 IChatID (org.eclipse.ecf.presence.im.IChatID)2 ISharedObjectContainer (org.eclipse.ecf.core.sharedobject.ISharedObjectContainer)1