Fixed compiler for Java code generation (not heavily tested yet, but fixes include...
[iot2.git] / iotjava / iotrmi / C++ / IoTRMIObject.hpp
index 084fb7b4f336a53f4e4d028f88a8098684623223..1dd3d57ba10b91c3c3a0d77d38dbd753eb9b95ea 100644 (file)
@@ -22,20 +22,23 @@ using namespace std;
 
 class IoTRMIObject {
        public:
-               IoTRMIObject(int _port, bool* _bResult, const string _methodSign[], const int _size);
+               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);
-               string          getSignature();
-               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:
-               map<int,string>         mapMethodId2Sign;
                IoTRMIUtil                      *rmiUtil;
                IoTSocketServer         *rmiServer;
                char*                           methodBytes;
@@ -47,7 +50,7 @@ class IoTRMIObject {
 
 
 // Constructor
-IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, const string _methodSign[], const int _size) {
+IoTRMIObject::IoTRMIObject(int _port, bool* _bResult) {
 
        rmiUtil = new IoTRMIUtil();
        if (rmiUtil == NULL) {
@@ -56,7 +59,6 @@ IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, const string _methodSign[]
 
        methodBytes = NULL;
        methodLen = 0;
-       getMethodIds(_methodSign, _size);
 
        rmiServer = new IoTSocketServer(_port, _bResult);
        if (rmiServer == NULL) {
@@ -65,6 +67,7 @@ IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, const string _methodSign[]
        fflush(NULL);
        rmiServer->connect();
        fflush(NULL);
+
 }
 
 
@@ -102,14 +105,24 @@ 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
-       cout << "Got into getMethodBytes()" << endl;
-       //fflush(NULL);
+       fflush(NULL);
        methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
-       cout << "Got into getMethodBytes() 2" << endl;
        fflush(NULL);
        return methodBytes;
 }
@@ -148,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 signature from the method-Id-to-method-signature map
-string IoTRMIObject::getSignature() {
+// Get methodId from bytes (static version)
+int IoTRMIObject::getMethodId(char* methodBytes) {
 
        // Get method Id
        char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
@@ -166,7 +185,7 @@ string IoTRMIObject::getSignature() {
        int methodId = 0;
        IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
        
-       return mapMethodId2Sign.find(methodId)->second;
+       return methodId;
 }
 
 
@@ -204,14 +223,51 @@ void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* para
 }
 
 
-// *************
-//    Helpers
-// *************
-void IoTRMIObject::getMethodIds(const string methodSign[], const int size) {
+// 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;
+}
+
 
-       for(int i = 0; i < size; i++) {
-               mapMethodId2Sign[i] = methodSign[i];
+// 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;
 }