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);
38 map<string,int> mapSign2MethodId;
40 IoTSocketClient *rmiClient;
43 void getMethodIds(const string methodSign[], const int size);
48 IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult) {
50 rmiUtil = new IoTRMIUtil();
51 if (rmiUtil == NULL) {
52 perror("IoTRMICall: IoTRMIUtil isn't initialized!");
54 rmiClient = new IoTSocketClient(_port, _address, _rev, _bResult);
55 if (rmiClient == NULL) {
56 perror("IoTRMICall: IoTSocketClient isn't initialized!");
62 IoTRMICall::~IoTRMICall() {
65 if (rmiUtil != NULL) {
70 if (rmiClient != NULL) {
80 // Calls a method remotely by passing in parameters and getting a return object
81 void* IoTRMICall::remoteCall(int objectId, int methodId, string retType, string paramCls[],
82 void* paramObj[], int numParam, void* retObj) {
84 // Critical section that is used by different objects
85 lock_guard<mutex> guard(mtx);
86 // Send input parameters
87 int len = methodLength(paramCls, paramObj, numParam);
89 methodToBytes(objectId, methodId, paramCls, paramObj, method, numParam);
90 // IoTRMIUtil::printBytes(method, len, false);
93 rmiClient->sendBytes(method, len);
95 // Receive return value and return it to caller
96 if (retType.compare("void") == 0)
97 // Just make it NULL if it's a void return
101 char* retObjBytes = NULL;
102 retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
103 // IoTRMIUtil::printBytes(retObjBytes, retLen, false);
104 retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes, retLen);
111 // Find the bytes length of a method
112 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
114 // Get byte arrays and calculate method bytes length
115 // Start from the object Id + method Id...
116 int methodLen = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN;
117 for (int i = 0; i < numParam; i++) {
118 // Find the parameter length
119 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
120 if (paramLen == -1) { // Store the length of the field - indefinite length
121 paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
122 // Some space for param length, i.e. 32 bits for integer
123 methodLen = methodLen + IoTRMIUtil::PARAM_LEN;
125 // Calculate methodLen
126 methodLen = methodLen + paramLen;
133 // Convert method and its parameters into bytes
134 char* IoTRMICall::methodToBytes(int objectId, int methId, string paramCls[],
135 void* paramObj[], char* method, int numParam) {
137 // Get object Id in bytes
138 char objId[IoTRMIUtil::OBJECT_ID_LEN];
139 IoTRMIUtil::intToByteArray(objectId, objId);
140 memcpy(method, objId, IoTRMIUtil::OBJECT_ID_LEN);
141 int pos = IoTRMIUtil::OBJECT_ID_LEN;
142 // Get method Id in bytes
143 char methodId[IoTRMIUtil::METHOD_ID_LEN];
144 IoTRMIUtil::intToByteArray(methId, methodId);
145 memcpy(method + pos, methodId, IoTRMIUtil::METHOD_ID_LEN);
146 pos = pos + IoTRMIUtil::METHOD_ID_LEN;
147 // Get byte arrays and calculate method bytes length
148 for (int i = 0; i < numParam; i++) {
149 // Find the parameter length
150 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
151 if (paramLen == -1) { // Store the length of the field - indefinite length
152 paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
153 // Write the parameter length
154 char prmLenBytes[IoTRMIUtil::METHOD_ID_LEN];
155 IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
156 memcpy(method + pos, prmLenBytes, IoTRMIUtil::PARAM_LEN);
157 pos = pos + IoTRMIUtil::PARAM_LEN;
159 // Get array of bytes and put it in the array of array of bytes
160 char objBytes[paramLen];
161 IoTRMIUtil::getObjectBytes(objBytes, paramObj[i], paramCls[i].c_str());
162 memcpy(method + pos, objBytes, paramLen);
163 pos = pos + paramLen;