99d0af92095d71b22a2f5b9832ce1a09c18a65e1
[iot2.git] / iotjava / iotrmi / Java / IoTRMICommClient.java
1 package iotrmi.Java;
2
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;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.lang.reflect.*;
12
13 import java.util.concurrent.*;
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16
17 /** Class IoTRMICommClient is a class that extends IoTRMIComm
18  *  <p>
19  *  This is a version of IoTRMIComm that sits on the main stub.
20  *
21  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
22  * @version     1.0
23  * @since       2017-01-27
24  */
25 public class IoTRMICommClient extends IoTRMIComm {
26
27         /**
28          * Class Properties
29          */
30         private IoTSocketClient rmiClientSend;
31         private IoTSocketClient rmiClientRecv;
32
33
34         /**
35          * Constructor (for stub) - send and recv from the perspective of RMI socket servers
36          */
37         public IoTRMICommClient(int _localPortSend, int _localPortRecv, int _portSend, int _portRecv, String _address, int _rev) throws  
38                 ClassNotFoundException, InstantiationException, 
39                         IllegalAccessException, IOException {
40
41                 super();
42                 rmiClientRecv = new IoTSocketClient(_localPortSend, _portSend, _address, _rev);
43                 rmiClientSend = new IoTSocketClient(_localPortRecv, _portRecv, _address, _rev);
44                 waitForPacketsOnClient();
45         }
46
47
48         /**
49          * waitForPacketsOnClient() starts a thread that waits for packet bytes on client side
50          */
51         public void waitForPacketsOnClient() {
52
53                 Thread thread = new Thread() {
54                         public void run() {
55                                 byte[] packetBytes = null;
56                                 while(true) {
57                                         try {
58                                                 packetBytes = rmiClientRecv.receiveBytes(packetBytes);
59                                                 if (packetBytes != null) {
60                                                         int packetType = IoTRMIComm.getPacketType(packetBytes);
61                                                         if (packetType == IoTRMIUtil.METHOD_TYPE) {
62                                                                 System.out.println("Method packet: " + Arrays.toString(packetBytes));
63                                                                 methodQueue.offer(packetBytes);
64                                                         } else if (packetType == IoTRMIUtil.RET_VAL_TYPE) {
65                                                                 System.out.println("Return value packet: " + Arrays.toString(packetBytes));
66                                                                 returnQueue.offer(packetBytes);
67                                                         } else
68                                                                 throw new Error("IoTRMICommClient: Packet type is unknown: " + packetType);
69                                                 } //else
70                                                 //      Thread.sleep(100);
71                                                 packetBytes = null;
72                                         } catch (Exception ex) {
73                                                 ex.printStackTrace();
74                                                 throw new Error("IoTRMICommClient: Error receiving return value bytes on client!");
75                                         }
76                                 }
77                         }
78                 };
79                 thread.start();
80         }
81
82
83         /**
84          * sendReturnObj() for non-struct objects (client side)
85          */
86         public synchronized void sendReturnObj(Object retObj, byte[] methodBytes) {
87
88                 // Send back return value
89                 byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
90                 // Send return value together with OBJECT_ID and METHOD_ID for arbitration
91                 int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
92                 int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
93                 byte[] retAllBytes = new byte[headerLen + retObjBytes.length];
94                 // Copy OBJECT_ID and METHOD_ID
95                 System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
96                 int packetType = IoTRMIUtil.RET_VAL_TYPE;       // This is a return value
97                 byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
98                 System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
99                 // Copy array of bytes (return object)
100                 System.arraycopy(retObjBytes, 0, retAllBytes, headerLen, retObjBytes.length);
101                 try {
102                         rmiClientSend.sendBytes(retAllBytes);
103                 } catch (IOException ex) {
104                         ex.printStackTrace();
105                         throw new Error("IoTRMICommClient: Error sending bytes in sendReturnObj()!");
106                 }
107         }
108
109
110         /**
111          * sendReturnObj() overloaded to send multiple return objects for structs (client side)
112          */
113         public synchronized void sendReturnObj(Class<?>[] retCls, Object[] retObj, byte[] methodBytes) {
114
115                 // Send back return value
116                 byte[] retObjBytes = returnToBytes(retCls, retObj);
117                 // Send return value together with OBJECT_ID and METHOD_ID for arbitration
118                 int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
119                 int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
120                 byte[] retAllBytes = new byte[headerLen + retObjBytes.length];
121                 // Copy OBJECT_ID and METHOD_ID
122                 System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
123                 int packetType = IoTRMIUtil.RET_VAL_TYPE;       // This is a return value
124                 byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
125                 System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
126                 // Copy array of bytes (return object)
127                 try {
128                         rmiClientSend.sendBytes(retAllBytes);
129                 } catch (IOException ex) {
130                         ex.printStackTrace();
131                         throw new Error("IoTRMICommClient: Error sending bytes in sendReturnObj()!");
132                 }
133         }
134
135
136         /**
137          * remoteCall() calls a method remotely by passing in parameters (client side)
138          */
139         public synchronized void remoteCall(int objectId, int methodId, Class<?>[] paramCls, Object[] paramObj) {
140
141                 // Send method info
142                 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
143                 try {
144                         rmiClientSend.sendBytes(methodBytes);
145                 } catch (IOException ex) {
146                         ex.printStackTrace();
147                         throw new Error("IoTRMICommClient: Error when sending bytes in remoteCall()!");
148                 }
149         }
150 }