First "Hello World" Sample with SIMON 1.1.0

Interfaces

First of all, we need some interfaces which should be available on the respective remote-installation:

ServerInterface.java:

package de.root1.simon.codesample.common;

public interface ServerInterface {

   public void login(ClientCallbackInterface clientCallback);

}

ClientCallbackInterface.java:

package de.root1.simon.codesample.common;

public interface ClientCallbackInterface {

   public void callback(String text);

} 

Implementations

Then we need the implementation for the server remote methods ...

ServerInterfaceImpl.java:

package de.root1.simon.codesample.server;

import de.root1.simon.Simon;
import de.root1.simon.codesample.common.ClientCallbackInterface;
import de.root1.simon.codesample.common.ServerInterface;
import de.root1.simon.annotation.SimonRemote;

// mark this class as a remote class and export all methods known in ServerInterface
@SimonRemote(value = {ServerInterface.class}) 
public class ServerInterfaceImpl implements ServerInterface {

   private static final long serialVersionUID = 1L;

   @Override 
   public void login(ClientCallbackInterface clientCallback) {

      clientCallback.callback("This is the callback. " +
         "Your address is "+
         Simon.getRemoteInetSocketAddress(clientCallback).getAddress()+
         " and your are connected from port "+
         Simon.getRemoteInetSocketAddress(clientCallback).getPort());

   }
}

And of course the callback methods that are available on the client side have the be implemented ...

ClientCallbackImpl.java:

package de.root1.simon.codesample.client;

import de.root1.simon.annotation.SimonRemote;
import de.root1.simon.codesample.common.ClientCallbackInterface;

// mark this class as a remote class and export all methods known in ClientCallbackInterface
@SimonRemote(value = {ClientCallbackInterface.class})
public class ClientCallbackImpl implements ClientCallbackInterface {

   private static final long serialVersionUID = 1L;

   @Override 
   public void callback(String text) {

      System.out.println("This message was received from the server: "+text);

   }
} 

Server and Client

Now it's time to write the "server":

Server.java:

package de.root1.simon.codesample.server;

import java.io.IOException;
import java.net.UnknownHostException;

import de.root1.simon.Registry;
import de.root1.simon.Simon;
import de.root1.simon.exceptions.NameBindingException;

public class Server {

    public static void main(String[] args)
            throws UnknownHostException, IOException, NameBindingException {

        // create the serverobject
        ServerInterfaceImpl serverImpl = new ServerInterfaceImpl();

        // create the server's registry ...
        Registry registry = Simon.createRegistry(22222);

        // ... where we can bind the serverobject to
        registry.bind("server", serverImpl);

        System.out.println("Server up and running!");

        // some mechanism to shutdown the server should be placed here
        // this should include the following command:
        // registry.unbind("server");
        // registry.stop();
    }
}

... last but not least: the "client":

Client.java:

package de.root1.simon.codesample.client;

import de.root1.mycommonproject.ServerInterface;
import de.root1.simon.Lookup;
import de.root1.simon.exceptions.EstablishConnectionFailed;
import java.io.IOException;

import de.root1.simon.Simon;
import de.root1.simon.exceptions.LookupFailedException;

public class Client {

    public static void main(String[] args) throws IOException, LookupFailedException, EstablishConnectionFailed {

        // create a callback object
        ClientCallbackImpl clientCallbackImpl = new ClientCallbackImpl();

        // 'lookup' the server object
        Lookup nameLookup = Simon.createNameLookup("127.0.0.1", 22222);
        ServerInterface server = (ServerInterface) nameLookup.lookup("server");

        // use the serverobject as it would exist on your local machine
        server.login(clientCallbackImpl);

        // do some more stuff
        // ...

        // and finally 'release' the serverobject to release to connection to the server
        nameLookup.release(server);
    }
}

That's all folks ... Quite easy, isn't it?