ed297c8c5b5d3e83a7861f082e72dda8650a5b58
[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 CallBackInterface cb;
12         private IoTRMIObject rmiObj;
13
14
15         /**
16          * Constructors
17          */
18         public CallBack_Skeleton(CallBackInterface _cb, int _port) throws
19                 ClassNotFoundException, InstantiationException,
20                         IllegalAccessException, IOException {
21
22                 cb = _cb;
23                 System.out.println("Creating CallBack_Skeleton and waiting!");
24                 rmiObj = new IoTRMIObject(_port);
25                 ___waitRequestInvokeMethod();
26         }
27
28
29         public int printInt() {
30                 return cb.printInt();
31         }
32         
33         
34         public void ___printInt() throws IOException {
35                 Object retObj = printInt();
36                 rmiObj.sendReturnObj(retObj);
37         }
38
39
40         public void setInt(int _i) {
41                 cb.setInt(_i);
42         }
43         
44         
45         public void ___setInt() {
46                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
47                         new Class<?>[] { null }, new Class<?>[] { null });
48                 setInt((int) paramObj[0]);
49         }
50         
51
52         private void ___waitRequestInvokeMethod() throws IOException {
53
54                 // Loop continuously waiting for incoming bytes
55                 while (true) {
56
57                         rmiObj.getMethodBytes();
58                         int objId = rmiObj.getObjectId();
59                         if (objId == objectId) {
60                         // Multiplex based on object Id
61                                 rmiObj.getMethodBytes();
62                                 int methodId = rmiObj.getMethodId();
63
64                                 switch (methodId) {
65                                         case 0: ___printInt(); break;
66                                         case 1: ___setInt(); break;
67                                         default:
68                                                 throw new Error("Signature not recognized!");
69                                 }
70                         }
71                 }
72         }
73
74
75         public static void main(String[] args) throws Exception {
76
77                 int port = 5010;
78                 CallBack cb = new CallBack(23);
79                 CallBack_Skeleton cbSkel = new CallBack_Skeleton(cb, port);
80                 //cbSkel.waitRequestInvokeMethod();
81         }
82 }