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 = {
26 "intsetACAndGetA(string,int)",
28 "voidregisterCallBack(CallBackInterface)",
29 "voidregisterCallBack(CallBackInterface[])",
31 "handleStruct(StructJ[])",
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, methodSignatures);
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 String ___sumArray() {
106 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
107 new Class<?>[] { null }, new Class<?>[] { null });
108 return sumArray((String[]) paramObj[0]);
112 public int setAndGetA(int newA) {
114 return tc.setAndGetA(newA);
118 public int ___setAndGetA() {
120 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
121 new Class<?>[] { null }, new Class<?>[] { null });
122 return setAndGetA((int) paramObj[0]);
126 public int setACAndGetA(String newC, int newA) {
128 return tc.setACAndGetA(newC, newA);
132 public int ___setACAndGetA() {
134 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
135 new Class<?>[] { null, null }, new Class<?>[] { null, null });
136 return setACAndGetA((String) paramObj[0], (int) paramObj[1]);
140 public void registerCallback(CallBackInterface _cb) {
142 tc.registerCallback(_cb);
146 public void ___registerCallback() throws IOException {
148 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
149 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
150 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
151 registerCallback((CallBackInterface) cbstub);
155 public void registerCallback(CallBackInterface[] _cb) {
157 tc.registerCallback(_cb);
161 // Use 4 underscores because this is a second instance of registerCallback
162 public void ____registerCallback() throws IOException {
164 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
165 new Class<?>[] { null }, new Class<?>[] { null });
166 int numStubs = (int) paramObj[0];
167 CallBackInterface[] stub = new CallBackInterface[numStubs];
168 for (int objId = 0; objId < numStubs; objId++) {
169 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
172 registerCallback(stub);
176 public void ___regCB() throws IOException {
178 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
179 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
180 String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
181 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
182 System.out.println("Creating a new IoTRMICall object");
186 public int callBack() {
188 return tc.callBack();
192 public int ___callBack() {
198 public void handleStruct(StructJ[] data) {
200 tc.handleStruct(data);
204 public int ___structSize() {
206 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
207 new Class<?>[] { null }, new Class<?>[] { null });
208 return (int) paramObj[0];
212 public void ___handleStruct(int structsize1) {
214 Class<?>[] paramCls = new Class<?>[3*structsize1];
215 Class<?>[] paramClsTyp1 = new Class<?>[3*structsize1];
216 Class<?>[] paramClsTyp2 = new Class<?>[3*structsize1];
218 for(int i=0; i < structsize1; i++) {
219 paramCls[pos] = String.class;
220 paramClsTyp1[pos] = null;
221 paramClsTyp2[pos++] = null;
222 paramCls[pos] = float.class;
223 paramClsTyp1[pos] = null;
224 paramClsTyp2[pos++] = null;
225 paramCls[pos] = int.class;
226 paramClsTyp1[pos] = null;
227 paramClsTyp2[pos++] = null;
229 Object[] paramObj = rmiObj.getMethodParams(paramCls,
230 paramClsTyp1, paramClsTyp2);
231 StructJ[] data = new StructJ[structsize1];
232 for (int i=0; i < structsize1; i++) {
233 data[i] = new StructJ();
236 for(int i=0; i < structsize1; i++) {
237 data[i].name = (String) paramObj[pos++];
238 data[i].value = (float) paramObj[pos++];
239 data[i].year = (int) paramObj[pos++];
241 tc.handleStruct(data);
245 private void ___waitRequestInvokeMethod() throws IOException {
249 // Loop continuously waiting for incoming bytes
252 rmiObj.getMethodBytes();
253 int _objectId = rmiObj.getObjectId();
254 if (_objectId == objectId) {
255 // Multiplex based on object Id
256 String methodSign = rmiObj.getSignature();
257 Object retObj = null;
258 if (methodSign.equals("voidsetA(int)")) {
260 } else if (methodSign.equals("voidsetB(float)")) {
262 } else if (methodSign.equals("voidsetC(string)")) {
264 } else if (methodSign.equals("sumArray(string[])")) {
265 retObj = ___sumArray();
266 } else if (methodSign.equals("intsetAndGetA(int)")) {
267 retObj = ___setAndGetA();
268 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
269 retObj = ___setACAndGetA();
270 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
271 ___registerCallback();
272 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
273 ____registerCallback();
274 } else if (methodSign.equals("intcallBack()")) {
275 retObj = ___callBack();
276 // Special option to register callback
277 } else if (methodSign.equals("registercallback")) {
279 // Struct handling (3 is the size of the struct)
280 } else if (methodSign.equals("structsize")) {
281 structsize1 = ___structSize();
282 } else if (methodSign.equals("handleStruct(StructJ[])")) {
283 ___handleStruct(structsize1);
285 throw new Error("Signature not recognized!");
287 if (retObj != null) {
288 rmiObj.sendReturnObj(retObj);
290 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
296 // Return method signatures
297 public static String[] getMethodSignatures() {
299 return methodSignatures;
303 public static void main(String[] args) throws Exception {
306 TestClass tc = new TestClass(3, 5f, "7911");
307 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
309 /* String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
310 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
311 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
313 // Can replace for-loop with while-loop if necessary
314 for (int i = 1; i < 3; i++) {
315 TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
316 mapCBObject.put(i, tcSkel);
319 Object retObj = null;
321 byte[] method = rmiObj.getMethodBytes();
322 int objId = IoTRMIObject.getObjectId(method);
323 TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
324 if (tcSkel != null) {
325 rmiObj.setMethodBytes(method);
326 retObj = tcSkel.invokeMethod(rmiObj);
328 if (retObj != null) {
329 rmiObj.sendReturnObj(retObj);
334 //System.out.println("Creating 0 object");
335 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
336 //System.out.println("Creating 1 object");
338 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
339 //System.out.println("Creating 2 object");
341 /*for (int i = 1; i < 3; i++) {
342 final int objectId = i;
343 Thread thread = new Thread() {
346 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
347 } catch (Exception ex){
348 ex.printStackTrace();
349 throw new Error("Error instantiating class CallBack_Skeleton!");