From de5ea412741de534377de34194621d8d780e2ef1 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Mon, 10 Oct 2016 11:47:24 -0700 Subject: [PATCH] Abstracting IoTSocket and extend it for client and server sides --- iotjava/Makefile | 12 ++- iotjava/iotrmi/Java/IoTSocket.java | 122 +++++++++++++++++++++++ iotjava/iotrmi/Java/IoTSocketClient.java | 98 +----------------- iotjava/iotrmi/Java/IoTSocketServer.java | 95 +----------------- 4 files changed, 139 insertions(+), 188 deletions(-) create mode 100644 iotjava/iotrmi/Java/IoTSocket.java diff --git a/iotjava/Makefile b/iotjava/Makefile index d40d939..c02a5fe 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -2,8 +2,9 @@ BASE := .. include $(BASE)/common.mk -all: tree parser compiler +all: tree parser compiler rmi +# Parser compilation and run PHONY += tree tree: $(JAVAC) -cp .:$(PARSERJARS) -d $(BIN_DIR) iotpolicy/tree/*.java @@ -21,11 +22,20 @@ PHONY += run-compiler run-compiler: cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler camerapolicy.pol camerarequires.pol lightbulbpolicy.pol lightbulbrequires.pol -cplus Cplus -java Java +# RMI compilation and run PHONY += rmi rmi: $(JAVAC) -cp . -d $(BIN_DIR) iotrmi/*.java $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/*.java +PHONY += run-rmiserver +run-rmiserver: + $(JAVA) -cp .:$(BIN_DIR) iotrmi.Java.IoTRMIObject + +PHONY += run-rmiclient +run-rmiclient: + $(JAVA) -cp .:$(BIN_DIR) iotrmi.Java.IoTRMICall + PHONY += doc doc: iotruntime iotinstaller $(JAVADOC) -d $(DOCS_DIR) iotpolicy/*.java diff --git a/iotjava/iotrmi/Java/IoTSocket.java b/iotjava/iotrmi/Java/IoTSocket.java new file mode 100644 index 0000000..f941e91 --- /dev/null +++ b/iotjava/iotrmi/Java/IoTSocket.java @@ -0,0 +1,122 @@ +package iotrmi.Java; + +// Java libraries +import java.io.*; +import java.net.*; +import java.awt.*; +import java.util.*; + + +/** Class IoTSocket is the basic class for IoT RMI + * socket communication. This class will be extended + * by both IoTSocketServer and IoTSocketClient + *

+ * Adapted from Java/C++ socket implementation + * by Keith Vertanen + * @see * Adapted from Java/C++ socket implementation * by Keith Vertanen @@ -19,28 +18,14 @@ import java.util.*; * @version 1.0 * @since 2016-08-17 */ -public class IoTSocketClient { - - /** - * Class Properties - */ - byte data[]; - int port; - Socket sock; - BufferedInputStream input; - BufferedOutputStream output; - - /** - * Class Constant - */ - static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold +public class IoTSocketClient extends IoTSocket { /** * Default constructor */ public IoTSocketClient(int _port, String _address, int rev) throws IOException { - port = _port; + super(_port); try { sock = new Socket( InetAddress.getByName(_address), port ); input = new BufferedInputStream(sock.getInputStream(), BUFFSIZE); @@ -49,85 +34,8 @@ public class IoTSocketClient { catch ( IOException e ) { e.printStackTrace(); } - data = new byte[BUFFSIZE]; // now we want to tell the server if we want reversed bytes or not output.write(rev); output.flush(); } - - - /** - * sendBytes() sends an array of bytes - */ - public void sendBytes(byte vals[]) throws IOException - { - int len = vals.length; - output.write(len); - output.flush(); - output.write(vals, 0, len); - output.flush(); - receiveAck(); - sendAck(); - } - - - /** - * receiveBytes() receives an array of bytes - */ - public byte[] receiveBytes(byte val[]) throws IOException - { - int i; - int totalbytes = 0; - int numbytes; - // Read the maxlen first - int maxlen = (int)input.read(); - if (maxlen>BUFFSIZE) - System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!"); - val = new byte[maxlen]; - while (totalbytes < maxlen) - { - numbytes = input.read(data); - // copy the bytes into the result buffer - for (i=totalbytes; i * Adapted from Java/C++ socket implementation * by Keith Vertanen @@ -19,36 +18,25 @@ import java.util.*; * @version 1.0 * @since 2016-08-17 */ -public class IoTSocketServer { +public class IoTSocketServer extends IoTSocket { /** * Class Properties */ - byte data[]; - int port; ServerSocket server; - Socket sock; - BufferedInputStream input; - BufferedOutputStream output; - - /** - * Class Constant - */ - static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold /** * Constructors */ public IoTSocketServer(int _port) throws IOException { - port = _port; + super(_port); try { server = new ServerSocket(port, 100); } catch ( IOException e ) { e.printStackTrace(); } - data = new byte[BUFFSIZE]; } @@ -65,82 +53,5 @@ public class IoTSocketServer { // now find out if we want reversed bytes input.read(rev); } - - - /** - * sendBytes() sends an array of bytes - */ - public void sendBytes(byte vals[]) throws IOException - { - int len = vals.length; - output.write(len); - output.flush(); - output.write(vals, 0, len); - output.flush(); - receiveAck(); - sendAck(); - } - - - /** - * receiveBytes() receives an array of bytes - */ - public byte[] receiveBytes(byte val[]) throws IOException - { - int i; - int totalbytes = 0; - int numbytes; - - // Read the maxlen first - int maxlen = (int)input.read(); - if (maxlen>BUFFSIZE) - System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!"); - val = new byte[maxlen]; - while (totalbytes < maxlen) - { - numbytes = input.read(data); - // copy the bytes into the result buffer - for (i=totalbytes; i