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:

1 package de.root1.simon.codesample.common;
2 
3 public interface ServerInterface {
4 
5    public void login(ClientCallbackInterface clientCallback);
6 
7 }

ClientCallbackInterface.java:

1 package de.root1.simon.codesample.common;
2 
3 public interface ClientCallbackInterface {
4 
5    public void callback(String text);
6 
7 } 

Implementations

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

ServerInterfaceImpl.java:

 1 package de.root1.simon.codesample.server;
 2 
 3 import de.root1.simon.Simon;
 4 import de.root1.simon.codesample.common.ClientCallbackInterface;
 5 import de.root1.simon.codesample.common.ServerInterface;
 6 import de.root1.simon.annotation.SimonRemote;
 7 
 8 // mark this class as a remote class and export all methods known in ServerInterface
 9 @SimonRemote(value = {ServerInterface.class}) 
10 public class ServerInterfaceImpl implements ServerInterface {
11 
12    private static final long serialVersionUID = 1L;
13 
14    @Override 
15    public void login(ClientCallbackInterface clientCallback) {
16 
17       clientCallback.callback("This is the callback. " +
18          "Your address is "+
19          Simon.getRemoteInetSocketAddress(clientCallback).getAddress()+
20          " and your are connected from port "+
21          Simon.getRemoteInetSocketAddress(clientCallback).getPort());
22 
23    }
24 }

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

ClientCallbackImpl.java:

 1 package de.root1.simon.codesample.client;
 2 
 3 import de.root1.simon.annotation.SimonRemote;
 4 import de.root1.simon.codesample.common.ClientCallbackInterface;
 5 
 6 // mark this class as a remote class and export all methods known in ClientCallbackInterface
 7 @SimonRemote(value = {ClientCallbackInterface.class})
 8 public class ClientCallbackImpl implements ClientCallbackInterface {
 9 
10    private static final long serialVersionUID = 1L;
11 
12    @Override 
13    public void callback(String text) {
14 
15       System.out.println("This message was received from the server: "+text);
16 
17    }
18 } 

Server and Client

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

Server.java:

 1 package de.root1.simon.codesample.server;
 2 
 3 import java.io.IOException;
 4 import java.net.UnknownHostException;
 5 
 6 import de.root1.simon.Registry;
 7 import de.root1.simon.Simon;
 8 import de.root1.simon.exceptions.NameBindingException;
 9 
10 public class Server {
11 
12     public static void main(String[] args)
13             throws UnknownHostException, IOException, NameBindingException {
14 
15         // create the serverobject
16         ServerInterfaceImpl serverImpl = new ServerInterfaceImpl();
17 
18         // create the server's registry ...
19         Registry registry = Simon.createRegistry(22222);
20 
21         // ... where we can bind the serverobject to
22         registry.bind("server", serverImpl);
23 
24         System.out.println("Server up and running!");
25 
26         // some mechanism to shutdown the server should be placed here
27         // this should include the following command:
28         // registry.unbind("server");
29         // registry.stop();
30     }
31 }

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

Client.java:

 1 package de.root1.simon.codesample.client;
 2 
 3 import de.root1.mycommonproject.ServerInterface;
 4 import de.root1.simon.Lookup;
 5 import de.root1.simon.exceptions.EstablishConnectionFailed;
 6 import java.io.IOException;
 7 
 8 import de.root1.simon.Simon;
 9 import de.root1.simon.exceptions.LookupFailedException;
10 
11 public class Client {
12 
13     public static void main(String[] args) throws IOException, LookupFailedException, EstablishConnectionFailed {
14 
15         // create a callback object
16         ClientCallbackImpl clientCallbackImpl = new ClientCallbackImpl();
17 
18         // 'lookup' the server object
19         Lookup nameLookup = Simon.createNameLookup("127.0.0.1", 22222);
20         ServerInterface server = (ServerInterface) nameLookup.lookup("server");
21 
22         // use the serverobject as it would exist on your local machine
23         server.login(clientCallbackImpl);
24 
25         // do some more stuff
26         // ...
27 
28         // and finally 'release' the serverobject to release to connection to the server
29         nameLookup.release(server);
30     }
31 }

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