1 package iotrmi.Java.sample;
3 import java.io.IOException;
4 import java.util.Arrays;
7 import java.util.HashMap;
10 import java.util.ArrayList;
11 import java.util.Collections;
13 import iotrmi.Java.IoTRMIObject;
14 import iotrmi.Java.IoTRMICall;
16 public class TestClass_Skeleton implements TestClassInterface {
21 private static int objIdCnt = 0; // Counter for callback object Ids
22 private TestClassInterface tc;
24 private IoTRMIObject rmiObj;
25 private IoTRMICall rmiCall;
26 private CallBackInterface cbstub;
29 private int object0Id = 0;
30 private static Integer[] object0Permission = { 0, 1, 2, 3, 4, 5 };
31 private List<Integer> set0Allowed;
37 public TestClass_Skeleton(TestClass _tc, int _port) throws
38 ClassNotFoundException, InstantiationException,
39 IllegalAccessException, IOException {
43 rmiObj = new IoTRMIObject(_port);
44 set0Allowed = Arrays.asList(object0Permission);
45 ___waitRequestInvokeMethod();
49 public void setA(int _int) {
55 public void ___setA() {
57 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
58 new Class<?>[] { null });
59 setA((int) paramObj[0]);
63 public void setB(float _float) {
69 public void ___setB() {
71 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
72 new Class<?>[] { null });
73 setB((float) paramObj[0]);
77 public void setC(String _string) {
83 public void ___setC() {
85 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
86 new Class<?>[] { null });
87 setC((String) paramObj[0]);
91 public String sumArray(String[] newA) {
93 return tc.sumArray(newA);
97 public void ___sumArray() throws IOException {
99 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
100 new Class<?>[] { null });
101 Object retObj = sumArray((String[]) paramObj[0]);
102 rmiObj.sendReturnObj(retObj);
106 public int setAndGetA(int newA) {
108 return tc.setAndGetA(newA);
112 public void ___setAndGetA() throws IOException {
114 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
115 new Class<?>[] { null });
116 Object retObj = setAndGetA((int) paramObj[0]);
117 rmiObj.sendReturnObj(retObj);
121 public int setACAndGetA(String newC, int newA) {
123 return tc.setACAndGetA(newC, newA);
127 public void ___setACAndGetA() throws IOException {
129 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
130 new Class<?>[] { null, null });
131 Object retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
132 rmiObj.sendReturnObj(retObj);
136 public void registerCallback(CallBackInterface _cb) {
138 tc.registerCallback(_cb);
142 public void ___registerCallback() throws IOException {
144 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
145 new Class<?>[] { null, null, null });
146 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
147 registerCallback((CallBackInterface) cbstub);
151 public void registerCallback(CallBackInterface[] _cb) {
153 tc.registerCallback(_cb);
157 // Use 4 underscores because this is a second instance of registerCallback
158 public void ____registerCallback() throws IOException {
160 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
161 new Class<?>[] { null });
162 int numStubs = (int) paramObj[0];
163 CallBackInterface[] stub = new CallBackInterface[numStubs];
164 for (int objId = 0; objId < numStubs; objId++) {
165 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
168 registerCallback(stub);
172 public void ___regCB() throws IOException {
174 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
175 new Class<?>[] { null, null, null });
176 //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
177 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
178 System.out.println("Creating a new IoTRMICall object");
182 public int callBack() {
184 return tc.callBack();
188 public void ___callBack() throws IOException {
190 Object retObj = callBack();
191 rmiObj.sendReturnObj(retObj);
195 public StructJ[] handleStruct(StructJ[] data) {
197 return tc.handleStruct(data);
201 public int ___structSize() {
203 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
204 new Class<?>[] { null });
205 return (int) paramObj[0];
209 public void ___handleStruct(int structsize1) throws IOException {
211 Class<?>[] paramCls = new Class<?>[3*structsize1];
212 Class<?>[] paramClsVal = new Class<?>[3*structsize1];
214 for(int i=0; i < structsize1; i++) {
215 paramCls[pos] = String.class;
216 paramClsVal[pos++] = null;
217 paramCls[pos] = float.class;
218 paramClsVal[pos++] = null;
219 paramCls[pos] = int.class;
220 paramClsVal[pos++] = null;
222 Object[] paramObj = rmiObj.getMethodParams(paramCls,
224 StructJ[] data = new StructJ[structsize1];
225 for (int i=0; i < structsize1; i++) {
226 data[i] = new StructJ();
229 for(int i=0; i < structsize1; i++) {
230 data[i].name = (String) paramObj[pos++];
231 data[i].value = (float) paramObj[pos++];
232 data[i].year = (int) paramObj[pos++];
234 // Just the following if there is no returned value
235 //tc.handleStruct(data);
236 StructJ[] retStruct = tc.handleStruct(data);
237 // Return length first
238 int structsize2 = retStruct.length;
239 Object retObj = structsize2;
240 rmiObj.sendReturnObj(retObj);
241 // Send the actual struct members
242 // Calculate the size of the array
243 Class<?>[] retCls = new Class<?>[3*structsize2];
244 Object[] retObj2 = new Object[3*structsize2];
245 // Handle with for loop
247 for(int i = 0; i < structsize2; i++) {
248 retCls[pos] = String.class;
249 retObj2[pos++] = data[i].name;
250 retCls[pos] = float.class;
251 retObj2[pos++] = data[i].value;
252 retCls[pos] = int.class;
253 retObj2[pos++] = data[i].year;
255 rmiObj.sendReturnObj(retCls, retObj2);
259 public EnumJ[] handleEnum(EnumJ[] en) {
261 return tc.handleEnum(en);
265 public void ___handleEnum() throws IOException {
267 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class },
268 new Class<?>[] { null });
270 int paramInt[] = (int[]) paramObj[0];
271 int enumSize1 = paramInt.length;
272 EnumJ[] enumJ = EnumJ.values();
273 EnumJ[] data = new EnumJ[enumSize1];
274 for (int i=0; i < enumSize1; i++) {
275 data[i] = enumJ[paramInt[i]];
277 // if void, just "handleEnum(data)"
278 // this is when we have return value EnumJ[]
279 EnumJ[] retEnum = handleEnum(data);
281 int enumSize2 = retEnum.length;
282 // Now send the array of integers
283 int[] retEnumInt = new int[enumSize2];
284 for (int i=0; i < enumSize2; i++) {
285 retEnumInt[i] = retEnum[i].ordinal();
287 Object retObj = retEnumInt;
288 rmiObj.sendReturnObj(retObj);
292 private void ___waitRequestInvokeMethod() throws IOException {
297 // Loop continuously waiting for incoming bytes
300 rmiObj.getMethodBytes();
301 int _objectId = rmiObj.getObjectId();
302 int methodId = rmiObj.getMethodId();
303 if (_objectId == object0Id) {
304 // Multiplex based on object Id
305 // Complain if the method is not allowed
306 if (!set0Allowed.contains(methodId))
307 throw new Error("TestClass_Skeleton: This object is not allowed to access method " + methodId);
308 // If we have more than 1 object Id...
309 //else if (_objectId == object1Id) {
312 throw new Error("TestClass_Skeleton: Unrecognizable object Id: " + _objectId);
316 case 0: ___setA(); break;
317 case 1: ___setB(); break;
318 case 2: ___setC(); break;
319 case 3: ___sumArray(); break;
320 case 4: ___setAndGetA(); break;
321 case 5: ___setACAndGetA(); break;
322 case 6: ___callBack(); break;
323 case 7: ___registerCallback(); break;
324 case 8: ____registerCallback(); break;
325 // Special option to register callback
326 case 9: ___regCB(); break;
327 // Struct handling (3 is the size of the struct)
328 case 10: ___handleStruct(structSize1); break;
329 case 11: structSize1 = ___structSize(); break;
330 case 12: ___handleEnum(); break;
332 throw new Error("Method Id not recognized!");
339 public static void main(String[] args) throws Exception {
342 TestClass tc = new TestClass(3, 5f, "7911");
343 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);