Adding permission check and error throw in C++
[iot2.git] / iotjava / iotrmi / C++ / sample / TestClass_Stub.hpp
index 660b6a7e877e331023be46f235dd3a02eded056d..78791a3598ed12f1e899f07ca9ea944ee835a090 100644 (file)
@@ -2,6 +2,7 @@
 #define _TESTCLASS_STUB_HPP__
 
 #include <iostream>
+#include <set>
 #include <thread>
 #include "../IoTRMICall.hpp"
 #include "../IoTRMIObject.hpp"
@@ -32,6 +33,8 @@ class TestClass_Stub : public TestClassInterface {
                void                            ____init_CallBack();    // thread
                void                            ____registerCallBack(); // tell the other side that we are ready
 
+               //exception_ptr         teptr = nullptr;
+
        private:                
                int                                                     intA;
                float                                           floatB;
@@ -46,12 +49,17 @@ class TestClass_Stub : public TestClassInterface {
                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 = "";
@@ -65,10 +73,19 @@ TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool*
        rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);
        ports = _ports;
        // Start thread
-//     thread th1 (&TestClass_Stub::____init_CallBack, this);
-//     th1.detach();
+       /*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();
-//     ____registerCallBack();
+       ____registerCallBack();
 }
 
 
@@ -97,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 =