Adding permission checks for Java; fixing type translations for return and parameter...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Skeleton.java
1 package iotrmi.Java.sample;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Set;
6 import java.util.Map;
7 import java.util.HashMap;
8
9 import java.util.List;
10 import java.util.ArrayList;
11 import java.util.Collections;
12
13 import iotrmi.Java.IoTRMIObject;
14 import iotrmi.Java.IoTRMICall;
15
16 public class TestClass_Skeleton implements TestClassInterface {
17
18         /**
19          * Class Constants
20          */
21         private static int objIdCnt = 0; // Counter for callback object Ids
22         private TestClassInterface tc;
23         private int port;
24         private IoTRMIObject rmiObj;
25         private IoTRMICall rmiCall;
26         private CallBackInterface cbstub;
27
28         // Object permission
29         private int object0Id = 0;
30         private static Integer[] object0Permission = { 0, 1, 2, 3, 4, 5 };
31         private List<Integer> set0Allowed;
32
33
34         /**
35          * Constructors
36          */
37         public TestClass_Skeleton(TestClass _tc, int _port) throws
38                 ClassNotFoundException, InstantiationException,
39                         IllegalAccessException, IOException {
40
41                 tc = _tc;
42                 port = _port;
43                 rmiObj = new IoTRMIObject(_port);
44                 set0Allowed = Arrays.asList(object0Permission);
45                 ___waitRequestInvokeMethod();
46         }
47
48         
49         public void setA(int _int) {
50                 
51                 tc.setA(_int);
52         }
53         
54         
55         public void ___setA() {
56
57                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
58                         new Class<?>[] { null });
59                 setA((int) paramObj[0]);
60         }
61         
62         
63         public void setB(float _float) {
64                 
65                 tc.setB(_float);
66         }
67         
68         
69         public void ___setB() {
70
71                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
72                         new Class<?>[] { null });
73                 setB((float) paramObj[0]);
74         }
75         
76         
77         public void setC(String _string) {
78                 
79                 tc.setC(_string);
80         }
81         
82         
83         public void ___setC() {
84                 
85                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class }, 
86                         new Class<?>[] { null });
87                 setC((String) paramObj[0]);
88         }
89         
90         
91         public String sumArray(String[] newA) {
92                 
93                 return tc.sumArray(newA);
94         }
95         
96         
97         public void ___sumArray() throws IOException {
98                 
99                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
100                         new Class<?>[] { null });
101                 Object retObj = sumArray((String[]) paramObj[0]);
102                 rmiObj.sendReturnObj(retObj);
103         }
104         
105         
106         public int setAndGetA(int newA) {
107                 
108                 return tc.setAndGetA(newA);
109         }
110         
111         
112         public void ___setAndGetA() throws IOException {
113                 
114                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
115                         new Class<?>[] { null });
116                 Object retObj = setAndGetA((int) paramObj[0]);
117                 rmiObj.sendReturnObj(retObj);
118         }
119         
120         
121         public int setACAndGetA(String newC, int newA) {
122                 
123                 return tc.setACAndGetA(newC, newA);
124         }
125         
126         
127         public void ___setACAndGetA() throws IOException {
128                 
129                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
130                         new Class<?>[] { null, null });
131                 Object retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
132                 rmiObj.sendReturnObj(retObj);
133         }
134         
135         
136         public void registerCallback(CallBackInterface _cb) {
137                 
138                 tc.registerCallback(_cb);
139         }
140         
141         
142         public void ___registerCallback() throws IOException {
143                 
144                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
145                         new Class<?>[] { null, null, null });
146                 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
147                 registerCallback((CallBackInterface) cbstub);
148         }
149         
150
151         public void registerCallback(CallBackInterface[] _cb) {
152                 
153                 tc.registerCallback(_cb);
154         }
155         
156         
157         // Use 4 underscores because this is a second instance of registerCallback
158         public void ____registerCallback() throws IOException {
159                 
160                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
161                         new Class<?>[] { null });
162                 int numStubs = (int) paramObj[0];
163                 CallBackInterface[] stub = new CallBackInterface[numStubs];
164                 for (int objId = 0; objId < numStubs; objId++) {
165                         stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
166                         objIdCnt++;
167                 }
168                 registerCallback(stub);
169         }
170         
171         
172         public void ___regCB() throws IOException {
173                 
174                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
175                         new Class<?>[] { null, null, null });
176                 //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
177                 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
178                 System.out.println("Creating a new IoTRMICall object");
179         }
180         
181         
182         public int callBack() {
183                 
184                 return tc.callBack();
185         }
186         
187         
188         public void ___callBack() throws IOException {
189                 
190                 Object retObj = callBack();
191                 rmiObj.sendReturnObj(retObj);
192         }
193         
194
195         public StructJ[] handleStruct(StructJ[] data) {
196
197                 return tc.handleStruct(data);
198         }
199         
200         
201         public int ___structSize() {
202                 
203                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
204                         new Class<?>[] { null });
205                 return (int) paramObj[0];
206         }
207         
208         
209         public void ___handleStruct(int structsize1) throws IOException {
210
211                 Class<?>[] paramCls = new Class<?>[3*structsize1];
212                 Class<?>[] paramClsVal = new Class<?>[3*structsize1];
213                 int pos = 0;
214                 for(int i=0; i < structsize1; i++) {
215                         paramCls[pos] = String.class;
216                         paramClsVal[pos++] = null;
217                         paramCls[pos] = float.class;
218                         paramClsVal[pos++] = null;
219                         paramCls[pos] = int.class;
220                         paramClsVal[pos++] = null;
221                 }
222                 Object[] paramObj = rmiObj.getMethodParams(paramCls, 
223                         paramClsVal);
224                 StructJ[] data = new StructJ[structsize1];
225                 for (int i=0; i < structsize1; i++) {
226                         data[i] = new StructJ();
227                 }
228                 pos = 0;
229                 for(int i=0; i < structsize1; i++) {
230                         data[i].name = (String) paramObj[pos++];
231                         data[i].value = (float) paramObj[pos++];
232                         data[i].year = (int) paramObj[pos++];
233                 }
234                 // Just the following if there is no returned value
235                 //tc.handleStruct(data);
236                 StructJ[] retStruct = tc.handleStruct(data);
237                 // Return length first
238                 int structsize2 = retStruct.length;
239                 Object retObj = structsize2;
240                 rmiObj.sendReturnObj(retObj);           
241                 // Send the actual struct members
242                 // Calculate the size of the array
243                 Class<?>[] retCls = new Class<?>[3*structsize2];
244                 Object[] retObj2 = new Object[3*structsize2];
245                 // Handle with for loop
246                 pos = 0;
247                 for(int i = 0; i < structsize2; i++) {
248                         retCls[pos] = String.class;
249                         retObj2[pos++] = data[i].name;
250                         retCls[pos] = float.class;
251                         retObj2[pos++] = data[i].value;
252                         retCls[pos] = int.class;
253                         retObj2[pos++] = data[i].year;
254                 }
255                 rmiObj.sendReturnObj(retCls, retObj2);
256         }
257
258
259         public EnumJ[] handleEnum(EnumJ[] en) {
260
261                 return tc.handleEnum(en);
262         }
263
264
265         public void ___handleEnum() throws IOException {
266
267                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class }, 
268                         new Class<?>[] { null }); 
269                 // Encoder/decoder
270                 int paramInt[] = (int[]) paramObj[0];
271                 int enumSize1 = paramInt.length;
272                 EnumJ[] enumJ = EnumJ.values();
273                 EnumJ[] data = new EnumJ[enumSize1];
274                 for (int i=0; i < enumSize1; i++) {
275                         data[i] = enumJ[paramInt[i]];
276                 }
277                 // if void, just "handleEnum(data)"
278                 // this is when we have return value EnumJ[]
279                 EnumJ[] retEnum = handleEnum(data);
280                 // Get length first
281                 int enumSize2 = retEnum.length;
282                 // Now send the array of integers
283                 int[] retEnumInt = new int[enumSize2];
284                 for (int i=0; i < enumSize2; i++) {
285                         retEnumInt[i] = retEnum[i].ordinal();
286                 }
287                 Object retObj = retEnumInt;
288                 rmiObj.sendReturnObj(retObj);
289         }
290         
291
292         private void ___waitRequestInvokeMethod() throws IOException {
293
294                 // Struct size
295                 int structSize1 = 0;
296                 int enumSize1 = 0;
297                 // Loop continuously waiting for incoming bytes
298                 while (true) {
299
300                         rmiObj.getMethodBytes();
301                         int _objectId = rmiObj.getObjectId();
302                         int methodId = rmiObj.getMethodId();
303                         if (_objectId == object0Id) {
304                         // Multiplex based on object Id
305                                 // Complain if the method is not allowed
306                                 if (!set0Allowed.contains(methodId))
307                                         throw new Error("TestClass_Skeleton: This object is not allowed to access method " + methodId);
308                         // If we have more than 1 object Id...
309                         //else if (_objectId == object1Id) {
310
311                         } else
312                                 throw new Error("TestClass_Skeleton: Unrecognizable object Id: " + _objectId);
313
314                         switch (methodId) {
315
316                                 case 0: ___setA(); break;
317                                 case 1: ___setB(); break;
318                                 case 2: ___setC(); break;
319                                 case 3: ___sumArray(); break;
320                                 case 4: ___setAndGetA(); break;
321                                 case 5: ___setACAndGetA(); break;
322                                 case 6: ___callBack(); break; 
323                                 case 7: ___registerCallback(); break;
324                                 case 8: ____registerCallback(); break;
325                                 // Special option to register callback
326                                 case 9: ___regCB(); break;
327                                 // Struct handling (3 is the size of the struct)
328                                 case 10: ___handleStruct(structSize1); break;
329                                 case 11: structSize1 = ___structSize(); break;
330                                 case 12: ___handleEnum(); break;
331                                 default:
332                                         throw new Error("Method Id not recognized!");
333                         }
334
335                 }
336         }
337         
338         
339         public static void main(String[] args) throws Exception {
340
341                 int port = 5010;
342                 TestClass tc = new TestClass(3, 5f, "7911");
343                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
344
345         }
346 }