Adding support to returning struct/list of struct objects
[iot2.git] / iotjava / iotrmi / C++ / IoTRMICall.hpp
index 729b03614ab4da6cbc14c7a90e04d72193fc83ce..2ac2509f39c54b84c938632a20a401f9d6127f10 100644 (file)
@@ -33,6 +33,8 @@ class IoTRMICall {
                                                                char* method, int numParam);
                void*   remoteCall(int objectId, int methodId, string retType, string paramCls[], 
                                                                void* paramObj[], int numParam, void* retObj);
+               void**  getStructObjects(string retType[], int numRet, void* retObj[]);
+               void**  getReturnObjects(char* retBytes, string retCls[], int numRet, void* retObj[]);
 
        private:
                map<string,int>         mapSign2MethodId;
@@ -87,7 +89,6 @@ void* IoTRMICall::remoteCall(int objectId, int methodId, string retType, string
        int len = methodLength(paramCls, paramObj, numParam);
        char method[len];
        methodToBytes(objectId, methodId, paramCls, paramObj, method, numParam);
-//     IoTRMIUtil::printBytes(method, len, false);
        // Send bytes
        fflush(NULL);
        rmiClient->sendBytes(method, len);
@@ -100,14 +101,33 @@ void* IoTRMICall::remoteCall(int objectId, int methodId, string retType, string
                int retLen = 0;
                char* retObjBytes = NULL;
                retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
-//             IoTRMIUtil::printBytes(retObjBytes, retLen, false);
                retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes, retLen);
+               // Delete received bytes object
+               delete[] retObjBytes;
        }
        
        return retObj;
 }
 
 
+// Get a set of return objects (struct)
+void** IoTRMICall::getStructObjects(string retType[], int numRet, void* retObj[]) {
+
+       // Critical section that is used by different objects
+       lock_guard<mutex> guard(mtx);
+       // Receive struct return value and return it to caller
+       int retLen = 0;
+       char* retObjBytes = NULL;
+       // Return size of array of struct
+       retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
+       retObj = getReturnObjects(retObjBytes, retType, numRet, retObj);
+       // Delete received bytes object
+       delete[] retObjBytes;
+       
+       return retObj;
+}
+
+
 // Find the bytes length of a method
 int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) {
 
@@ -151,7 +171,7 @@ char* IoTRMICall::methodToBytes(int objectId, int methId, string paramCls[],
                if (paramLen == -1) { // Store the length of the field - indefinite length
                        paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
                        // Write the parameter length
-                       char prmLenBytes[IoTRMIUtil::METHOD_ID_LEN];
+                       char prmLenBytes[IoTRMIUtil::PARAM_LEN];
                        IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
                        memcpy(method + pos, prmLenBytes, IoTRMIUtil::PARAM_LEN);                       
                        pos = pos + IoTRMIUtil::PARAM_LEN;
@@ -166,6 +186,32 @@ char* IoTRMICall::methodToBytes(int objectId, int methId, string paramCls[],
        return method;
 }
 
+
+void** IoTRMICall::getReturnObjects(char* retBytes, string retCls[], int numRet, void* retObj[]) {
+
+       // Byte scanning position
+       int pos = 0;
+       for (int i = 0; i < numRet; i++) {
+               int retLen = rmiUtil->getTypeSize(retCls[i]);
+               // Get the 32-bit field in the byte array to get the actual
+               //              length (this is a param with indefinite length)
+               if (retLen == -1) {
+                       char bytRetLen[IoTRMIUtil::RETURN_LEN];
+                       memcpy(bytRetLen, retBytes + pos, IoTRMIUtil::RETURN_LEN);
+                       pos = pos + IoTRMIUtil::RETURN_LEN;
+                       int* retLenPtr = IoTRMIUtil::byteArrayToInt(&retLen, bytRetLen);
+                       retLen = *retLenPtr;
+               }
+               char retObjBytes[retLen];
+               memcpy(retObjBytes, retBytes + pos, retLen);
+               pos = pos + retLen;
+               retObj[i] = IoTRMIUtil::getParamObject(retObj[i], retCls[i].c_str(), retObjBytes, retLen);
+       }
+
+       return retObj;
+}
+
+
 #endif