Removing printing statements from C++ RMI library - this will cause SO files to get...
[iot2.git] / iotjava / iotrmi / C++ / sample / TestClass_Stub.hpp
index b8cce2186ec0ac5f5cbba5e215d339e0cacc8552..78791a3598ed12f1e899f07ca9ea944ee835a090 100644 (file)
@@ -2,6 +2,7 @@
 #define _TESTCLASS_STUB_HPP__
 
 #include <iostream>
+#include <set>
 #include <thread>
 #include "../IoTRMICall.hpp"
 #include "../IoTRMIObject.hpp"
@@ -27,30 +28,38 @@ class TestClass_Stub : public TestClassInterface {
                void                            registerCallback(CallBackInterface* _cb);
                void                            registerCallback(vector<CallBackInterface*>_cb);
                int                                     callBack();
-               void                            handleStruct(vector<data> vecData);
+               vector<data>            handleStruct(vector<data> vecData);
                vector<EnumC>           handleEnum(vector<EnumC> vecEn);
                void                            ____init_CallBack();    // thread
                void                            ____registerCallBack(); // tell the other side that we are ready
 
+               //exception_ptr         teptr = nullptr;
+
        private:                
                int                                                     intA;
                float                                           floatB;
                string                                          stringC;
                //CallBackInterface                     cb;
                IoTRMICall                                      *rmiCall;
-               IoTRMIObject                            *rmiObj;
                string                                          address;
                vector<int>                                     ports;
-               vector<CallBackInterface*>      vecCBObj;
+               const static int                        objectId = 0;   // Default value is 0
 
-               int             objectId = 0;   // Default value is 0
+               // Specific for callbacks
+               IoTRMIObject                            *rmiObj;
+               vector<CallBackInterface*>      vecCBObj;
                static int      objIdCnt;
+               // Callback permission
+               const static set<int>           set0Allowed;
 };
 
 
 int TestClass_Stub::objIdCnt = 0;
 
 
+const set<int> TestClass_Stub::set0Allowed { 0, 1 };
+
+
 TestClass_Stub::TestClass_Stub() {
 
        address = "";
@@ -64,6 +73,15 @@ TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool*
        rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);
        ports = _ports;
        // Start thread
+       /*if (teptr) {
+               try {
+                       thread th1 (&TestClass_Stub::____init_CallBack, this);
+                       th1.detach();
+               } catch(const exception&) {
+                       cout << "Got here!" << endl;
+                       throw exception();
+               }
+       }*/
        thread th1 (&TestClass_Stub::____init_CallBack, this);
        th1.detach();
        //th1.join();
@@ -81,6 +99,7 @@ TestClass_Stub::~TestClass_Stub() {
                delete rmiObj;
                rmiObj = NULL;
        }
+       // Special for callbacks!!!
        for(CallBackInterface* cb : vecCBObj) {
                delete cb;
                cb = NULL;
@@ -95,6 +114,15 @@ void TestClass_Stub::____init_CallBack() {
        rmiObj = new IoTRMIObject(ports[0], &bResult);
        while (true) {
                char* method = rmiObj->getMethodBytes();
+               int methodId = IoTRMIObject::getMethodId(method);
+               // Permission check
+               // 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();
+                       //teptr = current_exception();
+               }
                int objId = IoTRMIObject::getObjectId(method);
                if (objId < vecCBObj.size()) {  // Check if still within range
                        CallBack_CBSkeleton* skel = 
@@ -253,7 +281,7 @@ int TestClass_Stub::callBack() {
 }
 
 
-void TestClass_Stub::handleStruct(vector<data> vecData) {
+vector<data> TestClass_Stub::handleStruct(vector<data> vecData) {
 
        int numParam = 1;
        int methodId = 11;
@@ -266,7 +294,7 @@ void TestClass_Stub::handleStruct(vector<data> vecData) {
 
        int numParam2 = 3*vecData.size();
        int methodId2 = 10;
-       string retType2 = "void";
+       string retType2 = "int";
        string paramCls2[numParam2];
        void* paramObj2[numParam2];
        int pos = 0;
@@ -278,8 +306,41 @@ void TestClass_Stub::handleStruct(vector<data> vecData) {
                paramCls2[pos] = "int";
                paramObj2[pos] = &vecData[i].year; pos++;
        }
-       void* retObj2 = NULL;
+       // RETURN STRUCT OBJECT
+       // Get length of struct array
+       int structsize1 = 0;
+       void* retObj2 = { &structsize1 };
+       // IF we don't have returned struct objects, then it's just "void* retObj2 = NULL;"
        rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2);
+       cout << "Struct length: " << structsize1 << endl;
+
+       // Get the returned objects
+       string retCls[3*structsize1];
+       void* retObj3[3*structsize1];
+       int numRet = 3*structsize1;
+       // define array of everything
+       string param1[structsize1];
+       float param2[structsize1];
+       int param3[structsize1];
+       pos = 0;
+       for(int i=0; i < structsize1; i++) {
+               retCls[pos] = "string";
+               retObj3[pos++] = &param1[i];
+               retCls[pos] = "float";
+               retObj3[pos++] = &param2[i];
+               retCls[pos] = "int";
+               retObj3[pos++] = &param3[i];
+       }
+       rmiCall->getStructObjects(retCls, numRet, retObj3);
+       vector<data> dat(structsize1);
+       pos = 0;
+       for (int i=0; i < structsize1; i++) {
+               dat[i].name = param1[i];
+               dat[i].value = param2[i];
+               dat[i].year = param3[i];
+       }
+
+       return dat;
 }