Initial version that handles multiple callback objects through 1 socket
[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 final static String[] methodSignatures = {
19
20                 "voidsetA(int)",
21                 "voidsetB(float)",
22                 "voidsetC(string)",
23                 "sumArray(string[])",
24                 "intsetAndGetA(int)",
25                 "intsetACAndGetA(string,int)",
26                 "intcallBack()",
27                 "voidregisterCallBack(CallBackInterface)",
28                 "voidregisterCallBack(CallBackInterface[])"
29         };
30
31         private TestClassInterface tc;
32         private int port;
33         private IoTRMIObject rmiObj;
34         private CallBackInterface cbstub;
35
36
37         /**
38          * Constructors
39          */
40         public TestClass_Skeleton(TestClass _tc, int _port) throws
41                 ClassNotFoundException, InstantiationException,
42                         IllegalAccessException, IOException {
43
44                 tc = _tc;
45                 port = _port;
46                 rmiObj = new IoTRMIObject(_port, methodSignatures);
47                 waitRequestInvokeMethod();
48         }
49
50
51         public void waitRequestInvokeMethod() throws IOException {
52
53                 // Loop continuously waiting for incoming bytes
54                 while (true) {
55
56                         rmiObj.getMethodBytes();
57                         int _objectId = rmiObj.getObjectId();
58                         if (_objectId == objectId) {
59                         // Multiplex based on object Id
60                                 String methodSign = rmiObj.getSignature();
61                                 Object[] paramObj = null;
62                                 Object retObj = null;
63
64                                 if (methodSign.equals("voidsetA(int)")) {
65                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
66                                                 new Class<?>[] { null }, new Class<?>[] { null });
67                                         setA((int) paramObj[0]);
68                                 } else if (methodSign.equals("voidsetB(float)")) {
69                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
70                                                 new Class<?>[] { null }, new Class<?>[] { null });
71                                         setB((float) paramObj[0]);
72                                 } else if (methodSign.equals("voidsetC(string)")) {
73                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class }, 
74                                                 new Class<?>[] { null }, new Class<?>[] { null });
75                                         setC((String) paramObj[0]);
76                                 } else if (methodSign.equals("sumArray(string[])")) {
77                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
78                                                 new Class<?>[] { null }, new Class<?>[] { null });
79                                         retObj = sumArray((String[]) paramObj[0]);
80                                 } else if (methodSign.equals("intsetAndGetA(int)")) {
81                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
82                                                 new Class<?>[] { null }, new Class<?>[] { null });
83                                         retObj = setAndGetA((int) paramObj[0]);
84                                 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
85                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
86                                                 new Class<?>[] { null, null }, new Class<?>[] { null, null });
87                                         retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
88                                 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
89                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
90                                                 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
91                                         CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
92                                         registerCallback((CallBackInterface) cbstub);
93                                 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
94                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class, int.class }, 
95                                                 new Class<?>[] { null, null, null, null }, new Class<?>[] { null, null, null, null });
96                                         String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
97                                         IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
98                                         int numStubs = (int) paramObj[3];
99                                         CallBackInterface[] stub = new CallBackInterface[numStubs];
100                                         for (int objId = 0; objId < numStubs; objId++) {
101                                                 stub[objId] = new CallBack_CBStub(rmiCall, objId, (String) paramObj[1]);
102                                         }
103                                         registerCallback(stub);
104                                 } else if (methodSign.equals("intcallBack()")) {
105                                         retObj = callBack();
106                                 } else
107                                         throw new Error("Signature not recognized!");
108
109                                 if (retObj != null) {
110                                         rmiObj.sendReturnObj(retObj);
111                                 }
112                                 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
113                         }
114                 }
115         }
116
117
118         // Return method signatures
119         public static String[] getMethodSignatures() {
120
121                 return methodSignatures;
122         }
123         
124         
125         public void setA(int _int) {
126                 
127                 tc.setA(_int);
128         }
129         
130         
131         public void setB(float _float) {
132                 
133                 tc.setB(_float);
134         }
135         
136         
137         public void setC(String _string) {
138                 
139                 tc.setC(_string);
140         }
141         
142         
143         public String sumArray(String[] newA) {
144                 
145                 return tc.sumArray(newA);
146         }
147         
148         
149         public int setAndGetA(int newA) {
150                 
151                 return tc.setAndGetA(newA);
152         }
153         
154         
155         public int setACAndGetA(String newC, int newA) {
156                 
157                 return tc.setACAndGetA(newC, newA);
158         }
159         
160         
161         public void registerCallback(CallBackInterface _cb) {
162                 
163                 tc.registerCallback(_cb);
164         }
165
166         public void registerCallback(CallBackInterface[] _cb) {
167                 
168                 tc.registerCallback(_cb);
169         }
170         
171         public int callBack() {
172                 
173                 return tc.callBack();
174         }
175
176
177         public static void main(String[] args) throws Exception {
178
179                 int port = 5010;
180                 TestClass tc = new TestClass(3, 5f, "7911");
181                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
182
183 /*              String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
184                 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
185                 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
186
187                 // Can replace for-loop with while-loop if necessary
188                 for (int i = 1; i < 3; i++) {
189                         TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
190                         mapCBObject.put(i, tcSkel);
191                 }
192
193                 Object retObj = null;
194                 while (true) {
195                         byte[] method = rmiObj.getMethodBytes();
196                         int objId = IoTRMIObject.getObjectId(method);
197                         TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
198                         if (tcSkel != null) {
199                                 rmiObj.setMethodBytes(method);
200                                 retObj = tcSkel.invokeMethod(rmiObj);
201                         }
202                         if (retObj != null) {
203                                 rmiObj.sendReturnObj(retObj);
204                         }
205                 }
206 */
207                 //int objectId = 1;
208                 //System.out.println("Creating 0 object");
209                 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
210                 //System.out.println("Creating 1 object");
211                 //objectId = 2;
212                 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
213                 //System.out.println("Creating 2 object");
214
215                 /*for (int i = 1; i < 3; i++) {
216                         final int objectId = i;
217                         Thread thread = new Thread() {
218                                 public void run() {
219                                 try{
220                                                 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
221                                         } catch (Exception ex){
222                                                 ex.printStackTrace();
223                                                 throw new Error("Error instantiating class CallBack_Skeleton!");
224                                 }
225                             }
226                         };
227                         thread.start();
228                 }*/
229         }
230 }