Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / drivers / Java / SpruceSensor / MoistureSensor_Skeleton.java
1 package iotcode.SpruceSensor;
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 MoistureSensor_Skeleton implements MoistureSensor {
18
19         private MoistureSensor mainObj;
20         private int objectId = 5;
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[] object5Permission = { 2, 4, 5, 1, 3, 0 };
28         private static List<Integer> set5Allowed;
29         
30
31         public MoistureSensor_Skeleton(MoistureSensor _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set5Allowed = new ArrayList<Integer>(Arrays.asList(object5Permission));
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 MoistureSensor_Skeleton(MoistureSensor _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set5Allowed = new ArrayList<Integer>(Arrays.asList(object5Permission));
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 float getMoisture() {
73                 return mainObj.getMoisture();
74         }
75
76         public long getTimestampOfLastReading() {
77                 return mainObj.getTimestampOfLastReading();
78         }
79
80         public void setId(int id) {
81                 mainObj.setId(id);
82         }
83
84         public int getId() {
85                 return mainObj.getId();
86         }
87
88         public void registerCallback(MoistureSensorSmartCallback _callbackTo) {
89                 mainObj.registerCallback(_callbackTo);
90         }
91
92         public void ___init() {
93                 byte[] localMethodBytes = methodBytes;
94                 rmiComm.setGetMethodBytes();
95                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
96                 init();
97         }
98
99         public void ___getMoisture() throws IOException {
100                 byte[] localMethodBytes = methodBytes;
101                 rmiComm.setGetMethodBytes();
102                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
103                 Object retObj = getMoisture();
104                 rmiComm.sendReturnObj(retObj, localMethodBytes);
105         }
106
107         public void ___getTimestampOfLastReading() throws IOException {
108                 byte[] localMethodBytes = methodBytes;
109                 rmiComm.setGetMethodBytes();
110                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
111                 Object retObj = getTimestampOfLastReading();
112                 rmiComm.sendReturnObj(retObj, localMethodBytes);
113         }
114
115         public void ___setId() {
116                 byte[] localMethodBytes = methodBytes;
117                 rmiComm.setGetMethodBytes();
118                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
119                 setId((int) paramObj[0]);
120         }
121
122         public void ___getId() throws IOException {
123                 byte[] localMethodBytes = methodBytes;
124                 rmiComm.setGetMethodBytes();
125                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
126                 Object retObj = getId();
127                 rmiComm.sendReturnObj(retObj, localMethodBytes);
128         }
129
130         public void ___registerCallback() {
131                 byte[] localMethodBytes = methodBytes;
132                 rmiComm.setGetMethodBytes();
133                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
134                 try {
135                         int[] stubIdArray0 = (int[]) paramObj[0];
136                         int objIdRecv0 = stubIdArray0[0];
137                         MoistureSensorSmartCallback newStub0 = null;
138                         if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
139                                 newStub0 = new MoistureSensorSmartCallback_Stub(rmiComm, objIdRecv0);
140                                 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
141                                 rmiComm.setObjectIdCounter(objIdRecv0);
142                                 rmiComm.decrementObjectIdCounter();
143                         }
144                         else {
145                                 newStub0 = (MoistureSensorSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
146                         }
147                         MoistureSensorSmartCallback stub0 = newStub0;
148                         registerCallback(stub0);
149                 } catch(Exception ex) {
150                         ex.printStackTrace();
151                         throw new Error("Exception from callback object instantiation!");
152                 }
153         }
154
155         public void ___waitRequestInvokeMethod() throws IOException {
156                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
157                 while (true) {
158                         if (!methodReceived.get()) {
159                                 continue;
160                         }
161                         methodBytes = rmiComm.getMethodBytes();
162                         methodReceived.set(false);
163                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
164                         int methodId = IoTRMIComm.getMethodId(methodBytes);
165                         if (_objectId == objectId) {
166                                 if (!set5Allowed.contains(methodId)) {
167                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
168                                 }
169                         }
170                         else {
171                                 continue;
172                         }
173                         switch (methodId) {
174                                 case 0:
175                                 new Thread() {
176                                         public void run() {
177                                                 try {
178                                                         ___init();
179                                                 }
180                                                 catch (Exception ex) {
181                                                         ex.printStackTrace();
182                                                 }
183                                         }
184                                 }.start();
185                                 break;
186                                 case 1:
187                                 new Thread() {
188                                         public void run() {
189                                                 try {
190                                                         ___getMoisture();
191                                                 }
192                                                 catch (Exception ex) {
193                                                         ex.printStackTrace();
194                                                 }
195                                         }
196                                 }.start();
197                                 break;
198                                 case 2:
199                                 new Thread() {
200                                         public void run() {
201                                                 try {
202                                                         ___getTimestampOfLastReading();
203                                                 }
204                                                 catch (Exception ex) {
205                                                         ex.printStackTrace();
206                                                 }
207                                         }
208                                 }.start();
209                                 break;
210                                 case 3:
211                                 new Thread() {
212                                         public void run() {
213                                                 try {
214                                                         ___setId();
215                                                 }
216                                                 catch (Exception ex) {
217                                                         ex.printStackTrace();
218                                                 }
219                                         }
220                                 }.start();
221                                 break;
222                                 case 4:
223                                 new Thread() {
224                                         public void run() {
225                                                 try {
226                                                         ___getId();
227                                                 }
228                                                 catch (Exception ex) {
229                                                         ex.printStackTrace();
230                                                 }
231                                         }
232                                 }.start();
233                                 break;
234                                 case 5:
235                                 new Thread() {
236                                         public void run() {
237                                                 try {
238                                                         ___registerCallback();
239                                                 }
240                                                 catch (Exception ex) {
241                                                         ex.printStackTrace();
242                                                 }
243                                         }
244                                 }.start();
245                                 break;
246                                 default: 
247                                 throw new Error("Method Id " + methodId + " not recognized!");
248                         }
249                 }
250         }
251
252 }