Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Groovy Jabber-RPC


Wiki Markup
{link:Jabber-RPC|http://www.jabber.org/jeps/jep-0009.html}{link}


Excerpt

allows you to make XML-RPC calls using the Jabber protocol

. Groovy has a Jabber-RPC implementation which allows you to create a local Jabber-RPC server and to make calls on remote Jabber-RPC servers. Jabber servers are widely available and very easy to set up and run. The Google GTalk service uses Jabber and the Groovy Jabber-RPC package works over GTalk.
We use the excellent

Wiki Markup
{link:Smack|http://www.jivesoftware.org/smack/}{link}


Jabber library from Jive Software to handle the protocol details.

The Server

It's really easy to set up a server which provides a set of remotely callable functions.

  1. Create a server object
    Code Block
    java
    java
    import groovy.net.xmlrpc.*
    import org.jivesoftware.smack.XMPPConnection
    
        def server = new JabberRPCServer()
    

  2. Add some methods
    Code Block
    java
    java
        sever.echo = {return it}  // the closure is now named "echo" and is remotely callable
    

  3. Start the server
    Code Block
    java
    java
        def serverConnection = new XMPPConnection("talk.example.org", 5222, "example.org")
        serverConnection.login("myServerId", "myServerPassword")   // logging in as myServerId@example.org
        server.startServer(serverConnection)
    

  4. You're done!

...

  1. Create a proxy object to represent the remote server
    Code Block
    java
    java
        def clientConnection = new XMPPConnection("talk.example.org", 5222, "example.org")
        clientConnection.login("myClientId", "myClientPassword")   // logging in as myClientId@example.orgm
        def serverProxy = new JabberRPCServerProxy(clientConnection, "myServerId")
    

  2. Call the remote method via the proxy
    Code Block
    java
    java
        println severProxy.echo("Hello World!")
    

  3. As long as myClientId@example.org and myServerId@example.org are buddies then the call will be made and the result returned