d6924b47dd5e2be33383dd4fd6e6e108bed12ddc
[iot2.git] / benchmarks / drivers / Java / MotionSensor / SmartthingsSensor_Skeleton.java
1 package iotcode.MotionSensor;
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 SmartthingsSensor_Skeleton implements SmartthingsSensor {
18
19         private SmartthingsSensor mainObj;
20         private int objectId = 2;
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[] object2Permission = { 3, 2, 5, 6, 1, 4, 0 };
28         private static List<Integer> set2Allowed;
29         
30
31         public SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
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 SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
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 int getValue() {
73                 return mainObj.getValue();
74         }
75
76         public boolean isActiveValue() {
77                 return mainObj.isActiveValue();
78         }
79
80         public long getTimestampOfLastReading() {
81                 return mainObj.getTimestampOfLastReading();
82         }
83
84         public void setId(int id) {
85                 mainObj.setId(id);
86         }
87
88         public int getId() {
89                 return mainObj.getId();
90         }
91
92         public void registerCallback(SmartthingsSensorSmartCallback _callbackTo) {
93                 mainObj.registerCallback(_callbackTo);
94         }
95
96         public void ___init() {
97                 byte[] localMethodBytes = methodBytes;
98                 rmiComm.setGetMethodBytes();
99                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
100                 init();
101         }
102
103         public void ___getValue() throws IOException {
104                 byte[] localMethodBytes = methodBytes;
105                 rmiComm.setGetMethodBytes();
106                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
107                 Object retObj = getValue();
108                 rmiComm.sendReturnObj(retObj, localMethodBytes);
109         }
110
111         public void ___isActiveValue() throws IOException {
112                 byte[] localMethodBytes = methodBytes;
113                 rmiComm.setGetMethodBytes();
114                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
115                 Object retObj = isActiveValue();
116                 rmiComm.sendReturnObj(retObj, localMethodBytes);
117         }
118
119         public void ___getTimestampOfLastReading() throws IOException {
120                 byte[] localMethodBytes = methodBytes;
121                 rmiComm.setGetMethodBytes();
122                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
123                 Object retObj = getTimestampOfLastReading();
124                 rmiComm.sendReturnObj(retObj, localMethodBytes);
125         }
126
127         public void ___setId() {
128                 byte[] localMethodBytes = methodBytes;
129                 rmiComm.setGetMethodBytes();
130                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
131                 setId((int) paramObj[0]);
132         }
133
134         public void ___getId() throws IOException {
135                 byte[] localMethodBytes = methodBytes;
136                 rmiComm.setGetMethodBytes();
137                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
138                 Object retObj = getId();
139                 rmiComm.sendReturnObj(retObj, localMethodBytes);
140         }
141
142         public void ___registerCallback() {
143                 byte[] localMethodBytes = methodBytes;
144                 rmiComm.setGetMethodBytes();
145                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
146                 try {
147                         int[] stubIdArray0 = (int[]) paramObj[0];
148                         int objIdRecv0 = stubIdArray0[0];
149                         SmartthingsSensorSmartCallback newStub0 = null;
150                         if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
151                                 newStub0 = new SmartthingsSensorSmartCallback_Stub(rmiComm, objIdRecv0);
152                                 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
153                                 rmiComm.setObjectIdCounter(objIdRecv0);
154                                 rmiComm.decrementObjectIdCounter();
155                         }
156                         else {
157                                 newStub0 = (SmartthingsSensorSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
158                         }
159                         SmartthingsSensorSmartCallback stub0 = newStub0;
160                         registerCallback(stub0);
161                 } catch(Exception ex) {
162                         ex.printStackTrace();
163                         throw new Error("Exception from callback object instantiation!");
164                 }
165         }
166
167         public void ___waitRequestInvokeMethod() throws IOException {
168                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
169                 while (true) {
170                         if (!methodReceived.get()) {
171                                 continue;
172                         }
173                         methodBytes = rmiComm.getMethodBytes();
174                         methodReceived.set(false);
175                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
176                         int methodId = IoTRMIComm.getMethodId(methodBytes);
177                         if (_objectId == objectId) {
178                                 if (!set2Allowed.contains(methodId)) {
179                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
180                                 }
181                         }
182                         else {
183                                 continue;
184                         }
185                         switch (methodId) {
186                                 case 0:
187                                 new Thread() {
188                                         public void run() {
189                                                 try {
190                                                         ___init();
191                                                 }
192                                                 catch (Exception ex) {
193                                                         ex.printStackTrace();
194                                                 }
195                                         }
196                                 }.start();
197                                 break;
198                                 case 1:
199                                 new Thread() {
200                                         public void run() {
201                                                 try {
202                                                         ___getValue();
203                                                 }
204                                                 catch (Exception ex) {
205                                                         ex.printStackTrace();
206                                                 }
207                                         }
208                                 }.start();
209                                 break;
210                                 case 2:
211                                 new Thread() {
212                                         public void run() {
213                                                 try {
214                                                         ___isActiveValue();
215                                                 }
216                                                 catch (Exception ex) {
217                                                         ex.printStackTrace();
218                                                 }
219                                         }
220                                 }.start();
221                                 break;
222                                 case 3:
223                                 new Thread() {
224                                         public void run() {
225                                                 try {
226                                                         ___getTimestampOfLastReading();
227                                                 }
228                                                 catch (Exception ex) {
229                                                         ex.printStackTrace();
230                                                 }
231                                         }
232                                 }.start();
233                                 break;
234                                 case 4:
235                                 new Thread() {
236                                         public void run() {
237                                                 try {
238                                                         ___setId();
239                                                 }
240                                                 catch (Exception ex) {
241                                                         ex.printStackTrace();
242                                                 }
243                                         }
244                                 }.start();
245                                 break;
246                                 case 5:
247                                 new Thread() {
248                                         public void run() {
249                                                 try {
250                                                         ___getId();
251                                                 }
252                                                 catch (Exception ex) {
253                                                         ex.printStackTrace();
254                                                 }
255                                         }
256                                 }.start();
257                                 break;
258                                 case 6:
259                                 new Thread() {
260                                         public void run() {
261                                                 try {
262                                                         ___registerCallback();
263                                                 }
264                                                 catch (Exception ex) {
265                                                         ex.printStackTrace();
266                                                 }
267                                         }
268                                 }.start();
269                                 break;
270                                 default: 
271                                 throw new Error("Method Id " + methodId + " not recognized!");
272                         }
273                 }
274         }
275
276 }