1 /** Class IoTRMIObject 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 skeleton part of the RMI
6 * communication. It instatiate an RMI object and activate
7 * a server process that handles RMI requests.
9 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
13 #ifndef _IOTRMIOBJECT_HPP__
14 #define _IOTRMIOBJECT_HPP__
18 #include "IoTRMIUtil.hpp"
19 #include "IoTSocketServer.hpp"
25 IoTRMIObject(int _port, bool* _bResult);
28 void sendReturnObj(void* retObj, string type);
29 void sendReturnObj(void* retObj[], string type[], int numRet);
30 int returnLength(void* retObj[], string retCls[], int numRet);
31 char* returnToBytes(void* retObj[], string retCls[], char* retBytes, int numRet);
32 char* getMethodBytes();
33 int getMethodBytesLen();
34 void setMethodBytes(char* _methodBytes);
36 static int getObjectId(char* methodBytes);
38 void** getMethodParams(string paramCls[], int numParam, void* paramObj[]);
42 IoTSocketServer *rmiServer;
47 void getMethodIds(const string methodSign[], const int size);
52 IoTRMIObject::IoTRMIObject(int _port, bool* _bResult) {
54 rmiUtil = new IoTRMIUtil();
55 if (rmiUtil == NULL) {
56 perror("IoTRMIObject: IoTRMIUtil isn't initialized!");
62 rmiServer = new IoTSocketServer(_port, _bResult);
63 if (rmiServer == NULL) {
64 perror("IoTRMIObject: IoTSocketServer isn't initialized!");
74 IoTRMIObject::~IoTRMIObject() {
77 if (rmiUtil != NULL) {
82 if (rmiServer != NULL) {
92 // Send return values in bytes to the caller
93 void IoTRMIObject::sendReturnObj(void* retObj, string type) {
95 // Find the length of return object in bytes
96 int retLen = rmiUtil->getTypeSize(type);
98 retLen = rmiUtil->getVarTypeSize(type, retObj);
100 // Need object bytes variable
101 char retObjBytes[retLen];
102 IoTRMIUtil::getObjectBytes(retObjBytes, retObj, type.c_str());
103 rmiServer->sendBytes(retObjBytes, retLen);
107 // Send return values in bytes to the caller (for more than one object - struct)
108 void IoTRMIObject::sendReturnObj(void* retObj[], string type[], int numRet) {
110 // Find the length of return object in bytes
111 int retLen = returnLength(retObj, type, numRet);
112 // Need object bytes variable
113 char retObjBytes[retLen];
114 returnToBytes(retObj, type, retObjBytes, numRet);
115 IoTRMIUtil::printBytes(retObjBytes, retLen, false);
116 rmiServer->sendBytes(retObjBytes, retLen);
120 // Get method bytes from the socket
121 char* IoTRMIObject::getMethodBytes() {
123 // Get method in bytes and update method length
125 methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
131 // Get method bytes length
132 int IoTRMIObject::getMethodBytesLen() {
138 // Get object Id from bytes
139 int IoTRMIObject::getObjectId() {
141 char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN];
142 memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN);
143 // Get method signature
145 IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes);
151 // Get object Id from bytes (static version)
152 int IoTRMIObject::getObjectId(char* methodBytes) {
154 char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN];
155 memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN);
156 // Get method signature
158 IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes);
165 int IoTRMIObject::getMethodId() {
168 char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
169 memcpy(methodIdBytes, methodBytes + IoTRMIUtil::OBJECT_ID_LEN, IoTRMIUtil::METHOD_ID_LEN);
170 // Get method signature
172 IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
178 // Get method parameters and return an array of parameter objects
180 // For primitive objects:
181 // | 32-bit method ID | m-bit actual data (fixed length) |
183 // For string, arrays, and non-primitive objects:
184 // | 32-bit method ID | 32-bit length | n-bit actual data | ...
185 void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* paramObj[]) {
187 // Byte scanning position
188 int pos = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN;
189 for (int i = 0; i < numParam; i++) {
190 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
191 // Get the 32-bit field in the byte array to get the actual
192 // length (this is a param with indefinite length)
193 if (paramLen == -1) {
194 char bytPrmLen[IoTRMIUtil::PARAM_LEN];
195 memcpy(bytPrmLen, methodBytes + pos, IoTRMIUtil::PARAM_LEN);
196 pos = pos + IoTRMIUtil::PARAM_LEN;
197 int* prmLenPtr = IoTRMIUtil::byteArrayToInt(¶mLen, bytPrmLen);
198 paramLen = *prmLenPtr;
200 char paramBytes[paramLen];
201 memcpy(paramBytes, methodBytes + pos, paramLen);
202 pos = pos + paramLen;
203 paramObj[i] = IoTRMIUtil::getParamObject(paramObj[i], paramCls[i].c_str(), paramBytes, paramLen);
205 // Delete methodBytes
206 delete[] methodBytes;
212 // Find the bytes length of a return object (struct that has more than 1 member)
213 int IoTRMIObject::returnLength(void* retObj[], string retCls[], int numRet) {
215 // Get byte arrays and calculate return bytes length
217 for (int i = 0; i < numRet; i++) {
218 // Find the return length
219 int retObjLen = rmiUtil->getTypeSize(retCls[i]);
220 if (retObjLen == -1) { // Store the length of the field - indefinite length
221 retObjLen = rmiUtil->getVarTypeSize(retCls[i], retObj[i]);
222 // Some space for return length, i.e. 32 bits for integer
223 returnLen = returnLen + IoTRMIUtil::RETURN_LEN;
225 // Calculate returnLen
226 returnLen = returnLen + retObjLen;
233 // Convert return object (struct members) into bytes
234 char* IoTRMIObject::returnToBytes(void* retObj[], string retCls[], char* retBytes, int numRet) {
237 // Get byte arrays and calculate return bytes length
238 for (int i = 0; i < numRet; i++) {
239 // Find the return length
240 int retObjLen = rmiUtil->getTypeSize(retCls[i]);
241 if (retObjLen == -1) { // Store the length of the field - indefinite length
242 retObjLen = rmiUtil->getVarTypeSize(retCls[i], retObj[i]);
243 // Write the return length
244 char retLenBytes[IoTRMIUtil::RETURN_LEN];
245 IoTRMIUtil::intToByteArray(retObjLen, retLenBytes);
246 memcpy(retBytes + pos, retLenBytes, IoTRMIUtil::RETURN_LEN);
247 pos = pos + IoTRMIUtil::RETURN_LEN;
249 // Get array of bytes and put it in the array of array of bytes
250 char objBytes[retObjLen];
251 IoTRMIUtil::getObjectBytes(objBytes, retObj[i], retCls[i].c_str());
252 memcpy(retBytes + pos, objBytes, retObjLen);
253 pos = pos + retObjLen;