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