using namespace std;
-class IoTRMIObject {
+class IoTRMIObject final {
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> mapHash2Sign;
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) {
methodBytes = NULL;
methodLen = 0;
- getMethodIds(_methodSign, _size);
rmiServer = new IoTSocketServer(_port, _bResult);
if (rmiServer == NULL) {
fflush(NULL);
rmiServer->connect();
fflush(NULL);
+
}
}
+// 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 mapHash2Sign.find(methodId)->second;
+ return methodId;
}
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
}
-// *************
-// 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++) {
- int methodId = IoTRMIUtil::hashCode(methodSign[i]);
- mapHash2Sign[methodId] = 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;
}