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 final static String[] methodSignatures = {
26 "intsetACAndGetA(string,int)",
28 "voidregisterCallBack(CallBackInterface)",
29 "voidregisterCallBack(CallBackInterface[])",
31 "handleStruct(StructJ[])",
35 private TestClassInterface tc;
37 private IoTRMIObject rmiObj;
38 private IoTRMICall rmiCall;
39 private CallBackInterface cbstub;
45 public TestClass_Skeleton(TestClass _tc, int _port) throws
46 ClassNotFoundException, InstantiationException,
47 IllegalAccessException, IOException {
51 rmiObj = new IoTRMIObject(_port, methodSignatures);
52 waitRequestInvokeMethod();
56 public void waitRequestInvokeMethod() throws IOException {
60 // Loop continuously waiting for incoming bytes
63 rmiObj.getMethodBytes();
64 int _objectId = rmiObj.getObjectId();
65 if (_objectId == objectId) {
66 // Multiplex based on object Id
67 String methodSign = rmiObj.getSignature();
68 Object[] paramObj = null;
71 if (methodSign.equals("voidsetA(int)")) {
72 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
73 new Class<?>[] { null }, new Class<?>[] { null });
74 setA((int) paramObj[0]);
75 } else if (methodSign.equals("voidsetB(float)")) {
76 paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
77 new Class<?>[] { null }, new Class<?>[] { null });
78 setB((float) paramObj[0]);
79 } else if (methodSign.equals("voidsetC(string)")) {
80 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
81 new Class<?>[] { null }, new Class<?>[] { null });
82 setC((String) paramObj[0]);
83 } else if (methodSign.equals("sumArray(string[])")) {
84 paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
85 new Class<?>[] { null }, new Class<?>[] { null });
86 retObj = sumArray((String[]) paramObj[0]);
87 } else if (methodSign.equals("intsetAndGetA(int)")) {
88 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
89 new Class<?>[] { null }, new Class<?>[] { null });
90 retObj = setAndGetA((int) paramObj[0]);
91 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
92 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
93 new Class<?>[] { null, null }, new Class<?>[] { null, null });
94 retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
95 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
96 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
97 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
98 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
99 registerCallback((CallBackInterface) cbstub);
100 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
101 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
102 new Class<?>[] { null }, new Class<?>[] { null });
103 //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
104 //IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
105 int numStubs = (int) paramObj[0];
106 CallBackInterface[] stub = new CallBackInterface[numStubs];
107 for (int objId = 0; objId < numStubs; objId++) {
108 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
111 registerCallback(stub);
112 } else if (methodSign.equals("intcallBack()")) {
114 // Special option to register callback
115 } else if (methodSign.equals("registercallback")) {
116 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
117 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
118 String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
119 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
120 System.out.println("Creating a new IoTRMICall object");
121 // Struct handling (3 is the size of the struct)
122 } else if (methodSign.equals("structsize")) {
123 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
124 new Class<?>[] { null }, new Class<?>[] { null });
125 structsize1 = (int) paramObj[0];
126 } else if (methodSign.equals("handleStruct(StructJ[])")) {
127 Class<?>[] paramCls = new Class<?>[3*structsize1];
128 Class<?>[] paramClsTyp1 = new Class<?>[3*structsize1];
129 Class<?>[] paramClsTyp2 = new Class<?>[3*structsize1];
131 for(int i=0; i < structsize1; i++) {
132 paramCls[pos] = String.class;
133 paramClsTyp1[pos] = null;
134 paramClsTyp2[pos++] = null;
135 paramCls[pos] = float.class;
136 paramClsTyp1[pos] = null;
137 paramClsTyp2[pos++] = null;
138 paramCls[pos] = int.class;
139 paramClsTyp1[pos] = null;
140 paramClsTyp2[pos++] = null;
142 paramObj = rmiObj.getMethodParams(paramCls,
143 paramClsTyp1, paramClsTyp2);
144 StructJ[] data = new StructJ[structsize1];
145 for (int i=0; i < structsize1; i++) {
146 data[i] = new StructJ();
149 for(int i=0; i < structsize1; i++) {
150 data[i].name = (String) paramObj[pos++];
151 data[i].value = (float) paramObj[pos++];
152 data[i].year = (int) paramObj[pos++];
154 tc.handleStruct(data);
156 throw new Error("Signature not recognized!");
158 if (retObj != null) {
159 rmiObj.sendReturnObj(retObj);
161 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
167 // Return method signatures
168 public static String[] getMethodSignatures() {
170 return methodSignatures;
174 public void setA(int _int) {
180 public void setB(float _float) {
186 public void setC(String _string) {
192 public String sumArray(String[] newA) {
194 return tc.sumArray(newA);
198 public int setAndGetA(int newA) {
200 return tc.setAndGetA(newA);
204 public int setACAndGetA(String newC, int newA) {
206 return tc.setACAndGetA(newC, newA);
210 public void registerCallback(CallBackInterface _cb) {
212 tc.registerCallback(_cb);
215 public void registerCallback(CallBackInterface[] _cb) {
217 tc.registerCallback(_cb);
220 public int callBack() {
222 return tc.callBack();
225 public void handleStruct(StructJ[] data) {
227 tc.handleStruct(data);
231 public static void main(String[] args) throws Exception {
234 TestClass tc = new TestClass(3, 5f, "7911");
235 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
237 /* String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
238 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
239 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
241 // Can replace for-loop with while-loop if necessary
242 for (int i = 1; i < 3; i++) {
243 TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
244 mapCBObject.put(i, tcSkel);
247 Object retObj = null;
249 byte[] method = rmiObj.getMethodBytes();
250 int objId = IoTRMIObject.getObjectId(method);
251 TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
252 if (tcSkel != null) {
253 rmiObj.setMethodBytes(method);
254 retObj = tcSkel.invokeMethod(rmiObj);
256 if (retObj != null) {
257 rmiObj.sendReturnObj(retObj);
262 //System.out.println("Creating 0 object");
263 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
264 //System.out.println("Creating 1 object");
266 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
267 //System.out.println("Creating 2 object");
269 /*for (int i = 1; i < 3; i++) {
270 final int objectId = i;
271 Thread thread = new Thread() {
274 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
275 } catch (Exception ex){
276 ex.printStackTrace();
277 throw new Error("Error instantiating class CallBack_Skeleton!");