Adding doorlock to the fourth benchmark
[iot2.git] / benchmarks / drivers / Java / DoorlockActuator / SmartthingsActuator_Skeleton.java
1 package iotcode.DoorlockActuator;
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 SmartthingsActuator_Skeleton implements SmartthingsActuator {
18
19         private SmartthingsActuator 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 = { 5, 7, 8, 2, 6, 4, 3, 0, 1 };
28         private static List<Integer> set1Allowed;
29         
30
31         public SmartthingsActuator_Skeleton(SmartthingsActuator _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 SmartthingsActuator_Skeleton(SmartthingsActuator _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 boolean actuate(int value) {
73                 return mainObj.actuate(value);
74         }
75
76         public void requestStatus() {
77                 mainObj.requestStatus();
78         }
79
80         public int getStatus() {
81                 return mainObj.getStatus();
82         }
83
84         public boolean isActiveStatus() {
85                 return mainObj.isActiveStatus();
86         }
87
88         public long getTimestampOfLastReading() {
89                 return mainObj.getTimestampOfLastReading();
90         }
91
92         public void setId(int id) {
93                 mainObj.setId(id);
94         }
95
96         public int getId() {
97                 return mainObj.getId();
98         }
99
100         public void registerCallback(SmartthingsActuatorSmartCallback _callbackTo) {
101                 mainObj.registerCallback(_callbackTo);
102         }
103
104         public void ___init() {
105                 byte[] localMethodBytes = methodBytes;
106                 rmiComm.setGetMethodBytes();
107                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
108                 init();
109         }
110
111         public void ___actuate() throws IOException {
112                 byte[] localMethodBytes = methodBytes;
113                 rmiComm.setGetMethodBytes();
114                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
115                 Object retObj = actuate((int) paramObj[0]);
116                 rmiComm.sendReturnObj(retObj, localMethodBytes);
117         }
118
119         public void ___requestStatus() {
120                 byte[] localMethodBytes = methodBytes;
121                 rmiComm.setGetMethodBytes();
122                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
123                 requestStatus();
124         }
125
126         public void ___getStatus() throws IOException {
127                 byte[] localMethodBytes = methodBytes;
128                 rmiComm.setGetMethodBytes();
129                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
130                 Object retObj = getStatus();
131                 rmiComm.sendReturnObj(retObj, localMethodBytes);
132         }
133
134         public void ___isActiveStatus() throws IOException {
135                 byte[] localMethodBytes = methodBytes;
136                 rmiComm.setGetMethodBytes();
137                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
138                 Object retObj = isActiveStatus();
139                 rmiComm.sendReturnObj(retObj, localMethodBytes);
140         }
141
142         public void ___getTimestampOfLastReading() throws IOException {
143                 byte[] localMethodBytes = methodBytes;
144                 rmiComm.setGetMethodBytes();
145                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
146                 Object retObj = getTimestampOfLastReading();
147                 rmiComm.sendReturnObj(retObj, localMethodBytes);
148         }
149
150         public void ___setId() {
151                 byte[] localMethodBytes = methodBytes;
152                 rmiComm.setGetMethodBytes();
153                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
154                 setId((int) paramObj[0]);
155         }
156
157         public void ___getId() throws IOException {
158                 byte[] localMethodBytes = methodBytes;
159                 rmiComm.setGetMethodBytes();
160                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
161                 Object retObj = getId();
162                 rmiComm.sendReturnObj(retObj, localMethodBytes);
163         }
164
165         public void ___registerCallback() {
166                 byte[] localMethodBytes = methodBytes;
167                 rmiComm.setGetMethodBytes();
168                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
169                 try {
170                         int[] stubIdArray0 = (int[]) paramObj[0];
171                         int objIdRecv0 = stubIdArray0[0];
172                         SmartthingsActuatorSmartCallback newStub0 = null;
173                         if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
174                                 newStub0 = new SmartthingsActuatorSmartCallback_Stub(rmiComm, objIdRecv0);
175                                 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
176                                 rmiComm.setObjectIdCounter(objIdRecv0);
177                                 rmiComm.decrementObjectIdCounter();
178                         }
179                         else {
180                                 newStub0 = (SmartthingsActuatorSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
181                         }
182                         SmartthingsActuatorSmartCallback stub0 = newStub0;
183                         registerCallback(stub0);
184                 } catch(Exception ex) {
185                         ex.printStackTrace();
186                         throw new Error("Exception from callback object instantiation!");
187                 }
188         }
189
190         public void ___waitRequestInvokeMethod() throws IOException {
191                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
192                 while (true) {
193                         if (!methodReceived.get()) {
194                                 continue;
195                         }
196                         methodBytes = rmiComm.getMethodBytes();
197                         methodReceived.set(false);
198                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
199                         int methodId = IoTRMIComm.getMethodId(methodBytes);
200                         if (_objectId == objectId) {
201                                 if (!set1Allowed.contains(methodId)) {
202                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
203                                 }
204                         }
205                         else {
206                                 continue;
207                         }
208                         switch (methodId) {
209                                 case 0:
210                                 new Thread() {
211                                         public void run() {
212                                                 try {
213                                                         ___init();
214                                                 }
215                                                 catch (Exception ex) {
216                                                         ex.printStackTrace();
217                                                 }
218                                         }
219                                 }.start();
220                                 break;
221                                 case 1:
222                                 new Thread() {
223                                         public void run() {
224                                                 try {
225                                                         ___actuate();
226                                                 }
227                                                 catch (Exception ex) {
228                                                         ex.printStackTrace();
229                                                 }
230                                         }
231                                 }.start();
232                                 break;
233                                 case 2:
234                                 new Thread() {
235                                         public void run() {
236                                                 try {
237                                                         ___requestStatus();
238                                                 }
239                                                 catch (Exception ex) {
240                                                         ex.printStackTrace();
241                                                 }
242                                         }
243                                 }.start();
244                                 break;
245                                 case 3:
246                                 new Thread() {
247                                         public void run() {
248                                                 try {
249                                                         ___getStatus();
250                                                 }
251                                                 catch (Exception ex) {
252                                                         ex.printStackTrace();
253                                                 }
254                                         }
255                                 }.start();
256                                 break;
257                                 case 4:
258                                 new Thread() {
259                                         public void run() {
260                                                 try {
261                                                         ___isActiveStatus();
262                                                 }
263                                                 catch (Exception ex) {
264                                                         ex.printStackTrace();
265                                                 }
266                                         }
267                                 }.start();
268                                 break;
269                                 case 5:
270                                 new Thread() {
271                                         public void run() {
272                                                 try {
273                                                         ___getTimestampOfLastReading();
274                                                 }
275                                                 catch (Exception ex) {
276                                                         ex.printStackTrace();
277                                                 }
278                                         }
279                                 }.start();
280                                 break;
281                                 case 6:
282                                 new Thread() {
283                                         public void run() {
284                                                 try {
285                                                         ___setId();
286                                                 }
287                                                 catch (Exception ex) {
288                                                         ex.printStackTrace();
289                                                 }
290                                         }
291                                 }.start();
292                                 break;
293                                 case 7:
294                                 new Thread() {
295                                         public void run() {
296                                                 try {
297                                                         ___getId();
298                                                 }
299                                                 catch (Exception ex) {
300                                                         ex.printStackTrace();
301                                                 }
302                                         }
303                                 }.start();
304                                 break;
305                                 case 8:
306                                 new Thread() {
307                                         public void run() {
308                                                 try {
309                                                         ___registerCallback();
310                                                 }
311                                                 catch (Exception ex) {
312                                                         ex.printStackTrace();
313                                                 }
314                                         }
315                                 }.start();
316                                 break;
317                                 default: 
318                                 throw new Error("Method Id " + methodId + " not recognized!");
319                         }
320                 }
321         }
322
323 }