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