Updating the third benchmark with new stubs and skeletons
[iot2.git] / benchmarks / Java / SpeakerController / SpeakerCallback_Skeleton.java
diff --git a/benchmarks/Java/SpeakerController/SpeakerCallback_Skeleton.java b/benchmarks/Java/SpeakerController/SpeakerCallback_Skeleton.java
new file mode 100644 (file)
index 0000000..316d3ea
--- /dev/null
@@ -0,0 +1,116 @@
+package SpeakerController;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+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 SpeakerCallback_Skeleton implements SpeakerCallback {
+
+       private SpeakerCallback mainObj;
+       private int objectId = 2;
+       // Communications and synchronizations
+       private IoTRMIComm rmiComm;
+       private AtomicBoolean didAlreadyInitWaitInvoke;
+       private AtomicBoolean methodReceived;
+       private byte[] methodBytes = null;
+       // Permissions
+       private static Integer[] object2Permission = { 0 };
+       private static List<Integer> set2Allowed;
+       
+
+       public SpeakerCallback_Skeleton(SpeakerCallback _mainObj, int _portSend, int _portRecv) throws Exception {
+               mainObj = _mainObj;
+               rmiComm = new IoTRMICommServer(_portSend, _portRecv);
+               set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
+               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 SpeakerCallback_Skeleton(SpeakerCallback _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {
+               mainObj = _mainObj;
+               rmiComm = _rmiComm;
+               objectId = _objectId;
+               set2Allowed = new ArrayList<Integer>(Arrays.asList(object2Permission));
+               didAlreadyInitWaitInvoke = new AtomicBoolean(false);
+               methodReceived = new AtomicBoolean(false);
+               rmiComm.registerSkeleton(objectId, methodReceived);
+       }
+
+       public boolean didAlreadyInitWaitInvoke() {
+               return didAlreadyInitWaitInvoke.get();
+       }
+
+       public void speakerDone() {
+               mainObj.speakerDone();
+       }
+
+       public void ___speakerDone() {
+               byte[] localMethodBytes = methodBytes;
+               rmiComm.setGetMethodBytes();
+               Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] {  }, new Class<?>[] {  }, localMethodBytes);
+               speakerDone();
+       }
+
+       public void ___waitRequestInvokeMethod() throws IOException {
+               didAlreadyInitWaitInvoke.compareAndSet(false, true);
+               while (true) {
+                       if (!methodReceived.get()) {
+                               continue;
+                       }
+                       methodBytes = rmiComm.getMethodBytes();
+                       methodReceived.set(false);
+                       int _objectId = IoTRMIComm.getObjectId(methodBytes);
+                       int methodId = IoTRMIComm.getMethodId(methodBytes);
+                       if (_objectId == objectId) {
+                               if (!set2Allowed.contains(methodId)) {
+                                       throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
+                               }
+                       }
+                       else {
+                               continue;
+                       }
+                       switch (methodId) {
+                               case 0:
+                               new Thread() {
+                                       public void run() {
+                                               try {
+                                                       ___speakerDone();
+                                               }
+                                               catch (Exception ex) {
+                                                       ex.printStackTrace();
+                                               }
+                                       }
+                               }.start();
+                               break;
+                               default: 
+                               throw new Error("Method Id " + methodId + " not recognized!");
+                       }
+               }
+       }
+
+}