Adding permission check and error throw in C++
authorrtrimana <rtrimana@uci.edu>
Thu, 10 Nov 2016 17:15:10 +0000 (09:15 -0800)
committerrtrimana <rtrimana@uci.edu>
Thu, 10 Nov 2016 17:15:10 +0000 (09:15 -0800)
iotjava/iotrmi/C++/IoTRMIObject.hpp
iotjava/iotrmi/C++/sample/CallBack_CBSkeleton.hpp
iotjava/iotrmi/C++/sample/TestClass_Skeleton.cpp
iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp
iotjava/iotrmi/C++/sample/TestClass_Stub.cpp
iotjava/iotrmi/C++/sample/TestClass_Stub.hpp

index 27931cb..8e688a3 100644 (file)
@@ -35,6 +35,7 @@ class IoTRMIObject {
                int                                     getObjectId();
                static int                      getObjectId(char* methodBytes);
                int                                     getMethodId();
+               static int                      getMethodId(char* methodBytes);
                void**                          getMethodParams(string paramCls[], int numParam, void* paramObj[]);
 
        private:
@@ -175,6 +176,20 @@ int IoTRMIObject::getMethodId() {
 }
 
 
+// Get methodId from bytes (static version)
+int IoTRMIObject::getMethodId(char* methodBytes) {
+
+       // 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 method parameters and return an array of parameter objects
 //
 // For primitive objects:
index 90c63a1..bc67c92 100644 (file)
@@ -22,7 +22,7 @@ class CallBack_CBSkeleton : public CallBackInterface {
 
        private:
                CallBackInterface       *cb;
-               int                                     objectId = 0;
+               int                                                     objectId = 0;
 };
 
 
index 4fcfe31..e16c727 100644 (file)
@@ -7,10 +7,15 @@ using namespace std;
 
 int main(int argc, char *argv[])
 {
-
-       int port = 5010;
-       TestClassInterface *tc = new TestClass(3, 5.0, "7911");
-       TestClass_Skeleton *tcSkel = new TestClass_Skeleton(tc, port);
+       TestClassInterface *tc;
+       TestClass_Skeleton *tcSkel;
+       try {
+               int port = 5010;
+               tc = new TestClass(3, 5.0, "7911");
+               tcSkel = new TestClass_Skeleton(tc, port);
+       } catch(const exception&) {
+               return EXIT_FAILURE;
+       }
        //tcSkel->waitRequestInvokeMethod();
 
        delete tc;
index bfb5ac0..9ea8bab 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"
@@ -49,6 +51,11 @@ class TestClass_Skeleton : public TestClassInterface {
        private:                
                TestClassInterface                      *tc;
                IoTRMIObject                            *rmiObj;
+               // Permission setup
+               const static int                        object0Id;
+               //const static int                      object0Permission[];
+               const static set<int>           set0Allowed;
+               
                IoTRMICall                                      *rmiCall;
                static int                                      objIdCnt;
                vector<CallBackInterface*>      vecCBObj;
@@ -56,6 +63,11 @@ class TestClass_Skeleton : public TestClassInterface {
 };
 
 
+// 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;
 
 
@@ -393,7 +405,24 @@ void TestClass_Skeleton::___waitRequestInvokeMethod() {
        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;
index 8f28f23..a4127d8 100644 (file)
@@ -5,6 +5,8 @@
 
 using namespace std;
 
+static exception_ptr teptr = nullptr;
+
 int main(int argc, char *argv[])
 {
 
@@ -30,7 +32,7 @@ int main(int argc, char *argv[])
 
        cout << "Return value: " << tcStub->sumArray(input) << endl;
        
-       /*CallBackInterface *cb1 = new CallBack(23);
+       CallBackInterface *cb1 = new CallBack(23);
        CallBackInterface *cb2 = new CallBack(33);
        CallBackInterface *cb3 = new CallBack(43);
        vector<CallBackInterface*> cb;
@@ -47,7 +49,7 @@ int main(int argc, char *argv[])
        cbsec.push_back(cb6);
        tcStub->registerCallback(cbsec);
        cout << "Return value from callback: " << tcStub->callBack() << endl;
-*/
+
        vector<data> dataset;
 
        data testdata;
index 660b6a7..78791a3 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 =