Updating the first benchmark with new stubs/skeletons; somehow motion detection is...
[iot2.git] / benchmarks / drivers / Java / AmcrestCamera / Camera_Skeleton.java
1 package iotcode.AmcrestCamera;
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 Camera_Skeleton implements Camera {
18
19         private Camera mainObj;
20         private int objectId = 0;
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 final static int object0Id = 0; //CameraSmart
28         private static Integer[] object0Permission = { 8, 7, 9, 6, 2, 1, 4, 3, 0, 10, 5 };
29         private static List<Integer> set0Allowed;
30         
31
32         public Camera_Skeleton(Camera _mainObj, int _portSend, int _portRecv) throws Exception {
33                 mainObj = _mainObj;
34                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
35                 set0Allowed = new ArrayList<Integer>(Arrays.asList(object0Permission));
36                 IoTRMIUtil.mapSkel.put(_mainObj, this);
37                 IoTRMIUtil.mapSkelId.put(_mainObj, objectId);
38                 didAlreadyInitWaitInvoke = new AtomicBoolean(false);
39                 methodReceived = new AtomicBoolean(false);
40                 rmiComm.registerSkeleton(objectId, methodReceived);
41                 Thread thread1 = new Thread() {
42                         public void run() {
43                                 try {
44                                         ___waitRequestInvokeMethod();
45                                 }
46                                 catch (Exception ex)
47                                 {
48                                         ex.printStackTrace();
49                                 }
50                         }
51                 };
52                 thread1.start();
53         }
54
55         public Camera_Skeleton(Camera _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
56                 mainObj = _mainObj;
57                 rmiComm = _rmiComm;
58                 objectId = _objectId;
59                 set0Allowed = new ArrayList<Integer>(Arrays.asList(object0Permission));
60                 didAlreadyInitWaitInvoke = new AtomicBoolean(false);
61                 methodReceived = new AtomicBoolean(false);
62                 rmiComm.registerSkeleton(objectId, methodReceived);
63         }
64
65         public boolean didAlreadyInitWaitInvoke() {
66                 return didAlreadyInitWaitInvoke.get();
67         }
68
69         public void init() {
70                 mainObj.init();
71         }
72
73         public void start() {
74                 mainObj.start();
75         }
76
77         public void stop() {
78                 mainObj.stop();
79         }
80
81         public byte[] getLatestFrame() {
82                 return mainObj.getLatestFrame();
83         }
84
85         public long getTimestamp() {
86                 return mainObj.getTimestamp();
87         }
88
89         public List<Resolution> getSupportedResolutions() {
90                 return mainObj.getSupportedResolutions();
91         }
92
93         public boolean setResolution(Resolution _res) {
94                 return mainObj.setResolution(_res);
95         }
96
97         public boolean setFPS(int _fps) {
98                 return mainObj.setFPS(_fps);
99         }
100
101         public int getMaxFPS() {
102                 return mainObj.getMaxFPS();
103         }
104
105         public int getMinFPS() {
106                 return mainObj.getMinFPS();
107         }
108
109         public void registerCallback(CameraSmartCallback _callbackTo) {
110                 mainObj.registerCallback(_callbackTo);
111         }
112
113         public void ___init() {
114                 byte[] localMethodBytes = methodBytes;
115                 rmiComm.setGetMethodBytes();
116                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
117                 init();
118         }
119
120         public void ___start() {
121                 byte[] localMethodBytes = methodBytes;
122                 rmiComm.setGetMethodBytes();
123                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
124                 start();
125         }
126
127         public void ___stop() {
128                 byte[] localMethodBytes = methodBytes;
129                 rmiComm.setGetMethodBytes();
130                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
131                 stop();
132         }
133
134         public void ___getLatestFrame() throws IOException {
135                 byte[] localMethodBytes = methodBytes;
136                 rmiComm.setGetMethodBytes();
137                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
138                 Object retObj = getLatestFrame();
139                 rmiComm.sendReturnObj(retObj, localMethodBytes);
140         }
141
142         public void ___getTimestamp() throws IOException {
143                 byte[] localMethodBytes = methodBytes;
144                 rmiComm.setGetMethodBytes();
145                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
146                 Object retObj = getTimestamp();
147                 rmiComm.sendReturnObj(retObj, localMethodBytes);
148         }
149
150         public void ___getSupportedResolutions() throws IOException {
151                 byte[] localMethodBytes = methodBytes;
152                 rmiComm.setGetMethodBytes();
153                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
154                 List<Resolution> retEnum = getSupportedResolutions();
155                 int retLen = retEnum.size();
156                 int[] retEnumVal = new int[retLen];
157                 for (int i = 0; i < retLen; i++) {
158                         retEnumVal[i] = retEnum.get(i).ordinal();
159                 }
160                 Object retObj = retEnumVal;
161                 rmiComm.sendReturnObj(retObj, localMethodBytes);
162         }
163
164         public void ___setResolution() throws IOException {
165                 byte[] localMethodBytes = methodBytes;
166                 rmiComm.setGetMethodBytes();
167                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
168                 int paramInt0[] = (int[]) paramObj[0];
169                 Resolution[] enumVals = Resolution.values();
170                 Resolution paramEnum0 = enumVals[paramInt0[0]];
171                 Object retObj = setResolution(paramEnum0);
172                 rmiComm.sendReturnObj(retObj, localMethodBytes);
173         }
174
175         public void ___setFPS() throws IOException {
176                 byte[] localMethodBytes = methodBytes;
177                 rmiComm.setGetMethodBytes();
178                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
179                 Object retObj = setFPS((int) paramObj[0]);
180                 rmiComm.sendReturnObj(retObj, localMethodBytes);
181         }
182
183         public void ___getMaxFPS() throws IOException {
184                 byte[] localMethodBytes = methodBytes;
185                 rmiComm.setGetMethodBytes();
186                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
187                 Object retObj = getMaxFPS();
188                 rmiComm.sendReturnObj(retObj, localMethodBytes);
189         }
190
191         public void ___getMinFPS() throws IOException {
192                 byte[] localMethodBytes = methodBytes;
193                 rmiComm.setGetMethodBytes();
194                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
195                 Object retObj = getMinFPS();
196                 rmiComm.sendReturnObj(retObj, localMethodBytes);
197         }
198
199         public void ___registerCallback() {
200                 byte[] localMethodBytes = methodBytes;
201                 rmiComm.setGetMethodBytes();
202                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
203                 try {
204                         int[] stubIdArray0 = (int[]) paramObj[0];
205                         int objIdRecv0 = stubIdArray0[0];
206                         CameraSmartCallback newStub0 = null;
207                         if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
208                                 newStub0 = new CameraSmartCallback_Stub(rmiComm, objIdRecv0);
209                                 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
210                                 rmiComm.setObjectIdCounter(objIdRecv0);
211                                 rmiComm.decrementObjectIdCounter();
212                         }
213                         else {
214                                 newStub0 = (CameraSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
215                         }
216                         CameraSmartCallback stub0 = newStub0;
217                         registerCallback(stub0);
218                 } catch(Exception ex) {
219                         ex.printStackTrace();
220                         throw new Error("Exception from callback object instantiation!");
221                 }
222         }
223
224         public void ___waitRequestInvokeMethod() throws IOException {
225                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
226                 while (true) {
227                         if (!methodReceived.get()) {
228                                 continue;
229                         }
230                         methodBytes = rmiComm.getMethodBytes();
231                         methodReceived.set(false);
232                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
233                         int methodId = IoTRMIComm.getMethodId(methodBytes);
234                         if (_objectId == objectId) {
235                                 if (!set0Allowed.contains(methodId)) {
236                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
237                                 }
238                         }
239                         else {
240                                 continue;
241                         }
242                         switch (methodId) {
243                                 case 0:
244                                 new Thread() {
245                                         public void run() {
246                                                 try {
247                                                         ___init();
248                                                 }
249                                                 catch (Exception ex) {
250                                                         ex.printStackTrace();
251                                                 }
252                                         }
253                                 }.start();
254                                 break;
255                                 case 1:
256                                 new Thread() {
257                                         public void run() {
258                                                 try {
259                                                         ___start();
260                                                 }
261                                                 catch (Exception ex) {
262                                                         ex.printStackTrace();
263                                                 }
264                                         }
265                                 }.start();
266                                 break;
267                                 case 2:
268                                 new Thread() {
269                                         public void run() {
270                                                 try {
271                                                         ___stop();
272                                                 }
273                                                 catch (Exception ex) {
274                                                         ex.printStackTrace();
275                                                 }
276                                         }
277                                 }.start();
278                                 break;
279                                 case 3:
280                                 new Thread() {
281                                         public void run() {
282                                                 try {
283                                                         ___getLatestFrame();
284                                                 }
285                                                 catch (Exception ex) {
286                                                         ex.printStackTrace();
287                                                 }
288                                         }
289                                 }.start();
290                                 break;
291                                 case 4:
292                                 new Thread() {
293                                         public void run() {
294                                                 try {
295                                                         ___getTimestamp();
296                                                 }
297                                                 catch (Exception ex) {
298                                                         ex.printStackTrace();
299                                                 }
300                                         }
301                                 }.start();
302                                 break;
303                                 case 5:
304                                 new Thread() {
305                                         public void run() {
306                                                 try {
307                                                         ___getSupportedResolutions();
308                                                 }
309                                                 catch (Exception ex) {
310                                                         ex.printStackTrace();
311                                                 }
312                                         }
313                                 }.start();
314                                 break;
315                                 case 6:
316                                 new Thread() {
317                                         public void run() {
318                                                 try {
319                                                         ___setResolution();
320                                                 }
321                                                 catch (Exception ex) {
322                                                         ex.printStackTrace();
323                                                 }
324                                         }
325                                 }.start();
326                                 break;
327                                 case 7:
328                                 new Thread() {
329                                         public void run() {
330                                                 try {
331                                                         ___setFPS();
332                                                 }
333                                                 catch (Exception ex) {
334                                                         ex.printStackTrace();
335                                                 }
336                                         }
337                                 }.start();
338                                 break;
339                                 case 8:
340                                 new Thread() {
341                                         public void run() {
342                                                 try {
343                                                         ___getMaxFPS();
344                                                 }
345                                                 catch (Exception ex) {
346                                                         ex.printStackTrace();
347                                                 }
348                                         }
349                                 }.start();
350                                 break;
351                                 case 9:
352                                 new Thread() {
353                                         public void run() {
354                                                 try {
355                                                         ___getMinFPS();
356                                                 }
357                                                 catch (Exception ex) {
358                                                         ex.printStackTrace();
359                                                 }
360                                         }
361                                 }.start();
362                                 break;
363                                 case 10:
364                                 new Thread() {
365                                         public void run() {
366                                                 try {
367                                                         ___registerCallback();
368                                                 }
369                                                 catch (Exception ex) {
370                                                         ex.printStackTrace();
371                                                 }
372                                         }
373                                 }.start();
374                                 break;
375                                 default: 
376                                 throw new Error("Method Id " + methodId + " not recognized!");
377                         }
378                 }
379         }
380
381 }