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, const string _methodSign[], const int _size);
28 void sendReturnObj(void* retObj, string type);
29 char* getMethodBytes();
30 int getMethodBytesLen();
31 void setMethodBytes(char* _methodBytes);
33 static int getObjectId(char* methodBytes);
34 string getSignature();
35 void** getMethodParams(string paramCls[], int numParam, void* paramObj[]);
38 map<int,string> mapMethodId2Sign;
40 IoTSocketServer *rmiServer;
45 void getMethodIds(const string methodSign[], const int size);
50 IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, const string _methodSign[], const int _size) {
52 rmiUtil = new IoTRMIUtil();
53 if (rmiUtil == NULL) {
54 perror("IoTRMIObject: IoTRMIUtil isn't initialized!");
59 getMethodIds(_methodSign, _size);
61 rmiServer = new IoTSocketServer(_port, _bResult);
62 if (rmiServer == NULL) {
63 perror("IoTRMIObject: IoTSocketServer isn't initialized!");
72 IoTRMIObject::~IoTRMIObject() {
75 if (rmiUtil != NULL) {
80 if (rmiServer != NULL) {
90 // Send return values in bytes to the caller
91 void IoTRMIObject::sendReturnObj(void* retObj, string type) {
93 // Find the length of return object in bytes
94 int retLen = rmiUtil->getTypeSize(type);
96 retLen = rmiUtil->getVarTypeSize(type, retObj);
98 // Need object bytes variable
99 char retObjBytes[retLen];
100 IoTRMIUtil::getObjectBytes(retObjBytes, retObj, type.c_str());
101 rmiServer->sendBytes(retObjBytes, retLen);
105 // Get method bytes from the socket
106 char* IoTRMIObject::getMethodBytes() {
108 // Get method in bytes and update method length
110 methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
116 // Get method bytes length
117 int IoTRMIObject::getMethodBytesLen() {
123 // Get object Id from bytes
124 int IoTRMIObject::getObjectId() {
126 char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN];
127 memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN);
128 // Get method signature
130 IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes);
136 // Get object Id from bytes (static version)
137 int IoTRMIObject::getObjectId(char* methodBytes) {
139 char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN];
140 memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN);
141 // Get method signature
143 IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes);
150 void IoTRMIObject::setMethodBytes(char* _methodBytes) {
153 methodBytes = _methodBytes;
157 // Get signature from the method-Id-to-method-signature map
158 string IoTRMIObject::getSignature() {
161 char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
162 memcpy(methodIdBytes, methodBytes + IoTRMIUtil::OBJECT_ID_LEN, IoTRMIUtil::METHOD_ID_LEN);
163 // Get method signature
165 IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
167 return mapMethodId2Sign.find(methodId)->second;
171 // Get method parameters and return an array of parameter objects
173 // For primitive objects:
174 // | 32-bit method ID | m-bit actual data (fixed length) |
176 // For string, arrays, and non-primitive objects:
177 // | 32-bit method ID | 32-bit length | n-bit actual data | ...
178 void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* paramObj[]) {
180 // Byte scanning position
181 int pos = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN;
182 for (int i = 0; i < numParam; i++) {
183 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
184 // Get the 32-bit field in the byte array to get the actual
185 // length (this is a param with indefinite length)
186 if (paramLen == -1) {
187 char bytPrmLen[IoTRMIUtil::PARAM_LEN];
188 memcpy(bytPrmLen, methodBytes + pos, IoTRMIUtil::PARAM_LEN);
189 pos = pos + IoTRMIUtil::PARAM_LEN;
190 int* prmLenPtr = IoTRMIUtil::byteArrayToInt(¶mLen, bytPrmLen);
191 paramLen = *prmLenPtr;
193 char paramBytes[paramLen];
194 memcpy(paramBytes, methodBytes + pos, paramLen);
195 pos = pos + paramLen;
196 paramObj[i] = IoTRMIUtil::getParamObject(paramObj[i], paramCls[i].c_str(), paramBytes, paramLen);
198 // Delete methodBytes
199 delete[] methodBytes;
208 void IoTRMIObject::getMethodIds(const string methodSign[], const int size) {
210 for(int i = 0; i < size; i++) {
211 mapMethodId2Sign[i] = methodSign[i];