Adding config file for sharing.
[iot2.git] / benchmarks / drivers / Java / BlossomSprinkler / Sprinkler_Skeleton.java
1 package iotcode.BlossomSprinkler;
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 Sprinkler_Skeleton implements Sprinkler {
18
19         private Sprinkler mainObj;
20         private int objectId = 6;
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[] object2Permission = { 4, 2, 0, 1, 3 };
28         private static List<Integer> set2Allowed;
29         
30
31         public Sprinkler_Skeleton(Sprinkler _mainObj, int _portSend, int _portRecv) throws Exception {
32                 mainObj = _mainObj;
33                 rmiComm = new IoTRMICommServer(_portSend, _portRecv);
34                 set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
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 Sprinkler_Skeleton(Sprinkler _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
55                 mainObj = _mainObj;
56                 rmiComm = _rmiComm;
57                 objectId = _objectId;
58                 set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
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 setZone(int _zone, boolean _onOff, int _onDurationSeconds) {
73                 mainObj.setZone(_zone, _onOff, _onDurationSeconds);
74         }
75
76         public List<ZoneState> getZoneStates() {
77                 return mainObj.getZoneStates();
78         }
79
80         public int getNumberOfZones() {
81                 return mainObj.getNumberOfZones();
82         }
83
84         public boolean doesHaveZoneTimers() {
85                 return mainObj.doesHaveZoneTimers();
86         }
87
88         public void ___init() {
89                 byte[] localMethodBytes = methodBytes;
90                 rmiComm.setGetMethodBytes();
91                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
92                 init();
93         }
94
95         public void ___setZone() {
96                 byte[] localMethodBytes = methodBytes;
97                 rmiComm.setGetMethodBytes();
98                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class, boolean.class, int.class }, new Class<?>[] { null, null, null }, localMethodBytes);
99                 setZone((int) paramObj[0], (boolean) paramObj[1], (int) paramObj[2]);
100         }
101
102         public void ___getZoneStates() throws IOException {
103                 byte[] localMethodBytes = methodBytes;
104                 rmiComm.setGetMethodBytes();
105                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
106                 List<ZoneState> retStruct = getZoneStates();
107                 int retLen = retStruct.size();
108                 Object retLenObj = retLen;
109                 rmiComm.sendReturnObj(retLenObj, localMethodBytes);
110                 Class<?>[] retCls = new Class<?>[3*retLen];
111                 Object[] retObj = new Object[3*retLen];
112                 int retPos = 0;
113                 for(int i = 0; i < retLen; i++) {
114                         retCls[retPos] = int.class;
115                         retObj[retPos++] = retStruct.get(i).zoneNumber;
116                         retCls[retPos] = boolean.class;
117                         retObj[retPos++] = retStruct.get(i).onOffState;
118                         retCls[retPos] = int.class;
119                         retObj[retPos++] = retStruct.get(i).duration;
120                 }
121                 rmiComm.sendReturnObj(retCls, retObj, localMethodBytes);
122         }
123
124         public void ___getNumberOfZones() throws IOException {
125                 byte[] localMethodBytes = methodBytes;
126                 rmiComm.setGetMethodBytes();
127                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
128                 Object retObj = getNumberOfZones();
129                 rmiComm.sendReturnObj(retObj, localMethodBytes);
130         }
131
132         public void ___doesHaveZoneTimers() throws IOException {
133                 byte[] localMethodBytes = methodBytes;
134                 rmiComm.setGetMethodBytes();
135                 Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
136                 Object retObj = doesHaveZoneTimers();
137                 rmiComm.sendReturnObj(retObj, localMethodBytes);
138         }
139
140         public void ___waitRequestInvokeMethod() throws IOException {
141                 didAlreadyInitWaitInvoke.compareAndSet(false, true);
142                 while (true) {
143                         if (!methodReceived.get()) {
144                                 continue;
145                         }
146                         methodBytes = rmiComm.getMethodBytes();
147                         methodReceived.set(false);
148                         int _objectId = IoTRMIComm.getObjectId(methodBytes);
149                         int methodId = IoTRMIComm.getMethodId(methodBytes);
150                         if (_objectId == objectId) {
151                                 if (!set2Allowed.contains(methodId)) {
152                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
153                                 }
154                         }
155                         else {
156                                 continue;
157                         }
158                         switch (methodId) {
159                                 case 0:
160                                 new Thread() {
161                                         public void run() {
162                                                 try {
163                                                         ___init();
164                                                 }
165                                                 catch (Exception ex) {
166                                                         ex.printStackTrace();
167                                                 }
168                                         }
169                                 }.start();
170                                 break;
171                                 case 1:
172                                 new Thread() {
173                                         public void run() {
174                                                 try {
175                                                         ___setZone();
176                                                 }
177                                                 catch (Exception ex) {
178                                                         ex.printStackTrace();
179                                                 }
180                                         }
181                                 }.start();
182                                 break;
183                                 case 2:
184                                 new Thread() {
185                                         public void run() {
186                                                 try {
187                                                         ___getZoneStates();
188                                                 }
189                                                 catch (Exception ex) {
190                                                         ex.printStackTrace();
191                                                 }
192                                         }
193                                 }.start();
194                                 break;
195                                 case 3:
196                                 new Thread() {
197                                         public void run() {
198                                                 try {
199                                                         ___getNumberOfZones();
200                                                 }
201                                                 catch (Exception ex) {
202                                                         ex.printStackTrace();
203                                                 }
204                                         }
205                                 }.start();
206                                 break;
207                                 case 4:
208                                 new Thread() {
209                                         public void run() {
210                                                 try {
211                                                         ___doesHaveZoneTimers();
212                                                 }
213                                                 catch (Exception ex) {
214                                                         ex.printStackTrace();
215                                                 }
216                                         }
217                                 }.start();
218                                 break;
219                                 default: 
220                                 throw new Error("Method Id " + methodId + " not recognized!");
221                         }
222                 }
223         }
224
225 }