Adding callback for C++ (still has bug for return values); adding struct as method...
[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 waitRequestInvokeMethod() throws IOException {
57
58                 // Struct size
59                 int structsize1 = 0;
60                 // Loop continuously waiting for incoming bytes
61                 while (true) {
62
63                         rmiObj.getMethodBytes();
64                         int _objectId = rmiObj.getObjectId();
65                         if (_objectId == objectId) {
66                         // Multiplex based on object Id
67                                 String methodSign = rmiObj.getSignature();
68                                 Object[] paramObj = null;
69                                 Object retObj = null;
70
71                                 if (methodSign.equals("voidsetA(int)")) {
72                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
73                                                 new Class<?>[] { null }, new Class<?>[] { null });
74                                         setA((int) paramObj[0]);
75                                 } else if (methodSign.equals("voidsetB(float)")) {
76                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
77                                                 new Class<?>[] { null }, new Class<?>[] { null });
78                                         setB((float) paramObj[0]);
79                                 } else if (methodSign.equals("voidsetC(string)")) {
80                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class }, 
81                                                 new Class<?>[] { null }, new Class<?>[] { null });
82                                         setC((String) paramObj[0]);
83                                 } else if (methodSign.equals("sumArray(string[])")) {
84                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
85                                                 new Class<?>[] { null }, new Class<?>[] { null });
86                                         retObj = sumArray((String[]) paramObj[0]);
87                                 } else if (methodSign.equals("intsetAndGetA(int)")) {
88                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
89                                                 new Class<?>[] { null }, new Class<?>[] { null });
90                                         retObj = setAndGetA((int) paramObj[0]);
91                                 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
92                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
93                                                 new Class<?>[] { null, null }, new Class<?>[] { null, null });
94                                         retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
95                                 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
96                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
97                                                 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
98                                         CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
99                                         registerCallback((CallBackInterface) cbstub);
100                                 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
101                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
102                                                 new Class<?>[] { null }, new Class<?>[] { null });
103                                         //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
104                                         //IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
105                                         int numStubs = (int) paramObj[0];
106                                         CallBackInterface[] stub = new CallBackInterface[numStubs];
107                                         for (int objId = 0; objId < numStubs; objId++) {
108                                                 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
109                                                 objIdCnt++;
110                                         }
111                                         registerCallback(stub);
112                                 } else if (methodSign.equals("intcallBack()")) {
113                                         retObj = callBack();
114                                 // Special option to register callback
115                                 } else if (methodSign.equals("registercallback")) {
116                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
117                                                 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
118                                         String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
119                                         rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
120                                         System.out.println("Creating a new IoTRMICall object");
121                                 // Struct handling (3 is the size of the struct)
122                                 } else if (methodSign.equals("structsize")) {
123                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
124                                                 new Class<?>[] { null }, new Class<?>[] { null });
125                                         structsize1 = (int) paramObj[0];
126                                 } else if (methodSign.equals("handleStruct(StructJ[])")) {
127                                         Class<?>[] paramCls = new Class<?>[3*structsize1];
128                                         Class<?>[] paramClsTyp1 = new Class<?>[3*structsize1];
129                                         Class<?>[] paramClsTyp2 = new Class<?>[3*structsize1];
130                                         int pos = 0;
131                                         for(int i=0; i < structsize1; i++) {
132                                                 paramCls[pos] = String.class;
133                                                 paramClsTyp1[pos] = null;
134                                                 paramClsTyp2[pos++] = null;
135                                                 paramCls[pos] = float.class;
136                                                 paramClsTyp1[pos] = null;
137                                                 paramClsTyp2[pos++] = null;
138                                                 paramCls[pos] = int.class;
139                                                 paramClsTyp1[pos] = null;
140                                                 paramClsTyp2[pos++] = null;
141                                         }
142                                         paramObj = rmiObj.getMethodParams(paramCls, 
143                                                 paramClsTyp1, paramClsTyp2);
144                                         StructJ[] data = new StructJ[structsize1];
145                                         for (int i=0; i < structsize1; i++) {
146                                                 data[i] = new StructJ();
147                                         }
148                                         pos = 0;
149                                         for(int i=0; i < structsize1; i++) {
150                                                 data[i].name = (String) paramObj[pos++];
151                                                 data[i].value = (float) paramObj[pos++];
152                                                 data[i].year = (int) paramObj[pos++];
153                                         }
154                                         tc.handleStruct(data);
155                                 } else
156                                         throw new Error("Signature not recognized!");
157
158                                 if (retObj != null) {
159                                         rmiObj.sendReturnObj(retObj);
160                                 }
161                                 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
162                         }
163                 }
164         }
165
166
167         // Return method signatures
168         public static String[] getMethodSignatures() {
169
170                 return methodSignatures;
171         }
172         
173         
174         public void setA(int _int) {
175                 
176                 tc.setA(_int);
177         }
178         
179         
180         public void setB(float _float) {
181                 
182                 tc.setB(_float);
183         }
184         
185         
186         public void setC(String _string) {
187                 
188                 tc.setC(_string);
189         }
190         
191         
192         public String sumArray(String[] newA) {
193                 
194                 return tc.sumArray(newA);
195         }
196         
197         
198         public int setAndGetA(int newA) {
199                 
200                 return tc.setAndGetA(newA);
201         }
202         
203         
204         public int setACAndGetA(String newC, int newA) {
205                 
206                 return tc.setACAndGetA(newC, newA);
207         }
208         
209         
210         public void registerCallback(CallBackInterface _cb) {
211                 
212                 tc.registerCallback(_cb);
213         }
214
215         public void registerCallback(CallBackInterface[] _cb) {
216                 
217                 tc.registerCallback(_cb);
218         }
219         
220         public int callBack() {
221                 
222                 return tc.callBack();
223         }
224
225         public void handleStruct(StructJ[] data) {
226
227                 tc.handleStruct(data);
228         }
229
230
231         public static void main(String[] args) throws Exception {
232
233                 int port = 5010;
234                 TestClass tc = new TestClass(3, 5f, "7911");
235                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
236
237 /*              String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
238                 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
239                 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
240
241                 // Can replace for-loop with while-loop if necessary
242                 for (int i = 1; i < 3; i++) {
243                         TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
244                         mapCBObject.put(i, tcSkel);
245                 }
246
247                 Object retObj = null;
248                 while (true) {
249                         byte[] method = rmiObj.getMethodBytes();
250                         int objId = IoTRMIObject.getObjectId(method);
251                         TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
252                         if (tcSkel != null) {
253                                 rmiObj.setMethodBytes(method);
254                                 retObj = tcSkel.invokeMethod(rmiObj);
255                         }
256                         if (retObj != null) {
257                                 rmiObj.sendReturnObj(retObj);
258                         }
259                 }
260 */
261                 //int objectId = 1;
262                 //System.out.println("Creating 0 object");
263                 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
264                 //System.out.println("Creating 1 object");
265                 //objectId = 2;
266                 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
267                 //System.out.println("Creating 2 object");
268
269                 /*for (int i = 1; i < 3; i++) {
270                         final int objectId = i;
271                         Thread thread = new Thread() {
272                                 public void run() {
273                                 try{
274                                                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
275                                         } catch (Exception ex){
276                                                 ex.printStackTrace();
277                                                 throw new Error("Error instantiating class CallBack_Skeleton!");
278                                 }
279                             }
280                         };
281                         thread.start();
282                 }*/
283         }
284 }