First "Hello World" Sample with SIMON 1.0.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;
import de.root1.simon.SimonRemote;
import de.root1.simon.exceptions.SimonRemoteException;

public interface ServerInterface extends SimonRemote {

   public void login(ClientCallbackInterface clientCallback) throws SimonRemoteException;

}

ClientCallbackInterface.java:

package de.root1.simon.codesample.common;

import de.root1.simon.SimonRemote;
import de.root1.simon.exceptions.SimonRemoteException;

public interface ClientCallbackInterface extends SimonRemote {

   public void callback(String text) throws SimonRemoteException;

} 

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.exceptions.SimonRemoteException;
import de.root1.simon.codesample.common.ClientCallbackInterface;
import de.root1.simon.codesample.common.ServerInterface;

public class ServerInterfaceImpl implements ServerInterface {

   private static final long serialVersionUID = 1L;

   public void login(ClientCallbackInterface clientCallback) throws SimonRemoteException {

      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.exceptions.SimonRemoteException;
import de.root1.simon.codesample.common.ClientCallbackInterface;

public class ClientCallbackImpl implements ClientCallbackInterface {

   private static final long serialVersionUID = 1L;

   public void callback(String text) throws SimonRemoteException {

      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");
   }
}

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

Client.java:

package de.root1.simon.codesample.client;

import java.io.IOException;

import de.root1.simon.Simon;
import de.root1.simon.exceptions.LookupFailedException;
import de.root1.simon.codesample.common.ServerInterface;

public class Client {

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

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

      // 'lookup' the server object
      ServerInterface server = (ServerInterface) Simon.lookup("127.0.0.1", 22222, "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
      Simon.release(server);
   }
}

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