Adding enum support for method argument; need to emulate the same functionality for...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
index 39411a75f64f8bc4927f7e11291923d203916231..a907ff7a5d37e9f73d81af61d7549aa1cb253186 100644 (file)
@@ -5,6 +5,10 @@ import iotrmi.Java.IoTRMICall;
 import iotruntime.master.CommunicationHandler;
 
 import java.util.Arrays;
+import java.util.List;
+import java.util.ArrayList;
+
+import iotrmi.Java.IoTRMIObject;
 
 public class TestClass_Stub implements TestClassInterface {
 
@@ -14,20 +18,27 @@ public class TestClass_Stub implements TestClassInterface {
        private IoTRMICall rmiCall;
        private String address;
        private int[] ports;
+       private List<CallBackInterface> listCBObj;
+       private IoTRMIObject rmiObj;
 
        /**
         * Class Constants
         */
        private final static int NUM_CB_OBJ = 1;
+       private int objectId = 0;       // Default value is 0
+       private static int objIdCnt = 0; // Counter for callback object Ids
 
        /**
         * Constructors
         */
-       public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws IOException {
+       public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
 
                address = _address;
                ports = _ports;
+               //rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
                rmiCall = new IoTRMICall(_port, _address, _rev);
+               listCBObj = new ArrayList<CallBackInterface>();
+               ___initCallBack();
        }
 
 
@@ -40,16 +51,51 @@ public class TestClass_Stub implements TestClassInterface {
        }
 
 
-       public void registerCallback(CallBackInterface _cb) {
-
-               //int port = 5011;      // Send this info to the other end to start the stub
-               //String address = "localhost";
+       // Initialize callback
+       public void ___initCallBack() {
 
                Thread thread = new Thread() {
                        public void run() {
+                       try{
+                                       //String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
+                                       //rmiObj = new IoTRMIObject(ports[0], methodSignatures);
+                                       rmiObj = new IoTRMIObject(ports[0]);
+                                       Object retObj = null;
+                                       while (true) {
+                                               byte[] method = rmiObj.getMethodBytes();
+                                               int objId = IoTRMIObject.getObjectId(method);
+                                               CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
+                                               if (skel != null) {
+                                                       //rmiObj.setMethodBytes(method);
+                                                       skel.invokeMethod(rmiObj);
+                                               }
+                                       }
+                               } catch (Exception ex){
+                                       ex.printStackTrace();
+                                       throw new Error("Error instantiating class CallBack_Skeleton!");
+                       }
+                   }
+               };
+               thread.start();
+
+               //String sign = "registercallback";     // can be any string
+               int methodId = 9;
+               Class<?> retType = void.class;
+               // port, address, rev
+               Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
+               Object[] paramObj = new Object[] { ports[0], address, 0 };
+               //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+       }
+
+
+       // Single callback handling
+       public void registerCallback(CallBackInterface _cb) {
+
+               /*Thread thread = new Thread() {
+                       public void run() {
                    try{
                                        CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
-                                       cbskel.waitRequestInvokeMethod();
                                } catch (Exception ex){
                                        ex.printStackTrace();
                                        throw new Error("Error instantiating class CallBack_Skeleton!");
@@ -63,105 +109,214 @@ public class TestClass_Stub implements TestClassInterface {
                // port, address, and rev
                Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
                Object[] paramObj = new Object[] { ports[0], address, 0 };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
+       }
+
+
+       public void registerCallback(CallBackInterface[] _cb) {
+
+               try {
+                       //for (int objId = 0; objId < _cb.length; objId++) {
+                       for (CallBackInterface cb : _cb) {
+                               CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
+                               listCBObj.add(skel);
+                       }
+               } catch (Exception ex){
+                       ex.printStackTrace();
+                       throw new Error("Class not found / instantiation / illegal access / IO error!");
+               }
+
+               //String sign = "voidregisterCallBack(CallBackInterface[])";
+               int methodId = 8;
+               Class<?> retType = void.class;
+               // port, address, rev, and number of objects
+               Class<?>[] paramCls = new Class<?>[] { int.class };
+               Object[] paramObj = new Object[] { _cb.length };
+               //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
        }
 
 
        public void setA(int _int) {
 
-               String sign = "voidsetA(int)";
+               //String sign = "voidsetA(int)";
+               int methodId = 0;
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] { int.class };
                Object[] paramObj = new Object[] { _int };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
        }
 
 
        public void setB(float _float) {
 
-               String sign = "voidsetB(float)";
+               int methodId = 1;
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] { float.class };
                Object[] paramObj = new Object[] { _float };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
        }
 
 
        public void setC(String _string) {
 
-               String sign = "voidsetC(string)";
+               int methodId = 2;
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] { String.class };
                Object[] paramObj = new Object[] { _string };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
        }
 
 
        // Getters
        public String sumArray(String[] newA) {
 
-               String sign = "sumArray(string[])";
+               int methodId = 3;
                Class<?> retType = String.class;
                Class<?>[] paramCls = new Class<?>[] { String[].class };
                Object[] paramObj = new Object[] { newA };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
                return (String)retObj;
        }
 
 
        public int setAndGetA(int newA) {
-               String sign = "intsetAndGetA(int)";
+               int methodId = 4;
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] { int.class };
                Object[] paramObj = new Object[] { newA };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
                return (int)retObj;
        }
 
 
        public int setACAndGetA(String newC, int newA) {
 
-               String sign = "intsetACAndGetA(string,int)";
+               int methodId = 5;
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
                Object[] paramObj = new Object[] { newC, newA };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
                return (int)retObj;
        }
 
 
        public int callBack() {
 
-               String sign = "intcallBack()";
+               int methodId = 6;
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] { };
                Object[] paramObj = new Object[] { };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
                return (int)retObj;
 
        }
 
 
+       public void handleStruct(StructJ[] data) {
+
+               int methodId = 11;
+               Class<?> retType = void.class;
+               Class<?>[] paramCls = new Class<?>[] { int.class };
+               Object[] paramObj = new Object[] { data.length };
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+
+               int methodId2 = 10;
+               Class<?> retType2 = void.class;
+               // Calculate the size of the array
+               Class<?>[] paramCls2 = new Class<?>[3*data.length];
+               Object[] paramObj2 = new Object[3*data.length];
+               // Handle with for loop
+               int pos = 0;
+               for(int i = 0; i < data.length; i++) {
+                       paramCls2[pos] = String.class;
+                       paramObj2[pos++] = data[i].name;
+                       paramCls2[pos] = float.class;
+                       paramObj2[pos++] = data[i].value;
+                       paramCls2[pos] = int.class;
+                       paramObj2[pos++] = data[i].year;
+               }
+               System.out.println(Arrays.toString(paramObj2));
+               rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+       }
+
+
+       public void handleEnum(EnumJ[] en) {
+
+               // Send length info first
+               int methodId = 13;
+               Class<?> retType = void.class;
+               Class<?>[] paramCls = new Class<?>[] { int.class };
+               Object[] paramObj = new Object[] { en.length };
+               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+
+               int methodId2 = 12;
+               Class<?> retType2 = void.class;
+               // Calculate the size of the array
+               Class<?>[] paramCls2 = new Class<?>[en.length];
+               Object[] paramObj2 = new Object[en.length];
+               // Handle with for loop
+               for(int i = 0; i < en.length; i++) {
+                       paramCls2[i] = int.class;
+                       paramObj2[i] = en[i].ordinal();
+               }
+               System.out.println(Arrays.toString(paramObj2));
+               rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);          
+       }
+
+
        public static void main(String[] args) throws Exception {
 
                CommunicationHandler comHan = new CommunicationHandler(true);
                int numOfPorts = TestClass_Stub.numCallbackObjects();
                int[] ports = comHan.getCallbackPorts(numOfPorts);
 
-               System.out.println("Allocated ports: " + Arrays.toString(ports));
-
                int port = 5010;
                String address = "localhost";
                int rev = 0;
 
+               System.out.println("Allocated ports: " + Arrays.toString(ports));
+
                TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
                System.out.println("Return value: " + tcstub.setAndGetA(123));
                System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
                System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
 
-               CallBack cb = new CallBack(23);
+               CallBackInterface cb1 = new CallBack(23);
+               CallBackInterface cb2 = new CallBack(33);
+               CallBackInterface cb3 = new CallBack(43);
+               CallBackInterface[] cb = { cb1, cb2, cb3 };
                tcstub.registerCallback(cb);
                System.out.println("Return value from callback: " + tcstub.callBack());
-               //System.out.println("Return value: " + tcstub.setAndGetA(1234));
+               CallBackInterface cb4 = new CallBack(10);
+               CallBackInterface cb5 = new CallBack(11);
+               CallBackInterface cb6 = new CallBack(12);
+               CallBackInterface[] cbt = { cb4, cb5, cb6 };
+               tcstub.registerCallback(cbt);
+               System.out.println("Return value from callback: " + tcstub.callBack());
+
+               StructJ[] data = new StructJ[2];
+               for (int i=0; i<2; i++) {
+                       data[i] = new StructJ();
+               }
+               data[0].name = "Rahmadi";
+               data[0].value = 0.123f;
+               data[0].year = 2016;
+               //data[1].name = "Trimananda";
+               //data[1].value = 0.223f;
+               //data[1].year = 2017;
+
+               for (StructJ str : data) {
+                       System.out.println("Name: " + str.name);
+                       System.out.println("Value: " + str.value);
+                       System.out.println("Year: " + str.year);
+               }
+               tcstub.handleStruct(data);
+
+               EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
+               tcstub.handleEnum(en);
+
        }
 }
+
+