Adding more directory structure to generated files by compiler, also script to copy...
[iot2.git] / others / tutorial / benchmarks / drivers / SmartLightBulb / LightBulb_Skeleton.java
1 package iotcode.LifxLightBulb;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Map;
8 import java.util.HashMap;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import iotrmi.Java.IoTRMIComm;
11 import iotrmi.Java.IoTRMICommClient;
12 import iotrmi.Java.IoTRMICommServer;
13 import iotrmi.Java.IoTRMIUtil;
14
15 import iotcode.interfaces.LightBulb;
16
17 public class LightBulb_Skeleton implements LightBulb {
18
19         private LightBulb mainObj;
20         private int objectId = 1;
21         // Communications and synchronizations
22         private IoTRMIComm rmiComm;
23         private AtomicBoolean didAlreadyInitWaitInvoke;
24         private AtomicBoolean methodReceived;
25         private byte[] methodBytes = null;
26         // Permissions
27         private static Integer[] object1Permission = { 2, 10, 1, 3, 11, 8, 12, 7, 13, 9, 6, 16, 17, 4, 0, 14, 15, 5 };
28         private static List<Integer> set1Allowed;
29         
30
31         public LightBulb_Skeleton(LightBulb _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set1Allowed = new ArrayList<Integer>(Arrays.asList(object1Permission));
35                 IoTRMIUtil.mapSkel.put(_mainObj, this);
36                 IoTRMIUtil.mapSkelId.put(_mainObj, objectId);
37                 didAlreadyInitWaitInvoke = new AtomicBoolean(false);
38                 methodReceived = new AtomicBoolean(false);
39                 rmiComm.registerSkeleton(objectId, methodReceived);
40                 Thread thread1 = new Thread() {
41                         public void run() {
42                                 try {
43                                         ___waitRequestInvokeMethod();
44                                 }
45                                 catch (Exception ex)
46                                 {
47                                         ex.printStackTrace();
48                                 }
49                         }
50                 };
51                 thread1.start();
52         }
53
54         public LightBulb_Skeleton(LightBulb _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set1Allowed = new ArrayList<Integer>(Arrays.asList(object1Permission));
59                 didAlreadyInitWaitInvoke = new AtomicBoolean(false);
60                 methodReceived = new AtomicBoolean(false);
61                 rmiComm.registerSkeleton(objectId, methodReceived);
62         }
63
64         public boolean didAlreadyInitWaitInvoke() {
65                 return didAlreadyInitWaitInvoke.get();
66         }
67
68         public void init() {
69                 mainObj.init();
70         }
71
72         public void turnOff() {
73                 mainObj.turnOff();
74         }
75
76         public void turnOn() {
77                 mainObj.turnOn();
78         }
79
80         public boolean getState() {
81                 return mainObj.getState();
82         }
83
84         public void setColor(double _hue, double _saturation, double _brightness) {
85                 mainObj.setColor(_hue, _saturation, _brightness);
86         }
87
88         public void setTemperature(int _temperature) {
89                 mainObj.setTemperature(_temperature);
90         }
91
92         public double getBrightness() {
93                 return mainObj.getBrightness();
94         }
95
96         public double getHue() {
97                 return mainObj.getHue();
98         }
99
100         public double getSaturation() {
101                 return mainObj.getSaturation();
102         }
103
104         public int getTemperature() {
105                 return mainObj.getTemperature();
106         }
107
108         public double getBrightnessRangeLowerBound() {
109                 return mainObj.getBrightnessRangeLowerBound();
110         }
111
112         public double getBrightnessRangeUpperBound() {
113                 return mainObj.getBrightnessRangeUpperBound();
114         }
115
116         public double getHueRangeLowerBound() {
117                 return mainObj.getHueRangeLowerBound();
118         }
119
120         public double getHueRangeUpperBound() {
121                 return mainObj.getHueRangeUpperBound();
122         }
123
124         public double getSaturationRangeLowerBound() {
125                 return mainObj.getSaturationRangeLowerBound();
126         }
127
128         public double getSaturationRangeUpperBound() {
129                 return mainObj.getSaturationRangeUpperBound();
130         }
131
132         public int getTemperatureRangeLowerBound() {
133                 return mainObj.getTemperatureRangeLowerBound();
134         }
135
136         public int getTemperatureRangeUpperBound() {
137                 return mainObj.getTemperatureRangeUpperBound();
138         }
139
140         public void ___init() {
141                 byte[] localMethodBytes = methodBytes;
142                 rmiComm.setGetMethodBytes();
143                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
144                 init();
145         }
146
147         public void ___turnOff() {
148                 byte[] localMethodBytes = methodBytes;
149                 rmiComm.setGetMethodBytes();
150                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
151                 turnOff();
152         }
153
154         public void ___turnOn() {
155                 byte[] localMethodBytes = methodBytes;
156                 rmiComm.setGetMethodBytes();
157                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
158                 turnOn();
159         }
160
161         public void ___getState() throws IOException {
162                 byte[] localMethodBytes = methodBytes;
163                 rmiComm.setGetMethodBytes();
164                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
165                 Object retObj = getState();
166                 rmiComm.sendReturnObj(retObj, localMethodBytes);
167         }
168
169         public void ___setColor() {
170                 byte[] localMethodBytes = methodBytes;
171                 rmiComm.setGetMethodBytes();
172                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { double.class, double.class, double.class }, new Class<?>[] { null, null, null }, localMethodBytes);
173                 setColor((double) paramObj[0], (double) paramObj[1], (double) paramObj[2]);
174         }
175
176         public void ___setTemperature() {
177                 byte[] localMethodBytes = methodBytes;
178                 rmiComm.setGetMethodBytes();
179                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
180                 setTemperature((int) paramObj[0]);
181         }
182
183         public void ___getBrightness() throws IOException {
184                 byte[] localMethodBytes = methodBytes;
185                 rmiComm.setGetMethodBytes();
186                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
187                 Object retObj = getBrightness();
188                 rmiComm.sendReturnObj(retObj, localMethodBytes);
189         }
190
191         public void ___getHue() throws IOException {
192                 byte[] localMethodBytes = methodBytes;
193                 rmiComm.setGetMethodBytes();
194                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
195                 Object retObj = getHue();
196                 rmiComm.sendReturnObj(retObj, localMethodBytes);
197         }
198
199         public void ___getSaturation() throws IOException {
200                 byte[] localMethodBytes = methodBytes;
201                 rmiComm.setGetMethodBytes();
202                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
203                 Object retObj = getSaturation();
204                 rmiComm.sendReturnObj(retObj, localMethodBytes);
205         }
206
207         public void ___getTemperature() throws IOException {
208                 byte[] localMethodBytes = methodBytes;
209                 rmiComm.setGetMethodBytes();
210                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
211                 Object retObj = getTemperature();
212                 rmiComm.sendReturnObj(retObj, localMethodBytes);
213         }
214
215         public void ___getBrightnessRangeLowerBound() throws IOException {
216                 byte[] localMethodBytes = methodBytes;
217                 rmiComm.setGetMethodBytes();
218                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
219                 Object retObj = getBrightnessRangeLowerBound();
220                 rmiComm.sendReturnObj(retObj, localMethodBytes);
221         }
222
223         public void ___getBrightnessRangeUpperBound() throws IOException {
224                 byte[] localMethodBytes = methodBytes;
225                 rmiComm.setGetMethodBytes();
226                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
227                 Object retObj = getBrightnessRangeUpperBound();
228                 rmiComm.sendReturnObj(retObj, localMethodBytes);
229         }
230
231         public void ___getHueRangeLowerBound() throws IOException {
232                 byte[] localMethodBytes = methodBytes;
233                 rmiComm.setGetMethodBytes();
234                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
235                 Object retObj = getHueRangeLowerBound();
236                 rmiComm.sendReturnObj(retObj, localMethodBytes);
237         }
238
239         public void ___getHueRangeUpperBound() throws IOException {
240                 byte[] localMethodBytes = methodBytes;
241                 rmiComm.setGetMethodBytes();
242                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
243                 Object retObj = getHueRangeUpperBound();
244                 rmiComm.sendReturnObj(retObj, localMethodBytes);
245         }
246
247         public void ___getSaturationRangeLowerBound() throws IOException {
248                 byte[] localMethodBytes = methodBytes;
249                 rmiComm.setGetMethodBytes();
250                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
251                 Object retObj = getSaturationRangeLowerBound();
252                 rmiComm.sendReturnObj(retObj, localMethodBytes);
253         }
254
255         public void ___getSaturationRangeUpperBound() throws IOException {
256                 byte[] localMethodBytes = methodBytes;
257                 rmiComm.setGetMethodBytes();
258                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
259                 Object retObj = getSaturationRangeUpperBound();
260                 rmiComm.sendReturnObj(retObj, localMethodBytes);
261         }
262
263         public void ___getTemperatureRangeLowerBound() throws IOException {
264                 byte[] localMethodBytes = methodBytes;
265                 rmiComm.setGetMethodBytes();
266                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
267                 Object retObj = getTemperatureRangeLowerBound();
268                 rmiComm.sendReturnObj(retObj, localMethodBytes);
269         }
270
271         public void ___getTemperatureRangeUpperBound() throws IOException {
272                 byte[] localMethodBytes = methodBytes;
273                 rmiComm.setGetMethodBytes();
274                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
275                 Object retObj = getTemperatureRangeUpperBound();
276                 rmiComm.sendReturnObj(retObj, localMethodBytes);
277         }
278
279         public void ___waitRequestInvokeMethod() throws IOException {
280                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
281                 while (true) {
282                         if (!methodReceived.get()) {
283                                 continue;
284                         }
285                         methodBytes = rmiComm.getMethodBytes();
286                         methodReceived.set(false);
287                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
288                         int methodId = IoTRMIComm.getMethodId(methodBytes);
289                         if (_objectId == objectId) {
290                                 if (!set1Allowed.contains(methodId)) {
291                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
292                                 }
293                         }
294                         else {
295                                 continue;
296                         }
297                         switch (methodId) {
298                                 case 0:
299                                 new Thread() {
300                                         public void run() {
301                                                 try {
302                                                         ___init();
303                                                 }
304                                                 catch (Exception ex) {
305                                                         ex.printStackTrace();
306                                                 }
307                                         }
308                                 }.start();
309                                 break;
310                                 case 1:
311                                 new Thread() {
312                                         public void run() {
313                                                 try {
314                                                         ___turnOff();
315                                                 }
316                                                 catch (Exception ex) {
317                                                         ex.printStackTrace();
318                                                 }
319                                         }
320                                 }.start();
321                                 break;
322                                 case 2:
323                                 new Thread() {
324                                         public void run() {
325                                                 try {
326                                                         ___turnOn();
327                                                 }
328                                                 catch (Exception ex) {
329                                                         ex.printStackTrace();
330                                                 }
331                                         }
332                                 }.start();
333                                 break;
334                                 case 3:
335                                 new Thread() {
336                                         public void run() {
337                                                 try {
338                                                         ___getState();
339                                                 }
340                                                 catch (Exception ex) {
341                                                         ex.printStackTrace();
342                                                 }
343                                         }
344                                 }.start();
345                                 break;
346                                 case 4:
347                                 new Thread() {
348                                         public void run() {
349                                                 try {
350                                                         ___setColor();
351                                                 }
352                                                 catch (Exception ex) {
353                                                         ex.printStackTrace();
354                                                 }
355                                         }
356                                 }.start();
357                                 break;
358                                 case 5:
359                                 new Thread() {
360                                         public void run() {
361                                                 try {
362                                                         ___setTemperature();
363                                                 }
364                                                 catch (Exception ex) {
365                                                         ex.printStackTrace();
366                                                 }
367                                         }
368                                 }.start();
369                                 break;
370                                 case 6:
371                                 new Thread() {
372                                         public void run() {
373                                                 try {
374                                                         ___getBrightness();
375                                                 }
376                                                 catch (Exception ex) {
377                                                         ex.printStackTrace();
378                                                 }
379                                         }
380                                 }.start();
381                                 break;
382                                 case 7:
383                                 new Thread() {
384                                         public void run() {
385                                                 try {
386                                                         ___getHue();
387                                                 }
388                                                 catch (Exception ex) {
389                                                         ex.printStackTrace();
390                                                 }
391                                         }
392                                 }.start();
393                                 break;
394                                 case 8:
395                                 new Thread() {
396                                         public void run() {
397                                                 try {
398                                                         ___getSaturation();
399                                                 }
400                                                 catch (Exception ex) {
401                                                         ex.printStackTrace();
402                                                 }
403                                         }
404                                 }.start();
405                                 break;
406                                 case 9:
407                                 new Thread() {
408                                         public void run() {
409                                                 try {
410                                                         ___getTemperature();
411                                                 }
412                                                 catch (Exception ex) {
413                                                         ex.printStackTrace();
414                                                 }
415                                         }
416                                 }.start();
417                                 break;
418                                 case 10:
419                                 new Thread() {
420                                         public void run() {
421                                                 try {
422                                                         ___getBrightnessRangeLowerBound();
423                                                 }
424                                                 catch (Exception ex) {
425                                                         ex.printStackTrace();
426                                                 }
427                                         }
428                                 }.start();
429                                 break;
430                                 case 11:
431                                 new Thread() {
432                                         public void run() {
433                                                 try {
434                                                         ___getBrightnessRangeUpperBound();
435                                                 }
436                                                 catch (Exception ex) {
437                                                         ex.printStackTrace();
438                                                 }
439                                         }
440                                 }.start();
441                                 break;
442                                 case 12:
443                                 new Thread() {
444                                         public void run() {
445                                                 try {
446                                                         ___getHueRangeLowerBound();
447                                                 }
448                                                 catch (Exception ex) {
449                                                         ex.printStackTrace();
450                                                 }
451                                         }
452                                 }.start();
453                                 break;
454                                 case 13:
455                                 new Thread() {
456                                         public void run() {
457                                                 try {
458                                                         ___getHueRangeUpperBound();
459                                                 }
460                                                 catch (Exception ex) {
461                                                         ex.printStackTrace();
462                                                 }
463                                         }
464                                 }.start();
465                                 break;
466                                 case 14:
467                                 new Thread() {
468                                         public void run() {
469                                                 try {
470                                                         ___getSaturationRangeLowerBound();
471                                                 }
472                                                 catch (Exception ex) {
473                                                         ex.printStackTrace();
474                                                 }
475                                         }
476                                 }.start();
477                                 break;
478                                 case 15:
479                                 new Thread() {
480                                         public void run() {
481                                                 try {
482                                                         ___getSaturationRangeUpperBound();
483                                                 }
484                                                 catch (Exception ex) {
485                                                         ex.printStackTrace();
486                                                 }
487                                         }
488                                 }.start();
489                                 break;
490                                 case 16:
491                                 new Thread() {
492                                         public void run() {
493                                                 try {
494                                                         ___getTemperatureRangeLowerBound();
495                                                 }
496                                                 catch (Exception ex) {
497                                                         ex.printStackTrace();
498                                                 }
499                                         }
500                                 }.start();
501                                 break;
502                                 case 17:
503                                 new Thread() {
504                                         public void run() {
505                                                 try {
506                                                         ___getTemperatureRangeUpperBound();
507                                                 }
508                                                 catch (Exception ex) {
509                                                         ex.printStackTrace();
510                                                 }
511                                         }
512                                 }.start();
513                                 break;
514                                 default: 
515                                 throw new Error("Method Id " + methodId + " not recognized!");
516                         }
517                 }
518         }
519
520 }