aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorymherklotz <ymherklotz@gmail.com>2017-01-29 17:14:05 +0000
committerymherklotz <ymherklotz@gmail.com>2017-01-29 17:14:05 +0000
commit787ea33338b67f6d2ee7cfb400efc7bc4affe74c (patch)
tree6d7c5c421521f70641544aa5bddf0b2366c39490
parent28e7f14035c8703939bfecc4b2c9f389671e4752 (diff)
downloadNetworkCoursework-787ea33338b67f6d2ee7cfb400efc7bc4affe74c.tar.gz
NetworkCoursework-787ea33338b67f6d2ee7cfb400efc7bc4affe74c.zip
Adding files to repo
-rwxr-xr-xMakefile26
-rwxr-xr-xcommon/MessageInfo.java39
-rwxr-xr-xinstall.bat12
-rwxr-xr-xinstall.sh15
-rwxr-xr-xpolicy4
-rwxr-xr-xrmi/RMIClient.java34
-rwxr-xr-xrmi/RMIServer.java61
-rwxr-xr-xrmi/RMIServerI.java13
-rwxr-xr-xrmiclient.sh3
-rwxr-xr-xrmiserver.sh4
-rwxr-xr-xscripts/Makefile26
-rwxr-xr-xscripts/build.bat64
-rwxr-xr-xscripts/rmiclient.bat3
-rwxr-xr-xscripts/rmiclient.sh3
-rwxr-xr-xscripts/rmiserver.bat4
-rwxr-xr-xscripts/rmiserver.sh4
-rwxr-xr-xscripts/udpclient.bat2
-rwxr-xr-xscripts/udpclient.sh2
-rwxr-xr-xscripts/udpserver.bat2
-rwxr-xr-xscripts/udpserver.sh2
-rwxr-xr-xudp/UDPClient.java61
-rwxr-xr-xudp/UDPServer.java68
-rwxr-xr-xudpclient.sh2
-rwxr-xr-xudpserver.sh2
24 files changed, 456 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100755
index 0000000..8e8a45f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,26 @@
+
+.PHONY : all
+all: rmi udp
+
+.PHONY : rmi
+rmi:
+ @echo "Building RMI Client/Server..."; \
+ cd common; \
+ javac -g -classpath . *.java; \
+ cd ../rmi; \
+ javac -g -classpath .:.. *.java; \
+ cd ..; \
+ rmic -classpath . rmi.RMIServer; \
+
+.PHONY : udp
+udp:
+ @echo "Building UDP Client / Server..."; \
+ cd common; \
+ javac -g -classpath . *.java; \
+ cd ../udp; \
+ javac -g -classpath .:.. *.java; \
+
+
+.PHONY : clean
+clean:
+ rm rmi/*.class udp/*.class common/*.class
diff --git a/common/MessageInfo.java b/common/MessageInfo.java
new file mode 100755
index 0000000..2bba99a
--- /dev/null
+++ b/common/MessageInfo.java
@@ -0,0 +1,39 @@
+/*
+ * Created on 01-Mar-2016
+ */
+package common;
+
+import java.io.Serializable;
+
+/**
+ * Utility class that encapsulates the message information to
+ * be passed from client to server. Information can be extracted
+ * or constructed as a String for use by the UDP example.
+ *
+ */
+public class MessageInfo implements Serializable {
+
+ public static final long serialVersionUID = 52L;
+
+ public int totalMessages;
+ public int messageNum;
+
+ public MessageInfo(int total, int msgNum) {
+ totalMessages = total;
+ messageNum = msgNum;
+ }
+
+ public MessageInfo(String msg) throws Exception {
+ String[] fields = msg.split(";");
+ if (fields.length!=2)
+ throw new Exception("MessageInfo: Invalid string for message construction: " + msg);
+ totalMessages = Integer.parseInt(fields[0]);
+ messageNum = Integer.parseInt(fields[1]);
+ }
+
+ public String toString(){
+ return new String(totalMessages+";"+messageNum+"\n");
+ }
+
+
+}
diff --git a/install.bat b/install.bat
new file mode 100755
index 0000000..df27c03
--- /dev/null
+++ b/install.bat
@@ -0,0 +1,12 @@
+@echo off
+
+echo Computer Networks and Distributed Systems Coursework - Install Utility
+echo (c) Daniele Sgandurra, Imperial College London, Mar 2016
+echo.
+
+del rmiclient.sh rmiserver.sh udpclient.sh udpserver.sh Makefile
+xcopy /Y scripts\*.bat .
+
+echo.
+echo Done!
+echo.
diff --git a/install.sh b/install.sh
new file mode 100755
index 0000000..3abd681
--- /dev/null
+++ b/install.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+echo Computer Networks and Distributed Systems Coursework - Install Utility
+echo \(c\) Daniele Sgandurra, Imperial College London, Mar 2016
+
+rm -f rmiclient.bat rmiserver.bat udpclient.bat udpserver.bat build.bat
+cp scripts/Makefile .
+cp scripts/*.sh .
+fromdos policy
+for i in *.sh; do
+ fromdos $i
+done
+chmod u+x *.sh
+
+echo Done!
diff --git a/policy b/policy
new file mode 100755
index 0000000..9b05641
--- /dev/null
+++ b/policy
@@ -0,0 +1,4 @@
+grant {
+ // Allow everything for now
+ permission java.security.AllPermission;
+};
diff --git a/rmi/RMIClient.java b/rmi/RMIClient.java
new file mode 100755
index 0000000..4de0e48
--- /dev/null
+++ b/rmi/RMIClient.java
@@ -0,0 +1,34 @@
+/*
+ * Created on 01-Mar-2016
+ */
+package rmi;
+
+import java.rmi.Naming;
+import java.rmi.NotBoundException;
+import java.rmi.RemoteException;
+
+import common.MessageInfo;
+
+public class RMIClient {
+
+ public static void main(String[] args) {
+
+ RMIServerI iRMIServer = null;
+
+ // Check arguments for Server host and number of messages
+ if (args.length < 2){
+ System.out.println("Needs 2 arguments: ServerHostName/IPAddress, TotalMessageCount");
+ System.exit(-1);
+ }
+
+ String urlServer = new String("rmi://" + args[0] + "/RMIServer");
+ int numMessages = Integer.parseInt(args[1]);
+
+ // TO-DO: Initialise Security Manager
+
+ // TO-DO: Bind to RMIServer
+
+ // TO-DO: Attempt to send messages the specified number of times
+
+ }
+}
diff --git a/rmi/RMIServer.java b/rmi/RMIServer.java
new file mode 100755
index 0000000..8193a4c
--- /dev/null
+++ b/rmi/RMIServer.java
@@ -0,0 +1,61 @@
+/*
+ * Created on 01-Mar-2016
+ */
+package rmi;
+
+import java.net.MalformedURLException;
+import java.rmi.Naming;
+import java.rmi.registry.LocateRegistry;
+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 {
+
+ private int totalMessages = -1;
+ private int[] receivedMessages;
+
+ public RMIServer() throws RemoteException {
+ }
+
+ public void receiveMessage(MessageInfo msg) throws RemoteException {
+
+ // TO-DO: On receipt of first message, initialise the receive buffer
+
+ // TO-DO: Log receipt of the message
+
+ // TO-DO: If this is the last expected message, then identify
+ // any missing messages
+
+ }
+
+
+ public static void main(String[] args) {
+
+ RMIServer rmis = null;
+
+ if(System.getSecurityManager() == null) {
+ System.setSecurityManager(new RMISecurityManager());
+ }
+
+ // TO-DO: Instantiate the server class
+
+ // TO-DO: Bind to RMI registry
+
+ }
+
+ protected static void rebindServer(String serverURL, RMIServer server) {
+
+ // TO-DO:
+ // 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:
+ // 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.
+ }
+}
diff --git a/rmi/RMIServerI.java b/rmi/RMIServerI.java
new file mode 100755
index 0000000..b211db3
--- /dev/null
+++ b/rmi/RMIServerI.java
@@ -0,0 +1,13 @@
+/*
+ * Created on 01-Mar-2016
+ */
+package rmi;
+
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+
+import common.*;
+
+public interface RMIServerI extends Remote {
+ public void receiveMessage(MessageInfo msg) throws RemoteException;
+}
diff --git a/rmiclient.sh b/rmiclient.sh
new file mode 100755
index 0000000..7d7235f
--- /dev/null
+++ b/rmiclient.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+export SECPOLICY="file:./policy"
+java -cp . -Djava.security.policy=$SECPOLICY rmi.RMIClient $*
diff --git a/rmiserver.sh b/rmiserver.sh
new file mode 100755
index 0000000..4f1017c
--- /dev/null
+++ b/rmiserver.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+export SECPOLICY="file:./policy"
+java -cp . -Djava.security.policy=$SECPOLICY rmi.RMIServer
diff --git a/scripts/Makefile b/scripts/Makefile
new file mode 100755
index 0000000..8e8a45f
--- /dev/null
+++ b/scripts/Makefile
@@ -0,0 +1,26 @@
+
+.PHONY : all
+all: rmi udp
+
+.PHONY : rmi
+rmi:
+ @echo "Building RMI Client/Server..."; \
+ cd common; \
+ javac -g -classpath . *.java; \
+ cd ../rmi; \
+ javac -g -classpath .:.. *.java; \
+ cd ..; \
+ rmic -classpath . rmi.RMIServer; \
+
+.PHONY : udp
+udp:
+ @echo "Building UDP Client / Server..."; \
+ cd common; \
+ javac -g -classpath . *.java; \
+ cd ../udp; \
+ javac -g -classpath .:.. *.java; \
+
+
+.PHONY : clean
+clean:
+ rm rmi/*.class udp/*.class common/*.class
diff --git a/scripts/build.bat b/scripts/build.bat
new file mode 100755
index 0000000..dc3172d
--- /dev/null
+++ b/scripts/build.bat
@@ -0,0 +1,64 @@
+@echo off
+
+echo Computer Networks and Distributed Systems Coursework - Build Utility
+echo (c) Daniele Sgandurra, Imperial College London, Mar 2016
+echo.
+
+if "%1"=="" goto lUsage
+
+REM Select target to be built
+if "%1"=="all" goto lBuildStart
+if "%1"=="rmi" goto lBuildRMI
+if "%1"=="udp" goto lBuildUDP
+if "%1"=="tcp" goto lBuildTCP
+if "%1"=="clean" goto lClean
+
+REM Missing a Target
+ echo Target not recongnised
+ goto lEnd
+
+REM Targets
+:lBuildStart
+:lBuildRMI
+ echo Building RMI Classes ...
+ cd common
+ javac -g -classpath . *.java
+ cd ..\rmi
+ javac -g -classpath .;.. *.java
+ cd ..
+ rmic rmi.RMIServer
+ echo.
+ if not "%1"=="all" goto lEnd
+
+:lBuildUDP
+ echo Building UDP Client / Server...
+ cd common
+ javac -g -classpath . *.java
+ cd ..\udp
+ javac -g -classpath .. *.java
+ cd ..
+ echo.
+ if not "%1"=="all" goto lEnd
+
+:lBuildEnd
+ goto lEnd
+
+:lClean
+ del common\*.class
+ del rmi\*.class
+ del udp\*.class
+ echo.
+ goto :lEnd
+
+REM End Targets
+
+
+:lUsage
+echo Usage: build 'target' (rmi / udp)
+goto lEnd
+
+
+
+:lEnd
+echo Done!
+echo.
diff --git a/scripts/rmiclient.bat b/scripts/rmiclient.bat
new file mode 100755
index 0000000..83422bf
--- /dev/null
+++ b/scripts/rmiclient.bat
@@ -0,0 +1,3 @@
+@echo off
+set SECPOLICY="file:./policy"
+java -cp .;rmi;rmi-common -Djava.security.policy=%SECPOLICY% rmi.RMIClient %*
diff --git a/scripts/rmiclient.sh b/scripts/rmiclient.sh
new file mode 100755
index 0000000..7d7235f
--- /dev/null
+++ b/scripts/rmiclient.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+export SECPOLICY="file:./policy"
+java -cp . -Djava.security.policy=$SECPOLICY rmi.RMIClient $*
diff --git a/scripts/rmiserver.bat b/scripts/rmiserver.bat
new file mode 100755
index 0000000..a40ea7d
--- /dev/null
+++ b/scripts/rmiserver.bat
@@ -0,0 +1,4 @@
+@echo off
+set SECPOLICY="file:./policy"
+java -cp .;rmi;common -Djava.security.policy=%SECPOLICY% rmi.RMIServer
+
diff --git a/scripts/rmiserver.sh b/scripts/rmiserver.sh
new file mode 100755
index 0000000..4f1017c
--- /dev/null
+++ b/scripts/rmiserver.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+export SECPOLICY="file:./policy"
+java -cp . -Djava.security.policy=$SECPOLICY rmi.RMIServer
diff --git a/scripts/udpclient.bat b/scripts/udpclient.bat
new file mode 100755
index 0000000..dd430a7
--- /dev/null
+++ b/scripts/udpclient.bat
@@ -0,0 +1,2 @@
+@echo off
+java -cp .;udp udp.UDPClient %*
diff --git a/scripts/udpclient.sh b/scripts/udpclient.sh
new file mode 100755
index 0000000..544e4f9
--- /dev/null
+++ b/scripts/udpclient.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp .:udp udp.UDPClient $*
diff --git a/scripts/udpserver.bat b/scripts/udpserver.bat
new file mode 100755
index 0000000..ad3246e
--- /dev/null
+++ b/scripts/udpserver.bat
@@ -0,0 +1,2 @@
+@echo off
+java -cp .;udp udp.UDPServer %*
diff --git a/scripts/udpserver.sh b/scripts/udpserver.sh
new file mode 100755
index 0000000..7b1dfd1
--- /dev/null
+++ b/scripts/udpserver.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp .:udp udp.UDPServer $*
diff --git a/udp/UDPClient.java b/udp/UDPClient.java
new file mode 100755
index 0000000..848e996
--- /dev/null
+++ b/udp/UDPClient.java
@@ -0,0 +1,61 @@
+/*
+ * Created on 01-Mar-2016
+ */
+package udp;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+
+import common.MessageInfo;
+
+public class UDPClient {
+
+ private DatagramSocket sendSoc;
+
+ public static void main(String[] args) {
+ InetAddress serverAddr = null;
+ int recvPort;
+ int countTo;
+ String message;
+
+ // Get the parameters
+ if (args.length < 3) {
+ System.err.println("Arguments required: server name/IP, recv port, message count");
+ System.exit(-1);
+ }
+
+ try {
+ serverAddr = InetAddress.getByName(args[0]);
+ } catch (UnknownHostException e) {
+ System.out.println("Bad server address in UDPClient, " + args[0] + " caused an unknown host exception " + e);
+ System.exit(-1);
+ }
+ recvPort = Integer.parseInt(args[1]);
+ countTo = Integer.parseInt(args[2]);
+
+
+ // TO-DO: Construct UDP client class and try to send messages
+ }
+
+ public UDPClient() {
+ // TO-DO: Initialise the UDP socket for sending data
+ }
+
+ private void testLoop(InetAddress serverAddr, int recvPort, int countTo) {
+ int tries = 0;
+
+ // TO-DO: Send the messages to the server
+ }
+
+ private void send(String payload, InetAddress destAddr, int destPort) {
+ int payloadSize;
+ byte[] pktData;
+ DatagramPacket pkt;
+
+ // TO-DO: build the datagram packet and send it to the server
+ }
+}
diff --git a/udp/UDPServer.java b/udp/UDPServer.java
new file mode 100755
index 0000000..8659d25
--- /dev/null
+++ b/udp/UDPServer.java
@@ -0,0 +1,68 @@
+/*
+ * Created on 01-Mar-2016
+ */
+package udp;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.util.Arrays;
+
+import common.MessageInfo;
+
+public class UDPServer {
+
+ private DatagramSocket recvSoc;
+ private int totalMessages = -1;
+ private int[] receivedMessages;
+ private boolean close;
+
+ private void run() {
+ int pacSize;
+ byte[] pacData;
+ DatagramPacket pac;
+
+ // TO-DO: Receive the messages and process them by calling processMessage(...).
+ // Use a timeout (e.g. 30 secs) to ensure the program doesn't block forever
+
+ }
+
+ public void processMessage(String data) {
+
+ MessageInfo msg = null;
+
+ // TO-DO: Use the data to construct a new MessageInfo object
+
+ // TO-DO: On receipt of first message, initialise the receive buffer
+
+ // TO-DO: Log receipt of the message
+
+ // TO-DO: If this is the last expected message, then identify
+ // any missing messages
+
+ }
+
+
+ public UDPServer(int rp) {
+ // TO-DO: Initialise UDP socket for receiving data
+
+ // Done Initialisation
+ System.out.println("UDPServer ready");
+ }
+
+ public static void main(String args[]) {
+ int recvPort;
+
+ // Get the parameters from command line
+ if (args.length < 1) {
+ System.err.println("Arguments required: recv port");
+ System.exit(-1);
+ }
+ recvPort = Integer.parseInt(args[0]);
+
+ // TO-DO: Construct Server object and start it by calling run().
+ }
+
+}
diff --git a/udpclient.sh b/udpclient.sh
new file mode 100755
index 0000000..544e4f9
--- /dev/null
+++ b/udpclient.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp .:udp udp.UDPClient $*
diff --git a/udpserver.sh b/udpserver.sh
new file mode 100755
index 0000000..7b1dfd1
--- /dev/null
+++ b/udpserver.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp .:udp udp.UDPServer $*