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;
152 // Convert method and its parameters into bytes
153 char* IoTRMICall::methodToBytes(int objectId, int methId, string paramCls[],
154 void* paramObj[], char* method, int numParam) {
156 // Get object Id in bytes
157 char objId[IoTRMIUtil::OBJECT_ID_LEN];
158 IoTRMIUtil::intToByteArray(objectId, objId);
159 memcpy(method, objId, IoTRMIUtil::OBJECT_ID_LEN);
160 int pos = IoTRMIUtil::OBJECT_ID_LEN;
161 // Get method Id in bytes
162 char methodId[IoTRMIUtil::METHOD_ID_LEN];
163 IoTRMIUtil::intToByteArray(methId, methodId);
164 memcpy(method + pos, methodId, IoTRMIUtil::METHOD_ID_LEN);
165 pos = pos + IoTRMIUtil::METHOD_ID_LEN;
166 // Get byte arrays and calculate method bytes length
167 for (int i = 0; i < numParam; i++) {
168 // Find the parameter length
169 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
170 if (paramLen == -1) { // Store the length of the field - indefinite length
171 paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
172 // Write the parameter length
173 char prmLenBytes[IoTRMIUtil::PARAM_LEN];
174 IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
175 memcpy(method + pos, prmLenBytes, IoTRMIUtil::PARAM_LEN);
176 pos = pos + IoTRMIUtil::PARAM_LEN;
178 // Get array of bytes and put it in the array of array of bytes
179 char objBytes[paramLen];
180 IoTRMIUtil::getObjectBytes(objBytes, paramObj[i], paramCls[i].c_str());
181 memcpy(method + pos, objBytes, paramLen);
182 pos = pos + paramLen;
189 void** IoTRMICall::getReturnObjects(char* retBytes, string retCls[], int numRet, void* retObj[]) {
191 // Byte scanning position
193 for (int i = 0; i < numRet; i++) {
194 int retLen = rmiUtil->getTypeSize(retCls[i]);
195 // Get the 32-bit field in the byte array to get the actual
196 // length (this is a param with indefinite length)
198 char bytRetLen[IoTRMIUtil::RETURN_LEN];
199 memcpy(bytRetLen, retBytes + pos, IoTRMIUtil::RETURN_LEN);
200 pos = pos + IoTRMIUtil::RETURN_LEN;
201 int* retLenPtr = IoTRMIUtil::byteArrayToInt(&retLen, bytRetLen);
204 char retObjBytes[retLen];
205 memcpy(retObjBytes, retBytes + pos, retLen);
207 retObj[i] = IoTRMIUtil::getParamObject(retObj[i], retCls[i].c_str(), retObjBytes, retLen);