1 package iotrmi.Java.sample;
3 import java.io.IOException;
4 import java.util.Arrays;
7 import java.util.HashMap;
9 import iotrmi.Java.IoTRMIObject;
10 import iotrmi.Java.IoTRMICall;
12 public class TestClass_Skeleton implements TestClassInterface {
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 = {
22 "voidsetB(float)", // 1
23 "voidsetC(string)", // 2
24 "sumArray(string[])", // 3
25 "intsetAndGetA(int)", // 4
26 "intsetACAndGetA(string,int)", // 5
28 "voidregisterCallBack(CallBackInterface)", // 7
29 "voidregisterCallBack(CallBackInterface[])", // 8
30 "registercallback", // 9
31 "handleStruct(StructJ[])", // 10
35 private TestClassInterface tc;
37 private IoTRMIObject rmiObj;
38 private IoTRMICall rmiCall;
39 private CallBackInterface cbstub;
45 public TestClass_Skeleton(TestClass _tc, int _port) throws
46 ClassNotFoundException, InstantiationException,
47 IllegalAccessException, IOException {
51 rmiObj = new IoTRMIObject(_port);
52 ___waitRequestInvokeMethod();
56 public void setA(int _int) {
62 public void ___setA() {
64 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
65 new Class<?>[] { null }, new Class<?>[] { null });
66 setA((int) paramObj[0]);
70 public void setB(float _float) {
76 public void ___setB() {
78 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
79 new Class<?>[] { null }, new Class<?>[] { null });
80 setB((float) paramObj[0]);
84 public void setC(String _string) {
90 public void ___setC() {
92 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
93 new Class<?>[] { null }, new Class<?>[] { null });
94 setC((String) paramObj[0]);
98 public String sumArray(String[] newA) {
100 return tc.sumArray(newA);
104 public void ___sumArray() throws IOException {
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);
113 public int setAndGetA(int newA) {
115 return tc.setAndGetA(newA);
119 public void ___setAndGetA() throws IOException {
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);
128 public int setACAndGetA(String newC, int newA) {
130 return tc.setACAndGetA(newC, newA);
134 public void ___setACAndGetA() throws IOException {
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);
143 public void registerCallback(CallBackInterface _cb) {
145 tc.registerCallback(_cb);
149 public void ___registerCallback() throws IOException {
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);
158 public void registerCallback(CallBackInterface[] _cb) {
160 tc.registerCallback(_cb);
164 // Use 4 underscores because this is a second instance of registerCallback
165 public void ____registerCallback() throws IOException {
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);
175 registerCallback(stub);
179 public void ___regCB() throws IOException {
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");
189 public int callBack() {
191 return tc.callBack();
195 public void ___callBack() throws IOException {
197 Object retObj = callBack();
198 rmiObj.sendReturnObj(retObj);
202 public void handleStruct(StructJ[] data) {
204 tc.handleStruct(data);
208 public int ___structSize() {
210 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
211 new Class<?>[] { null }, new Class<?>[] { null });
212 return (int) paramObj[0];
216 public void ___handleStruct(int structsize1) {
218 Class<?>[] paramCls = new Class<?>[3*structsize1];
219 Class<?>[] paramClsTyp1 = new Class<?>[3*structsize1];
220 Class<?>[] paramClsTyp2 = new Class<?>[3*structsize1];
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;
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();
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++];
245 tc.handleStruct(data);
249 private void ___waitRequestInvokeMethod() throws IOException {
253 // Loop continuously waiting for incoming bytes
256 rmiObj.getMethodBytes();
257 int _objectId = rmiObj.getObjectId();
258 if (_objectId == objectId) {
259 // Multiplex based on object Id
260 int methodId = rmiObj.getMethodId();
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;
278 throw new Error("Signature not recognized!");
285 // Return method signatures
286 public static String[] getMethodSignatures() {
288 return methodSignatures;
292 public static void main(String[] args) throws Exception {
295 TestClass tc = new TestClass(3, 5f, "7911");
296 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
298 /* String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
299 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
300 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
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);
308 Object retObj = null;
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);
317 if (retObj != null) {
318 rmiObj.sendReturnObj(retObj);
323 //System.out.println("Creating 0 object");
324 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
325 //System.out.println("Creating 1 object");
327 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
328 //System.out.println("Creating 2 object");
330 /*for (int i = 1; i < 3; i++) {
331 final int objectId = i;
332 Thread thread = new Thread() {
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!");