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