1 /** Class IoTRMICall 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 stub part of the RMI
6 * communication. It bridges and creates RMI requests to be
7 * transferred into the RMI object.
9 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
13 #ifndef _IOTRMICALL_HPP__
14 #define _IOTRMICALL_HPP__
18 #include "IoTRMIUtil.hpp"
19 #include "IoTSocketClient.hpp"
25 IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult);
28 int methodLength(string paramCls[], void* paramObj[], int numParam);
29 char* methodToBytes(string methodSign, string paramCls[], void* paramObj[],
30 char* method, int numParam);
31 void* remoteCall(string methodSign, string retType, string paramCls[],
32 void* paramObj[], int numParam, void* retObj);
36 IoTSocketClient *rmiClient;
41 IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult) {
43 rmiUtil = new IoTRMIUtil();
44 if (rmiUtil == NULL) {
45 perror("IoTRMICall: IoTRMIUtil isn't initialized!");
47 rmiClient = new IoTSocketClient(_port, _address, _rev, _bResult);
48 if (rmiClient == NULL) {
49 perror("IoTRMICall: IoTSocketClient isn't initialized!");
55 IoTRMICall::~IoTRMICall() {
58 if (rmiUtil != NULL) {
63 if (rmiClient != NULL) {
73 // Calls a method remotely by passing in parameters and getting a return object
74 void* IoTRMICall::remoteCall(string methodSign, string retType, string paramCls[],
75 void* paramObj[], int numParam, void* retObj) {
77 // Send input parameters
78 int len = methodLength(paramCls, paramObj, numParam);
80 methodToBytes(methodSign, paramCls, paramObj, method, numParam);
83 rmiClient->sendBytes(method, len);
85 // Receive return value and return it to caller
86 if (retType.compare("void") == 0)
87 // Just make it NULL if it's a void return
91 char* retObjBytes = NULL;
92 retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
93 retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes);
100 // Find the bytes length of a method
101 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
103 // Get byte arrays and calculate method bytes length
104 // Start from the method Id...
105 int methodLen = IoTRMIUtil::METHOD_ID_LEN;
106 for (int i = 0; i < numParam; i++) {
107 // Find the parameter length
108 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
109 if (paramLen == -1) { // Store the length of the field - indefinite length
110 if (paramCls[i].compare("string") == 0) {
111 // Get the length of the string through void* casting to string*
112 paramLen = (*(string*)paramObj[i]).length();
114 // Some space for param length, i.e. 32 bits for integer
115 methodLen = methodLen + IoTRMIUtil::PARAM_LEN;
117 // Calculate methodLen
118 methodLen = methodLen + paramLen;
125 // Convert method and its parameters into bytes
126 char* IoTRMICall::methodToBytes(string methodSign, string paramCls[],
127 void* paramObj[], char* method, int numParam) {
129 // Get method ID in bytes
130 char methodId[IoTRMIUtil::METHOD_ID_LEN];
131 IoTRMIUtil::getHashCodeBytes(methodSign, methodId);
132 memcpy(method, methodId, IoTRMIUtil::METHOD_ID_LEN);
133 int pos = IoTRMIUtil::METHOD_ID_LEN;
134 // Get byte arrays and calculate method bytes length
135 for (int i = 0; i < numParam; i++) {
136 // Find the parameter length
137 int paramLen = rmiUtil->getTypeSize(paramCls[i]);
138 if (paramLen == -1) { // Store the length of the field - indefinite length
139 if (paramCls[i].compare("string") == 0) {
140 // Get the length of the string through void* casting to string*
141 paramLen = (*(string*)paramObj[i]).length();
143 // Write the parameter length
144 char prmLenBytes[IoTRMIUtil::METHOD_ID_LEN];
145 IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
146 memcpy(method + pos, prmLenBytes, IoTRMIUtil::PARAM_LEN);
147 pos = pos + IoTRMIUtil::PARAM_LEN;
149 // Get array of bytes and put it in the array of array of bytes
150 char objBytes[paramLen];
151 IoTRMIUtil::getObjectBytes(objBytes, paramObj[i], paramCls[i].c_str());
152 memcpy(method + pos, objBytes, paramLen);
153 pos = pos + paramLen;