Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / Java / IrrigationController / WeatherGatewaySmart_Stub.java
index e8922c5289c6c4f7d7899ef9854d250438083895..9c1052827a26f57b211cf88c2b954cf749d289f0 100644 (file)
@@ -4,35 +4,48 @@ 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 WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
 
-       private IoTRMICall rmiCall;
-       private String callbackAddress;
-       private int[] ports;
-
-       private final static int objectId = 0;
-       // Callback properties
-       private IoTRMIObject rmiObj;
-       List<WeatherGatewayCallback> listCallbackObj;
-       private int objIdCnt = 0;
-       private final static int object0Id = 0; //WeatherGatewaySmartCallback
-       private static Integer[] object0Permission = { 0 };
-       private static List<Integer> set0Allowed;
+       private int objectId = 3;
+       private IoTRMIComm rmiComm;
+       // Synchronization variables
+       private AtomicBoolean retValueReceived3 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived6 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived5 = new AtomicBoolean(false);
+       private AtomicBoolean retValueReceived4 = new AtomicBoolean(false);
        
 
-       public WeatherGatewaySmart_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<WeatherGatewayCallback>();
-               set0Allowed.add(-9999);
-               ___initCallBack();
+       public WeatherGatewaySmart_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, 3, retValueReceived3);
+               rmiComm.registerStub(objectId, 6, retValueReceived6);
+               rmiComm.registerStub(objectId, 5, retValueReceived5);
+               rmiComm.registerStub(objectId, 4, retValueReceived4);
+               IoTRMIUtil.mapStub.put(objectId, this);
+       }
+
+       public WeatherGatewaySmart_Stub(IoTRMIComm _rmiComm, int _objectId) throws Exception {
+               rmiComm = _rmiComm;
+               objectId = _objectId;
+               rmiComm.registerStub(objectId, 3, retValueReceived3);
+               rmiComm.registerStub(objectId, 6, retValueReceived6);
+               rmiComm.registerStub(objectId, 5, retValueReceived5);
+               rmiComm.registerStub(objectId, 4, retValueReceived4);
        }
 
        public double getInchesPerWeek() {
@@ -40,7 +53,13 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                Class<?> retType = double.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 (double)retObj;
        }
 
@@ -49,7 +68,13 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                Class<?> retType = double.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 (!retValueReceived6.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived6.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (double)retObj;
        }
 
@@ -58,14 +83,44 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                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();
+
                return (int)retObj;
        }
 
        public void registerCallback(WeatherGatewayCallback _callbackTo) {
+               int[] objIdSent0 = new int[1];
                try {
-                       WeatherGatewayCallback_CallbackSkeleton skel0 = new WeatherGatewayCallback_CallbackSkeleton(_callbackTo, callbackAddress, objIdCnt++);
-                       listCallbackObj.add(skel0);
+                       if (!IoTRMIUtil.mapSkel.containsKey(_callbackTo)) {
+                               int newObjIdSent = rmiComm.getObjectIdCounter();
+                               objIdSent0[0] = newObjIdSent;
+                               rmiComm.decrementObjectIdCounter();
+                               WeatherGatewayCallback_Skeleton skel0 = new WeatherGatewayCallback_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 WeatherGatewayCallback_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!");
@@ -73,43 +128,9 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
 
                int methodId = 7;
                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);
-                                               WeatherGatewayCallback_CallbackSkeleton skel = (WeatherGatewayCallback_CallbackSkeleton) listCallbackObj.get(objId);
-                                               if (skel != null) {
-                                                       int methodId = IoTRMIObject.getMethodId(method);
-                                                       if (!set0Allowed.contains(methodId)) {
-                                                               throw new Error("Callback object for WeatherGatewayCallback is not allowed to access method: " + methodId);
-                                                       }
-                                                       skel.invokeMethod(rmiObj);
-                                               } else {
-                                                       throw new Error("WeatherGatewayCallback: Object with Id " + objId + " not found!");
-                                               }
-                                       }
-                               } catch (Exception ex) {
-                                       ex.printStackTrace();
-                                       throw new Error("Error instantiating class WeatherGatewayCallback_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 void stop() {
@@ -117,7 +138,7 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                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() {
@@ -125,7 +146,7 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                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 init() {
@@ -133,7 +154,7 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                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 int getWeatherZipCode() {
@@ -141,7 +162,13 @@ public class WeatherGatewaySmart_Stub implements WeatherGatewaySmart {
                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 (!retValueReceived4.get());
+               Object retObj = rmiComm.getReturnValue(retType, null);
+               retValueReceived4.set(false);
+               rmiComm.setGetReturnBytes();
+
                return (int)retObj;
        }