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, string _methodSign[], int _size);
28 void sendReturnObj(void* retObj, string type);
29 void getMethodBytes();
30 string getSignature();
31 void** getMethodParams(string paramCls[], int numParam, void* paramObj[]);
34 map<int,string> mapHash2Sign;
36 IoTSocketServer *rmiServer;
41 void getMethodIds(string methodSign[], int size);
46 IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int _size) {
48 rmiUtil = new IoTRMIUtil();
49 if (rmiUtil == NULL) {
50 perror("IoTRMIObject: IoTRMIUtil isn't initialized!");
55 getMethodIds(_methodSign, _size);
57 rmiServer = new IoTSocketServer(_port, _bResult);
58 if (rmiServer == NULL) {
59 perror("IoTRMIObject: IoTSocketServer isn't initialized!");
68 IoTRMIObject::~IoTRMIObject() {
71 if (rmiUtil != NULL) {
76 if (rmiServer != NULL) {
86 // Send return values in bytes to the caller
87 void IoTRMIObject::sendReturnObj(void* retObj, string type) {
89 // Find the length of return object in bytes
90 int retLen = rmiUtil->getTypeSize(type);
92 if (type.compare("string") == 0) {
93 // Get the length of the string through void* casting to string*
94 retLen = (*(string*)retObj).length();
97 // Need object bytes variable
98 char retObjBytes[retLen];
99 IoTRMIUtil::getObjectBytes(retObjBytes, retObj, type.c_str());
100 rmiServer->sendBytes(retObjBytes, retLen);
104 // Get method bytes from the socket
105 void IoTRMIObject::getMethodBytes() {
107 // Get method in bytes and update method length
108 methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
110 IoTRMIUtil::printBytes(methodBytes, methodLen, false);
114 // Get signature from the method-Id-to-method-signature map
115 string IoTRMIObject::getSignature() {
118 char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
119 memcpy(methodIdBytes, methodBytes, IoTRMIUtil::METHOD_ID_LEN);
122 IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
123 cout << "Method Id: " << methodId << endl;
125 return mapHash2Sign.find(methodId)->second;
129 // Get method parameters and return an array of parameter objects
131 // For primitive objects:
132 // | 32-bit method ID | m-bit actual data (fixed length) |
134 // For string, arrays, and non-primitive objects:
135 // | 32-bit method ID | 32-bit length | n-bit actual data | ...
136 void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* paramObj[]) {
138 // Byte scanning position
139 int pos = IoTRMIUtil::METHOD_ID_LEN;
140 for (int i = 0; i < numParam; i++) {
142 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
143 // Get the 32-bit field in the byte array to get the actual
144 // length (this is a param with indefinite length)
145 if (paramLen == -1) {
146 char bytPrmLen[IoTRMIUtil::PARAM_LEN];
147 memcpy(bytPrmLen, methodBytes + pos, IoTRMIUtil::PARAM_LEN);
148 pos = pos + IoTRMIUtil::PARAM_LEN;
149 int* prmLenPtr = IoTRMIUtil::byteArrayToInt(¶mLen, bytPrmLen);
150 paramLen = *prmLenPtr;
152 char paramBytes[paramLen];
153 memcpy(paramBytes, methodBytes + pos, paramLen);
154 pos = pos + paramLen;
155 paramObj[i] = IoTRMIUtil::getParamObject(paramObj[i], paramCls[i].c_str(), paramBytes);
158 // Delete methodBytes
159 delete[] methodBytes;
168 void IoTRMIObject::getMethodIds(string methodSign[], int size) {
170 for(int i = 0; i < size; i++) {
171 int methodId = IoTRMIUtil::hashCode(methodSign[i]);
172 mapHash2Sign[methodId] = methodSign[i];