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