Early version of RMI system for Java part; supports primitives, one-dimensional array...
[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 {
9
10         private String[] methodSignatures = {
11
12                 "intprintInt()",
13                 "voidsetInt(int)"
14         };
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                         String methodSign = rmiObj.getSignature();
40                         Object[] paramObj = null;
41                         Object retObj = null;
42                         System.out.println("Method sign: " + methodSign);
43
44                         if (methodSign.equals("intprintInt()")) {
45                                 //paramObj = rmiObj.getMethodParams(new Class<?>[] { }, 
46                                 //      new Class<?>[] { null }, new Class<?>[] { null });
47                                 retObj = cb.printInt();
48                         } else if (methodSign.equals("voidsetInt(int)")) {
49                                 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
50                                         new Class<?>[] { null }, new Class<?>[] { null });
51                                 cb.setInt((int) paramObj[0]);
52                         } else
53                                 throw new Error("Signature un-recognized!");
54                         System.out.println("Return object: " + retObj);
55
56                         if (retObj != null) {
57                                 rmiObj.sendReturnObj(retObj);
58                         }
59                         System.out.println("Servicing remote call for method: " + methodSign);
60                 }
61         }
62
63
64         public static void main(String[] args) throws Exception {
65
66                 int port = 5010;
67                 CallBack cb = new CallBack(23);
68                 CallBack_Skeleton cbSkel = new CallBack_Skeleton(cb, port);
69                 cbSkel.waitRequestInvokeMethod();
70         }
71 }