3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.util.Arrays;
6 import java.util.ArrayList;
7 import java.util.HashMap;
11 import java.lang.reflect.*;
13 import java.util.concurrent.*;
14 import java.util.concurrent.atomic.AtomicBoolean;
17 /** Class IoTRMICommServer is a class that extends IoTRMI
19 * We will arbitrate packets into 2 queues and wake up the right threads/callers.
20 * We separate traffics one-directionally.
22 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
26 public final class IoTRMICommServer extends IoTRMIComm {
31 private IoTSocketServer rmiServerSend;
32 private IoTSocketServer rmiServerRecv;
33 private AtomicBoolean didServerSendConnect;
34 private AtomicBoolean didServerRecvConnect;
38 * Constructor (for skeleton)
40 public IoTRMICommServer(int _portSend, int _portRecv) throws
41 ClassNotFoundException, InstantiationException,
42 IllegalAccessException, IOException {
45 didServerSendConnect = new AtomicBoolean(false);
46 didServerRecvConnect = new AtomicBoolean(false);
47 rmiServerSend = new IoTSocketServer(_portSend);
48 rmiServerRecv = new IoTSocketServer(_portRecv);
49 waitForConnectionOnServerSend();
50 waitForConnectionOnServerRecv();
51 while(!didServerSendConnect.get()); // Wait until server is connected
52 while(!didServerRecvConnect.get()); // Wait until server is connected
53 waitForPacketsOnServer();
58 * waitForConnectionOnServerRecv() starts a thread that waits server connection
60 public void waitForConnectionOnServerRecv() {
62 Thread thread = new Thread() {
65 rmiServerRecv.connect();
66 didServerRecvConnect.set(true);
67 } catch (Exception ex) {
69 throw new Error("IoTRMICommServer: Error starting receiver server!");
78 * waitForConnectionOnServerSend() starts a thread that waits server connection
80 public void waitForConnectionOnServerSend() {
82 Thread thread = new Thread() {
85 rmiServerSend.connect();
86 didServerSendConnect.set(true);
87 } catch (Exception ex) {
89 throw new Error("IoTRMICommServer: Error starting sender server!");
98 * waitForPacketsOnServer() starts a thread that waits for packet bytes on server side
100 public void waitForPacketsOnServer() {
102 Thread thread = new Thread() {
104 byte[] packetBytes = null;
107 packetBytes = rmiServerRecv.receiveBytes(packetBytes);
108 if (packetBytes != null) {
109 int packetType = IoTRMIComm.getPacketType(packetBytes);
110 if (packetType == IoTRMIUtil.METHOD_TYPE) {
111 methodQueue.offer(packetBytes);
112 } else if (packetType == IoTRMIUtil.RET_VAL_TYPE) {
113 returnQueue.offer(packetBytes);
115 throw new Error("IoTRMICommServer: Packet type is unknown: " + packetType);
118 } catch (Exception ex) {
119 ex.printStackTrace();
120 throw new Error("IoTRMICommServer: Error receiving return value bytes on server!");
130 * sendReturnObj() for non-struct objects (server side)
132 public synchronized void sendReturnObj(Object retObj, byte[] methodBytes) {
134 // Send back return value
135 byte[] retObjBytes = null;
136 if (retObj != null) // Handle nullness
137 retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
138 // Send return value together with OBJECT_ID and METHOD_ID for arbitration
139 int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
140 int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
141 byte[] retAllBytes = null;
142 if (retObj == null) // Handle nullness
143 retAllBytes = new byte[headerLen];
145 retAllBytes = new byte[headerLen + retObjBytes.length];
146 // Copy OBJECT_ID and METHOD_ID
147 System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
148 int packetType = IoTRMIUtil.RET_VAL_TYPE; // This is a return value
149 byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
150 System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
151 // Copy array of bytes (return object)
153 System.arraycopy(retObjBytes, 0, retAllBytes, headerLen, retObjBytes.length);
155 rmiServerSend.sendBytes(retAllBytes);
156 } catch (IOException ex) {
157 ex.printStackTrace();
158 throw new Error("IoTRMICommServer: Error sending bytes in sendReturnObj()!");
164 * sendReturnObj() overloaded to send multiple return objects for structs (server side)
166 public synchronized void sendReturnObj(Class<?>[] retCls, Object[] retObj, byte[] methodBytes) {
168 // Send back return value
169 byte[] retObjBytes = returnToBytes(retCls, retObj);
170 // Send return value together with OBJECT_ID and METHOD_ID for arbitration
171 int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
172 int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
173 byte[] retAllBytes = new byte[headerLen + retObjBytes.length];
174 // Copy OBJECT_ID and METHOD_ID
175 System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
176 int packetType = IoTRMIUtil.RET_VAL_TYPE; // This is a return value
177 byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
178 System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
179 // Copy array of bytes (return object)
180 System.arraycopy(retObjBytes, 0, retAllBytes, headerLen, retObjBytes.length);
182 rmiServerSend.sendBytes(retAllBytes);
183 } catch (IOException ex) {
184 ex.printStackTrace();
185 throw new Error("IoTRMICommServer: Error sending bytes in sendReturnObj()!");
191 * remoteCall() calls a method remotely by passing in parameters (server side)
193 public synchronized void remoteCall(int objectId, int methodId, Class<?>[] paramCls, Object[] paramObj) {
196 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
198 rmiServerSend.sendBytes(methodBytes);
199 } catch (IOException ex) {
200 ex.printStackTrace();
201 throw new Error("IoTRMICommServer: Error when sending bytes in remoteCall()!");