74103684ee37dbf06fca49e33f43a5510207ab06
[iot2.git] / benchmarks / Java / SmartLightsController / CameraCallback_Skeleton.java
1 package SmartLightsController;
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 CameraCallback_Skeleton implements CameraCallback {
18
19         private CameraCallback 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; //CameraSmartCallback
28         private static Integer[] object0Permission = { 0 };
29         private static List<Integer> set0Allowed;
30         
31
32         public CameraCallback_Skeleton(CameraCallback _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 CameraCallback_Skeleton(CameraCallback _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 newCameraFrameAvailable(byte latestFrame[], long timeStamp) {
70                 mainObj.newCameraFrameAvailable(latestFrame, timeStamp);
71         }
72
73         public void ___newCameraFrameAvailable() {
74                 byte[] localMethodBytes = methodBytes;
75                 rmiComm.setGetMethodBytes();
76                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { byte[].class, long.class }, new Class<?>[] { null, null }, localMethodBytes);
77                 newCameraFrameAvailable((byte[]) paramObj[0], (long) paramObj[1]);
78         }
79
80         public void ___waitRequestInvokeMethod() throws IOException {
81                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
82                 while (true) {
83                         if (!methodReceived.get()) {
84                                 continue;
85                         }
86                         methodBytes = rmiComm.getMethodBytes();
87                         methodReceived.set(false);
88                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
89                         int methodId = IoTRMIComm.getMethodId(methodBytes);
90                         if (_objectId == objectId) {
91                                 if (!set0Allowed.contains(methodId)) {
92                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
93                                 }
94                         }
95                         else {
96                                 continue;
97                         }
98                         switch (methodId) {
99                                 case 0:
100                                 new Thread() {
101                                         public void run() {
102                                                 try {
103                                                         ___newCameraFrameAvailable();
104                                                 }
105                                                 catch (Exception ex) {
106                                                         ex.printStackTrace();
107                                                 }
108                                         }
109                                 }.start();
110                                 break;
111                                 default: 
112                                 throw new Error("Method Id " + methodId + " not recognized!");
113                         }
114                 }
115         }
116
117 }