Updating stubs and skeletons for the second benchmark and drivers; tested the second...
[iot2.git] / benchmarks / drivers / Java / IHome / Speaker_Skeleton.java
index 75f2f6d11dc37abb9bfe01c3e1fb17bf69a265cd..f2b994db858ba0da69ccf990494e46776095477e 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 Speaker_Skeleton implements Speaker {
 
        private Speaker mainObj;
-       private IoTRMIObject rmiObj;
-
-       private String callbackAddress;
-       private int objIdCnt = 0;
-       private IoTRMICall rmiCall;
-       private int[] ports;
-
-       private final static int object0Id = 0; //SpeakerSmart
-       private static Integer[] object0Permission = { 6, 2, 9, 1, 3, 4, 5, 7, 8, 0, 10 };
-       private static List<Integer> set0Allowed;
+       private int objectId = 1;
+       // Communications and synchronizations
+       private IoTRMIComm rmiComm;
+       private AtomicBoolean didAlreadyInitWaitInvoke;
+       private AtomicBoolean methodReceived;
+       private byte[] methodBytes = null;
+       // Permissions
+       private static Integer[] object1Permission = { 6, 2, 9, 1, 3, 4, 5, 7, 8, 0, 10 };
+       private static List<Integer> set1Allowed;
        
 
-       public Speaker_Skeleton(Speaker _mainObj, String _callbackAddress, int _port) throws Exception {
+       public Speaker_Skeleton(Speaker _mainObj, int _portSend, int _portRecv) throws Exception {
+               mainObj = _mainObj;
+               rmiComm = new IoTRMICommServer(_portSend, _portRecv);
+               set1Allowed = new ArrayList<Integer>(Arrays.asList(object1Permission));
+               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 Speaker_Skeleton(Speaker _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;
+               set1Allowed = new ArrayList<Integer>(Arrays.asList(object1Permission));
+               didAlreadyInitWaitInvoke = new AtomicBoolean(false);
+               methodReceived = new AtomicBoolean(false);
+               rmiComm.registerSkeleton(objectId, methodReceived);
+       }
+
+       public boolean didAlreadyInitWaitInvoke() {
+               return didAlreadyInitWaitInvoke.get();
        }
 
        public void init() {
@@ -77,84 +109,100 @@ public class Speaker_Skeleton implements Speaker {
                mainObj.registerCallback(_cb);
        }
 
-       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 ___startPlayback() 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 = startPlayback();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___stopPlayback() 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 = stopPlayback();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getPlaybackState() 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 = getPlaybackState();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___setVolume() throws IOException {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { float.class }, 
-               new Class<?>[] { null });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { float.class }, new Class<?>[] { null }, localMethodBytes);
                Object retObj = setVolume((float) paramObj[0]);
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getVolume() 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 = getVolume();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___getPosition() 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 = getPosition();
-               rmiObj.sendReturnObj(retObj);
+               rmiComm.sendReturnObj(retObj, localMethodBytes);
        }
 
        public void ___setPosition() {
-               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);
                setPosition((int) paramObj[0]);
        }
 
        public void ___loadData() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { short[].class, int.class, int.class }, 
-               new Class<?>[] { null, null, null });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { short[].class, int.class, int.class }, new Class<?>[] { null, null, null }, localMethodBytes);
                loadData((short[]) paramObj[0], (int) paramObj[1], (int) paramObj[2]);
        }
 
        public void ___clearData() {
-               Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
-               new Class<?>[] {  });
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
                clearData();
        }
 
        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 {
-                       SpeakerSmartCallback stub0 = new SpeakerSmartCallback_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);
-                       objIdCnt++;
+                       int[] stubIdArray0 = (int[]) paramObj[0];
+                       int objIdRecv0 = stubIdArray0[0];
+                       SpeakerSmartCallback newStub0 = null;
+                       if(!IoTRMIUtil.mapStub.containsKey(objIdRecv0)) {
+                               newStub0 = new SpeakerSmartCallback_Stub(rmiComm, objIdRecv0);
+                               IoTRMIUtil.mapStub.put(objIdRecv0, newStub0);
+                               rmiComm.setObjectIdCounter(objIdRecv0);
+                               rmiComm.decrementObjectIdCounter();
+                       }
+                       else {
+                               newStub0 = (SpeakerSmartCallback_Stub) IoTRMIUtil.mapStub.get(objIdRecv0);
+                       }
+                       SpeakerSmartCallback stub0 = newStub0;
                        registerCallback(stub0);
                } catch(Exception ex) {
                        ex.printStackTrace();
@@ -162,32 +210,157 @@ public class Speaker_Skeleton implements Speaker {
                }
        }
 
-       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 (!set1Allowed.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: ___startPlayback(); break;
-                               case 2: ___stopPlayback(); break;
-                               case 3: ___getPlaybackState(); break;
-                               case 4: ___setVolume(); break;
-                               case 5: ___getVolume(); break;
-                               case 6: ___getPosition(); break;
-                               case 7: ___setPosition(); break;
-                               case 8: ___loadData(); break;
-                               case 9: ___clearData(); break;
-                               case 10: ___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 {
+                                                       ___startPlayback();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 2:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___stopPlayback();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 3:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getPlaybackState();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 4:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___setVolume();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 5:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getVolume();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 6:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___getPosition();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 7:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___setPosition();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 8:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___loadData();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 9:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___clearData();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               case 10:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___registerCallback();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
                                default: 
                                throw new Error("Method Id " + methodId + " not recognized!");
                        }