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
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
--- /dev/null
+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();
+ }
+}
/** 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
* @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);
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<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();
- }
}
/** 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
* @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];
}
// 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();
- }
}