Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / drivers / Java / GPSPhoneGateway / GPSGateway_Skeleton.java
1 package iotcode.GPSPhoneGateway;
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 GPSGateway_Skeleton implements GPSGateway {
18
19         private GPSGateway mainObj;
20         private int objectId = 3;
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[] object3Permission = { 5, 6, 2, 1, 0, 3, 7, 4 };
28         private static List<Integer> set3Allowed;
29         
30
31         public GPSGateway_Skeleton(GPSGateway _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set3Allowed = new ArrayList<Integer>(Arrays.asList(object3Permission));
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 GPSGateway_Skeleton(GPSGateway _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set3Allowed = new ArrayList<Integer>(Arrays.asList(object3Permission));
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 int getRoomID() {
81                 return mainObj.getRoomID();
82         }
83
84         public boolean getRingStatus() {
85                 return mainObj.getRingStatus();
86         }
87
88         public void setNewRoomIDAvailable(boolean bValue) {
89                 mainObj.setNewRoomIDAvailable(bValue);
90         }
91
92         public void setNewRingStatusAvailable(boolean bValue) {
93                 mainObj.setNewRingStatusAvailable(bValue);
94         }
95
96         public void registerCallback(GPSGatewaySmartCallback _callbackTo) {
97                 mainObj.registerCallback(_callbackTo);
98         }
99
100         public void ___init() {
101                 byte[] localMethodBytes = methodBytes;
102                 rmiComm.setGetMethodBytes();
103                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
104                 init();
105         }
106
107         public void ___start() {
108                 byte[] localMethodBytes = methodBytes;
109                 rmiComm.setGetMethodBytes();
110                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
111                 start();
112         }
113
114         public void ___stop() {
115                 byte[] localMethodBytes = methodBytes;
116                 rmiComm.setGetMethodBytes();
117                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
118                 stop();
119         }
120
121         public void ___getRoomID() throws IOException {
122                 byte[] localMethodBytes = methodBytes;
123                 rmiComm.setGetMethodBytes();
124                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
125                 Object retObj = getRoomID();
126                 rmiComm.sendReturnObj(retObj, localMethodBytes);
127         }
128
129         public void ___getRingStatus() throws IOException {
130                 byte[] localMethodBytes = methodBytes;
131                 rmiComm.setGetMethodBytes();
132                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
133                 Object retObj = getRingStatus();
134                 rmiComm.sendReturnObj(retObj, localMethodBytes);
135         }
136
137         public void ___setNewRoomIDAvailable() {
138                 byte[] localMethodBytes = methodBytes;
139                 rmiComm.setGetMethodBytes();
140                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { boolean.class }, new Class<?>[] { null }, localMethodBytes);
141                 setNewRoomIDAvailable((boolean) paramObj[0]);
142         }
143
144         public void ___setNewRingStatusAvailable() {
145                 byte[] localMethodBytes = methodBytes;
146                 rmiComm.setGetMethodBytes();
147                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { boolean.class }, new Class<?>[] { null }, localMethodBytes);
148                 setNewRingStatusAvailable((boolean) paramObj[0]);
149         }
150
151         public void ___registerCallback() {
152                 byte[] localMethodBytes = methodBytes;
153                 rmiComm.setGetMethodBytes();
154                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
155                 try {
156                         int[] stubIdArray0 = (int[]) paramObj[0];
157                         int objIdRecv0 = stubIdArray0[0];
158                         GPSGatewaySmartCallback newStub0 = null;
159                         if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
160                                 newStub0 = new GPSGatewaySmartCallback_Stub(rmiComm, objIdRecv0);
161                                 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
162                                 rmiComm.setObjectIdCounter(objIdRecv0);
163                                 rmiComm.decrementObjectIdCounter();
164                         }
165                         else {
166                                 newStub0 = (GPSGatewaySmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
167                         }
168                         GPSGatewaySmartCallback stub0 = newStub0;
169                         registerCallback(stub0);
170                 } catch(Exception ex) {
171                         ex.printStackTrace();
172                         throw new Error("Exception from callback object instantiation!");
173                 }
174         }
175
176         public void ___waitRequestInvokeMethod() throws IOException {
177                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
178                 while (true) {
179                         if (!methodReceived.get()) {
180                                 continue;
181                         }
182                         methodBytes = rmiComm.getMethodBytes();
183                         methodReceived.set(false);
184                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
185                         int methodId = IoTRMIComm.getMethodId(methodBytes);
186                         if (_objectId == objectId) {
187                                 if (!set3Allowed.contains(methodId)) {
188                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
189                                 }
190                         }
191                         else {
192                                 continue;
193                         }
194                         switch (methodId) {
195                                 case 0:
196                                 new Thread() {
197                                         public void run() {
198                                                 try {
199                                                         ___init();
200                                                 }
201                                                 catch (Exception ex) {
202                                                         ex.printStackTrace();
203                                                 }
204                                         }
205                                 }.start();
206                                 break;
207                                 case 1:
208                                 new Thread() {
209                                         public void run() {
210                                                 try {
211                                                         ___start();
212                                                 }
213                                                 catch (Exception ex) {
214                                                         ex.printStackTrace();
215                                                 }
216                                         }
217                                 }.start();
218                                 break;
219                                 case 2:
220                                 new Thread() {
221                                         public void run() {
222                                                 try {
223                                                         ___stop();
224                                                 }
225                                                 catch (Exception ex) {
226                                                         ex.printStackTrace();
227                                                 }
228                                         }
229                                 }.start();
230                                 break;
231                                 case 3:
232                                 new Thread() {
233                                         public void run() {
234                                                 try {
235                                                         ___getRoomID();
236                                                 }
237                                                 catch (Exception ex) {
238                                                         ex.printStackTrace();
239                                                 }
240                                         }
241                                 }.start();
242                                 break;
243                                 case 4:
244                                 new Thread() {
245                                         public void run() {
246                                                 try {
247                                                         ___getRingStatus();
248                                                 }
249                                                 catch (Exception ex) {
250                                                         ex.printStackTrace();
251                                                 }
252                                         }
253                                 }.start();
254                                 break;
255                                 case 5:
256                                 new Thread() {
257                                         public void run() {
258                                                 try {
259                                                         ___setNewRoomIDAvailable();
260                                                 }
261                                                 catch (Exception ex) {
262                                                         ex.printStackTrace();
263                                                 }
264                                         }
265                                 }.start();
266                                 break;
267                                 case 6:
268                                 new Thread() {
269                                         public void run() {
270                                                 try {
271                                                         ___setNewRingStatusAvailable();
272                                                 }
273                                                 catch (Exception ex) {
274                                                         ex.printStackTrace();
275                                                 }
276                                         }
277                                 }.start();
278                                 break;
279                                 case 7:
280                                 new Thread() {
281                                         public void run() {
282                                                 try {
283                                                         ___registerCallback();
284                                                 }
285                                                 catch (Exception ex) {
286                                                         ex.printStackTrace();
287                                                 }
288                                         }
289                                 }.start();
290                                 break;
291                                 default: 
292                                 throw new Error("Method Id " + methodId + " not recognized!");
293                         }
294                 }
295         }
296
297 }