Fixing bug for return value from callback in C++ (sendReturnObj is called twice)...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Skeleton.java
index acab4ac979bace199434218c1fa39c23e51c05ee..a1647400bda010ae0749d4b5aff92fbf2472aded 100644 (file)
@@ -27,7 +27,9 @@ public class TestClass_Skeleton implements TestClassInterface {
                "intcallBack()",
                "voidregisterCallBack(CallBackInterface)",
                "voidregisterCallBack(CallBackInterface[])",
-               "registercallback"
+               "registercallback",
+               "handleStruct(StructJ[])",
+               "structsize"
        };
 
        private TestClassInterface tc;
@@ -47,91 +49,9 @@ public class TestClass_Skeleton implements TestClassInterface {
                tc = _tc;
                port = _port;
                rmiObj = new IoTRMIObject(_port, methodSignatures);
-               waitRequestInvokeMethod();
+               ___waitRequestInvokeMethod();
        }
 
-
-       public void waitRequestInvokeMethod() throws IOException {
-
-               // Loop continuously waiting for incoming bytes
-               while (true) {
-
-                       rmiObj.getMethodBytes();
-                       int _objectId = rmiObj.getObjectId();
-                       if (_objectId == objectId) {
-                       // Multiplex based on object Id
-                               String methodSign = rmiObj.getSignature();
-                               Object[] paramObj = null;
-                               Object retObj = null;
-
-                               if (methodSign.equals("voidsetA(int)")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-                                               new Class<?>[] { null }, new Class<?>[] { null });
-                                       setA((int) paramObj[0]);
-                               } else if (methodSign.equals("voidsetB(float)")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
-                                               new Class<?>[] { null }, new Class<?>[] { null });
-                                       setB((float) paramObj[0]);
-                               } else if (methodSign.equals("voidsetC(string)")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class }, 
-                                               new Class<?>[] { null }, new Class<?>[] { null });
-                                       setC((String) paramObj[0]);
-                               } else if (methodSign.equals("sumArray(string[])")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
-                                               new Class<?>[] { null }, new Class<?>[] { null });
-                                       retObj = sumArray((String[]) paramObj[0]);
-                               } else if (methodSign.equals("intsetAndGetA(int)")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-                                               new Class<?>[] { null }, new Class<?>[] { null });
-                                       retObj = setAndGetA((int) paramObj[0]);
-                               } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
-                                               new Class<?>[] { null, null }, new Class<?>[] { null, null });
-                                       retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]);
-                               } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
-                                               new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
-                                       CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
-                                       registerCallback((CallBackInterface) cbstub);
-                               } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-                                               new Class<?>[] { null }, new Class<?>[] { null });
-                                       //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
-                                       //IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
-                                       int numStubs = (int) paramObj[0];
-                                       CallBackInterface[] stub = new CallBackInterface[numStubs];
-                                       for (int objId = 0; objId < numStubs; objId++) {
-                                               stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
-                                               objIdCnt++;
-                                       }
-                                       registerCallback(stub);
-                               } else if (methodSign.equals("intcallBack()")) {
-                                       retObj = callBack();
-                               // Special option to register callback
-                               } else if (methodSign.equals("registercallback")) {
-                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
-                                               new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
-                                       String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
-                                       rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
-                                       System.out.println("Creating a new IoTRMICall object");
-                               } else
-                                       throw new Error("Signature not recognized!");
-
-                               if (retObj != null) {
-                                       rmiObj.sendReturnObj(retObj);
-                               }
-                               System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
-                       }
-               }
-       }
-
-
-       // Return method signatures
-       public static String[] getMethodSignatures() {
-
-               return methodSignatures;
-       }
-       
        
        public void setA(int _int) {
                
@@ -139,52 +59,247 @@ public class TestClass_Skeleton implements TestClassInterface {
        }
        
        
+       public void ___setA() {
+
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               setA((int) paramObj[0]);
+       }
+       
+       
        public void setB(float _float) {
                
                tc.setB(_float);
        }
        
        
+       public void ___setB() {
+
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               setB((float) paramObj[0]);
+       }
+       
+       
        public void setC(String _string) {
                
                tc.setC(_string);
        }
        
        
+       public void ___setC() {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               setC((String) paramObj[0]);
+       }
+       
+       
        public String sumArray(String[] newA) {
                
                return tc.sumArray(newA);
        }
        
        
+       public String ___sumArray() {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String[].class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               return sumArray((String[]) paramObj[0]);
+       }
+       
+       
        public int setAndGetA(int newA) {
                
                return tc.setAndGetA(newA);
        }
        
        
+       public int ___setAndGetA() {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               return setAndGetA((int) paramObj[0]);
+       }
+       
+       
        public int setACAndGetA(String newC, int newA) {
                
                return tc.setACAndGetA(newC, newA);
        }
        
        
+       public int ___setACAndGetA() {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { String.class, int.class }, 
+                       new Class<?>[] { null, null }, new Class<?>[] { null, null });
+               return setACAndGetA((String) paramObj[0], (int) paramObj[1]);
+       }
+       
+       
        public void registerCallback(CallBackInterface _cb) {
                
                tc.registerCallback(_cb);
        }
