Adjusting back into the original lengthy version of IrrigationController; for testing...
[iot2.git] / benchmarks / Java / HomeSecurityController / SmartthingsSensorCallback_Skeleton.java
1 package HomeSecurityController;
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.*;
16
17 public class SmartthingsSensorCallback_Skeleton implements SmartthingsSensorCallback {
18
19         private SmartthingsSensorCallback mainObj;
20         private int objectId = 3;
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[] object3Permission = { 0 };
28         private static List<Integer> set3Allowed;
29         
30
31         public SmartthingsSensorCallback_Skeleton(SmartthingsSensorCallback _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set3Allowed = new ArrayList<Integer>(Arrays.asList(object3Permission));
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 SmartthingsSensorCallback_Skeleton(SmartthingsSensorCallback _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set3Allowed = new ArrayList<Integer>(Arrays.asList(object3Permission));
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 newReadingAvailable(int _sensorId, int _value, boolean _activeValue) {
69                 mainObj.newReadingAvailable(_sensorId, _value, _activeValue);
70         }
71
72         public void ___newReadingAvailable() {
73                 byte[] localMethodBytes = methodBytes;
74                 rmiComm.setGetMethodBytes();
75                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class, int.class, boolean.class }, new Class<?>[] { null, null, null }, localMethodBytes);
76                 newReadingAvailable((int) paramObj[0], (int) paramObj[1], (boolean) paramObj[2]);
77         }
78
79         public void ___waitRequestInvokeMethod() throws IOException {
80                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
81                 while (true) {
82                         if (!methodReceived.get()) {
83                                 continue;
84                         }
85                         methodBytes = rmiComm.getMethodBytes();
86                         methodReceived.set(false);
87                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
88                         int methodId = IoTRMIComm.getMethodId(methodBytes);
89                         if (_objectId == objectId) {
90                                 if (!set3Allowed.contains(methodId)) {
91                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
92                                 }
93                         }
94                         else {
95                                 continue;
96                         }
97                         switch (methodId) {
98                                 case 0:
99                                 new Thread() {
100                                         public void run() {
101                                                 try {
102                                                         ___newReadingAvailable();
103                                                 }
104                                                 catch (Exception ex) {
105                                                         ex.printStackTrace();
106                                                 }
107                                         }
108                                 }.start();
109                                 break;
110                                 default: 
111                                 throw new Error("Method Id " + methodId + " not recognized!");
112                         }
113                 }
114         }
115
116 }