798e398e3dc5894b85e35b7995be463b0a7f9ddf
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_CBStub.java
1 package iotrmi.Java.sample;
2
3 import java.io.IOException;
4 import iotrmi.Java.IoTRMICall;
5 import iotruntime.master.CommunicationHandler;
6
7 import java.util.Arrays;
8 import java.util.List;
9 import java.util.ArrayList;
10
11 import iotrmi.Java.IoTRMIObject;
12
13 public class TestClass_CBStub implements TestClassInterface {
14
15         /**
16          * Class Properties
17          */
18         private IoTRMICall rmiCall;
19         private String address;
20         private int[] ports;
21         private List<CallBackInterface> listCBObj;
22
23         /**
24          * Class Constants
25          */
26         private final static int NUM_CB_OBJ = 1;
27         private int objectId = 0;       // Default value is 0
28         private final static String[] methodSignatures = {
29
30                 "voidsetA(int)",
31                 "voidsetB(float)",
32                 "voidsetC(string)",
33                 "sumArray(string[])",
34                 "intsetAndGetA(int)",
35                 "intsetACAndGetA(string,int)",
36                 "intcallBack()",
37                 "voidregisterCallBack(CallBackInterface)",
38                 "voidregisterCallBack(CallBackInterface[])"
39         };
40
41         /**
42          * Constructors
43          */
44         // Assign rmiCall from outside
45         public TestClass_CBStub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
46
47                 address = _address;
48                 ports = _ports;
49                 objectId = _objectId;
50                 rmiCall = _rmiCall;
51         }
52
53
54         /**
55          * Instantiation of callback objects
56          */
57         public static int numCallbackObjects() {
58
59                 return NUM_CB_OBJ;      // Generated by the IoTCompiler
60         }
61
62
63         // Return method signatures
64         public static String[] getMethodSignatures() {
65
66                 return methodSignatures;
67         }
68
69
70         public void registerCallback(CallBackInterface _cb) {
71
72                 Thread thread = new Thread() {
73                         public void run() {
74                     try{
75                                         CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
76                                         cbskel.waitRequestInvokeMethod();
77                                 } catch (Exception ex){
78                                         ex.printStackTrace();
79                                         throw new Error("Error instantiating class CallBack_Skeleton!");
80                     }
81                 }
82             };
83                 thread.start();
84
85                 String sign = "voidregisterCallBack(CallBackInterface)";
86                 Class<?> retType = void.class;
87                 // port, address, and rev
88                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
89                 Object[] paramObj = new Object[] { ports[0], address, 0 };
90                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
91         }
92
93
94         // Multiple callback handling
95         public void registerCallback(CallBackInterface[] _cb) {
96
97                 try {
98                         for (int objId = 0; objId < _cb.length; objId++) {
99                                 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(_cb[objId], objId);
100                                 listCBObj.add(skel);
101                         }
102                 } catch (       ClassNotFoundException | 
103                                         InstantiationException |
104                                         IllegalAccessException |
105                                         IOException ex) {
106                         ex.printStackTrace();
107                         throw new Error("Class not found / instantiation / illegal access / IO error!");
108                 }
109
110                 Thread thread = new Thread() {
111                         public void run() {
112                     try{
113                                         String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
114                                         IoTRMIObject rmiObj = new IoTRMIObject(ports[0], methodSignatures);
115                                         Object retObj = null;
116                                         while (true) {
117                                                 byte[] method = rmiObj.getMethodBytes();
118                                                 int objId = IoTRMIObject.getObjectId(method);
119                                                 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
120                                                 if (skel != null) {
121                                                         rmiObj.setMethodBytes(method);
122                                                         retObj = skel.invokeMethod(rmiObj);
123                                                 }
124                                                 if (retObj != null) {
125                                                         rmiObj.sendReturnObj(retObj);
126                                                 }
127                                         }
128                                 } catch (Exception ex){
129                                         ex.printStackTrace();
130                                         throw new Error("Error instantiating class CallBack_Skeleton!");
131                     }
132                 }
133             };
134                 thread.start();
135
136                 String sign = "voidregisterCallBack(CallBackInterface[])";
137                 Class<?> retType = void.class;
138                 // port, address, rev, and number of objects
139                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class, int.class };
140                 Object[] paramObj = new Object[] { ports[0], address, 0, _cb.length };
141                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
142         }
143
144
145         public void setA(int _int) {
146
147                 String sign = "voidsetA(int)";
148                 Class<?> retType = void.class;
149                 Class<?>[] paramCls = new Class<?>[] { int.class };
150                 Object[] paramObj = new Object[] { _int };
151                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
152         }
153
154
155         public void setB(float _float) {
156
157                 String sign = "voidsetB(float)";
158                 Class<?> retType = void.class;
159                 Class<?>[] paramCls = new Class<?>[] { float.class };
160                 Object[] paramObj = new Object[] { _float };
161                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
162         }
163
164
165         public void setC(String _string) {
166
167                 String sign = "voidsetC(string)";
168                 Class<?> retType = void.class;
169                 Class<?>[] paramCls = new Class<?>[] { String.class };
170                 Object[] paramObj = new Object[] { _string };
171                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
172         }
173
174
175         // Getters
176         public String sumArray(String[] newA) {
177
178                 String sign = "sumArray(string[])";
179                 Class<?> retType = String.class;
180                 Class<?>[] paramCls = new Class<?>[] { String[].class };
181                 Object[] paramObj = new Object[] { newA };
182                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
183                 return (String)retObj;
184         }
185
186
187         public int setAndGetA(int newA) {
188                 String sign = "intsetAndGetA(int)";
189                 Class<?> retType = int.class;
190                 Class<?>[] paramCls = new Class<?>[] { int.class };
191                 Object[] paramObj = new Object[] { newA };
192                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
193                 return (int)retObj;
194         }
195
196
197         public int setACAndGetA(String newC, int newA) {
198
199                 String sign = "intsetACAndGetA(string,int)";
200                 Class<?> retType = int.class;
201                 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
202                 Object[] paramObj = new Object[] { newC, newA };
203                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
204                 return (int)retObj;
205         }
206
207
208         public int callBack() {
209
210                 String sign = "intcallBack()";
211                 Class<?> retType = int.class;
212                 Class<?>[] paramCls = new Class<?>[] { };
213                 Object[] paramObj = new Object[] { };
214                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
215                 return (int)retObj;
216
217         }
218
219
220         public static void main(String[] args) throws Exception {
221
222         }
223 }
224
225