1 package iotrmi.Java.sample;
3 import java.io.IOException;
4 import java.util.Arrays;
7 import java.util.HashMap;
9 import iotrmi.Java.IoTRMIObject;
10 import iotrmi.Java.IoTRMICall;
12 public class TestClass_Skeleton implements TestClassInterface {
17 private int objectId = 0; // Default value is 0
18 private static int objIdCnt = 0; // Counter for callback object Ids
19 private TestClassInterface tc;
21 private IoTRMIObject rmiObj;
22 private IoTRMICall rmiCall;
23 private CallBackInterface cbstub;
29 public TestClass_Skeleton(TestClass _tc, int _port) throws
30 ClassNotFoundException, InstantiationException,
31 IllegalAccessException, IOException {
35 rmiObj = new IoTRMIObject(_port);
36 ___waitRequestInvokeMethod();
40 public void setA(int _int) {
46 public void ___setA() {
48 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
49 new Class<?>[] { null });
50 setA((int) paramObj[0]);
54 public void setB(float _float) {
60 public void ___setB() {
62 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
63 new Class<?>[] { null });
64 setB((float) paramObj[0]);
68 public void setC(String _string) {
74 public void ___setC() {
76 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
77 new Class<?>[] { null });
78 setC((String) paramObj[0]);
82 public String sumArray(String[] newA) {
84 return tc.sumArray(newA);
88 public void ___sumArray() throws IOException {
90 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
91 new Class<?>[] { null });
92 Object retObj = sumArray((String[]) paramObj[0]);
93 rmiObj.sendReturnObj(retObj);
97 public int setAndGetA(int newA) {
99 return tc.setAndGetA(newA);
103 public void ___setAndGetA() throws IOException {
105 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
106 new Class<?>[] { null });
107 Object retObj = setAndGetA((int) paramObj[0]);
108 rmiObj.sendReturnObj(retObj);
112 public int setACAndGetA(String newC, int newA) {
114 return tc.setACAndGetA(newC, newA);
118 public void ___setACAndGetA() throws IOException {
120 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
121 new Class<?>[] { null, null });
122 Object retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
123 rmiObj.sendReturnObj(retObj);
127 public void registerCallback(CallBackInterface _cb) {
129 tc.registerCallback(_cb);
133 public void ___registerCallback() throws IOException {
135 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
136 new Class<?>[] { null, null, null });
137 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
138 registerCallback((CallBackInterface) cbstub);
142 public void registerCallback(CallBackInterface[] _cb) {
144 tc.registerCallback(_cb);
148 // Use 4 underscores because this is a second instance of registerCallback
149 public void ____registerCallback() throws IOException {
151 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
152 new Class<?>[] { null });
153 int numStubs = (int) paramObj[0];
154 CallBackInterface[] stub = new CallBackInterface[numStubs];
155 for (int objId = 0; objId < numStubs; objId++) {
156 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
159 registerCallback(stub);
163 public void ___regCB() throws IOException {
165 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
166 new Class<?>[] { null, null, null });
167 //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
168 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
169 System.out.println("Creating a new IoTRMICall object");
173 public int callBack() {
175 return tc.callBack();
179 public void ___callBack() throws IOException {
181 Object retObj = callBack();
182 rmiObj.sendReturnObj(retObj);
186 public void handleStruct(StructJ[] data) {
188 tc.handleStruct(data);
192 public int ___structSize() {
194 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
195 new Class<?>[] { null });
196 return (int) paramObj[0];
200 public void ___handleStruct(int structsize1) {
202 Class<?>[] paramCls = new Class<?>[3*structsize1];
203 Class<?>[] paramClsVal = new Class<?>[3*structsize1];
205 for(int i=0; i < structsize1; i++) {
206 paramCls[pos] = String.class;
207 paramClsVal[pos++] = null;
208 paramCls[pos] = float.class;
209 paramClsVal[pos++] = null;
210 paramCls[pos] = int.class;
211 paramClsVal[pos++] = null;
213 Object[] paramObj = rmiObj.getMethodParams(paramCls,
215 StructJ[] data = new StructJ[structsize1];
216 for (int i=0; i < structsize1; i++) {
217 data[i] = new StructJ();
220 for(int i=0; i < structsize1; i++) {
221 data[i].name = (String) paramObj[pos++];
222 data[i].value = (float) paramObj[pos++];
223 data[i].year = (int) paramObj[pos++];
225 tc.handleStruct(data);
229 public EnumJ[] handleEnum(EnumJ[] en) {
231 return tc.handleEnum(en);
235 public void ___handleEnum() throws IOException {
237 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class },
238 new Class<?>[] { null });
240 int paramInt[] = (int[]) paramObj[0];
241 int enumSize1 = paramInt.length;
242 EnumJ[] enumJ = EnumJ.values();
243 EnumJ[] data = new EnumJ[enumSize1];
244 for (int i=0; i < enumSize1; i++) {
245 data[i] = enumJ[paramInt[i]];
247 // if void, just "handleEnum(data)"
248 // this is when we have return value EnumJ[]
249 EnumJ[] retEnum = handleEnum(data);
251 int enumSize2 = retEnum.length;
252 // Now send the array of integers
253 int[] retEnumInt = new int[enumSize2];
254 for (int i=0; i < enumSize2; i++) {
255 retEnumInt[i] = retEnum[i].ordinal();
257 Object retObj = retEnumInt;
258 rmiObj.sendReturnObj(retObj);
262 private void ___waitRequestInvokeMethod() throws IOException {
267 // Loop continuously waiting for incoming bytes
270 rmiObj.getMethodBytes();
271 int _objectId = rmiObj.getObjectId();
272 if (_objectId == objectId) {
273 // Multiplex based on object Id
274 int methodId = rmiObj.getMethodId();
277 case 0: ___setA(); break;
278 case 1: ___setB(); break;
279 case 2: ___setC(); break;
280 case 3: ___sumArray(); break;
281 case 4: ___setAndGetA(); break;
282 case 5: ___setACAndGetA(); break;
283 case 6: ___callBack(); break;
284 case 7: ___registerCallback(); break;
285 case 8: ____registerCallback(); break;
286 // Special option to register callback
287 case 9: ___regCB(); break;
288 // Struct handling (3 is the size of the struct)
289 case 10: ___handleStruct(structSize1); break;
290 case 11: structSize1 = ___structSize(); break;
291 case 12: ___handleEnum(); break;
293 throw new Error("Method Id not recognized!");
300 public static void main(String[] args) throws Exception {
303 TestClass tc = new TestClass(3, 5f, "7911");
304 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
306 /* String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
307 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
308 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
310 // Can replace for-loop with while-loop if necessary
311 for (int i = 1; i < 3; i++) {
312 TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
313 mapCBObject.put(i, tcSkel);
316 Object retObj = null;
318 byte[] method = rmiObj.getMethodBytes();
319 int objId = IoTRMIObject.getObjectId(method);
320 TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
321 if (tcSkel != null) {
322 rmiObj.setMethodBytes(method);
323 retObj = tcSkel.invokeMethod(rmiObj);
325 if (retObj != null) {
326 rmiObj.sendReturnObj(retObj);
331 //System.out.println("Creating 0 object");
332 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
333 //System.out.println("Creating 1 object");
335 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
336 //System.out.println("Creating 2 object");
338 /*for (int i = 1; i < 3; i++) {
339 final int objectId = i;
340 Thread thread = new Thread() {
343 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
344 } catch (Exception ex){
345 ex.printStackTrace();
346 throw new Error("Error instantiating class CallBack_Skeleton!");