Fixing bug for return value from callback in C++ (sendReturnObj is called twice)...
[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                                         const string _methodSign[], const int _size);
30                 ~IoTRMICall();
31                 // Public methods
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);
37
38         private:
39                 map<string,int>         mapSign2MethodId;
40                 IoTRMIUtil                      *rmiUtil;
41                 IoTSocketClient         *rmiClient;
42
43                 // Private methods
44                 void                            getMethodIds(const string methodSign[], const int size);
45 };
46
47
48 // Constructor
49 IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, const string _methodSign[], const int _size) {
50
51         getMethodIds(_methodSign, _size);
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, string methodSign, 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, methodSign, paramCls, paramObj, method, numParam);
92 //      IoTRMIUtil::printBytes(method, len, false);
93         // Send bytes
94         fflush(NULL);
95         rmiClient->sendBytes(method, len);
96         fflush(NULL);
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
100                 retObj = NULL;
101         else {
102                 int retLen = 0;
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);
107         }
108         
109         return retObj;
110 }
111
112
113 // Find the bytes length of a method
114 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
115
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;
126                 }
127                 // Calculate methodLen
128                 methodLen = methodLen + paramLen;
129         }
130
131         return methodLen;
132 }
133
134
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) {
138
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;
161                 }
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;
167         }
168
169         return method;
170 }
171
172
173 // *************
174 //    Helpers
175 // *************
176 void IoTRMICall::getMethodIds(const string methodSign[], const int size) {
177
178         for(int i = 0; i < size; i++) {
179                 mapSign2MethodId[methodSign[i]] = i;
180         }
181 }
182
183
184
185 #endif
186
187