Committing changes in driver files for paper evaluation
[iot2.git] / benchmarks / drivers / Java / IHome / Speaker_Skeleton.java
1 package iotcode.IHome;
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 Speaker_Skeleton implements Speaker {
18
19         private Speaker mainObj;
20         private int objectId = 1;
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[] object1Permission = { 6, 2, 9, 1, 3, 4, 5, 7, 8, 0, 10 };
28         private static List<Integer> set1Allowed;
29         
30
31         public Speaker_Skeleton(Speaker _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set1Allowed = new ArrayList<Integer>(Arrays.asList(object1Permission));
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 Speaker_Skeleton(Speaker _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set1Allowed = new ArrayList<Integer>(Arrays.asList(object1Permission));
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 boolean startPlayback() {
73                 return mainObj.startPlayback();
74         }
75
76         public boolean stopPlayback() {
77                 return mainObj.stopPlayback();
78         }
79
80         public boolean getPlaybackState() {
81                 return mainObj.getPlaybackState();
82         }
83
84         public boolean setVolume(float _percent) {
85                 return mainObj.setVolume(_percent);
86         }
87
88         public float getVolume() {
89                 return mainObj.getVolume();
90         }
91
92         public int getPosition() {
93                 return mainObj.getPosition();
94         }
95
96         public void setPosition(int _mSec) {
97                 mainObj.setPosition(_mSec);
98         }
99
100         public void loadData(short _samples[], int _offs, int _len) {
101                 mainObj.loadData(_samples, _offs, _len);
102         }
103
104         public void clearData() {
105                 mainObj.clearData();
106         }
107
108         public void registerCallback(SpeakerSmartCallback _cb) {
109                 mainObj.registerCallback(_cb);
110         }
111
112         public void ___init() {
113                 byte[] localMethodBytes = methodBytes;
114                 rmiComm.setGetMethodBytes();
115                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
116                 init();
117         }
118
119         public void ___startPlayback() throws IOException {
120                 byte[] localMethodBytes = methodBytes;
121                 rmiComm.setGetMethodBytes();
122                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
123                 Object retObj = startPlayback();
124                 rmiComm.sendReturnObj(retObj, localMethodBytes);
125         }
126
127         public void ___stopPlayback() throws IOException {
128                 byte[] localMethodBytes = methodBytes;
129                 rmiComm.setGetMethodBytes();
130                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
131                 Object retObj = stopPlayback();
132                 rmiComm.sendReturnObj(retObj, localMethodBytes);
133         }
134
135         public void ___getPlaybackState() throws IOException {
136                 byte[] localMethodBytes = methodBytes;
137                 rmiComm.setGetMethodBytes();
138                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
139                 Object retObj = getPlaybackState();
140                 rmiComm.sendReturnObj(retObj, localMethodBytes);
141         }
142
143         public void ___setVolume() throws IOException {
144                 byte[] localMethodBytes = methodBytes;
145                 rmiComm.setGetMethodBytes();
146                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { float.class }, new Class<?>[] { null }, localMethodBytes);
147                 Object retObj = setVolume((float) paramObj[0]);
148                 rmiComm.sendReturnObj(retObj, localMethodBytes);
149         }
150
151         public void ___getVolume() throws IOException {
152                 byte[] localMethodBytes = methodBytes;
153                 rmiComm.setGetMethodBytes();
154                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
155                 Object retObj = getVolume();
156                 rmiComm.sendReturnObj(retObj, localMethodBytes);
157         }
158
159         public void ___getPosition() throws IOException {
160                 byte[] localMethodBytes = methodBytes;
161                 rmiComm.setGetMethodBytes();
162                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
163                 Object retObj = getPosition();
164                 rmiComm.sendReturnObj(retObj, localMethodBytes);
165         }
166
167         public void ___setPosition() {
168                 byte[] localMethodBytes = methodBytes;
169                 rmiComm.setGetMethodBytes();
170                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
171                 setPosition((int) paramObj[0]);
172         }
173
174         public void ___loadData() {
175                 byte[] localMethodBytes = methodBytes;
176                 rmiComm.setGetMethodBytes();
177                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { short[].class, int.class, int.class }, new Class<?>[] { null, null, null }, localMethodBytes);
178                 loadData((short[]) paramObj[0], (int) paramObj[1], (int) paramObj[2]);
179         }
180
181         public void ___clearData() {
182                 byte[] localMethodBytes = methodBytes;
183                 rmiComm.setGetMethodBytes();
184                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
185                 clearData();
186         }
187
188         public void ___registerCallback() {
189                 byte[] localMethodBytes = methodBytes;
190                 rmiComm.setGetMethodBytes();
191                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
192                 try {
193                         int[] stubIdArray0 = (int[]) paramObj[0];
194                         int objIdRecv0 = stubIdArray0[0];
195                         SpeakerSmartCallback newStub0 = null;
196                         if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
197                                 newStub0 = new SpeakerSmartCallback_Stub(rmiComm, objIdRecv0);
198                                 IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
199                                 rmiComm.setObjectIdCounter(objIdRecv0);
200                                 rmiComm.decrementObjectIdCounter();
201                         }
202                         else {
203                                 newStub0 = (SpeakerSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
204                         }
205                         SpeakerSmartCallback stub0 = newStub0;
206                         registerCallback(stub0);
207                 } catch(Exception ex) {
208                         ex.printStackTrace();
209                         throw new Error("Exception from callback object instantiation!");
210                 }
211         }
212
213         public void ___waitRequestInvokeMethod() throws IOException {
214                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
215                 while (true) {
216                         if (!methodReceived.get()) {
217                                 continue;
218                         }
219                         methodBytes = rmiComm.getMethodBytes();
220                         methodReceived.set(false);
221                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
222                         int methodId = IoTRMIComm.getMethodId(methodBytes);
223                         if (_objectId == objectId) {
224                                 if (!set1Allowed.contains(methodId)) {
225                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
226                                 }
227                         }
228                         else {
229                                 continue;
230                         }
231                         switch (methodId) {
232                                 case 0:
233                                 new Thread() {
234                                         public void run() {
235                                                 try {
236                                                         ___init();
237                                                 }
238                                                 catch (Exception ex) {
239                                                         ex.printStackTrace();
240                                                 }
241                                         }
242                                 }.start();
243                                 break;
244                                 case 1:
245                                 new Thread() {
246                                         public void run() {
247                                                 try {
248                                                         ___startPlayback();
249                                                 }
250                                                 catch (Exception ex) {
251                                                         ex.printStackTrace();
252                                                 }
253                                         }
254                                 }.start();
255                                 break;
256                                 case 2:
257                                 new Thread() {
258                                         public void run() {
259                                                 try {
260                                                         ___stopPlayback();
261                                                 }
262                                                 catch (Exception ex) {
263                                                         ex.printStackTrace();
264                                                 }
265                                         }
266                                 }.start();
267                                 break;
268                                 case 3:
269                                 new Thread() {
270                                         public void run() {
271                                                 try {
272                                                         ___getPlaybackState();
273                                                 }
274                                                 catch (Exception ex) {
275                                                         ex.printStackTrace();
276                                                 }
277                                         }
278                                 }.start();
279                                 break;
280                                 case 4:
281                                 new Thread() {
282                                         public void run() {
283                                                 try {
284                                                         ___setVolume();
285                                                 }
286                                                 catch (Exception ex) {
287                                                         ex.printStackTrace();
288                                                 }
289                                         }
290                                 }.start();
291                                 break;
292                                 case 5:
293                                 new Thread() {
294                                         public void run() {
295                                                 try {
296                                                         ___getVolume();
297                                                 }
298                                                 catch (Exception ex) {
299                                                         ex.printStackTrace();
300                                                 }
301                                         }
302                                 }.start();
303                                 break;
304                                 case 6:
305                                 new Thread() {
306                                         public void run() {
307                                                 try {
308                                                         ___getPosition();
309                                                 }
310                                                 catch (Exception ex) {
311                                                         ex.printStackTrace();
312                                                 }
313                                         }
314                                 }.start();
315                                 break;
316                                 case 7:
317                                 new Thread() {
318                                         public void run() {
319                                                 try {
320                                                         ___setPosition();
321                                                 }
322                                                 catch (Exception ex) {
323                                                         ex.printStackTrace();
324                                                 }
325                                         }
326                                 }.start();
327                                 break;
328                                 case 8:
329                                 new Thread() {
330                                         public void run() {
331                                                 try {
332                                                         ___loadData();
333                                                 }
334                                                 catch (Exception ex) {
335                                                         ex.printStackTrace();
336                                                 }
337                                         }
338                                 }.start();
339                                 break;
340                                 case 9:
341                                 new Thread() {
342                                         public void run() {
343                                                 try {
344                                                         ___clearData();
345                                                 }
346                                                 catch (Exception ex) {
347                                                         ex.printStackTrace();
348                                                 }
349                                         }
350                                 }.start();
351                                 break;
352                                 case 10:
353                                 new Thread() {
354                                         public void run() {
355                                                 try {
356                                                         ___registerCallback();
357                                                 }
358                                                 catch (Exception ex) {
359                                                         ex.printStackTrace();
360                                                 }
361                                         }
362                                 }.start();
363                                 break;
364                                 default: 
365                                 throw new Error("Method Id " + methodId + " not recognized!");
366                         }
367                 }
368         }
369
370 }