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[])",
33 private TestClassInterface tc;
35 private IoTRMIObject rmiObj;
36 private IoTRMICall rmiCall;
37 private CallBackInterface cbstub;
43 public TestClass_Skeleton(TestClass _tc, int _port) throws
44 ClassNotFoundException, InstantiationException,
45 IllegalAccessException, IOException {
49 rmiObj = new IoTRMIObject(_port, methodSignatures);
50 waitRequestInvokeMethod();
54 public void waitRequestInvokeMethod() throws IOException {
56 // Loop continuously waiting for incoming bytes
59 rmiObj.getMethodBytes();
60 int _objectId = rmiObj.getObjectId();
61 if (_objectId == objectId) {
62 // Multiplex based on object Id
63 String methodSign = rmiObj.getSignature();
64 Object[] paramObj = null;
67 if (methodSign.equals("voidsetA(int)")) {
68 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
69 new Class<?>[] { null }, new Class<?>[] { null });
70 setA((int) paramObj[0]);
71 } else if (methodSign.equals("voidsetB(float)")) {
72 paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
73 new Class<?>[] { null }, new Class<?>[] { null });
74 setB((float) paramObj[0]);
75 } else if (methodSign.equals("voidsetC(string)")) {
76 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
77 new Class<?>[] { null }, new Class<?>[] { null });
78 setC((String) paramObj[0]);
79 } else if (methodSign.equals("sumArray(string[])")) {
80 paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
81 new Class<?>[] { null }, new Class<?>[] { null });
82 retObj = sumArray((String[]) paramObj[0]);
83 } else if (methodSign.equals("intsetAndGetA(int)")) {
84 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
85 new Class<?>[] { null }, new Class<?>[] { null });
86 retObj = setAndGetA((int) paramObj[0]);
87 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
88 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
89 new Class<?>[] { null, null }, new Class<?>[] { null, null });
90 retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
91 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
92 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
93 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
94 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
95 registerCallback((CallBackInterface) cbstub);
96 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
97 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
98 new Class<?>[] { null }, new Class<?>[] { null });
99 //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
100 //IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
101 int numStubs = (int) paramObj[0];
102 CallBackInterface[] stub = new CallBackInterface[numStubs];
103 for (int objId = 0; objId < numStubs; objId++) {
104 stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
107 registerCallback(stub);
108 } else if (methodSign.equals("intcallBack()")) {
110 // Special option to register callback
111 } else if (methodSign.equals("registercallback")) {
112 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
113 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
114 String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
115 rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
116 System.out.println("Creating a new IoTRMICall object");
118 throw new Error("Signature not recognized!");
120 if (retObj != null) {
121 rmiObj.sendReturnObj(retObj);
123 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
129 // Return method signatures
130 public static String[] getMethodSignatures() {
132 return methodSignatures;
136 public void setA(int _int) {
142 public void setB(float _float) {
148 public void setC(String _string) {
154 public String sumArray(String[] newA) {
156 return tc.sumArray(newA);
160 public int setAndGetA(int newA) {
162 return tc.setAndGetA(newA);
166 public int setACAndGetA(String newC, int newA) {
168 return tc.setACAndGetA(newC, newA);
172 public void registerCallback(CallBackInterface _cb) {
174 tc.registerCallback(_cb);
177 public void registerCallback(CallBackInterface[] _cb) {
179 tc.registerCallback(_cb);
182 public int callBack() {
184 return tc.callBack();
188 public static void main(String[] args) throws Exception {
191 TestClass tc = new TestClass(3, 5f, "7911");
192 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
194 /* String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
195 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
196 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
198 // Can replace for-loop with while-loop if necessary
199 for (int i = 1; i < 3; i++) {
200 TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
201 mapCBObject.put(i, tcSkel);
204 Object retObj = null;
206 byte[] method = rmiObj.getMethodBytes();
207 int objId = IoTRMIObject.getObjectId(method);
208 TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
209 if (tcSkel != null) {
210 rmiObj.setMethodBytes(method);
211 retObj = tcSkel.invokeMethod(rmiObj);
213 if (retObj != null) {
214 rmiObj.sendReturnObj(retObj);
219 //System.out.println("Creating 0 object");
220 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
221 //System.out.println("Creating 1 object");
223 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
224 //System.out.println("Creating 2 object");
226 /*for (int i = 1; i < 3; i++) {
227 final int objectId = i;
228 Thread thread = new Thread() {
231 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
232 } catch (Exception ex){
233 ex.printStackTrace();
234 throw new Error("Error instantiating class CallBack_Skeleton!");