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 retLen = rmiUtil->getVarTypeSize(type, retObj);
94 // Need object bytes variable
95 char retObjBytes[retLen];
96 IoTRMIUtil::getObjectBytes(retObjBytes, retObj, type.c_str());
97 rmiServer->sendBytes(retObjBytes, retLen);
101 // Get method bytes from the socket
102 void IoTRMIObject::getMethodBytes() {
104 // Get method in bytes and update method length
105 methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
110 // Get signature from the method-Id-to-method-signature map
111 string IoTRMIObject::getSignature() {
114 char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
115 memcpy(methodIdBytes, methodBytes, IoTRMIUtil::METHOD_ID_LEN);
118 IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
120 return mapHash2Sign.find(methodId)->second;
124 // Get method parameters and return an array of parameter objects
126 // For primitive objects:
127 // | 32-bit method ID | m-bit actual data (fixed length) |
129 // For string, arrays, and non-primitive objects:
130 // | 32-bit method ID | 32-bit length | n-bit actual data | ...
131 void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* paramObj[]) {
133 // Byte scanning position
134 int pos = IoTRMIUtil::METHOD_ID_LEN;
135 for (int i = 0; i < numParam; i++) {
136 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
137 // Get the 32-bit field in the byte array to get the actual
138 // length (this is a param with indefinite length)
139 if (paramLen == -1) {
140 char bytPrmLen[IoTRMIUtil::PARAM_LEN];
141 memcpy(bytPrmLen, methodBytes + pos, IoTRMIUtil::PARAM_LEN);
142 pos = pos + IoTRMIUtil::PARAM_LEN;
143 int* prmLenPtr = IoTRMIUtil::byteArrayToInt(¶mLen, bytPrmLen);
144 paramLen = *prmLenPtr;
146 char paramBytes[paramLen];
147 memcpy(paramBytes, methodBytes + pos, paramLen);
148 pos = pos + paramLen;
149 paramObj[i] = IoTRMIUtil::getParamObject(paramObj[i], paramCls[i].c_str(), paramBytes, paramLen);
151 // Delete methodBytes
152 delete[] methodBytes;
161 void IoTRMIObject::getMethodIds(string methodSign[], int size) {
163 for(int i = 0; i < size; i++) {
164 int methodId = IoTRMIUtil::hashCode(methodSign[i]);
165 mapHash2Sign[methodId] = methodSign[i];