Adding support to returning struct/list of struct objects
[iot2.git] / iotjava / iotrmi / C++ / IoTRMICall.hpp
1 /** Class IoTRMICall provides methods that the upper
2  *  layers can use to transport and invoke methods
3  *  when using IoTSocket, IoTSocketClient and IoTSocketServer.
4  *  <p>
5  *  This class serves in the stub part of the RMI
6  *  communication. It bridges and creates RMI requests to be
7  *  transferred into the RMI object.
8  *
9  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
10  * @version     1.0
11  * @since       2016-10-18
12  */
13 #ifndef _IOTRMICALL_HPP__
14 #define _IOTRMICALL_HPP__
15
16 #include <iostream>
17 #include <string>
18 #include <mutex>
19 #include "IoTRMIUtil.hpp"
20 #include "IoTSocketClient.hpp"
21
22 using namespace std;
23
24 mutex mtx;
25
26 class IoTRMICall {
27         public:
28                 IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult);
29                 ~IoTRMICall();
30                 // Public methods
31                 int             methodLength(string paramCls[], void* paramObj[], int numParam);
32                 char*   methodToBytes(int objectId, int methId, string paramCls[], void* paramObj[],
33                                                                 char* method, int numParam);
34                 void*   remoteCall(int objectId, int methodId, string retType, string paramCls[], 
35                                                                 void* paramObj[], int numParam, void* retObj);
36                 void**  getStructObjects(string retType[], int numRet, void* retObj[]);
37                 void**  getReturnObjects(char* retBytes, string retCls[], int numRet, void* retObj[]);
38
39         private:
40                 map<string,int>         mapSign2MethodId;
41                 IoTRMIUtil                      *rmiUtil;
42                 IoTSocketClient         *rmiClient;
43
44                 // Private methods
45                 void                            getMethodIds(const string methodSign[], const int size);
46 };
47
48
49 // Constructor
50 IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult) {
51
52         rmiUtil = new IoTRMIUtil();
53         if (rmiUtil == NULL) {
54                 perror("IoTRMICall: IoTRMIUtil isn't initialized!");
55         }
56         rmiClient = new IoTSocketClient(_port, _address, _rev, _bResult);
57         if (rmiClient == NULL) {
58                 perror("IoTRMICall: IoTSocketClient isn't initialized!");
59         }
60 }
61
62
63 // Destructor
64 IoTRMICall::~IoTRMICall() {
65
66         // Clean up
67         if (rmiUtil != NULL) {
68                 
69                 delete rmiUtil;
70                 rmiUtil = NULL;         
71         }
72         if (rmiClient != NULL) {
73
74                 fflush(NULL);
75                 rmiClient->close();             
76                 delete rmiClient;
77                 rmiClient = NULL;               
78         }
79 }
80
81
82 // Calls a method remotely by passing in parameters and getting a return object
83 void* IoTRMICall::remoteCall(int objectId, int methodId, string retType, string paramCls[], 
84                                                                 void* paramObj[], int numParam, void* retObj) {
85
86         // Critical section that is used by different objects
87         lock_guard<mutex> guard(mtx);
88         // Send input parameters
89         int len = methodLength(paramCls, paramObj, numParam);
90         char method[len];
91         methodToBytes(objectId, methodId, paramCls, paramObj, method, numParam);
92         // Send bytes
93         fflush(NULL);
94         rmiClient->sendBytes(method, len);
95         fflush(NULL);
96         // Receive return value and return it to caller
97         if (retType.compare("void") == 0)
98                 // Just make it NULL if it's a void return
99                 retObj = NULL;
100         else {
101                 int retLen = 0;
102                 char* retObjBytes = NULL;
103                 retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
104                 retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes, retLen);
105                 // Delete received bytes object
106                 delete[] retObjBytes;
107         }
108         
109         return retObj;
110 }
111
112
113 // Get a set of return objects (struct)
114 void** IoTRMICall::getStructObjects(string retType[], int numRet, void* retObj[]) {
115
116         // Critical section that is used by different objects
117         lock_guard<mutex> guard(mtx);
118         // Receive struct return value and return it to caller
119         int retLen = 0;
120         char* retObjBytes = NULL;
121         // Return size of array of struct
122         retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
123         retObj = getReturnObjects(retObjBytes, retType, numRet, retObj);
124         // Delete received bytes object
125         delete[] retObjBytes;
126         
127         return retObj;
128 }
129
130
131 // Find the bytes length of a method
132 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
133
134         // Get byte arrays and calculate method bytes length
135         // Start from the object Id + method Id...
136         int methodLen = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN;
137         for (int i = 0; i < numParam; i++) {
138                 // Find the parameter length
139                 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
140                 if (paramLen == -1) { // Store the length of the field - indefinite length
141                         paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
142                         // Some space for param length, i.e. 32 bits for integer                
143                         methodLen = methodLen + IoTRMIUtil::PARAM_LEN;
144                 }
145                 // Calculate methodLen
146                 methodLen = methodLen + paramLen;
147         }
148
149         return methodLen;
150 }
151
152
153 // Convert method and its parameters into bytes
154 char* IoTRMICall::methodToBytes(int objectId, int methId, string paramCls[], 
155                 void* paramObj[], char* method, int numParam) {
156
157         // Get object Id in bytes
158         char objId[IoTRMIUtil::OBJECT_ID_LEN];
159         IoTRMIUtil::intToByteArray(objectId, objId);
160         memcpy(method, objId, IoTRMIUtil::OBJECT_ID_LEN);
161         int pos = IoTRMIUtil::OBJECT_ID_LEN;
162         // Get method Id in bytes
163         char methodId[IoTRMIUtil::METHOD_ID_LEN];
164         IoTRMIUtil::intToByteArray(methId, methodId);
165         memcpy(method + pos, methodId, IoTRMIUtil::METHOD_ID_LEN);
166         pos = pos + IoTRMIUtil::METHOD_ID_LEN;
167         // Get byte arrays and calculate method bytes length
168         for (int i = 0; i < numParam; i++) {
169                 // Find the parameter length
170                 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
171                 if (paramLen == -1) { // Store the length of the field - indefinite length
172                         paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
173                         // Write the parameter length
174                         char prmLenBytes[IoTRMIUtil::PARAM_LEN];
175                         IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
176                         memcpy(method + pos, prmLenBytes, IoTRMIUtil::PARAM_LEN);                       
177                         pos = pos + IoTRMIUtil::PARAM_LEN;
178                 }
179                 // Get array of bytes and put it in the array of array of bytes
180                 char objBytes[paramLen];
181                 IoTRMIUtil::getObjectBytes(objBytes, paramObj[i], paramCls[i].c_str());
182                 memcpy(method + pos, objBytes, paramLen);
183                 pos = pos + paramLen;
184         }
185
186         return method;
187 }
188
189
190 void** IoTRMICall::getReturnObjects(char* retBytes, string retCls[], int numRet, void* retObj[]) {
191
192         // Byte scanning position
193         int pos = 0;
194         for (int i = 0; i < numRet; i++) {
195                 int retLen = rmiUtil->getTypeSize(retCls[i]);
196                 // Get the 32-bit field in the byte array to get the actual
197                 //              length (this is a param with indefinite length)
198                 if (retLen == -1) {
199                         char bytRetLen[IoTRMIUtil::RETURN_LEN];
200                         memcpy(bytRetLen, retBytes + pos, IoTRMIUtil::RETURN_LEN);
201                         pos = pos + IoTRMIUtil::RETURN_LEN;
202                         int* retLenPtr = IoTRMIUtil::byteArrayToInt(&retLen, bytRetLen);
203                         retLen = *retLenPtr;
204                 }
205                 char retObjBytes[retLen];
206                 memcpy(retObjBytes, retBytes + pos, retLen);
207                 pos = pos + retLen;
208                 retObj[i] = IoTRMIUtil::getParamObject(retObj[i], retCls[i].c_str(), retObjBytes, retLen);
209         }
210
211         return retObj;
212 }
213
214
215 #endif
216
217