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 final static String[] methodSignatures = {
25 "intsetACAndGetA(string,int)",
27 "voidregisterCallBack(CallBackInterface)",
28 "voidregisterCallBack(CallBackInterface[])"
31 private TestClassInterface tc;
33 private IoTRMIObject rmiObj;
34 private CallBackInterface cbstub;
40 public TestClass_Skeleton(TestClass _tc, int _port) throws
41 ClassNotFoundException, InstantiationException,
42 IllegalAccessException, IOException {
46 rmiObj = new IoTRMIObject(_port, methodSignatures);
47 waitRequestInvokeMethod();
51 public void waitRequestInvokeMethod() throws IOException {
53 // Loop continuously waiting for incoming bytes
56 rmiObj.getMethodBytes();
57 int _objectId = rmiObj.getObjectId();
58 if (_objectId == objectId) {
59 // Multiplex based on object Id
60 String methodSign = rmiObj.getSignature();
61 Object[] paramObj = null;
64 if (methodSign.equals("voidsetA(int)")) {
65 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
66 new Class<?>[] { null }, new Class<?>[] { null });
67 setA((int) paramObj[0]);
68 } else if (methodSign.equals("voidsetB(float)")) {
69 paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class },
70 new Class<?>[] { null }, new Class<?>[] { null });
71 setB((float) paramObj[0]);
72 } else if (methodSign.equals("voidsetC(string)")) {
73 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class },
74 new Class<?>[] { null }, new Class<?>[] { null });
75 setC((String) paramObj[0]);
76 } else if (methodSign.equals("sumArray(string[])")) {
77 paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class },
78 new Class<?>[] { null }, new Class<?>[] { null });
79 retObj = sumArray((String[]) paramObj[0]);
80 } else if (methodSign.equals("intsetAndGetA(int)")) {
81 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
82 new Class<?>[] { null }, new Class<?>[] { null });
83 retObj = setAndGetA((int) paramObj[0]);
84 } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
85 paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class },
86 new Class<?>[] { null, null }, new Class<?>[] { null, null });
87 retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
88 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
89 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
90 new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
91 CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
92 registerCallback((CallBackInterface) cbstub);
93 } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
94 paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class, int.class },
95 new Class<?>[] { null, null, null, null }, new Class<?>[] { null, null, null, null });
96 String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
97 IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
98 int numStubs = (int) paramObj[3];
99 CallBackInterface[] stub = new CallBackInterface[numStubs];
100 for (int objId = 0; objId < numStubs; objId++) {
101 stub[objId] = new CallBack_CBStub(rmiCall, objId, (String) paramObj[1]);
103 registerCallback(stub);
104 } else if (methodSign.equals("intcallBack()")) {
107 throw new Error("Signature not recognized!");
109 if (retObj != null) {
110 rmiObj.sendReturnObj(retObj);
112 System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
118 // Return method signatures
119 public static String[] getMethodSignatures() {
121 return methodSignatures;
125 public void setA(int _int) {
131 public void setB(float _float) {
137 public void setC(String _string) {
143 public String sumArray(String[] newA) {
145 return tc.sumArray(newA);
149 public int setAndGetA(int newA) {
151 return tc.setAndGetA(newA);
155 public int setACAndGetA(String newC, int newA) {
157 return tc.setACAndGetA(newC, newA);
161 public void registerCallback(CallBackInterface _cb) {
163 tc.registerCallback(_cb);
166 public void registerCallback(CallBackInterface[] _cb) {
168 tc.registerCallback(_cb);
171 public int callBack() {
173 return tc.callBack();
177 public static void main(String[] args) throws Exception {
180 TestClass tc = new TestClass(3, 5f, "7911");
181 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
183 /* String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
184 IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
185 Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
187 // Can replace for-loop with while-loop if necessary
188 for (int i = 1; i < 3; i++) {
189 TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
190 mapCBObject.put(i, tcSkel);
193 Object retObj = null;
195 byte[] method = rmiObj.getMethodBytes();
196 int objId = IoTRMIObject.getObjectId(method);
197 TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
198 if (tcSkel != null) {
199 rmiObj.setMethodBytes(method);
200 retObj = tcSkel.invokeMethod(rmiObj);
202 if (retObj != null) {
203 rmiObj.sendReturnObj(retObj);
208 //System.out.println("Creating 0 object");
209 //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
210 //System.out.println("Creating 1 object");
212 //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
213 //System.out.println("Creating 2 object");
215 /*for (int i = 1; i < 3; i++) {
216 final int objectId = i;
217 Thread thread = new Thread() {
220 TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
221 } catch (Exception ex){
222 ex.printStackTrace();
223 throw new Error("Error instantiating class CallBack_Skeleton!");