Abstracting IoTSocket and extend it for client and server sides
authorrtrimana <rtrimana@uci.edu>
Mon, 10 Oct 2016 18:47:24 +0000 (11:47 -0700)
committerrtrimana <rtrimana@uci.edu>
Mon, 10 Oct 2016 18:47:24 +0000 (11:47 -0700)
iotjava/Makefile
iotjava/iotrmi/Java/IoTSocket.java [new file with mode: 0644]
iotjava/iotrmi/Java/IoTSocketClient.java
iotjava/iotrmi/Java/IoTSocketServer.java

index d40d939850a1d3682de104a25eebb34fabe3119b..c02a5feaffb227418705b3a1ca3e40ae6e351f06 100644 (file)
@@ -2,8 +2,9 @@ BASE := ..
 
 include $(BASE)/common.mk
 
 
 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
 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
 
 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 += 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
 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 (file)
index 0000000..f941e91
--- /dev/null
@@ -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
+ *  <p>
+ *  Adapted from Java/C++ socket implementation
+ *  by Keith Vertanen
+ *  @see        <a href="https://www.keithv.com/software/socket/</a>
+ *
+ * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
+ * @version     1.0
+ * @since       2016-08-17
+ */
+public abstract class IoTSocket {
+
+       /**
+        * Class Properties
+        */
+       protected byte data[];
+       protected int port;
+       protected Socket sock;
+       protected BufferedInputStream input;
+       protected BufferedOutputStream output;
+
+       /**
+        * Class Constant
+        */
+       protected static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold 
+
+       /**
+        * Default constructor
+        */
+       protected IoTSocket(int _port) throws IOException
+       {
+               port = _port;
+               data = new byte[BUFFSIZE];
+       }
+
+
+       /**
+        * 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<totalbytes+numbytes; i++)
+                               val[i] = data[i-totalbytes];
+                       totalbytes += numbytes;
+               }
+               // we now send an acknowledgement to the server to let them
+               // know we've got it
+               sendAck();
+               receiveAck();
+
+               return val;
+       }
+
+
+       /**
+        * Close socket connection
+        */
+       public void close() throws IOException
+       {
+               sock.close();
+       }
+
+
+       /**
+        * Send ACK
+        */
+       public void sendAck() throws IOException
+       {
+               int ack;
+               ack = 0;
+               output.write(ack);
+               output.flush();
+       }
+
+
+       /**
+        * Receive ACK
+        */
+       public void receiveAck() throws IOException
+       {
+               int ack;
+               ack = (int) input.read();
+       }
+}
index 3d3e8b7770f4ab69a0c0e5ef3f36789f313510a2..961719cf2ae113a42999d8c364f7e7ea17ad1cf4 100644 (file)
@@ -8,8 +8,7 @@ import java.util.*;
 
 
 /** Class IoTSocketClient is a communication class
 
 
 /** Class IoTSocketClient is a communication class
- *  that provides interfaces to connect to either
- *  Java or C++ socket endpoint
+ *  that extends IoTSocket. This is the client side.
  *  <p>
  *  Adapted from Java/C++ socket implementation
  *  by Keith Vertanen
  *  <p>
  *  Adapted from Java/C++ socket implementation
  *  by Keith Vertanen
@@ -19,28 +18,14 @@ import java.util.*;
  * @version     1.0
  * @since       2016-08-17
  */
  * @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
        {
 
        /**
         * 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);
                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();
                }
                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();
        }
                // 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<totalbytes+numbytes; i++)
-                               val[i] = data[i-totalbytes];
-                       totalbytes += numbytes;
-               }
-               // we now send an acknowledgement to the server to let them
-               // know we've got it
-               sendAck();
-               receiveAck();
-
-               return val;
-       }
-
-
-       /**
-        * Close socket connection
-        */
-       public void close() throws IOException
-       {
-               sock.close();
-       }
-
-
-       /**
-        * Send ACK
-        */
-       private void sendAck() throws IOException
-       {
-               int ack;
-               ack = 0;
-               output.write(ack);
-               output.flush();
-       }
-
-
-       /**
-        * Receive ACK
-        */
-       private void receiveAck() throws IOException
-       {
-               int ack;
-               ack = (int) input.read();
-       }
 }
 }
index 480b2e640c762548810eb40f482bd6398b42001f..3beecc61b8228ab169e3e35fb926b6cee3249b41 100644 (file)
@@ -8,8 +8,7 @@ import java.util.*;
 
 
 /** Class IoTSocketServer is a communication class
 
 
 /** Class IoTSocketServer is a communication class
- *  that provides interfaces to connect to either
- *  Java or C++ socket endpoint
+ *  that extends IoTSocket. This is the server side.
  *  <p>
  *  Adapted from Java/C++ socket implementation
  *  by Keith Vertanen
  *  <p>
  *  Adapted from Java/C++ socket implementation
  *  by Keith Vertanen
@@ -19,36 +18,25 @@ import java.util.*;
  * @version     1.0
  * @since       2016-08-17
  */
  * @version     1.0
  * @since       2016-08-17
  */
-public class IoTSocketServer {
+public class IoTSocketServer extends IoTSocket {
 
        /**
         * Class Properties
         */
 
        /**
         * Class Properties
         */
-       byte data[];
-       int port;
        ServerSocket server;
        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
        {
 
        /**
         * Constructors
         */
        public IoTSocketServer(int _port) throws IOException
        {
-               port = _port;
+               super(_port);
                try {
                        server = new ServerSocket(port, 100);
                }
                catch ( IOException e ) {
                        e.printStackTrace();
                }
                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);
        }
                // 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<totalbytes+numbytes; i++)
-                               val[i] = data[i-totalbytes];
-                       totalbytes += numbytes;
-               }
-               // we now send an acknowledgement to the server to let them
-               // know we've got it
-               sendAck();
-               receiveAck();
-
-               return val;
-       }
-
-
-       /**
-        * Close socket connection
-        */
-       public void close() throws IOException
-       {
-               sock.close();
-       }
-
-
-       /**
-        * Send ACK
-        */
-       private void sendAck() throws IOException
-       {
-               int ack;
-               ack = 0;
-               output.write(ack);
-               output.flush();
-       }
-
-
-       /**
-        * Receive ACK
-        */
-       private void receiveAck() throws IOException
-       {
-               int ack;
-               ack = (int) input.read();
-       }
 }
 
 }