a1647400bda010ae0749d4b5aff92fbf2472aded
[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)",
22                 "voidsetB(float)",
23                 "voidsetC(string)",
24                 "sumArray(string[])",
25                 "intsetAndGetA(int)",
26                 "intsetACAndGetA(string,int)",
27                 "intcallBack()",
28                 "voidregisterCallBack(CallBackInterface)",
29                 "voidregisterCallBack(CallBackInterface[])",
30                 "registercallback",
31                 "handleStruct(StructJ[])",
32                 "structsize"
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, methodSignatures);
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 String ___sumArray() {
105                 
106                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
107                         new Class<?>[] { null }, new Class<?>[] { null });
108                 return sumArray((String[]) paramObj[0]);
109         }
110         
111         
112         public int setAndGetA(int newA) {
113                 
114                 return tc.setAndGetA(newA);
115         }
116         
117         
118         public int ___setAndGetA() {
119                 
120                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
121                         new Class<?>[] { null }, new Class<?>[] { null });
122                 return setAndGetA((int) paramObj[0]);
123         }
124         
125         
126         public int setACAndGetA(String newC, int newA) {
127                 
128                 return tc.setACAndGetA(newC, newA);
129         }
130         
131         
132         public int ___setACAndGetA() {
133                 
134                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
135                         new Class<?>[] { null, null }, new Class<?>[] { null, null });
136                 return setACAndGetA((String) paramObj[0], (int) paramObj[1]);
137         }
138         
139         
140         public void registerCallback(CallBackInterface _cb) {
141                 
142                 tc.registerCallback(_cb);
143         }
144         
145         
146         public void ___registerCallback() throws IOException {
147                 
148                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
149                         new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
150                 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
151                 registerCallback((CallBackInterface) cbstub);
152         }
153         
154
155         public void registerCallback(CallBackInterface[] _cb) {
156                 
157                 tc.registerCallback(_cb);
158         }
159         
160         
161         // Use 4 underscores because this is a second instance of registerCallback
162         public void ____registerCallback() throws IOException {
163                 
164                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
165                         new Class<?>[] { null }, new Class<?>[] { null });
166                 int numStubs = (int) paramObj[0];
167                 CallBackInterface[] stub = new CallBackInterface[numStubs];
168                 for (int objId = 0; objId < numStubs; objId++) {
169                         stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
170                         objIdCnt++;
171                 }
172                 registerCallback(stub);
173         }
174         
175         
176         public void ___regCB() throws IOException {
177                 
178                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
179                         new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
180                 String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
181                 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
182                 System.out.println("Creating a new IoTRMICall object");
183         }
184         
185         
186         public int callBack() {
187                 
188                 return tc.callBack();
189         }
190         
191         
192         public int ___callBack() {
193                 
194                 return callBack();
195         }
196         
197
198         public void handleStruct(StructJ[] data) {
199
200                 tc.handleStruct(data);
201         }
202         
203         
204         public int ___structSize() {
205                 
206                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
207                         new Class<?>[] { null }, new Class<?>[] { null });
208                 return (int) paramObj[0];               
209         }
210         
211         
212         public void ___handleStruct(int structsize1) {
213
214                 Class<?>[] paramCls = new Class<?>[3*structsize1];
215                 Class<?>[] paramClsTyp1 = new Class<?>[3*structsize1];
216                 Class<?>[] paramClsTyp2 = new Class<?>[3*structsize1];
217                 int pos = 0;
218                 for(int i=0; i < structsize1; i++) {
219                         paramCls[pos] = String.class;
220                         paramClsTyp1[pos] = null;
221                         paramClsTyp2[pos++] = null;
222                         paramCls[pos] = float.class;
223                         paramClsTyp1[pos] = null;
224                         paramClsTyp2[pos++] = null;
225                         paramCls[pos] = int.class;
226                         paramClsTyp1[pos] = null;
227                         paramClsTyp2[pos++] = null;
228                 }
229                 Object[] paramObj = rmiObj.getMethodParams(paramCls, 
230                         paramClsTyp1, paramClsTyp2);
231                 StructJ[] data = new StructJ[structsize1];
232                 for (int i=0; i < structsize1; i++) {
233                         data[i] = new StructJ();
234                 }
235                 pos = 0;
236                 for(int i=0; i < structsize1; i++) {
237                         data[i].name = (String) paramObj[pos++];
238                         data[i].value = (float) paramObj[pos++];
239                         data[i].year = (int) paramObj[pos++];
240                 }
241                 tc.handleStruct(data);
242         }
243         
244
245         private void ___waitRequestInvokeMethod() throws IOException {
246
247                 // Struct size
248                 int structsize1 = 0;
249                 // Loop continuously waiting for incoming bytes
250                 while (true) {
251
252                         rmiObj.getMethodBytes();
253                         int _objectId = rmiObj.getObjectId();
254                         if (_objectId == objectId) {
255                         // Multiplex based on object Id
256                                 String methodSign = rmiObj.getSignature();
257                                 Object retObj = null;
258                                 if (methodSign.equals("voidsetA(int)")) {
259                                         ___setA();
260                                 } else if (methodSign.equals("voidsetB(float)")) {
261                                         ___setB();
262                                 } else if (methodSign.equals("voidsetC(string)")) {
263                                         ___setC();
264                                 } else if (methodSign.equals("sumArray(string[])")) {
265                                         retObj = ___sumArray();
266                                 } else if (methodSign.equals("intsetAndGetA(int)")) {
267                                         retObj = ___setAndGetA();
268                                 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
269                                         retObj = ___setACAndGetA();
270                                 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
271                                         ___registerCallback();
272                                 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
273                                         ____registerCallback();
274                                 } else if (methodSign.equals("intcallBack()")) {
275                                         retObj = ___callBack();
276                                 // Special option to register callback
277                                 } else if (methodSign.equals("registercallback")) {
278                                         ___regCB();
279                                 // Struct handling (3 is the size of the struct)
280                                 } else if (methodSign.equals("structsize")) {
281                                         structsize1 = ___structSize();
282                                 } else if (methodSign.equals("handleStruct(StructJ[])")) {
283                                         ___handleStruct(structsize1);
284                                 } else
285                                         throw new Error("Signature not recognized!");
286
287                                 if (retObj != null) {
288                                         rmiObj.sendReturnObj(retObj);
289                                 }
290                                 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
291                         }
292                 }
293         }
294
295
296         // Return method signatures
297         public static String[] getMethodSignatures() {
298
299                 return methodSignatures;
300         }
301         
302         
303         public static void main(String[] args) throws Exception {
304
305                 int port = 5010;
306                 TestClass tc = new TestClass(3, 5f, "7911");
307                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
308
309 /*              String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
310                 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
311                 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
312
313                 // Can replace for-loop with while-loop if necessary
314                 for (int i = 1; i < 3; i++) {
315                         TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
316                         mapCBObject.put(i, tcSkel);
317                 }
318
319                 Object retObj = null;
320                 while (true) {
321                         byte[] method = rmiObj.getMethodBytes();
322                         int objId = IoTRMIObject.getObjectId(method);
323                         TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
324                         if (tcSkel != null) {
325                                 rmiObj.setMethodBytes(method);
326                                 retObj = tcSkel.invokeMethod(rmiObj);
327                         }
328                         if (retObj != null) {
329                                 rmiObj.sendReturnObj(retObj);
330                         }
331                 }
332 */
333                 //int objectId = 1;
334                 //System.out.println("Creating 0 object");
335                 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
336                 //System.out.println("Creating 1 object");
337                 //objectId = 2;
338                 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
339                 //System.out.println("Creating 2 object");
340
341                 /*for (int i = 1; i < 3; i++) {
342                         final int objectId = i;
343                         Thread thread = new Thread() {
344                                 public void run() {
345                                 try{
346                                                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
347                                         } catch (Exception ex){
348                                                 ex.printStackTrace();
349                                                 throw new Error("Error instantiating class CallBack_Skeleton!");
350                                 }
351                             }
352                         };
353                         thread.start();
354                 }*/
355         }
356 }