Adding support to returning struct/list of struct objects
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
index 4a3f3230e416a5590db1a6d43a96aa153ca36c68..89e6a6e05a391886b3ad0cf5c3d825f92a74787d 100644 (file)
@@ -35,7 +35,6 @@ public class TestClass_Stub implements TestClassInterface {
 
                address = _address;
                ports = _ports;
-               //rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
                rmiCall = new IoTRMICall(_port, _address, _rev);
                listCBObj = new ArrayList<CallBackInterface>();
                ___initCallBack();
@@ -57,8 +56,7 @@ public class TestClass_Stub implements TestClassInterface {
                Thread thread = new Thread() {
                        public void run() {
                        try{
-                                       //String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
-                                       //rmiObj = new IoTRMIObject(ports[0], methodSignatures);
+                                       System.out.println("Created server with port: " + ports[0]);
                                        rmiObj = new IoTRMIObject(ports[0]);
                                        Object retObj = null;
                                        while (true) {
@@ -84,7 +82,6 @@ public class TestClass_Stub implements TestClassInterface {
                // 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);
        }
 
@@ -213,7 +210,7 @@ public class TestClass_Stub implements TestClassInterface {
        }
 
 
-       public void handleStruct(StructJ[] data) {
+       public StructJ[] handleStruct(StructJ[] data) {
 
                int methodId = 11;
                Class<?> retType = void.class;
@@ -222,7 +219,7 @@ public class TestClass_Stub implements TestClassInterface {
                rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
 
                int methodId2 = 10;
-               Class<?> retType2 = void.class;
+               Class<?> retType2 = int.class;  // return type is integer if it is a struct!!!
                // Calculate the size of the array
                Class<?>[] paramCls2 = new Class<?>[3*data.length];
                Object[] paramObj2 = new Object[3*data.length];
@@ -237,7 +234,36 @@ public class TestClass_Stub implements TestClassInterface {
                        paramObj2[pos++] = data[i].year;
                }
                System.out.println(Arrays.toString(paramObj2));
-               rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+               Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+               // RETURN STRUCT
+               // Get the length of the struct first
+               int structsize1 = (int) retObj;
+               // Construct the struct
+               Class<?>[] retCls = new Class<?>[3*structsize1];
+               Class<?>[] retClsVal = new Class<?>[3*structsize1];
+               pos = 0;
+               for(int i=0; i < structsize1; i++) {
+                       retCls[pos] = String.class;
+                       retClsVal[pos++] = null;
+                       retCls[pos] = float.class;
+                       retClsVal[pos++] = null;
+                       retCls[pos] = int.class;
+                       retClsVal[pos++] = null;
+               }
+               Object[] retObj2 = rmiCall.getStructObjects(retCls, 
+                       retClsVal);
+               StructJ[] dataRet = new StructJ[structsize1];
+               for (int i=0; i < structsize1; i++) {
+                       dataRet[i] = new StructJ();
+               }
+               pos = 0;
+               for(int i=0; i < structsize1; i++) {
+                       dataRet[i].name = (String) retObj2[pos++];
+                       dataRet[i].value = (float) retObj2[pos++];
+                       dataRet[i].year = (int) retObj2[pos++];
+               }
+
+               return dataRet;
        }
 
 
@@ -299,7 +325,7 @@ public class TestClass_Stub implements TestClassInterface {
                CallBackInterface cb6 = new CallBack(12);
                CallBackInterface[] cbt = { cb4, cb5, cb6 };
                tcstub.registerCallback(cbt);
-               System.out.println("Return value from callback: " + tcstub.callBack());
+               System.out.println("Return value from callback: " + tcstub.callBack());*/
 
                StructJ[] data = new StructJ[2];
                for (int i=0; i<2; i++) {
@@ -317,7 +343,13 @@ public class TestClass_Stub implements TestClassInterface {
                        System.out.println("Value: " + str.value);
                        System.out.println("Year: " + str.year);
                }
-               tcstub.handleStruct(data);*/
+               StructJ[] strj = tcstub.handleStruct(data);
+               for (StructJ str : strj) {
+                       System.out.println("Name: " + str.name);
+                       System.out.println("Value: " + str.value);
+                       System.out.println("Year: " + str.year);
+               }
+
 
                EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
                EnumJ[] res = tcstub.handleEnum(en);