Exchanging cameras between benchmarks; updating database
[iot2.git] / benchmarks / Java / HomeSecurityController / CameraSmart_Stub.java
index d58dfba..a62b4b2 100644 (file)
@@ -4,35 +4,57 @@ 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 CameraSmart_Stub implements CameraSmart {
 
-       private IoTRMICall rmiCall;
-       private String callbackAddress;
-       private int[] ports;
-
-       private final static int objectId = 0;
-       // Callback properties
-       private IoTRMIObject rmiObj;
-       List<CameraCallback> listCallbackObj;
-       private static int objIdCnt = 0;
-       private final static int object0Id = 0; //CameraSmartCallback
-       private static Integer[] object0Permission = { 0 };
-       private static List<Integer> set0Allowed;
+       private int objectId = 2;
+       private IoTRMIComm rmiComm;
+       // Synchronization variables
+       private AtomicBoolean retValueReceived8 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived7 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived9 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived6 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived4 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived3 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived5 = new AtomicBoolean(false);
        
 
-       public CameraSmart_Stub(int _port, String _skeletonAddress, String _callbackAddress, int _rev, int[] _ports) throws Exception {
-               callbackAddress = _callbackAddress;
-               ports = _ports;
-               rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev);
-               set0Allowed = new ArrayList<Integer>(Arrays.asList(object0Permission));
-               listCallbackObj = new ArrayList<CameraCallback>();
-               set0Allowed.add(-9999);
-               ___initCallBack();
+       public CameraSmart_Stub(int _localPortSend, int _localPortRecv, int _portSend, int _portRecv, String _skeletonAddress, int _rev) throws Exception {
+               if (_localPortSend != 0 && _localPortRecv != 0) {
+                       rmiComm = new IoTRMICommClient(_localPortSend, _localPortRecv, _portSend, _portRecv, _skeletonAddress, _rev);
+               } else
+               {
+                       rmiComm = new IoTRMICommClient(_portSend, _portRecv, _skeletonAddress, _rev);
+               }
+               rmiComm.registerStub(objectId, 8, retValueReceived8);
+               rmiComm.registerStub(objectId, 7, retValueReceived7);
+               rmiComm.registerStub(objectId, 9, retValueReceived9);
+               rmiComm.registerStub(objectId, 6, retValueReceived6);
+               rmiComm.registerStub(objectId, 4, retValueReceived4);
+               rmiComm.registerStub(objectId, 3, retValueReceived3);
+               rmiComm.registerStub(objectId, 5, retValueReceived5);
+               IoTRMIUtil.mapStub.put(objectId, this);
+       }
+
+       public CameraSmart_Stub(IoTRMIComm _rmiComm, int _objectId) throws Exception {
+               rmiComm = _rmiComm;
+               objectId = _objectId;
+               rmiComm.registerStub(objectId, 8, retValueReceived8);
+               rmiComm.registerStub(objectId, 7, retValueReceived7);
+               rmiComm.registerStub(objectId, 9, retValueReceived9);
+               rmiComm.registerStub(objectId, 6, retValueReceived6);
+               rmiComm.registerStub(objectId, 4, retValueReceived4);
+               rmiComm.registerStub(objectId, 3, retValueReceived3);
+               rmiComm.registerStub(objectId, 5, retValueReceived5);
        }
 
        public int getMaxFPS() {
@@ -40,7 +62,13 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived8.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived8.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (int)retObj;
        }
 
@@ -49,7 +77,13 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = boolean.class;
                Class<?>[] paramCls = new Class<?>[] { int.class };
                Object[] paramObj = new Object[] { _fps };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived7.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived7.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (boolean)retObj;
        }
 
@@ -58,7 +92,13 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived9.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived9.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (int)retObj;
        }
 
@@ -69,7 +109,13 @@ public class CameraSmart_Stub implements CameraSmart {
                paramEnum0[0] = _res.ordinal();
                Class<?>[] paramCls = new Class<?>[] { int[].class };
                Object[] paramObj = new Object[] { paramEnum0 };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived6.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived6.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (boolean)retObj;
        }
 
@@ -78,7 +124,7 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
        }
 
        public void start() {
@@ -86,7 +132,7 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
        }
 
        public long getTimestamp() {
@@ -94,7 +140,13 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = long.class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived4.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived4.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (long)retObj;
        }
 
