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