Initial version that handles multiple callback objects through 1 socket
[iot2.git] / iotjava / iotrmi / Java / sample / CallBack_Skeleton.java
index 46e0df6fcffd56ff69640b5a3d59f223853779b8..70d8a812162527dfcfd3f4661516257b955b4365 100644 (file)
@@ -5,14 +5,14 @@ import java.util.Set;
 import java.util.Arrays;
 import iotrmi.Java.IoTRMIObject;
 
-public class CallBack_Skeleton {
+public class CallBack_Skeleton implements CallBackInterface {
 
-       private String[] methodSignatures = {
+       private int objectId = 0;       // Default value is 0
+       private final static String[] methodSignatures = {
 
                "intprintInt()",
                "voidsetInt(int)"
        };
-
        private CallBackInterface cb;
        private IoTRMIObject rmiObj;
 
@@ -36,31 +36,51 @@ public class CallBack_Skeleton {
                while (true) {
 
                        rmiObj.getMethodBytes();
-                       String methodSign = rmiObj.getSignature();
-                       Object[] paramObj = null;
-                       Object retObj = null;
-                       System.out.println("Method sign: " + methodSign);
-
-                       if (methodSign.equals("intprintInt()")) {
-                               //paramObj = rmiObj.getMethodParams(new Class<?>[] { }, 
-                               //      new Class<?>[] { null }, new Class<?>[] { null });
-                               retObj = cb.printInt();
-                       } else if (methodSign.equals("voidsetInt(int)")) {
-                               paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-                                       new Class<?>[] { null }, new Class<?>[] { null });
-                               cb.setInt((int) paramObj[0]);
-                       } else
-                               throw new Error("Signature un-recognized!");
-                       System.out.println("Return object: " + retObj);
-
-                       if (retObj != null) {
-                               rmiObj.sendReturnObj(retObj);
+                       int objId = rmiObj.getObjectId();
+                       if (objId == objectId) {
+                       // Multiplex based on object Id
+                               rmiObj.getMethodBytes();
+                               String methodSign = rmiObj.getSignature();
+                               Object[] paramObj = null;
+                               Object retObj = null;
+                               System.out.println("Method sign: " + methodSign);
+
+                               if (methodSign.equals("intprintInt()")) {
+                                       retObj = printInt();
+                               } else if (methodSign.equals("voidsetInt(int)")) {
+                                       paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
+                                               new Class<?>[] { null }, new Class<?>[] { null });
+                                       setInt((int) paramObj[0]);
+                               } else
+                                       throw new Error("Signature not recognized!");
+                               System.out.println("Return object: " + retObj);
+
+                               if (retObj != null) {
+                                       rmiObj.sendReturnObj(retObj);
+                               }
+                               System.out.println("Servicing remote call for method: " + methodSign);
                        }
-                       System.out.println("Servicing remote call for method: " + methodSign);
                }
        }
 
 
+       // Return method signatures
+       public static String[] getMethodSignatures() {
+
+               return methodSignatures;
+       }
+
+
+       public int printInt() {
+               return cb.printInt();
+       }
+
+
+       public void setInt(int _i) {
+               cb.setInt(_i);
+       }
+
+
        public static void main(String[] args) throws Exception {
 
                int port = 5010;