@@ -103,7 +155,13 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = byte[].class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived3.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived3.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (byte[])retObj;
        }
 
@@ -112,13 +170,37 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
        }
 
        public void registerCallback(CameraCallback _callbackTo) {
+               int[] objIdSent0 = new int[1];
                try {
-                       CameraCallback_CallbackSkeleton skel0 = new CameraCallback_CallbackSkeleton(_callbackTo, callbackAddress, objIdCnt++);
-                       listCallbackObj.add(skel0);
+                       if (!IoTRMIUtil.mapSkel.containsKey(_callbackTo)) {
+                               int newObjIdSent = rmiComm.getObjectIdCounter();
+                               objIdSent0[0] = newObjIdSent;
+                               rmiComm.decrementObjectIdCounter();
+                               CameraCallback_Skeleton skel0 = new CameraCallback_Skeleton(_callbackTo, rmiComm, newObjIdSent);
+                               IoTRMIUtil.mapSkel.put(_callbackTo, skel0);
+                               IoTRMIUtil.mapSkelId.put(_callbackTo, newObjIdSent);
+                               Thread thread = new Thread() {
+                                       public void run() {
+                                               try {
+                                                       skel0.___waitRequestInvokeMethod();
+                                                       } catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                                       throw new Error("Exception when trying to run ___waitRequestInvokeMethod() for CameraCallback_Skeleton!");
+                                               }
+                                       }
+                               };
+                               thread.start();
+                               while(!skel0.didAlreadyInitWaitInvoke());
+                       }
+                       else
+                       {
+                               int newObjIdSent = IoTRMIUtil.mapSkelId.get(_callbackTo);
+                               objIdSent0[0] = newObjIdSent;
+                       }
                } catch (Exception ex) {
                        ex.printStackTrace();
                        throw new Error("Exception when generating skeleton objects!");
@@ -126,43 +208,9 @@ public class CameraSmart_Stub implements CameraSmart {
 
                int methodId = 10;
                Class<?> retType = void.class;
-               Class<?>[] paramCls = new Class<?>[] { int.class };
-               Object[] paramObj = new Object[] { new Integer(1) };
-               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
-       }
-
-       public void ___initCallBack() {
-               Thread thread = new Thread() {
-                       public void run() {
-                               try {
-                                       rmiObj = new IoTRMIObject(ports[0]);
-                                       while (true) {
-                                               byte[] method = rmiObj.getMethodBytes();
-                                               int objId = IoTRMIObject.getObjectId(method);
-                                               CameraCallback_CallbackSkeleton skel = (CameraCallback_CallbackSkeleton) listCallbackObj.get(objId);
-                                               if (skel != null) {
-                                                       int methodId = IoTRMIObject.getMethodId(method);
-                                                       if (!set0Allowed.contains(methodId)) {
-                                                               throw new Error("Callback object for CameraCallback is not allowed to access method: " + methodId);
-                                                       }
-                                                       skel.invokeMethod(rmiObj);
-                                               } else {
-                                                       throw new Error("CameraCallback: Object with Id " + objId + " not found!");
-                                               }
-                                       }
-                               } catch (Exception ex) {
-                                       ex.printStackTrace();
-                                       throw new Error("Error instantiating class CameraCallback_CallbackSkeleton!");
-                               }
-                       }
-               };
-               thread.start();
-
-               int methodId = -9998;
-               Class<?> retType = void.class;
-               Class<?>[] paramCls = new Class<?>[] { int[].class, String.class, int.class };
-               Object[] paramObj = new Object[] { ports, callbackAddress, 0 };
-               rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               Class<?>[] paramCls = new Class<?>[] { int[].class };
+               Object[] paramObj = new Object[] { objIdSent0 };
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
        }
 
        public List<Resolution> getSupportedResolutions() {
@@ -170,7 +218,13 @@ public class CameraSmart_Stub implements CameraSmart {
                Class<?> retType = int[].class;
                Class<?>[] paramCls = new Class<?>[] {  };
                Object[] paramObj = new Object[] {  };
-               Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
+               rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);
+               // Waiting for return value
+               while (!retValueReceived5.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived5.set(false);
+               rmiComm.setGetReturnBytes();
+
                int[] retEnum = (int[]) retObj;
                Resolution[] enumVals = Resolution.values();
                int retLen = retEnum.length;