diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-01-31 02:19:56 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-01-31 02:19:56 +0000 |
commit | e8b3b7c2596b2b0f394b54c6732830d95ea17bcd (patch) | |
tree | 0c404402e7a7a67a4cf08c920e3c7bef9ca9be12 | |
parent | 18afaec076602d242161cbf907d0735f99756b33 (diff) | |
download | NetworkCoursework-e8b3b7c2596b2b0f394b54c6732830d95ea17bcd.tar.gz NetworkCoursework-e8b3b7c2596b2b0f394b54c6732830d95ea17bcd.zip |
Successfully transmitted packets over localhost, now have to clean up
-rwxr-xr-x | rmi/RMIClient.java | 30 | ||||
-rwxr-xr-x | rmi/RMIServer.java | 54 | ||||
-rwxr-xr-x | rmiclient.sh | 1 |
3 files changed, 70 insertions, 15 deletions
diff --git a/rmi/RMIClient.java b/rmi/RMIClient.java index 60e25c8..dc086ea 100755 --- a/rmi/RMIClient.java +++ b/rmi/RMIClient.java @@ -6,6 +6,8 @@ package rmi; import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
import common.MessageInfo;
@@ -22,10 +24,30 @@ public class RMIClient { String urlServer = new String("rmi://" + args[0] + "/RMIServer");
int numMessages = Integer.parseInt(args[1]);
- // TO-DO: Initialise Security Manager
-
- // TO-DO: Bind to RMIServer
+ // TODO: Initialise Security Manager
+ if(System.getSecurityManager() == null)
+ System.setSecurityManager(new SecurityManager());
+
+ // TODO: Bind to RMIServer
+ try {
+ String name = "RMIServer";
+ Registry registry = LocateRegistry.getRegistry();
+ iRMIServer = (RMIServerI)registry.lookup(name);
+ } catch(Exception e) {
+ System.err.println("RMIClient exception:");
+ e.printStackTrace();
+ }
- // TO-DO: Attempt to send messages the specified number of times
+ // TODO: Attempt to send messages the specified number of times
+ for(int i = 1; i < numMessages + 1; i++) {
+ MessageInfo msg = new MessageInfo(numMessages, i);
+ try {
+ iRMIServer.receiveMessage(msg);
+ System.out.println("Successfully sent msg: " + Integer.toString(i));
+ } catch(Exception e) {
+ System.err.println("RMIClient exception:");
+ e.printStackTrace();
+ }
+ }
}
}
diff --git a/rmi/RMIServer.java b/rmi/RMIServer.java index 2ce8d56..458f745 100755 --- a/rmi/RMIServer.java +++ b/rmi/RMIServer.java @@ -6,49 +6,81 @@ package rmi; import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
import java.rmi.RemoteException;
-import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.util.Arrays;
import common.*;
public class RMIServer extends UnicastRemoteObject implements RMIServerI {
+ public static final long serialVersionUID = 52L;
+
private int totalMessages = -1;
private int[] receivedMessages;
public RMIServer() throws RemoteException {
+ super();
}
public void receiveMessage(MessageInfo msg) throws RemoteException {
- // TO-DO: On receipt of first message, initialise the receive buffer
+ // TODO: On receipt of first message, initialise the receive buffer
+ if(msg.messageNum == 1)
+ receivedMessages = new int[msg.totalMessages];
+
+ // this may be a better implementations ????
+ // if(receivedMessages == null)
+ // receivedMessages = new int[msg.totalMessages];
- // TO-DO: Log receipt of the message
+ // TODO: Log receipt of the message
+ System.out.println("Receieved Message: " + Integer.toString(msg.messageNum) + " out of " + Integer.toString(msg.totalMessages));
- // TO-DO: If this is the last expected message, then identify
+ // TODO: If this is the last expected message, then identify
// any missing messages
}
public static void main(String[] args) {
- RMIServer rmis = null;
+ RMIServerI rmis = null;
+ // TODO: Initialise Security Manager
if(System.getSecurityManager() == null)
- System.setSecurityManager(new RMISecurityManager());
+ System.setSecurityManager(new SecurityManager());
+
+ // TODO: Instantiate the server class
- // TO-DO: Instantiate the server class
+ // TODO: Bind to RMI registry
+ try {
+ rmis = new RMIServer();
- // TO-DO: Bind to RMI registry
+ // Binding server
+ rebindServer("//127.0.0.1:1099/RMIServer", rmis);
+
+ System.out.println("RMIServer ready");
+ } catch(Exception e) {
+ System.err.println("RMIServer exception:");
+ e.printStackTrace();
+ }
}
- protected static void rebindServer(String serverURL, RMIServer server) {
- // TO-DO:
+ protected static void rebindServer(String serverURL, RMIServerI server) {
+ // TODO:
// Start / find the registry (hint use LocateRegistry.createRegistry(...)
// If we *know* the registry is running we could skip this (eg run rmiregistry in the start script)
- // TO-DO:
+ // TODO:
// Now rebind the server to the registry (rebind replaces any existing servers bound to the serverURL)
// Note - Registry.rebind (as returned by createRegistry / getRegistry) does something similar but
// expects different things from the URL field.
+ try {
+ LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
+
+ Naming.rebind(serverURL, server);
+
+ System.out.println("RMIServer bound");
+ } catch(Exception e) {
+ System.err.println("RMIRebindServer exception:");
+ e.printStackTrace();
+ }
}
}
diff --git a/rmiclient.sh b/rmiclient.sh index 7d7235f..1d69b4a 100755 --- a/rmiclient.sh +++ b/rmiclient.sh @@ -1,3 +1,4 @@ #!/bin/bash + export SECPOLICY="file:./policy" java -cp . -Djava.security.policy=$SECPOLICY rmi.RMIClient $* |