Fixed compiler for Java code generation (not heavily tested yet, but fixes include...
[iot2.git] / iotjava / iotrmi / C++ / IoTRMIObject.hpp
index af0541befdd53420d6c661a5f484dbc9954316cd..1dd3d57ba10b91c3c3a0d77d38dbd753eb9b95ea 100644 (file)
@@ -22,28 +22,35 @@ using namespace std;
 
 class IoTRMIObject {
        public:
-               IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int _size);
+               IoTRMIObject(int _port, bool* _bResult);
                ~IoTRMIObject();
                // Public methods
-               void            sendReturnObj(void* retObj, string type);
-               void            getMethodBytes();
-               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;
                int                                     methodLen;
 
                // Private methods
-               void                            getMethodIds(string methodSign[], int size);
+               void                            getMethodIds(const string methodSign[], const int size);
 };
 
 
 // Constructor
-IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int _size) {
+IoTRMIObject::IoTRMIObject(int _port, bool* _bResult) {
 
        rmiUtil = new IoTRMIUtil();
        if (rmiUtil == NULL) {
@@ -52,7 +59,6 @@ IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int
 
        methodBytes = NULL;
        methodLen = 0;
-       getMethodIds(_methodSign, _size);
 
        rmiServer = new IoTSocketServer(_port, _bResult);
        if (rmiServer == NULL) {
@@ -61,6 +67,7 @@ IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int
        fflush(NULL);
        rmiServer->connect();
        fflush(NULL);
+
 }
 
 
@@ -98,26 +105,87 @@ 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
-void IoTRMIObject::getMethodBytes() {
+char* IoTRMIObject::getMethodBytes() {
 
        // Get method in bytes and update method length
+       fflush(NULL);
        methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen);
        fflush(NULL);
+       return methodBytes;
 }
 
 
-// Get signature from the method-Id-to-method-signature map
-string IoTRMIObject::getSignature() {
+// Get method bytes length
+int IoTRMIObject::getMethodBytesLen() {
+
+       return methodLen;
+}
+
+
+// Get object Id from bytes
+int IoTRMIObject::getObjectId() {
+
+       char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN];
+       memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN);
+       // Get method signature 
+       int objectId = 0;
+       IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes);
+       
+       return objectId;
+}
+
+
+// Get object Id from bytes (static version)
+int IoTRMIObject::getObjectId(char* methodBytes) {
+
+       char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN];
+       memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN);
+       // Get method signature 
+       int objectId = 0;
+       IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes);
+       
+       return objectId;
+}
+
+
+// Get methodId
+int IoTRMIObject::getMethodId() {
+
+       // 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 from bytes (static version)
+int IoTRMIObject::getMethodId(char* methodBytes) {
 
        // Get method Id
        char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN];
-       memcpy(methodIdBytes, methodBytes, IoTRMIUtil::METHOD_ID_LEN);
-       // Get method object 
+       memcpy(methodIdBytes, methodBytes + IoTRMIUtil::OBJECT_ID_LEN, IoTRMIUtil::METHOD_ID_LEN);
+       // Get method signature 
        int methodId = 0;
        IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes);
        
-       return mapMethodId2Sign.find(methodId)->second;
+       return methodId;
 }
 
 
@@ -131,7 +199,7 @@ string IoTRMIObject::getSignature() {
 void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* paramObj[]) {
 
        // Byte scanning position
-       int pos = IoTRMIUtil::METHOD_ID_LEN;
+       int pos = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN;
        for (int i = 0; i < numParam; i++) {
                int paramLen = rmiUtil->getTypeSize(paramCls[i]);
                // Get the 32-bit field in the byte array to get the actual
@@ -155,14 +223,51 @@ void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* para
 }
 
 
-// *************
-//    Helpers
-// *************
-void IoTRMIObject::getMethodIds(string methodSign[], 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;
 }