Removing printing statements from C++ RMI library - this will cause SO files to get...
[iot2.git] / iotjava / iotrmi / C++ / sample / TestClass_Skeleton.hpp
index 28bf1570c6288d18883db1a6f2393d4dd6aa7850..96b37b45276782a977dd4db14e742f6104253a7c 100644 (file)
@@ -2,6 +2,8 @@
 #define _TESTCLASS_SKELETON_HPP__
 
 #include <iostream>
+#include <exception>
+#include <set>
 #include "../IoTRMIObject.hpp"
 #include "../IoTRMICall.hpp"
 #include "CallBack_CBStub.hpp"
@@ -25,8 +27,8 @@ class TestClass_Skeleton : public TestClassInterface {
                void                    registerCallback(CallBackInterface* _cb);
                void                    registerCallback(vector<CallBackInterface*> _cb);
                int                             callBack();
-               void                    handleStruct(vector<data> vecData);
-               void                    handleEnum(vector<EnumC> vecEn);
+               vector<data>    handleStruct(vector<data> vecData);
+               vector<EnumC>   handleEnum(vector<EnumC> vecEn);
                
                void                    ___setA();
                void                    ___setB();
@@ -46,12 +48,14 @@ class TestClass_Skeleton : public TestClassInterface {
                int                             ___enumSize();
                void                    ___handleEnum(int enumsize1);
 
-               const static int        size = 12;
-               const static string methodSignatures[size];
-
        private:                
                TestClassInterface                      *tc;
                IoTRMIObject                            *rmiObj;
+               // Permission setup
+               const static int                        object0Id = 0;
+               //const static int                      object0Permission[];
+               const static set<int>           set0Allowed;
+               
                IoTRMICall                                      *rmiCall;
                static int                                      objIdCnt;
                vector<CallBackInterface*>      vecCBObj;
@@ -59,22 +63,10 @@ class TestClass_Skeleton : public TestClassInterface {
 };
 
 
-const string TestClass_Skeleton::methodSignatures[TestClass_Skeleton::size] = {
-       "voidsetA(int)", // 0
-       "voidsetB(float)", // 1
-       "voidsetC(string)", // 2
-       "sumArray(string[])", // 3
-       //"sumArray(int[])",
-       "intsetAndGetA(int)", // 4
-       "intsetACAndGetA(string,int)", // 5
-       "intcallBack()", // 6
-       "voidregisterCallBack(CallBackInterface)", // 7
-       "voidregisterCallBack(CallBackInterface[])", // 8
-       "registercallback", // 9
-       "handleStruct(StructJ[])", // 10
-       "structsize" // 11
-};
-
+// Permission setup
+//const int TestClass_Skeleton::object0Id = 0;
+//const int TestClass_Skeleton::object0Permission[] = {0, 1, 2, 3, 4, 5};
+const set<int> TestClass_Skeleton::set0Allowed {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
 
 int TestClass_Skeleton::objIdCnt = 0;
 
@@ -253,6 +245,9 @@ void TestClass_Skeleton::___regCB() {
        rmiObj->getMethodParams(paramCls, numParam, paramObj);
        // Instantiate IoTRMICall object
        bool bResult = false;
+       cout << "Port: " << param1 << endl;
+       cout << "Address: " << param2 << endl;
+
        rmiCall = new IoTRMICall(param1, param2.c_str(), param3, &bResult);
 }
 
@@ -289,9 +284,9 @@ void TestClass_Skeleton::___callBack() {
 }
 
 
-void TestClass_Skeleton::handleStruct(vector<data> vecData) {
+vector<data> TestClass_Skeleton::handleStruct(vector<data> vecData) {
 
-       tc->handleStruct(vecData);
+       return tc->handleStruct(vecData);
 }
 
 
@@ -332,13 +327,33 @@ void TestClass_Skeleton::___handleStruct(int structsize1) {
                dat[i].value = param2[i];
                dat[i].year = param3[i];
        }
-       handleStruct(dat);
+       // This is a return value of type vector of struct
+       // If no return value, then just "handleStruct(dat)"
+       vector<data> retData = handleStruct(dat);
+       // Send the length first!
+       int retLength = retData.size();
+       void* retObj = &retLength;
+       rmiObj->sendReturnObj(retObj, "int");
+       // Send the actual bytes - struct of 3 members
+       int numRetObj = 3*retLength;
+       string retCls[numRetObj];
+       void* retObj2[numRetObj];
+       pos = 0;
+       for(int i = 0; i < retLength; i++) {
+               retCls[pos] = "string";
+               retObj2[pos] = &retData[i].name; pos++;
+               retCls[pos] = "float";
+               retObj2[pos] = &retData[i].value; pos++;
+               retCls[pos] = "int";
+               retObj2[pos] = &retData[i].year; pos++;
+       }
+       rmiObj->sendReturnObj(retObj2, retCls, numRetObj);
 }
 
 
-void TestClass_Skeleton::handleEnum(vector<EnumC> vecEn) {
+vector<EnumC> TestClass_Skeleton::handleEnum(vector<EnumC> vecEn) {
 
-       tc->handleEnum(vecEn);
+       return tc->handleEnum(vecEn);
 }
 
 
@@ -353,35 +368,61 @@ int TestClass_Skeleton::___enumSize() {
 }
 
 
-void TestClass_Skeleton::___handleEnum(int enumsize1) {
+void TestClass_Skeleton::___handleEnum() {
 
-       string paramCls[enumsize1];
-       void* paramObj[enumsize1];
-       int numParam = enumsize1;
-       // define array of everything
-       int param1[enumsize1];
-       for(int i=0; i < enumsize1; i++) {
-               paramCls[i] = "int";
-               paramObj[i] = &param1[i];
-       }
+       int numParam = 1;
+       string paramCls[] = { "int[]" };
+       vector<int> paramInt;
+       void* paramObj[] = { &paramInt };
+       // Receive the array of integers
        rmiObj->getMethodParams(paramCls, numParam, paramObj);
-       vector<EnumC> en(enumsize1);
-       for (int i=0; i < enumsize1; i++) {
-               en[i] = (EnumC) param1[i];
+       int enumSize1 = paramInt.size();
+       vector<EnumC> en(enumSize1);
+       for (int i=0; i < enumSize1; i++) {
+               en[i] = (EnumC) paramInt[i];
        }
-       handleEnum(en);
+       //handleEnum(en);
+       // if void, just "handleEnum(en)"
+       // this is when we have return value vector<EnumC>
+       vector<EnumC> retEnum = handleEnum(en);
+       // Get length first
+       int enumSize2 = retEnum.size();
+       // Now send the array of integers
+       vector<int> retEnumInt(enumSize2);
+       for (int i=0; i < enumSize2; i++) {
+               retEnumInt[i] = (int) retEnum[i];
+       }
+       void* retObj = &retEnumInt;
+       rmiObj->sendReturnObj(retObj, "int[]");
 }
 
 
 void TestClass_Skeleton::___waitRequestInvokeMethod() {
 
        int structsize1 = 0;
-       int enumsize1 = 0;
+
        // Loop continuously waiting for incoming bytes
        while (true) {
 
                rmiObj->getMethodBytes();
+               int _objectId = rmiObj->getObjectId();
                int methodId = rmiObj->getMethodId();
+               if (_objectId == object0Id) {
+               // Multiplex based on object Id
+                       // Complain if the method is not allowed
+                       if (set0Allowed.find(methodId) == set0Allowed.end()) {
+                               cerr << "TestClass_Skeleton: This object is not allowed to access method " << methodId << endl;
+                               //exit(1);
+                               throw exception();
+                       }
+               // If we have more than 1 object Id...
+               //else if (_objectId == object1Id) {
+
+               } else {
+                       cerr << "TestClass_Skeleton: Unrecognizable object Id: " << _objectId << endl;
+                       throw exception();
+                       //exit(1);
+               }
                
                switch (methodId) {
                        case 0: ___setA(); break;
@@ -398,8 +439,7 @@ void TestClass_Skeleton::___waitRequestInvokeMethod() {
                        // Handle struct
                        case 10: ___handleStruct(structsize1); break;
                        case 11: structsize1 = ___structSize(); break;
-                       case 12: ___handleEnum(enumsize1); break;
-                       case 13: enumsize1 = ___enumSize(); break;
+                       case 12: ___handleEnum(); break;
                        default:
                                string error = "Method Id not recognized!";
                                throw error;