Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / drivers / Java / WeatherPhoneGateway / WeatherGateway_Skeleton.java
index afb488a5f99c7c19cedb0a7e0d36423e7e838280..3a472bf02c06c764ffeebe2baa415631cb106ac7 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 WeatherGateway_Skeleton implements WeatherGateway {
 
        private WeatherGateway mainObj;
-       private IoTRMIObject rmiObj;
-
-       private String callbackAddress;
-       private int objIdCnt = 0;
-       private IoTRMICall rmiCall;
-       private int[] ports;
-
-       private final static int object0Id = 0; //WeatherGatewaySmart
-       private static Integer[] object0Permission = { 3, 6, 5, 7, 2, 1, 0, 4 };
-       private static List<Integer> set0Allowed;
+       private int objectId = 3;
+       // Communications and synchronizations
+       private IoTRMIComm rmiComm;
+       private AtomicBoolean didAlreadyInitWaitInvoke;
+       private AtomicBoolean methodReceived;
+       private byte[] methodBytes = null;
+       // Permissions
+       private static Integer[] object3Permission = { 3, 6, 5, 7, 2, 1, 0, 4 };
+       private static List<Integer> set3Allowed;
        
 
-       public WeatherGateway_Skeleton(WeatherGateway _mainObj, String _callbackAddress, int _port) throws Exception {
+       public WeatherGateway_Skeleton(WeatherGateway _mainObj, int _portSend, int _portRecv) throws Exception {
+               mainObj = _mainObj;
+               rmiComm = new IoTRMICommServer(_portSend, _portRecv);
+               set3Allowed = new ArrayList<Integer>(Arrays.asList(object3Permission));
+               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 WeatherGateway_Skeleton(WeatherGateway _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;
+               set3Allowed = new ArrayList<Integer>(Arrays.asList(object3Permission));
+               didAlreadyInitWaitInvoke = new AtomicBoolean(false);
+               methodReceived = new AtomicBoolean(false);
+               rmiComm.registerSkeleton(objectId, methodReceived);
+       }
+
+       public boolean didAlreadyInitWaitInvoke() {
+               return didAlreadyInitWaitInvoke.get();
        }
 
        public void init() {
@@ -65,64 +97,77 @@ public class WeatherGateway_Skeleton implements WeatherGateway {
                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 ___start() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                start();
        }
 
        public void ___stop() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                stop();
        }
 
        public void ___getInchesPerWeek() 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 = getInchesPerWeek();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getWeatherZipCode() 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 = getWeatherZipCode();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getDaysToWaterOn() 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 = getDaysToWaterOn();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getInchesPerMinute() 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 = getInchesPerMinute();
-               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 {
-                       WeatherGatewaySmartCallback stub0 = new WeatherGatewaySmartCallback_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);
-                       objIdCnt++;
+                       int[] stubIdArray0 = (int[]) paramObj[0];
+                       int objIdRecv0 = stubIdArray0[0];
+                       WeatherGatewaySmartCallback newStub0 = null;
+                       if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
+                               newStub0 = new WeatherGatewaySmartCallback_Stub(rmiComm, objIdRecv0);
+                               IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
+                               rmiComm.setObjectIdCounter(objIdRecv0);
+                               rmiComm.decrementObjectIdCounter();
+                       }
+                       else {
+                               newStub0 = (WeatherGatewaySmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
+                       }
+                       WeatherGatewaySmartCallback stub0 = newStub0;
                        registerCallback(stub0);
                } catch(Exception ex) {
                        ex.printStackTrace();
@@ -130,29 +175,121 @@ public class WeatherGateway_Skeleton implements WeatherGateway {
                }
        }
 
-       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 (!set3Allowed.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: ___start(); break;
-                               case 2: ___stop(); break;
-                               case 3: ___getInchesPerWeek(); break;
-                               case 4: ___getWeatherZipCode(); break;
-                               case 5: ___getDaysToWaterOn(); break;
-                               case 6: ___getInchesPerMinute(); break;
-                               case 7: ___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 {
+                                                       ___start();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 2:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___stop();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 3:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getInchesPerWeek();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 4:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getWeatherZipCode();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 5:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getDaysToWaterOn();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 6:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getInchesPerMinute();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 7:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___registerCallback();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
                                default: 
                                throw new Error("Method Id " + methodId + " not recognized!");
                        }