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,
29 const string _methodSign[], const int _size);
32 int methodLength(string paramCls[], void* paramObj[], int numParam);
33 char* methodToBytes(int objectId, string methodSign, string paramCls[], void* paramObj[],
34 char* method, int numParam);
35 void* remoteCall(int objectId, string methodSign, string retType, string paramCls[],
36 void* paramObj[], int numParam, void* retObj);
39 map<string,int> mapSign2MethodId;
41 IoTSocketClient *rmiClient;
44 void getMethodIds(const string methodSign[], const int size);
49 IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, const string _methodSign[], const int _size) {
51 getMethodIds(_methodSign, _size);
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, string methodSign, 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, methodSign, paramCls, paramObj, method, numParam);
92 // IoTRMIUtil::printBytes(method, len, false);
95 rmiClient->sendBytes(method, len);
97 // Receive return value and return it to caller
98 if (retType.compare("void") == 0)
99 // Just make it NULL if it's a void return
103 char* retObjBytes = NULL;
104 retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
105 // IoTRMIUtil::printBytes(retObjBytes, retLen, false);
106 retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes, retLen);
113 // Find the bytes length of a method
114 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
116 // Get byte arrays and calculate method bytes length
117 // Start from the object Id + method Id...
118 int methodLen = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN;
119 for (int i = 0; i < numParam; i++) {
120 // Find the parameter length
121 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
122 if (paramLen == -1) { // Store the length of the field - indefinite length
123 paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
124 // Some space for param length, i.e. 32 bits for integer
125 methodLen = methodLen + IoTRMIUtil::PARAM_LEN;
127 // Calculate methodLen
128 methodLen = methodLen + paramLen;
135 // Convert method and its parameters into bytes
136 char* IoTRMICall::methodToBytes(int objectId, string methodSign, string paramCls[],
137 void* paramObj[], char* method, int numParam) {
139 // Get object Id in bytes
140 char objId[IoTRMIUtil::OBJECT_ID_LEN];
141 IoTRMIUtil::intToByteArray(objectId, objId);
142 memcpy(method, objId, IoTRMIUtil::OBJECT_ID_LEN);
143 int pos = IoTRMIUtil::OBJECT_ID_LEN;
144 // Get method Id in bytes
145 char methodId[IoTRMIUtil::METHOD_ID_LEN];
146 int methId = mapSign2MethodId.find(methodSign)->second;
147 IoTRMIUtil::intToByteArray(methId, methodId);
148 memcpy(method + pos, methodId, IoTRMIUtil::METHOD_ID_LEN);
149 pos = pos + IoTRMIUtil::METHOD_ID_LEN;
150 // Get byte arrays and calculate method bytes length
151 for (int i = 0; i < numParam; i++) {
152 // Find the parameter length
153 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
154 if (paramLen == -1) { // Store the length of the field - indefinite length
155 paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
156 // Write the parameter length
157 char prmLenBytes[IoTRMIUtil::METHOD_ID_LEN];
158 IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
159 memcpy(method + pos, prmLenBytes, IoTRMIUtil::PARAM_LEN);
160 pos = pos + IoTRMIUtil::PARAM_LEN;
162 // Get array of bytes and put it in the array of array of bytes
163 char objBytes[paramLen];
164 IoTRMIUtil::getObjectBytes(objBytes, paramObj[i], paramCls[i].c_str());
165 memcpy(method + pos, objBytes, paramLen);
166 pos = pos + paramLen;
176 void IoTRMICall::getMethodIds(const string methodSign[], const int size) {
178 for(int i = 0; i < size; i++) {
179 mapSign2MethodId[methodSign[i]] = i;