Working C++ IoTSlave for both IoTSet and IoTRelation; threading works for C++ LifxLig...
[iot2.git] / benchmarks / drivers / Cpp / LabRoom / Room_Skeleton.hpp
1 #ifndef _ROOM_SKELETON_HPP__
2 #define _ROOM_SKELETON_HPP__
3 #include <iostream>
4 #include <fstream>
5 #include "Room.hpp"
6
7 #include <vector>
8 #include <set>
9 #include "IoTRMICall.hpp"
10 #include "IoTRMIObject.hpp"
11
12 using namespace std;
13
14 class Room_Skeleton : public Room
15 {
16         private:
17
18         Room *mainObj;
19         vector<int> ports;
20         string callbackAddress;
21         IoTRMIObject *rmiObj;
22
23         const static int object0Id = 0; //RoomSmart
24         static set<int> set0Allowed;
25         
26         ofstream log;
27         public:
28
29         Room_Skeleton(Room *_mainObj, string _callbackAddress, int _port) {
30                 bool _bResult = false;
31                 mainObj = _mainObj;
32                 callbackAddress = _callbackAddress;
33                 // Logging
34                 int i=0;
35                 string file = "Room_Skeleton_cpp" + to_string(i) + ".log";
36                 while (ifstream(file.c_str())) {
37                         i++;
38                         file = "Room_Skeleton_cpp" + to_string(i) + ".log";
39                 }
40                 log.open(file);
41                 log << "Callback address: " << callbackAddress << endl;
42                 log << "Port: " << _port << endl;
43                 rmiObj = new IoTRMIObject(_port, &_bResult);
44                 log << "Established connection with slave! Wait request invoke now..." << endl;
45                 ___waitRequestInvokeMethod();
46         }
47
48         ~Room_Skeleton() {
49                 if (rmiObj != NULL) {
50                         delete rmiObj;
51                         rmiObj = NULL;
52                 }
53         }
54         
55         int getRoomID() {
56                 return mainObj->getRoomID();
57         }
58
59         void ___getRoomID() {
60                 string paramCls[] = {  };
61                 int numParam = 0;
62                 void* paramObj[] = {  };
63                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
64                 int retVal = getRoomID();
65                 void* retObj = &retVal;
66                 rmiObj->sendReturnObj(retObj, "int");
67         }
68
69         void ___waitRequestInvokeMethod() {
70                 while (true) {
71                         log << "Getting into the while loop" << endl;
72                         rmiObj->getMethodBytes();
73                         log << "Getting method bytes now" << endl;
74                         log << "Method len: " << rmiObj->getMethodBytesLen() << endl;
75                         int _objectId = rmiObj->getObjectId();
76                         log << "Object Id: " << _objectId << endl;
77                         int methodId = rmiObj->getMethodId();
78                         log << "Method Id: " << methodId << endl;
79                         if (_objectId == object0Id) {
80                                 if (set0Allowed.find(methodId) == set0Allowed.end()) {
81                                         cerr << "Object with object Id: " << _objectId << "  is not allowed to access method: " << methodId << endl;
82                                         return;
83                                 }
84                         }
85                         else {
86                                 cerr << "Object Id: " << _objectId << " not recognized!" << endl;
87                                 return;
88                         }
89                         switch (methodId) {
90                                 case 0: ___getRoomID(); break;
91                                 default: 
92                                 cerr << "Method Id " << methodId << " not recognized!" << endl;
93                                 throw exception();
94                         }
95                 }
96         }
97
98 };
99 set<int> Room_Skeleton::set0Allowed { 0 };
100 #endif