Early version of RMI system for Java part; supports primitives, one-dimensional array...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Skeleton.java
1 package iotrmi.Java.sample;
2
3 import java.io.IOException;
4 import java.util.Set;
5 import iotrmi.Java.IoTRMIObject;
6
7 import java.util.Arrays;
8
9 public class TestClass_Skeleton {
10
11         private String[] methodSignatures = {
12
13                 "voidsetA(int)",
14                 "voidsetB(float)",
15                 "voidsetC(string)",
16                 "sumArray(string[])",
17                 "intsetAndGetA(int)",
18                 "intsetACAndGetA(string,int)",
19                 "intcallBack()",
20                 "voidregisterCallBack(CallBackInterface)"
21         };
22
23         private TestClass tc;
24         private IoTRMIObject rmiObj;
25         private CallBackInterface cbstub;
26
27
28         /**
29          * Constructors
30          */
31         //public TestClass_Skeleton(Object[] paramObj, int _port) throws
32         public TestClass_Skeleton(TestClass _tc, int _port) throws
33                 ClassNotFoundException, InstantiationException,
34                         IllegalAccessException, IOException {
35
36                 //tc = new TestClass((int)paramObj[0], (float)paramObj[1], (String)paramObj[2]);
37                 tc = _tc;
38                 rmiObj = new IoTRMIObject(_port, methodSignatures);
39         }
40
41
42         public void waitRequestInvokeMethod() throws IOException {
43
44                 // Loop continuously waiting for incoming bytes
45                 while (true) {
46
47                         rmiObj.getMethodBytes();
48                         String methodSign = rmiObj.getSignature();
49                         Object[] paramObj = null;
50                         Object retObj = null;
51                         System.out.println("Method sign: " + methodSign);
52
53                         if (methodSign.equals("voidsetA(int)")) {
54                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
55                                         new Class<?>[] { null }, new Class<?>[] { null });
56                                 tc.setA((int) paramObj[0]);
57                         } else if (methodSign.equals("voidsetB(float)")) {
58                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
59                                         new Class<?>[] { null }, new Class<?>[] { null });
60                                 tc.setB((float) paramObj[0]);
61                         } else if (methodSign.equals("voidsetC(string)")) {
62                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class }, 
63                                         new Class<?>[] { null }, new Class<?>[] { null });
64                                 tc.setC((String) paramObj[0]);
65                         } else if (methodSign.equals("sumArray(string[])")) {
66                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
67                                         new Class<?>[] { null }, new Class<?>[] { null });
68                                 retObj = tc.sumArray((String[]) paramObj[0]);
69                         } else if (methodSign.equals("intsetAndGetA(int)")) {
70                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
71                                         new Class<?>[] { null }, new Class<?>[] { null });
72                                 retObj = tc.setAndGetA((int) paramObj[0]);
73                         } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
74                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
75                                         new Class<?>[] { null, null }, new Class<?>[] { null, null });
76                                 retObj = tc.setACAndGetA((String) paramObj[0], (int) paramObj[1]);
77                         } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
78                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
79                                         new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
80                                 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
81                                 tc.registerCallback((CallBackInterface) cbstub);
82                         } else if (methodSign.equals("intcallBack()")) {
83                                 retObj = tc.callBack();
84                         } else
85                                 throw new Error("Signature un-recognized!");
86
87                         if (retObj != null) {
88                                 rmiObj.sendReturnObj(retObj);
89                         }
90                         System.out.println("Servicing remote call for method: " + methodSign);
91                 }
92         }
93
94
95         public static void main(String[] args) throws Exception {
96
97                 int port = 5010;
98                 TestClass tc = new TestClass(3, 5f, "7911");
99                 //TestClass_Skeleton tcSkel = new TestClass_Skeleton(new Object[] { 3, 5f, "7911"}, port);
100                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
101                 tcSkel.waitRequestInvokeMethod();
102         }
103 }