1 package iotrmi.Java.sample;
3 import java.io.IOException;
4 import java.util.Arrays;
8 import iotrmi.Java.IoTRMIObject;
9 import iotrmi.Java.IoTRMICall;
11 public class TestClass_Skeleton implements TestClassInterface {
16 private TestClassInterface tc;
18 private IoTRMIObject rmiObj;
21 private static int objIdCnt = 0; // Counter for callback object Ids
22 private IoTRMICall rmiCall;
23 private CallBackInterface cbstub;
26 private int object0Id = 0;
27 private static Integer[] object0Permission = { 0, 1, 2, 3, 4, 5 };
28 private List<Integer> set0Allowed;
34 public TestClass_Skeleton(TestClass _tc, int _port) throws
35 ClassNotFoundException, InstantiationException,
36 IllegalAccessException, IOException {
40 rmiObj = new IoTRMIObject(_port);
41 set0Allowed = Arrays.asList(object0Permission);
42 ___waitRequestInvokeMethod();
46 public void setA(int _int) {
52 public void ___setA() {
54 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
55 new Class<?>[] { null });
56 setA((int) paramObj[0]);
60 public void setB(float _float) {
66 public void ___setB() {
68 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
69 new Class<?>[] { null });
70 setB((float) paramObj[0]);
74 public void setC(String _string) {
80 public void ___setC() {
82 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
83 new Class<?>[] { null });
84 setC((String) paramObj[0]);
88 public String sumArray(String[] newA) {
90 return tc.sumArray(newA);
94 public void ___sumArray() throws IOException {
96 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
97 new Class<?>[] { null });
98 Object retObj = sumArray((String[]) paramObj[0]);
99 rmiObj.sendReturnObj(retObj);
103 public int setAndGetA(int newA) {
105 return tc.setAndGetA(newA);
109 public void ___setAndGetA() throws IOException {
111 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
112 new Class<?>[] { null });
113 Object retObj = setAndGetA((int) paramObj[0]);
114 rmiObj.sendReturnObj(retObj);
118 public int setACAndGetA(String newC, int newA) {
120 return tc.setACAndGetA(newC, newA);
124 public void ___setACAndGetA() throws IOException {
126 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
127 new Class<?>[] { null, null });
128 Object retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
129 rmiObj.sendReturnObj(retObj);
133 public void registerCallback(CallBackInterface _cb) {
135 tc.registerCallback(_cb);
139 public void ___registerCallback() throws IOException {
141 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
142 new Class<?>[] { null, null, null });
143 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
144 registerCallback((CallBackInterface) cbstub);
148 public void registerCallback(CallBackInterface[] _cb) {
150 tc.registerCallback(_cb);
154 // Use 4 underscores because this is a second instance of registerCallback
155 public void ____registerCallback() throws IOException {
157 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
158 new Class<?>[] { null });
159 int numStubs = (int) paramObj[0];
160 CallBackInterface[] stub = new CallBackInterface[numStubs];
161 for (int objId = 0; objId < numStubs; objId++) {
162 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
165 registerCallback(stub);
169 public void ___regCB() throws IOException {
171 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
172 new Class<?>[] { null, null, null });
173 //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
174 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
175 System.out.println("Creating a new IoTRMICall object");
179 public int callBack() {
181 return tc.callBack();
185 public void ___callBack() throws IOException {
187 Object retObj = callBack();
188 rmiObj.sendReturnObj(retObj);
192 public StructJ[] handleStruct(StructJ[] data) {
194 return tc.handleStruct(data);
198 public int ___structSize() {
200 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
201 new Class<?>[] { null });
202 return (int) paramObj[0];
206 public void ___handleStruct(int structsize1) throws IOException {
208 Class<?>[] paramCls = new Class<?>[3*structsize1];
209 Class<?>[] paramClsVal = new Class<?>[3*structsize1];
211 for(int i=0; i < structsize1; i++) {
212 paramCls[pos] = String.class;
213 paramClsVal[pos++] = null;
214 paramCls[pos] = float.class;
215 paramClsVal[pos++] = null;
216 paramCls[pos] = int.class;
217 paramClsVal[pos++] = null;
219 Object[] paramObj = rmiObj.getMethodParams(paramCls,
221 StructJ[] data = new StructJ[structsize1];
222 for (int i=0; i < structsize1; i++) {
223 data[i] = new StructJ();
226 for(int i=0; i < structsize1; i++) {
227 data[i].name = (String) paramObj[pos++];
228 data[i].value = (float) paramObj[pos++];
229 data[i].year = (int) paramObj[pos++];
231 // Just the following if there is no returned value
232 //tc.handleStruct(data);
233 StructJ[] retStruct = tc.handleStruct(data);
234 // Return length first
235 int structsize2 = retStruct.length;
236 Object retObj = structsize2;
237 rmiObj.sendReturnObj(retObj);
238 // Send the actual struct members
239 // Calculate the size of the array
240 Class<?>[] retCls = new Class<?>[3*structsize2];
241 Object[] retObj2 = new Object[3*structsize2];
242 // Handle with for loop
244 for(int i = 0; i < structsize2; i++) {
245 retCls[pos] = String.class;
246 retObj2[pos++] = data[i].name;
247 retCls[pos] = float.class;
248 retObj2[pos++] = data[i].value;
249 retCls[pos] = int.class;
250 retObj2[pos++] = data[i].year;
252 rmiObj.sendReturnObj(retCls, retObj2);
256 public EnumJ[] handleEnum(EnumJ[] en) {
258 return tc.handleEnum(en);
262 public void ___handleEnum() throws IOException {
264 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class },
265 new Class<?>[] { null });
267 int paramInt[] = (int[]) paramObj[0];
268 int enumSize1 = paramInt.length;
269 EnumJ[] enumJ = EnumJ.values();
270 EnumJ[] data = new EnumJ[enumSize1];
271 for (int i=0; i < enumSize1; i++) {
272 data[i] = enumJ[paramInt[i]];
274 // if void, just "handleEnum(data)"
275 // this is when we have return value EnumJ[]
276 EnumJ[] retEnum = handleEnum(data);
278 int enumSize2 = retEnum.length;
279 // Now send the array of integers
280 int[] retEnumInt = new int[enumSize2];
281 for (int i=0; i < enumSize2; i++) {
282 retEnumInt[i] = retEnum[i].ordinal();
284 Object retObj = retEnumInt;
285 rmiObj.sendReturnObj(retObj);
289 private void ___waitRequestInvokeMethod() throws IOException {
294 // Loop continuously waiting for incoming bytes
297 rmiObj.getMethodBytes();
298 int _objectId = rmiObj.getObjectId();
299 int methodId = rmiObj.getMethodId();
300 if (_objectId == object0Id) {
301 // Multiplex based on object Id
302 // Complain if the method is not allowed
303 if (!set0Allowed.contains(methodId))
304 throw new Error("TestClass_Skeleton: This object is not allowed to access method " + methodId);
305 // If we have more than 1 object Id...
306 //else if (_objectId == object1Id) {
309 throw new Error("TestClass_Skeleton: Unrecognizable object Id: " + _objectId);
313 case 0: ___setA(); break;
314 case 1: ___setB(); break;
315 case 2: ___setC(); break;
316 case 3: ___sumArray(); break;
317 case 4: ___setAndGetA(); break;
318 case 5: ___setACAndGetA(); break;
319 case 6: ___callBack(); break;
320 case 7: ___registerCallback(); break;
321 case 8: ____registerCallback(); break;
322 // Special option to register callback
323 case 9: ___regCB(); break;
324 // Struct handling (3 is the size of the struct)
325 case 10: ___handleStruct(structSize1); break;
326 case 11: structSize1 = ___structSize(); break;
327 case 12: ___handleEnum(); break;
329 throw new Error("Method Id " + methodId + " not recognized!");
336 public static void main(String[] args) throws Exception {
339 TestClass tc = new TestClass(3, 5f, "7911");
340 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);