Making sure that sample stub and skeleton classes inherit from a common interface
[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 implements TestClassInterface {
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                                 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                                 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                                 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 = 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 = 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 = 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                                 registerCallback((CallBackInterface) cbstub);
82                         } else if (methodSign.equals("intcallBack()")) {
83                                 retObj = 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 void setA(int _int) {
96                 
97                 tc.setA(_int);
98         }
99         
100         
101         public void setB(float _float) {
102                 
103                 tc.setB(_float);
104         }
105         
106         
107         public void setC(String _string) {
108                 
109                 tc.setC(_string);
110         }
111         
112         
113         public String sumArray(String[] newA) {
114                 
115                 return tc.sumArray(newA);
116         }
117         
118         
119         public int setAndGetA(int newA) {
120                 
121                 return tc.setAndGetA(newA);
122         }
123         
124         
125         public int setACAndGetA(String newC, int newA) {
126                 
127                 return tc.setACAndGetA(newC, newA);
128         }
129         
130         
131         public void registerCallback(CallBackInterface _cb) {
132                 
133                 tc.registerCallback(_cb);
134         }
135         
136         
137         public int callBack() {
138                 
139                 return tc.callBack();
140         }
141
142
143         public static void main(String[] args) throws Exception {
144
145                 int port = 5010;
146                 TestClass tc = new TestClass(3, 5f, "7911");
147                 //TestClass_Skeleton tcSkel = new TestClass_Skeleton(new Object[] { 3, 5f, "7911"}, port);
148                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
149                 tcSkel.waitRequestInvokeMethod();
150         }
151 }