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