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:
1 package de.root1.simon.codesample.common;
2 import de.root1.simon.SimonRemote;
3 import de.root1.simon.exceptions.SimonRemoteException;
4
5 public interface ServerInterface extends SimonRemote {
6
7 public void login(ClientCallbackInterface clientCallback) throws SimonRemoteException;
8
9 }
ClientCallbackInterface.java:
1 package de.root1.simon.codesample.common;
2
3 import de.root1.simon.SimonRemote;
4 import de.root1.simon.exceptions.SimonRemoteException;
5
6 public interface ClientCallbackInterface extends SimonRemote {
7
8 public void callback(String text) throws SimonRemoteException;
9
10 }
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.exceptions.SimonRemoteException;
5 import de.root1.simon.codesample.common.ClientCallbackInterface;
6 import de.root1.simon.codesample.common.ServerInterface;
7
8 public class ServerInterfaceImpl implements ServerInterface {
9
10 private static final long serialVersionUID = 1L;
11
12 public void login(ClientCallbackInterface clientCallback) throws SimonRemoteException {
13
14 clientCallback.callback("This is the callback. " +
15 "Your address is "+
16 Simon.getRemoteInetSocketAddress(clientCallback).getAddress()+" "+
17 "and your are connected from port "+
18 Simon.getRemoteInetSocketAddress(clientCallback).getPort());
19
20 }
21 }
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.exceptions.SimonRemoteException;
4 import de.root1.simon.codesample.common.ClientCallbackInterface;
5
6 public class ClientCallbackImpl implements ClientCallbackInterface {
7
8 private static final long serialVersionUID = 1L;
9
10 public void callback(String text) throws SimonRemoteException {
11
12 System.out.println("This message was received from the server: "+text);
13
14 }
15 }
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 }
30 }
... last but not least: the "client":
Client.java:
1 package de.root1.simon.codesample.client;
2
3 import java.io.IOException;
4
5 import de.root1.simon.Simon;
6 import de.root1.simon.exceptions.LookupFailedException;
7 import de.root1.simon.codesample.common.ServerInterface;
8
9 public class Client {
10
11 public static void main(String[] args) throws IOException, LookupFailedException {
12
13 // create a callback object
14 ClientCallbackImpl clientCallbackImpl = new ClientCallbackImpl();
15
16 // 'lookup' the server object
17 ServerInterface server = (ServerInterface) Simon.lookup("127.0.0.1", 22222, "server");
18
19 // use the serverobject as it would exist on your local machine
20 server.login(clientCallbackImpl);
21
22 // do some more stuff
23 // ...
24
25 // and finally 'release' the serverobject to release to connection to the server
26 Simon.release(server);
27 }
28 }
That's all folks ... Quite easy, isn't it?