Cleaning up drivers/Cpp, Cpp/Lifxtest, virtuals, and iotrmi/C++ (revisiting the C...
[iot2.git] / benchmarks / Cpp / Lifxtest / LightBulbTest_Stub.cpp
1 #include <iostream>
2 #include "LightBulbTest_Stub.hpp"
3
4 using namespace std;
5
6 //LightBulbTest_Stub::LightBulbTest_Stub(int _portSend, int _portRecv, const char* _skeletonAddress, int _rev, bool* _bResult) {
7 LightBulbTest_Stub::LightBulbTest_Stub(int _portSend, int _portRecv, const char* _skeletonAddress, int _rev, bool* _bResult) {
8
9         int i=0;
10         string file = "LightBulbTest_cpp" + to_string(i) + ".log";
11         while (ifstream(file.c_str())) {
12                 i++;
13                 file = "LightBulbTest_cpp" + to_string(i) + ".log";
14         }
15         log.open(file);
16         log << "Constructing LightBulbTest_Stub!" << endl;
17         log << "Port send: " << _portSend << endl;
18         log << "Port recv: " << _portRecv << endl;
19         log << "Skeleton addr: " << _skeletonAddress << endl;
20         log << "Rev: " << _rev << endl;
21         log << "Result: " << _bResult << endl;
22
23         rmiComm = new IoTRMICommClient(_portSend, _portRecv, _skeletonAddress, _rev, _bResult);
24         log << "Reached 1!" << endl;
25         rmiComm->registerStub(objectId, 6, &retValueReceived6);
26         rmiComm->registerStub(objectId, 3, &retValueReceived3);
27         rmiComm->registerStub(objectId, 8, &retValueReceived8);
28         rmiComm->registerStub(objectId, 7, &retValueReceived7);
29         rmiComm->registerStub(objectId, 9, &retValueReceived9);
30         IoTRMIUtil::mapStub->insert(make_pair(objectId, this));
31 }
32
33 LightBulbTest_Stub::LightBulbTest_Stub(IoTRMIComm* _rmiComm, int _objectId) {
34         rmiComm = _rmiComm;
35         objectId = _objectId;
36         rmiComm->registerStub(objectId, 6, &retValueReceived6);
37         rmiComm->registerStub(objectId, 3, &retValueReceived3);
38         rmiComm->registerStub(objectId, 8, &retValueReceived8);
39         rmiComm->registerStub(objectId, 7, &retValueReceived7);
40         rmiComm->registerStub(objectId, 9, &retValueReceived9);
41 }
42
43 LightBulbTest_Stub::~LightBulbTest_Stub() {
44         if (rmiComm != NULL) {
45                 delete rmiComm;
46                 rmiComm = NULL;
47         }
48 }
49
50 mutex mtxLightBulbTest_StubMethodExec2;
51 void LightBulbTest_Stub::turnOn() { 
52         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec2);
53         int methodId = 2;
54         string retType = "void";
55         int numParam = 0;
56         string paramCls[] = {  };
57         void* paramObj[] = {  };
58         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
59 }
60
61 mutex mtxLightBulbTest_StubMethodExec6;
62 double LightBulbTest_Stub::getBrightness() { 
63         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec6);
64         int methodId = 6;
65         string retType = "double";
66         int numParam = 0;
67         string paramCls[] = {  };
68         void* paramObj[] = {  };
69         double retVal = 0;
70         void* retObj = &retVal;
71         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
72         // Waiting for return value
73         while (!retValueReceived6);
74         rmiComm->getReturnValue(retType, retObj);
75         retValueReceived6 = false;
76         didGetReturnBytes.exchange(true);
77
78         return retVal;
79 }
80
81 mutex mtxLightBulbTest_StubMethodExec1;
82 void LightBulbTest_Stub::turnOff() { 
83         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec1);
84         int methodId = 1;
85         string retType = "void";
86         int numParam = 0;
87         string paramCls[] = {  };
88         void* paramObj[] = {  };
89         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
90 }
91
92 mutex mtxLightBulbTest_StubMethodExec3;
93 bool LightBulbTest_Stub::getState() { 
94         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec3);
95         int methodId = 3;
96         string retType = "boolean";
97         int numParam = 0;
98         string paramCls[] = {  };
99         void* paramObj[] = {  };
100         bool retVal = false;
101         void* retObj = &retVal;
102         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
103         // Waiting for return value
104         while (!retValueReceived3);
105         rmiComm->getReturnValue(retType, retObj);
106         retValueReceived3 = false;
107         didGetReturnBytes.exchange(true);
108
109         return retVal;
110 }
111
112 mutex mtxLightBulbTest_StubMethodExec4;
113 void LightBulbTest_Stub::setColor(double _hue, double _saturation, double _brightness) { 
114         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec4);
115         int methodId = 4;
116         string retType = "void";
117         int numParam = 3;
118         string paramCls[] = { "double", "double", "double" };
119         void* paramObj[] = { &_hue, &_saturation, &_brightness };
120         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
121 }
122
123 mutex mtxLightBulbTest_StubMethodExec8;
124 double LightBulbTest_Stub::getSaturation() { 
125         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec8);
126         int methodId = 8;
127         string retType = "double";
128         int numParam = 0;
129         string paramCls[] = {  };
130         void* paramObj[] = {  };
131         double retVal = 0;
132         void* retObj = &retVal;
133         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
134         // Waiting for return value
135         while (!retValueReceived8);
136         rmiComm->getReturnValue(retType, retObj);
137         retValueReceived8 = false;
138         didGetReturnBytes.exchange(true);
139
140         return retVal;
141 }
142
143 mutex mtxLightBulbTest_StubMethodExec0;
144 void LightBulbTest_Stub::init() { 
145         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec0);
146         int methodId = 0;
147         string retType = "void";
148         int numParam = 0;
149         string paramCls[] = {  };
150         void* paramObj[] = {  };
151         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
152 }
153
154 mutex mtxLightBulbTest_StubMethodExec5;
155 void LightBulbTest_Stub::setTemperature(int _temperature) { 
156         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec5);
157         int methodId = 5;
158         string retType = "void";
159         int numParam = 1;
160         string paramCls[] = { "int" };
161         void* paramObj[] = { &_temperature };
162         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
163 }
164
165 mutex mtxLightBulbTest_StubMethodExec7;
166 double LightBulbTest_Stub::getHue() { 
167         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec7);
168         int methodId = 7;
169         string retType = "double";
170         int numParam = 0;
171         string paramCls[] = {  };
172         void* paramObj[] = {  };
173         double retVal = 0;
174         void* retObj = &retVal;
175         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
176         // Waiting for return value
177         while (!retValueReceived7);
178         rmiComm->getReturnValue(retType, retObj);
179         retValueReceived7 = false;
180         didGetReturnBytes.exchange(true);
181
182         return retVal;
183 }
184
185 mutex mtxLightBulbTest_StubMethodExec9;
186 int LightBulbTest_Stub::getTemperature() { 
187         lock_guard<mutex> guard(mtxLightBulbTest_StubMethodExec9);
188         int methodId = 9;
189         string retType = "int";
190         int numParam = 0;
191         string paramCls[] = {  };
192         void* paramObj[] = {  };
193         int retVal = 0;
194         void* retObj = &retVal;
195         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
196         // Waiting for return value
197         while (!retValueReceived9);
198         rmiComm->getReturnValue(retType, retObj);
199         retValueReceived9 = false;
200         didGetReturnBytes.exchange(true);
201
202         return retVal;
203 }
204
205 extern "C" void* createLightBulbTest_Stub(void** params) {
206         // Args: int _portSend, int _portRecv, const char* _skeletonAddress, int _rev, bool* _bResult
207         return new LightBulbTest_Stub(*((int*) params[0]), *((int*) params[1]), ((string*) params[2])->c_str(), *((int*) params[3]), (bool*) params[4]);
208 }
209
210 extern "C" void destroyLightBulbTest_Stub(void* t) {
211         LightBulbTest_Stub* obj = (LightBulbTest_Stub*) t;
212         delete obj;
213 }
214
215 extern "C" void initLightBulbTest_Stub(void* t) {
216 }
217
218 /*int main() {
219         return 0;
220 }*/
221
222 int main()
223 {
224
225         int send_port = 5010;
226         int recv_port = 5011;
227         //const char* skeletonAddress = "127.0.0.1";
228         const char* skeletonAddress = "localhost";
229         int rev = 0;
230         bool result = false;
231
232         LightBulbTest_Stub *lbs = new LightBulbTest_Stub(send_port, recv_port, skeletonAddress, rev, &result);
233         cout << "Successfully instantiated stub!" << endl;
234         lbs->init();
235         lbs->turnOn();
236         this_thread::sleep_for (chrono::milliseconds(1000));
237         lbs->turnOff();
238         for (int i = 0; i < 2; i++) {
239                 lbs->turnOff();
240                 cout << "Turning off!" << endl;
241                 this_thread::sleep_for (chrono::milliseconds(1000));
242                 lbs->turnOn();
243                 cout << "Turning on!" << endl;
244                 this_thread::sleep_for (chrono::milliseconds(1000));
245                 double hue = lbs->getHue();
246                 double saturation = lbs->getSaturation();
247                 cout << "Hue: " << hue << endl;
248                 cout << "Saturation: " << saturation << endl;
249                 this_thread::sleep_for (chrono::milliseconds(1000));
250         }
251         //lbs->turnOff();
252         cout << "Done controlling! Exit now!" << endl;
253         
254         delete lbs;
255
256         return 0;
257 }