+       
+       
+       public void ___registerCallback() throws IOException {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
+                       new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
+               CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
+               registerCallback((CallBackInterface) cbstub);
+       }
+       
 
        public void registerCallback(CallBackInterface[] _cb) {
                
                tc.registerCallback(_cb);
        }
        
+       
+       // Use 4 underscores because this is a second instance of registerCallback
+       public void ____registerCallback() throws IOException {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               int numStubs = (int) paramObj[0];
+               CallBackInterface[] stub = new CallBackInterface[numStubs];
+               for (int objId = 0; objId < numStubs; objId++) {
+                       stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
+                       objIdCnt++;
+               }
+               registerCallback(stub);
+       }
+       
+       
+       public void ___regCB() throws IOException {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class }, 
+                       new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
+               String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
+               rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
+               System.out.println("Creating a new IoTRMICall object");
+       }
+       
+       
        public int callBack() {
                
                return tc.callBack();
        }
+       
+       
+       public int ___callBack() {
+               
+               return callBack();
+       }
+       
 
+       public void handleStruct(StructJ[] data) {
 
+               tc.handleStruct(data);
+       }
+       
+       
+       public int ___structSize() {
+               
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
+                       new Class<?>[] { null }, new Class<?>[] { null });
+               return (int) paramObj[0];               
+       }
+       
+       
+       public void ___handleStruct(int structsize1) {
+
+               Class<?>[] paramCls = new Class<?>[3*structsize1];
+               Class<?>[] paramClsTyp1 = new Class<?>[3*structsize1];
+               Class<?>[] paramClsTyp2 = new Class<?>[3*structsize1];
+               int pos = 0;
+               for(int i=0; i < structsize1; i++) {
+                       paramCls[pos] = String.class;
+                       paramClsTyp1[pos] = null;
+                       paramClsTyp2[pos++] = null;
+                       paramCls[pos] = float.class;
+                       paramClsTyp1[pos] = null;
+                       paramClsTyp2[pos++] = null;
+                       paramCls[pos] = int.class;
+                       paramClsTyp1[pos] = null;
+                       paramClsTyp2[pos++] = null;
+               }
+               Object[] paramObj = rmiObj.getMethodParams(paramCls, 
+                       paramClsTyp1, paramClsTyp2);
+               StructJ[] data = new StructJ[structsize1];
+               for (int i=0; i < structsize1; i++) {
+                       data[i] = new StructJ();
+               }
+               pos = 0;
+               for(int i=0; i < structsize1; i++) {
+                       data[i].name = (String) paramObj[pos++];
+                       data[i].value = (float) paramObj[pos++];
+                       data[i].year = (int) paramObj[pos++];
+               }
+               tc.handleStruct(data);
+       }
+       
+
+       private void ___waitRequestInvokeMethod() throws IOException {
+
+               // Struct size
+               int structsize1 = 0;
+               // Loop continuously waiting for incoming bytes
+               while (true) {
+
+                       rmiObj.getMethodBytes();
+                       int _objectId = rmiObj.getObjectId();
+                       if (_objectId == objectId) {
+                       // Multiplex based on object Id
+                               String methodSign = rmiObj.getSignature();
+                               Object retObj = null;
+                               if (methodSign.equals("voidsetA(int)")) {
+                                       ___setA();
+                               } else if (methodSign.equals("voidsetB(float)")) {
+                                       ___setB();
+                               } else if (methodSign.equals("voidsetC(string)")) {
+                                       ___setC();
+                               } else if (methodSign.equals("sumArray(string[])")) {
+                                       retObj = ___sumArray();
+                               } else if (methodSign.equals("intsetAndGetA(int)")) {
+                                       retObj = ___setAndGetA();
+                               } else if (methodSign.equals("intsetACAndGetA(string,int)")) {
+                                       retObj = ___setACAndGetA();
+                               } else if (methodSign.equals("voidregisterCallBack(CallBackInterface)")) {
+                                       ___registerCallback();
+                               } else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
+                                       ____registerCallback();
+                               } else if (methodSign.equals("intcallBack()")) {
+                                       retObj = ___callBack();
+                               // Special option to register callback
+                               } else if (methodSign.equals("registercallback")) {
+                                       ___regCB();
+                               // Struct handling (3 is the size of the struct)
+                               } else if (methodSign.equals("structsize")) {
+                                       structsize1 = ___structSize();
+                               } else if (methodSign.equals("handleStruct(StructJ[])")) {
+                                       ___handleStruct(structsize1);
+                               } else
+                                       throw new Error("Signature not recognized!");
+
+                               if (retObj != null) {
+                                       rmiObj.sendReturnObj(retObj);
+                               }
+                               System.out.println("Servicing remote call for object: " + objectId + " - method: " + methodSign);
+                       }
+               }
+       }
+
+
+       // Return method signatures
+       public static String[] getMethodSignatures() {
+
+               return methodSignatures;
+       }
+       
+       
        public static void main(String[] args) throws Exception {
 
                int port = 5010;