Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / drivers / Java / SpruceSensor / MoistureSensor_Skeleton.java
index 47e2bc0294f56136e4f3fde664cbead86d778a2f..290613504cef8b73fe9f4debf37955c6e26525c6 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 MoistureSensor_Skeleton implements MoistureSensor {
 
        private MoistureSensor mainObj;
-       private IoTRMIObject rmiObj;
-
-       private String callbackAddress;
-       private int objIdCnt = 0;
-       private IoTRMICall rmiCall;
-       private int[] ports;
-
-       private final static int object0Id = 0; //MoistureSensorSmart
-       private static Integer[] object0Permission = { 2, 4, 5, 1, 3, 0 };
-       private static List<Integer> set0Allowed;
+       private int objectId = 5;
+       // Communications and synchronizations
+       private IoTRMIComm rmiComm;
+       private AtomicBoolean didAlreadyInitWaitInvoke;
+       private AtomicBoolean methodReceived;
+       private byte[] methodBytes = null;
+       // Permissions
+       private static Integer[] object5Permission = { 2, 4, 5, 1, 3, 0 };
+       private static List<Integer> set5Allowed;
        
 
-       public MoistureSensor_Skeleton(MoistureSensor _mainObj, String _callbackAddress, int _port) throws Exception {
+       public MoistureSensor_Skeleton(MoistureSensor _mainObj, int _portSend, int _portRecv) throws Exception {
+               mainObj = _mainObj;
+               rmiComm = new IoTRMICommServer(_portSend, _portRecv);
+               set5Allowed = new ArrayList<Integer>(Arrays.asList(object5Permission));
+               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 MoistureSensor_Skeleton(MoistureSensor _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;
+               set5Allowed = new ArrayList<Integer>(Arrays.asList(object5Permission));
+               didAlreadyInitWaitInvoke = new AtomicBoolean(false);
+               methodReceived = new AtomicBoolean(false);
+               rmiComm.registerSkeleton(objectId, methodReceived);
+       }
+
+       public boolean didAlreadyInitWaitInvoke() {
+               return didAlreadyInitWaitInvoke.get();
        }
 
        public void init() {
@@ -57,51 +89,62 @@ public class MoistureSensor_Skeleton implements MoistureSensor {
                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 ___getMoisture() 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 = getMoisture();
-               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 {
-                       MoistureSensorSmartCallback stub0 = new MoistureSensorSmartCallback_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);
-                       objIdCnt++;
+                       int[] stubIdArray0 = (int[]) paramObj[0];
+                       int objIdRecv0 = stubIdArray0[0];
+                       MoistureSensorSmartCallback newStub0 = null;
+                       if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
+                               newStub0 = new MoistureSensorSmartCallback_Stub(rmiComm, objIdRecv0);
+                               IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
+                               rmiComm.setObjectIdCounter(objIdRecv0);
+                               rmiComm.decrementObjectIdCounter();
+                       }
+                       else {
+                               newStub0 = (MoistureSensorSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
+                       }
+                       MoistureSensorSmartCallback stub0 = newStub0;
                        registerCallback(stub0);
                } catch(Exception ex) {
                        ex.printStackTrace();
@@ -109,27 +152,97 @@ public class MoistureSensor_Skeleton implements MoistureSensor {
                }
        }
 
-       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 (!set5Allowed.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: ___getMoisture(); break;
-                               case 2: ___getTimestampOfLastReading(); break;
-                               case 3: ___setId(); break;
-                               case 4: ___getId(); break;
-                               case 5: ___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 {
+                                                       ___getMoisture();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 2:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getTimestampOfLastReading();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 3:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___setId();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 4:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getId();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 5:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___registerCallback();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
                                default: 
                                throw new Error("Method Id " + methodId + " not recognized!");
                        }