1 /** Class IoTRMICall provides methods that the upper
2 * layers can use to transport and invoke methods
3 * when using IoTSocket, IoTSocketClient and IoTSocketServer.
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.
9 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
13 #ifndef _IOTRMICALL_HPP__
14 #define _IOTRMICALL_HPP__
19 #include "IoTRMIUtil.hpp"
20 #include "IoTSocketClient.hpp"
28 IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult);
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[]);
40 map<string,int> mapSign2MethodId;
42 IoTSocketClient *rmiClient;
45 void getMethodIds(const string methodSign[], const int size);
50 IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult) {
52 rmiUtil = new IoTRMIUtil();
53 if (rmiUtil == NULL) {
54 perror("IoTRMICall: IoTRMIUtil isn't initialized!");
56 rmiClient = new IoTSocketClient(_port, _address, _rev, _bResult);
57 if (rmiClient == NULL) {
58 perror("IoTRMICall: IoTSocketClient isn't initialized!");
64 IoTRMICall::~IoTRMICall() {
67 if (rmiUtil != NULL) {
72 if (rmiClient != NULL) {
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) {
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);
91 methodToBytes(objectId, methodId, paramCls, paramObj, method, numParam);
94 rmiClient->sendBytes(method, len);
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
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;
113 // Get a set of return objects (struct)
114 void** IoTRMICall::getStructObjects(string retType[], int numRet, void* retObj[]) {
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
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;
131 // Find the bytes length of a method
132 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
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;
145 // Calculate methodLen
146 methodLen = methodLen + paramLen;
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) {
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;
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;
190 void** IoTRMICall::getReturnObjects(char* retBytes, string retCls[], int numRet, void* retObj[]) {
192 // Byte scanning position
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)
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);
205 char retObjBytes[retLen];
206 memcpy(retObjBytes, retBytes + pos, retLen);
208 retObj[i] = IoTRMIUtil::getParamObject(retObj[i], retCls[i].c_str(), retObjBytes, retLen);