Adjusting and cleaning up ZigbeeTest to install Vigilia ZigBee gateway and devices.
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Skeleton.java
index 73580a25e6735262e428ee4ebe07b21248ca139a..822e0644c318090528f08697baf85a3b6f9a2198 100644 (file)
@@ -2,9 +2,8 @@ package iotrmi.Java.sample;
 
 import java.io.IOException;
 import java.util.Arrays;
-import java.util.Set;
-import java.util.Map;
-import java.util.HashMap;
+
+import java.util.List;
 
 import iotrmi.Java.IoTRMIObject;
 import iotrmi.Java.IoTRMICall;
@@ -14,14 +13,20 @@ public class TestClass_Skeleton implements TestClassInterface {
        /**
         * Class Constants
         */
-       private int objectId = 0;       // Default value is 0
-       private static int objIdCnt = 0; // Counter for callback object Ids
        private TestClassInterface tc;
        private int port;
        private IoTRMIObject rmiObj;
+
+       // Callback stuff
+       private static int objIdCnt = 0; // Counter for callback object Ids
        private IoTRMICall rmiCall;
        private CallBackInterface cbstub;
 
+       // Object permission
+       private int object0Id = 0;
+       private static Integer[] object0Permission = { 0, 1, 2, 3, 4, 5 };
+       private List<Integer> set0Allowed;
+
 
        /**
         * Constructors
@@ -33,6 +38,7 @@ public class TestClass_Skeleton implements TestClassInterface {
                tc = _tc;
                port = _port;
                rmiObj = new IoTRMIObject(_port);
+               set0Allowed = Arrays.asList(object0Permission);
                ___waitRequestInvokeMethod();
        }
 
@@ -183,9 +189,9 @@ public class TestClass_Skeleton implements TestClassInterface {
        }
        
 
-       public void handleStruct(StructJ[] data) {
+       public StructJ[] handleStruct(StructJ[] data) {
 
-               tc.handleStruct(data);
+               return tc.handleStruct(data);
        }
        
        
@@ -197,7 +203,7 @@ public class TestClass_Skeleton implements TestClassInterface {
        }
        
        
-       public void ___handleStruct(int structsize1) {
+       public void ___handleStruct(int structsize1) throws IOException {
 
                Class<?>[] paramCls = new Class<?>[3*structsize1];
                Class<?>[] paramClsVal = new Class<?>[3*structsize1];
@@ -222,7 +228,28 @@ public class TestClass_Skeleton implements TestClassInterface {
                        data[i].value = (float) paramObj[pos++];
                        data[i].year = (int) paramObj[pos++];
                }
-               tc.handleStruct(data);
+               // Just the following if there is no returned value
+               //tc.handleStruct(data);
+               StructJ[] retStruct = tc.handleStruct(data);
+               // Return length first
+               int structsize2 = retStruct.length;
+               Object retObj = structsize2;
+               rmiObj.sendReturnObj(retObj);           
+               // Send the actual struct members
+               // Calculate the size of the array
+               Class<?>[] retCls = new Class<?>[3*structsize2];
+               Object[] retObj2 = new Object[3*structsize2];
+               // Handle with for loop
+               pos = 0;
+               for(int i = 0; i < structsize2; i++) {
+                       retCls[pos] = String.class;
+                       retObj2[pos++] = data[i].name;
+                       retCls[pos] = float.class;
+                       retObj2[pos++] = data[i].value;
+                       retCls[pos] = int.class;
+                       retObj2[pos++] = data[i].year;
+               }
+               rmiObj.sendReturnObj(retCls, retObj2);
        }
 
 
@@ -232,29 +259,17 @@ public class TestClass_Skeleton implements TestClassInterface {
        }
 
 
-       public int ___enumSize() {
-               
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-                       new Class<?>[] { null });
-               return (int) paramObj[0];
-       }
-
-
-       public void ___handleEnum(int enumSize1) throws IOException {
+       public void ___handleEnum() throws IOException {
 
-               Class<?>[] paramCls = new Class<?>[enumSize1];
-               Class<?>[] paramClsVal = new Class<?>[enumSize1];
-               for(int i=0; i < enumSize1; i++) {
-                       paramCls[i] = int.class;
-                       paramClsVal[i] = null;
-               }
-               Object[] paramObj = rmiObj.getMethodParams(paramCls, 
-                       paramClsVal);
+               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class }, 
+                       new Class<?>[] { null }); 
                // Encoder/decoder
+               int paramInt[] = (int[]) paramObj[0];
+               int enumSize1 = paramInt.length;
                EnumJ[] enumJ = EnumJ.values();
                EnumJ[] data = new EnumJ[enumSize1];
                for (int i=0; i < enumSize1; i++) {
-                       data[i] = enumJ[(int) paramObj[i]];
+                       data[i] = enumJ[paramInt[i]];
                }
                // if void, just "handleEnum(data)"
                // this is when we have return value EnumJ[]
@@ -281,31 +296,39 @@ public class TestClass_Skeleton implements TestClassInterface {
 
                        rmiObj.getMethodBytes();
                        int _objectId = rmiObj.getObjectId();
-                       if (_objectId == objectId) {
+                       int methodId = rmiObj.getMethodId();
+                       if (_objectId == object0Id) {
                        // Multiplex based on object Id
-                               int methodId = rmiObj.getMethodId();
-                               switch (methodId) {
-
-                                       case 0: ___setA(); break;
-                                       case 1: ___setB(); break;
-                                       case 2: ___setC(); break;
-                                       case 3: ___sumArray(); break;
-                                       case 4: ___setAndGetA(); break;
-                                       case 5: ___setACAndGetA(); break;
-                                       case 6: ___callBack(); break; 
-                                       case 7: ___registerCallback(); break;
-                                       case 8: ____registerCallback(); break;
-                                       // Special option to register callback
-                                       case 9: ___regCB(); break;
-                                       // Struct handling (3 is the size of the struct)
-                                       case 10: ___handleStruct(structSize1); break;
-                                       case 11: structSize1 = ___structSize(); break;
-                                       case 12: ___handleEnum(enumSize1); break;
-                                       case 13: enumSize1 = ___enumSize(); break;
-                                       default:
-                                               throw new Error("Method Id not recognized!");
-                               }
+                               // Complain if the method is not allowed
+                               if (!set0Allowed.contains(methodId))
+                                       throw new Error("TestClass_Skeleton: This object is not allowed to access method " + methodId);
+                       // If we have more than 1 object Id...
+                       //else if (_objectId == object1Id) {
+
+                       } else
+                               throw new Error("TestClass_Skeleton: Unrecognizable object Id: " + _objectId);
+
+                       switch (methodId) {
+
+                               case 0: ___setA(); break;
+                               case 1: ___setB(); break;
+                               case 2: ___setC(); break;
+                               case 3: ___sumArray(); break;
+                               case 4: ___setAndGetA(); break;
+                               case 5: ___setACAndGetA(); break;
+                               case 6: ___callBack(); break; 
+                               case 7: ___registerCallback(); break;
+                               case 8: ____registerCallback(); break;
+                               // Special option to register callback
+                               case 9: ___regCB(); break;
+                               // Struct handling (3 is the size of the struct)
+                               case 10: ___handleStruct(structSize1); break;
+                               case 11: structSize1 = ___structSize(); break;
+                               case 12: ___handleEnum(); break;
+                               default:
+                                       throw new Error("Method Id " + methodId + " not recognized!");
                        }
+
                }
        }
        
@@ -316,51 +339,5 @@ public class TestClass_Skeleton implements TestClassInterface {
                TestClass tc = new TestClass(3, 5f, "7911");
                TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, port);
 
-/*             String[] methodSignatures = TestClass_CBSkeleton.getMethodSignatures();
-               IoTRMIObject rmiObj = new IoTRMIObject(port, methodSignatures);
-               Map<Integer,TestClassInterface> mapCBObject = new HashMap<Integer,TestClassInterface>();
-
-               // Can replace for-loop with while-loop if necessary
-               for (int i = 1; i < 3; i++) {
-                       TestClassInterface tcSkel = new TestClass_CBSkeleton(tc, i);
-                       mapCBObject.put(i, tcSkel);
-               }
-
-               Object retObj = null;
-               while (true) {
-                       byte[] method = rmiObj.getMethodBytes();
-                       int objId = IoTRMIObject.getObjectId(method);
-                       TestClass_CBSkeleton tcSkel = (TestClass_CBSkeleton) mapCBObject.get(objId);
-                       if (tcSkel != null) {
-                               rmiObj.setMethodBytes(method);
-                               retObj = tcSkel.invokeMethod(rmiObj);
-                       }
-                       if (retObj != null) {
-                               rmiObj.sendReturnObj(retObj);
-                       }
-               }
-*/
-               //int objectId = 1;
-               //System.out.println("Creating 0 object");
-               //TestClass_Skeleton tcSkel1 = new TestClass_Skeleton(tc, rmiObj, objectId);
-               //System.out.println("Creating 1 object");
-               //objectId = 2;
-               //TestClass_Skeleton tcSkel2 = new TestClass_Skeleton(tc, rmiObj, objectId);
-               //System.out.println("Creating 2 object");
-
-               /*for (int i = 1; i < 3; i++) {
-                       final int objectId = i;
-                       Thread thread = new Thread() {
-                               public void run() {
-                               try{
-                                               TestClass_Skeleton tcSkel = new TestClass_Skeleton(tc, rmiObj, objectId);
-                                       } catch (Exception ex){
-                                               ex.printStackTrace();
-                                               throw new Error("Error instantiating class CallBack_Skeleton!");
-                               }
-                           }
-                       };
-                       thread.start();
-               }*/
        }
 }