Fixed compiler for Java code generation (not heavily tested yet, but fixes include...
[iot2.git] / iotjava / iotrmi / C++ / IoTRMIObject.hpp
index 6cffb4ee4d52665f7bb244733952bf8b69c014c4..1dd3d57ba10b91c3c3a0d77d38dbd753eb9b95ea 100644 (file)
@@ -25,17 +25,20 @@ class IoTRMIObject {
                IoTRMIObject(int _port, bool* _bResult);
                ~IoTRMIObject();
                // Public methods
                IoTRMIObject(int _port, bool* _bResult);
                ~IoTRMIObject();
                // Public methods
-               void            sendReturnObj(void* retObj, string type);
-               char*           getMethodBytes();
-               int                     getMethodBytesLen();
-               void            setMethodBytes(char* _methodBytes);
-               int                     getObjectId();
-               static int      getObjectId(char* methodBytes);
-               int                     getMethodId();
-               void**          getMethodParams(string paramCls[], int numParam, void* paramObj[]);
+               void                            sendReturnObj(void* retObj, string type);
+               void                            sendReturnObj(void* retObj[], string type[], int numRet);
+               int                                     returnLength(void* retObj[], string retCls[], int numRet);
+               char*                           returnToBytes(void* retObj[], string retCls[], char* retBytes, int numRet);
+               char*                           getMethodBytes();
+               int                                     getMethodBytesLen();
+               void                            setMethodBytes(char* _methodBytes);
+               int                                     getObjectId();
+               static int                      getObjectId(char* methodBytes);
+               int                                     getMethodId();
+               static int                      getMethodId(char* methodBytes);
+               void**                          getMethodParams(string paramCls[], int numParam, void* paramObj[]);
 
        private:
 
        private:
-               //map<int,string>               mapMethodId2Sign;
                IoTRMIUtil                      *rmiUtil;
                IoTSocketServer         *rmiServer;
                char*                           methodBytes;
                IoTRMIUtil                      *rmiUtil;
                IoTSocketServer         *rmiServer;
                char*                           methodBytes;
@@ -56,7 +59,6 @@ IoTRMIObject::IoTRMIObject(int _port, bool* _bResult) {
 
        methodBytes = NULL;
        methodLen = 0;
 
        methodBytes = NULL;
        methodLen = 0;
-       //getMethodIds(_methodSign, _size);
 
        rmiServer = new IoTSocketServer(_port, _bResult);
        if (rmiServer == NULL) {
 
        rmiServer = new IoTSocketServer(_port, _bResult);
        if (rmiServer == NULL) {
@@ -65,6 +67,7 @@ IoTRMIObject::IoTRMIObject(int _port, bool* _bResult) {
        fflush(NULL);
        rmiServer->connect();
        fflush(NULL);
        fflush(NULL);
        rmiServer->connect();
        fflush(NULL);
+
 }
 
 
 }
 
 
@@ -102,11 +105,23 @@ void IoTRMIObject::sendReturnObj(void* retObj, string type) {
 }
 
 
 }
 
 
+// Send return values in bytes to the caller (for more than one object - struct)
+void IoTRMIObject::sendReturnObj(void* retObj[], string type[], int numRet) {
+
+       // Find the length of return object in bytes
+       int retLen = returnLength(retObj, type, numRet);
+       // Need object bytes variable
+       char retObjBytes[retLen];
+       returnToBytes(retObj, type, retObjBytes, numRet);
+       rmiServer->sendBytes(retObjBytes, retLen);
+}
+
+
 // Get method bytes from the socket
 char* IoTRMIObject::getMethodBytes() {
 
        // Get method in bytes and update method length
 // Get method bytes from the socket
 char* IoTRMIObject::getMethodBytes() {
 
        // Get method in bytes and update method length
-       //fflush(NULL);
+       fflush(NULL);
        methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
        fflush(NULL);
        return methodBytes;
        methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
        fflush(NULL);
        return methodBytes;
@@ -146,16 +161,22 @@ int IoTRMIObject::getObjectId(char* methodBytes) {
 }
 
 
 }
 
 
-// Set method bytes
-/*void IoTRMIObject::setMethodBytes(char* _methodBytes) {
+// Get methodId
+int IoTRMIObject::getMethodId() {
 
 
-       // Set method bytes
-       methodBytes = _methodBytes;
-}*/
+       // Get method Id
+       char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
+       memcpy(methodIdBytes, methodBytes + IoTRMIUtil::OBJECT_ID_LEN, IoTRMIUtil::METHOD_ID_LEN);
+       // Get method signature 
+       int methodId = 0;
+       IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
+       
+       return methodId;
+}
 
 
 
 
-// Get methodId
-int IoTRMIObject::getMethodId() {
+// Get methodId from bytes (static version)
+int IoTRMIObject::getMethodId(char* methodBytes) {
 
        // Get method Id
        char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
 
        // Get method Id
        char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
@@ -202,6 +223,54 @@ void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* para
 }
 
 
 }
 
 
+// Find the bytes length of a return object (struct that has more than 1 member)
+int    IoTRMIObject::returnLength(void* retObj[], string retCls[], int numRet) {
+
+       // Get byte arrays and calculate return bytes length
+       int returnLen = 0;
+       for (int i = 0; i < numRet; i++) {
+               // Find the return length
+               int retObjLen = rmiUtil->getTypeSize(retCls[i]);
+               if (retObjLen == -1) { // Store the length of the field - indefinite length
+                       retObjLen = rmiUtil->getVarTypeSize(retCls[i], retObj[i]);
+                       // Some space for return length, i.e. 32 bits for integer               
+                       returnLen = returnLen + IoTRMIUtil::RETURN_LEN;
+               }
+               // Calculate returnLen
+               returnLen = returnLen + retObjLen;
+       }
+
+       return returnLen;
+}
+
+
+// Convert return object (struct members) into bytes
+char* IoTRMIObject::returnToBytes(void* retObj[], string retCls[], char* retBytes, int numRet) {
+
+       int pos = 0;
+       // Get byte arrays and calculate return bytes length
+       for (int i = 0; i < numRet; i++) {
+               // Find the return length
+               int retObjLen = rmiUtil->getTypeSize(retCls[i]);
+               if (retObjLen == -1) { // Store the length of the field - indefinite length
+                       retObjLen = rmiUtil->getVarTypeSize(retCls[i], retObj[i]);
+                       // Write the return length
+                       char retLenBytes[IoTRMIUtil::RETURN_LEN];
+                       IoTRMIUtil::intToByteArray(retObjLen, retLenBytes);
+                       memcpy(retBytes + pos, retLenBytes, IoTRMIUtil::RETURN_LEN);                    
+                       pos = pos + IoTRMIUtil::RETURN_LEN;
+               }
+               // Get array of bytes and put it in the array of array of bytes
+               char objBytes[retObjLen];
+               IoTRMIUtil::getObjectBytes(objBytes, retObj[i], retCls[i].c_str());
+               memcpy(retBytes + pos, objBytes, retObjLen);
+               pos = pos + retObjLen;
+       }
+
+       return retBytes;
+}
+
+
 #endif
 
 
 #endif