70d8a812162527dfcfd3f4661516257b955b4365
[iot2.git] / iotjava / iotrmi / Java / sample / CallBack_Skeleton.java
1 package iotrmi.Java.sample;
2
3 import java.io.IOException;
4 import java.util.Set;
5 import java.util.Arrays;
6 import iotrmi.Java.IoTRMIObject;
7
8 public class CallBack_Skeleton implements CallBackInterface {
9
10         private int objectId = 0;       // Default value is 0
11         private final static String[] methodSignatures = {
12
13                 "intprintInt()",
14                 "voidsetInt(int)"
15         };
16         private CallBackInterface cb;
17         private IoTRMIObject rmiObj;
18
19
20         /**
21          * Constructors
22          */
23         public CallBack_Skeleton(CallBackInterface _cb, int _port) throws
24                 ClassNotFoundException, InstantiationException,
25                         IllegalAccessException, IOException {
26
27                 cb = _cb;
28                 System.out.println("Creating CallBack_Skeleton and waiting!");
29                 rmiObj = new IoTRMIObject(_port, methodSignatures);
30         }
31
32
33         public void waitRequestInvokeMethod() throws IOException {
34
35                 // Loop continuously waiting for incoming bytes
36                 while (true) {
37
38                         rmiObj.getMethodBytes();
39                         int objId = rmiObj.getObjectId();
40                         if (objId == objectId) {
41                         // Multiplex based on object Id
42                                 rmiObj.getMethodBytes();
43                                 String methodSign = rmiObj.getSignature();
44                                 Object[] paramObj = null;
45                                 Object retObj = null;
46                                 System.out.println("Method sign: " + methodSign);
47
48                                 if (methodSign.equals("intprintInt()")) {
49                                         retObj = printInt();
50                                 } else if (methodSign.equals("voidsetInt(int)")) {
51                                         paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
52                                                 new Class<?>[] { null }, new Class<?>[] { null });
53                                         setInt((int) paramObj[0]);
54                                 } else
55                                         throw new Error("Signature not recognized!");
56                                 System.out.println("Return object: " + retObj);
57
58                                 if (retObj != null) {
59                                         rmiObj.sendReturnObj(retObj);
60                                 }
61                                 System.out.println("Servicing remote call for method: " + methodSign);
62                         }
63                 }
64         }
65
66
67         // Return method signatures
68         public static String[] getMethodSignatures() {
69
70                 return methodSignatures;
71         }
72
73
74         public int printInt() {
75                 return cb.printInt();
76         }
77
78
79         public void setInt(int _i) {
80                 cb.setInt(_i);
81         }
82
83
84         public static void main(String[] args) throws Exception {
85
86                 int port = 5010;
87                 CallBack cb = new CallBack(23);
88                 CallBack_Skeleton cbSkel = new CallBack_Skeleton(cb, port);
89                 cbSkel.waitRequestInvokeMethod();
90         }
91 }