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