Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / drivers / Java / WaterLeakSensor / SmartthingsSensor_Skeleton.java
index e52f126146377b22f2fc753e7fa6fd5567858f12..62cf61c6489f289ca56ea935ced0f78f1f777d50 100644 (file)
@@ -4,33 +4,65 @@ import java.io.IOException;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Arrays;
-import iotrmi.Java.IoTRMICall;
-import iotrmi.Java.IoTRMIObject;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import iotrmi.Java.IoTRMIComm;
+import iotrmi.Java.IoTRMICommClient;
+import iotrmi.Java.IoTRMICommServer;
+import iotrmi.Java.IoTRMIUtil;
 
 import iotcode.interfaces.*;
 
 public class SmartthingsSensor_Skeleton implements SmartthingsSensor {
 
        private SmartthingsSensor mainObj;
-       private IoTRMIObject rmiObj;
-
-       private String callbackAddress;
-       private int objIdCnt = 0;
-       private IoTRMICall rmiCall;
-       private int[] ports;
-
-       private final static int object0Id = 0; //SmartthingsSensorSmart
-       private static Integer[] object0Permission = { 3, 2, 5, 6, 1, 4, 0 };
-       private static List<Integer> set0Allowed;
+       private int objectId = 2;
+       // Communications and synchronizations
+       private IoTRMIComm rmiComm;
+       private AtomicBoolean didAlreadyInitWaitInvoke;
+       private AtomicBoolean methodReceived;
+       private byte[] methodBytes = null;
+       // Permissions
+       private static Integer[] object2Permission = { 3, 2, 5, 6, 1, 4, 0 };
+       private static List<Integer> set2Allowed;
        
 
-       public SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, String _callbackAddress, int _port) throws Exception {
+       public SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, int _portSend, int _portRecv) throws Exception {
+               mainObj = _mainObj;
+               rmiComm = new IoTRMICommServer(_portSend, _portRecv);
+               set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
+               IoTRMIUtil.mapSkel.put(_mainObj, this);
+               IoTRMIUtil.mapSkelId.put(_mainObj, objectId);
+               didAlreadyInitWaitInvoke = new AtomicBoolean(false);
+               methodReceived = new AtomicBoolean(false);
+               rmiComm.registerSkeleton(objectId, methodReceived);
+               Thread thread1 = new Thread() {
+                       public void run() {
+                               try {
+                                       ___waitRequestInvokeMethod();
+                               }
+                               catch (Exception ex)
+                               {
+                                       ex.printStackTrace();
+                               }
+                       }
+               };
+               thread1.start();
+       }
+
+       public SmartthingsSensor_Skeleton(SmartthingsSensor _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
                mainObj = _mainObj;
-               callbackAddress = _callbackAddress;
-               rmiObj = new IoTRMIObject(_port);
-               set0Allowed = new ArrayList<Integer>(Arrays.asList(object0Permission));
-               set0Allowed.add(-9998);
-               ___waitRequestInvokeMethod();
+               rmiComm = _rmiComm;
+               objectId = _objectId;
+               set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
+               didAlreadyInitWaitInvoke = new AtomicBoolean(false);
+               methodReceived = new AtomicBoolean(false);
+               rmiComm.registerSkeleton(objectId, methodReceived);
+       }
+
+       public boolean didAlreadyInitWaitInvoke() {
+               return didAlreadyInitWaitInvoke.get();
        }
 
        public void init() {
@@ -61,58 +93,70 @@ public class SmartthingsSensor_Skeleton implements SmartthingsSensor {
                mainObj.registerCallback(_callbackTo);
        }
 
-       public void ___regCB() throws IOException {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class, String.class, int.class },new Class<?>[] { null, null, null });
-               ports = (int[]) paramObj[0];
-               rmiCall = new IoTRMICall(ports[0], (String) paramObj[1], (int) paramObj[2]);
-       }
-
        public void ___init() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                init();
        }
 
        public void ___getValue() throws IOException {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                Object retObj = getValue();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___isActiveValue() throws IOException {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                Object retObj = isActiveValue();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getTimestampOfLastReading() throws IOException {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                Object retObj = getTimestampOfLastReading();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___setId() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-               new Class<?>[] { null });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null }, localMethodBytes);
                setId((int) paramObj[0]);
        }
 
        public void ___getId() throws IOException {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                Object retObj = getId();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___registerCallback() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
-               new Class<?>[] { null });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { int[].class }, new Class<?>[] { null }, localMethodBytes);
                try {
-                       SmartthingsSensorSmartCallback stub0 = new SmartthingsSensorSmartCallback_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);
-                       objIdCnt++;
+                       int[] stubIdArray0 = (int[]) paramObj[0];
+                       int objIdRecv0 = stubIdArray0[0];
+                       SmartthingsSensorSmartCallback newStub0 = null;
+                       if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
+                               newStub0 = new SmartthingsSensorSmartCallback_Stub(rmiComm, objIdRecv0);
+                               IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
+                               rmiComm.setObjectIdCounter(objIdRecv0);
+                               rmiComm.decrementObjectIdCounter();
+                       }
+                       else {
+                               newStub0 = (SmartthingsSensorSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
+                       }
+                       SmartthingsSensorSmartCallback stub0 = newStub0;
                        registerCallback(stub0);
                } catch(Exception ex) {
                        ex.printStackTrace();
@@ -120,28 +164,109 @@ public class SmartthingsSensor_Skeleton implements SmartthingsSensor {
                }
        }
 
-       private void ___waitRequestInvokeMethod() throws IOException {
+       public void ___waitRequestInvokeMethod() throws IOException {
+               didAlreadyInitWaitInvoke.compareAndSet(false, true);
                while (true) {
-                       rmiObj.getMethodBytes();
-                       int _objectId = rmiObj.getObjectId();
-                       int methodId = rmiObj.getMethodId();
-                       if (_objectId == object0Id) {
-                               if (!set0Allowed.contains(methodId)) {
+                       if (!methodReceived.get()) {
+                               continue;
+                       }
+                       methodBytes = rmiComm.getMethodBytes();
+                       methodReceived.set(false);
+                       int _objectId = IoTRMIComm.getObjectId(methodBytes);
+                       int methodId = IoTRMIComm.getMethodId(methodBytes);
+                       if (_objectId == objectId) {
+                               if (!set2Allowed.contains(methodId)) {
                                        throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
                                }
                        }
                        else {
-                               throw new Error("Object Id: " + _objectId + " not recognized!");
+                               continue;
                        }
                        switch (methodId) {
-                               case 0: ___init(); break;
-                               case 1: ___getValue(); break;
-                               case 2: ___isActiveValue(); break;
-                               case 3: ___getTimestampOfLastReading(); break;
-                               case 4: ___setId(); break;
-                               case 5: ___getId(); break;
-                               case 6: ___registerCallback(); break;
-                               case -9998: ___regCB(); break;
+                               case 0:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___init();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 1:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getValue();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 2:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___isActiveValue();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 3:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getTimestampOfLastReading();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 4:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___setId();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 5:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getId();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 6:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___registerCallback();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
                                default: 
                                throw new Error("Method Id " + methodId + " not recognized!");
                        }