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 IoTRMICommClient is a class that extends IoTRMIComm
19 * This is a version of IoTRMIComm that sits on the main stub.
21 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
25 public final class IoTRMICommClient extends IoTRMIComm {
30 private IoTSocketClient rmiClientSend;
31 private IoTSocketClient rmiClientRecv;
35 * Constructor (for stub) - send and recv from the perspective of RMI socket servers
37 public IoTRMICommClient(int _localPortSend, int _localPortRecv, int _portSend, int _portRecv, String _address, int _rev) throws
38 ClassNotFoundException, InstantiationException,
39 IllegalAccessException, IOException {
42 rmiClientRecv = new IoTSocketClient(_localPortSend, _portSend, _address, _rev);
43 rmiClientSend = new IoTSocketClient(_localPortRecv, _portRecv, _address, _rev);
44 waitForPacketsOnClient();
49 * Constructor (for stub) - only destination port numbers
51 public IoTRMICommClient(int _portSend, int _portRecv, String _address, int _rev) throws
52 ClassNotFoundException, InstantiationException,
53 IllegalAccessException, IOException {
56 rmiClientRecv = new IoTSocketClient(_portSend, _address, _rev);
57 rmiClientSend = new IoTSocketClient(_portRecv, _address, _rev);
58 waitForPacketsOnClient();
63 * waitForPacketsOnClient() starts a thread that waits for packet bytes on client side
65 public void waitForPacketsOnClient() {
67 Thread thread = new Thread() {
69 byte[] packetBytes = null;
72 packetBytes = rmiClientRecv.receiveBytes(packetBytes);
73 if (packetBytes != null) {
74 int packetType = IoTRMIComm.getPacketType(packetBytes);
75 if (packetType == IoTRMIUtil.METHOD_TYPE) {
76 methodQueue.offer(packetBytes);
77 } else if (packetType == IoTRMIUtil.RET_VAL_TYPE) {
78 returnQueue.offer(packetBytes);
80 throw new Error("IoTRMICommClient: Packet type is unknown: " + packetType);
83 } catch (Exception ex) {
85 throw new Error("IoTRMICommClient: Error receiving return value bytes on client!");
95 * sendReturnObj() for non-struct objects (client side)
97 public synchronized void sendReturnObj(Object retObj, byte[] methodBytes) {
99 // Send back return value
100 byte[] retObjBytes = null;
101 if (retObj != null) // Handle nullness
102 retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
103 // Send return value together with OBJECT_ID and METHOD_ID for arbitration
104 int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
105 int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
106 byte[] retAllBytes = null;
107 if (retObj == null) // Handle nullness
108 retAllBytes = new byte[headerLen];
110 retAllBytes = new byte[headerLen + retObjBytes.length];
111 // Copy OBJECT_ID and METHOD_ID
112 System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
113 int packetType = IoTRMIUtil.RET_VAL_TYPE; // This is a return value
114 byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
115 System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
116 // Copy array of bytes (return object)
118 System.arraycopy(retObjBytes, 0, retAllBytes, headerLen, retObjBytes.length);
120 rmiClientSend.sendBytes(retAllBytes);
121 } catch (IOException ex) {
122 ex.printStackTrace();
123 throw new Error("IoTRMICommClient: Error sending bytes in sendReturnObj()!");
129 * sendReturnObj() overloaded to send multiple return objects for structs (client side)
131 public synchronized void sendReturnObj(Class<?>[] retCls, Object[] retObj, byte[] methodBytes) {
133 // Send back return value
134 byte[] retObjBytes = returnToBytes(retCls, retObj);
135 // Send return value together with OBJECT_ID and METHOD_ID for arbitration
136 int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
137 int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
138 byte[] retAllBytes = new byte[headerLen + retObjBytes.length];
139 // Copy OBJECT_ID and METHOD_ID
140 System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
141 int packetType = IoTRMIUtil.RET_VAL_TYPE; // This is a return value
142 byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
143 System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
144 // Copy array of bytes (return object)
146 rmiClientSend.sendBytes(retAllBytes);
147 } catch (IOException ex) {
148 ex.printStackTrace();
149 throw new Error("IoTRMICommClient: Error sending bytes in sendReturnObj()!");
155 * remoteCall() calls a method remotely by passing in parameters (client side)
157 public synchronized void remoteCall(int objectId, int methodId, Class<?>[] paramCls, Object[] paramObj) {
160 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
162 rmiClientSend.sendBytes(methodBytes);
163 } catch (IOException ex) {
164 ex.printStackTrace();
165 throw new Error("IoTRMICommClient: Error when sending bytes in remoteCall()!");