Updating the third benchmark with new stubs and skeletons
[iot2.git] / benchmarks / Java / SpeakerController / GPSGatewayCallback_Skeleton.java
1 package SpeakerController;
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 GPSGatewayCallback_Skeleton implements GPSGatewayCallback {
18
19         private GPSGatewayCallback mainObj;
20         private int objectId = 4;
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[] object4Permission = { 0, 1 };
28         private static List<Integer> set4Allowed;
29         
30
31         public GPSGatewayCallback_Skeleton(GPSGatewayCallback _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set4Allowed = new ArrayList<Integer>(Arrays.asList(object4Permission));
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 GPSGatewayCallback_Skeleton(GPSGatewayCallback _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set4Allowed = new ArrayList<Integer>(Arrays.asList(object4Permission));
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 newRoomIDRetrieved(int _roomIdentifier) {
69                 mainObj.newRoomIDRetrieved(_roomIdentifier);
70         }
71
72         public void newRingStatusRetrieved(boolean _ringStatus) {
73                 mainObj.newRingStatusRetrieved(_ringStatus);
74         }
75
76         public void ___newRoomIDRetrieved() {
77                 byte[] localMethodBytes = methodBytes;
78                 rmiComm.setGetMethodBytes();
79                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
80                 newRoomIDRetrieved((int) paramObj[0]);
81         }
82
83         public void ___newRingStatusRetrieved() {
84                 byte[] localMethodBytes = methodBytes;
85                 rmiComm.setGetMethodBytes();
86                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { boolean.class }, new Class<?>[] { null }, localMethodBytes);
87                 newRingStatusRetrieved((boolean) paramObj[0]);
88         }
89
90         public void ___waitRequestInvokeMethod() throws IOException {
91                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
92                 while (true) {
93                         if (!methodReceived.get()) {
94                                 continue;
95                         }
96                         methodBytes = rmiComm.getMethodBytes();
97                         methodReceived.set(false);
98                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
99                         int methodId = IoTRMIComm.getMethodId(methodBytes);
100                         if (_objectId == objectId) {
101                                 if (!set4Allowed.contains(methodId)) {
102                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
103                                 }
104                         }
105                         else {
106                                 continue;
107                         }
108                         switch (methodId) {
109                                 case 0:
110                                 new Thread() {
111                                         public void run() {
112                                                 try {
113                                                         ___newRoomIDRetrieved();
114                                                 }
115                                                 catch (Exception ex) {
116                                                         ex.printStackTrace();
117                                                 }
118                                         }
119                                 }.start();
120                                 break;
121                                 case 1:
122                                 new Thread() {
123                                         public void run() {
124                                                 try {
125                                                         ___newRingStatusRetrieved();
126                                                 }
127                                                 catch (Exception ex) {
128                                                         ex.printStackTrace();
129                                                 }
130                                         }
131                                 }.start();
132                                 break;
133                                 default: 
134                                 throw new Error("Method Id " + methodId + " not recognized!");
135                         }
136                 }
137         }
138
139 }