8c587f8c07c94006382cb998dc494983d264d08e
[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                 ___waitRequestInvokeMethod();
31         }
32
33
34         public int printInt() {
35                 return cb.printInt();
36         }
37         
38         
39         public int ___printInt() {
40                 return printInt();
41         }
42
43
44         public void setInt(int _i) {
45                 cb.setInt(_i);
46         }
47         
48         
49         public void ___setInt() {
50                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
51                         new Class<?>[] { null }, new Class<?>[] { null });
52                 setInt((int) paramObj[0]);
53         }
54         
55
56         private void ___waitRequestInvokeMethod() throws IOException {
57
58                 // Loop continuously waiting for incoming bytes
59                 while (true) {
60
61                         rmiObj.getMethodBytes();
62                         int objId = rmiObj.getObjectId();
63                         if (objId == objectId) {
64                         // Multiplex based on object Id
65                                 rmiObj.getMethodBytes();
66                                 String methodSign = rmiObj.getSignature();
67                                 Object[] paramObj = null;
68                                 Object retObj = null;
69                                 System.out.println("Method sign: " + methodSign);
70
71                                 if (methodSign.equals("intprintInt()")) {
72                                         retObj = ___printInt();
73                                 } else if (methodSign.equals("voidsetInt(int)")) {
74                                         ___setInt();
75                                 } else
76                                         throw new Error("Signature not recognized!");
77                                 System.out.println("Return object: " + retObj);
78
79                                 if (retObj != null) {
80                                         rmiObj.sendReturnObj(retObj);
81                                 }
82                                 System.out.println("Servicing remote call for method: " + methodSign);
83                         }
84                 }
85         }
86
87
88         // Return method signatures
89         public static String[] getMethodSignatures() {
90
91                 return methodSignatures;
92         }
93
94
95         public static void main(String[] args) throws Exception {
96
97                 int port = 5010;
98                 CallBack cb = new CallBack(23);
99                 CallBack_Skeleton cbSkel = new CallBack_Skeleton(cb, port);
100                 //cbSkel.waitRequestInvokeMethod();
101         }
102 }