Preparing 3rd benchmark for porting with capability-based RMI
[iot2.git] / benchmarks / SpeakerController / SpeakerController.java
index 17fcd67f2fe0ad08d3ceb4097dfcd49107a6c98d..00b6dbaf5d0d8dccfd6275dca269e21d42023f80 100644 (file)
@@ -7,7 +7,7 @@ import iotruntime.slave.IoTRelation;
 
 // IoT driver packages
 import iotcode.interfaces.*;
-//import iotcode.annotation.*;
+import iotcode.annotation.*;
 
 
 // Standard Java packages
@@ -26,7 +26,7 @@ import java.rmi.RemoteException;
 import java.rmi.server.UnicastRemoteObject;
 
 // Checker annotations
-import iotchecker.qual.*;
+//import iotchecker.qual.*;
 
 /** Class SpeakerController for the smart home application benchmark
  *  <p>
@@ -50,23 +50,23 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
        /**
         * IoT Sets of Devices
         */
-       @config private IoTSet < @NonLocalRemote GPSGateway > gpsSet;
-       @config private IoTSet < @NonLocalRemote Speaker > speakerSet;
+       @config private IoTSet < GPSGatewaySmart > gpsSet;
+       @config private IoTSet < SpeakerSmart > speakerSet;
 
        /**
         * IoT Sets of Things that are not devices such as rooms
         */
-       @config private IoTSet < @NonLocalRemote Room > audioRooms;
+       @config private IoTSet < RoomSmart > audioRooms;
 
        /**
         * IoT Relations
         */
-       @config private IoTRelation < @NonLocalRemote Room, @NonLocalRemote Speaker > roomSpeakerRel;
+       @config private IoTRelation < RoomSmart, SpeakerSmart > roomSpeakerRel;
 
        /**
         * The state that the room main lights are supposed to be in
         */
-       Map < @NonLocalRemote Room, Boolean > roomSpeakersOnOffStatus = new HashMap < @NonLocalRemote Room, Boolean > ();
+       Map < RoomSmart, Boolean > roomSpeakersOnOffStatus = new HashMap < RoomSmart, Boolean > ();
 
        // used to notify if new data is available
        private AtomicBoolean newDataAvailable = new AtomicBoolean(false);
@@ -87,22 +87,13 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
        /** Callback method for when room ID is retrieved.
         *
-        * @param _ggw [GPSGateway].
+        * @param _roomIdentifier [int].
         * @return [void] None.
         */
-       public void newRoomIDRetrieved(@NonLocalRemote GPSGateway _ggw) {
-
-               try {
-                       // get the parameters that the interface (phone app) reads from the user
-                       roomIdentifier = _ggw.getRoomID();
+       public void newRoomIDRetrieved(int _roomIdentifier) {
 
-                       System.out.println("DEBUG: New room ID is retrieved from phone!!! Room: " + roomIdentifier);
-
-                       // Data is read, so we set this back to false
-                       _ggw.setNewRoomIDAvailable(false);
-               } catch (RemoteException ex) {
-                       ex.printStackTrace();
-               }
+               roomIdentifier = _roomIdentifier;
+               System.out.println("DEBUG: New room ID is retrieved from phone!!! Room: " + roomIdentifier);
 
                // new data available so set it to true
                newDataAvailable.set(true);
@@ -111,22 +102,13 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
        /** Callback method for when ring status is retrieved.
         *
-        * @param _ggw [GPSGateway].
+        * @param _ringStatus [boolean].
         * @return [void] None.
         */
-       public void newRingStatusRetrieved(@NonLocalRemote GPSGateway _ggw) {
-
-               try {
-                       // get the parameters that the interface (phone app) reads from the user
-                       ringStatus = _ggw.getRingStatus();
+       public void newRingStatusRetrieved(boolean _ringStatus) {
 
-                       System.out.println("DEBUG: New ring status is retrieved from phone!!! Status: " + ringStatus);
-
-                       // Data is read, so we set this back to false
-                       _ggw.setNewRingStatusAvailable(false);
-               } catch (RemoteException ex) {
-                       ex.printStackTrace();
-               }
+               ringStatus = _ringStatus;
+               System.out.println("DEBUG: New ring status is retrieved from phone!!! Status: " + ringStatus);
 
                // new data available so set it to true
                newDataAvailable.set(true);
@@ -135,11 +117,10 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
        /** Callback method when a speaker has finished playing what is in its audio buffer.
         *
-        * @param _speaker [Speaker].
         * @return [void] None.
         */
-       public void speakerDone(@NonLocalRemote Speaker _speaker) {
-               for (@NonLocalRemote Speaker speakers : speakerSet.values()) {
+       public void speakerDone() {
+               for (SpeakerSmart speakers : speakerSet.values()) {
                        playbackDone.set(true);
                }
        }
@@ -157,7 +138,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
        private void updateSpeakersAction() {
 
                // Stream music on speakers based on their status
-               for (@NonLocalRemote Room room : audioRooms.values()) {
+               for (RoomSmart room : audioRooms.values()) {
 
                        // Check status of the room
                        if (roomSpeakersOnOffStatus.get(room)) {
@@ -168,15 +149,15 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
                                // Get the speaker objects one by one assuming that we could have
                                // more than one speaker per room
-                               for (@NonLocalRemote Speaker speakers : roomSpeakerRel.get(room)) {
+                               for (SpeakerSmart speakers : roomSpeakerRel.get(room)) {
                                        // System.out.println("DEBUG: Turn on speaker!");
 
-                                       try {
+                                       //try {
 
                                                // start the speaker playback if the speaker is not playing yet
                                                if (!speakers.getPlaybackState()) {
 
-                                                       System.out.println("Turning a speaker On");
+                                                       System.out.println("Turning a speaker On in room: " + room.getRoomID());
                                                        speakers.startPlayback();
                                                        speakers.setPosition(currentPosition);
 
@@ -187,9 +168,9 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                                                }
 
 
-                                       } catch (RemoteException e) {
-                                               e.printStackTrace();
-                                       }
+                                       //} catch (RemoteException e) {
+                                       //      e.printStackTrace();
+                                       //}
                                }
 
                                if (currPosCount != 0) {
@@ -201,12 +182,12 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                                        if (Math.abs(currentPosOfSpeakers - currentPosition) > CURRENT_POSITION_SAMPLE_THRESHOLD) {
                                                // we were kind of far so update all the positions
 
-                                               for (@NonLocalRemote Speaker speakers : roomSpeakerRel.get(room)) {
-                                                       try {
+                                               for (SpeakerSmart speakers : roomSpeakerRel.get(room)) {
+                                                       //try {
                                                                speakers.setPosition(currentPosOfSpeakers);
-                                                       } catch (RemoteException e) {
-                                                               e.printStackTrace();
-                                                       }
+                                                       //} catch (RemoteException e) {
+                                                       //      e.printStackTrace();
+                                                       //}
                                                }
                                        }
 
@@ -216,7 +197,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
 
 
-                       } else {
+                       } /*else {
                                // Room status is "off"
 
                                // used to get the average of the speakers position
@@ -225,7 +206,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
                                // Get the speaker objects one by one assuming that we could have
                                // more than one speaker per room
-                               for (@NonLocalRemote Speaker speakers : roomSpeakerRel.get(room)) {
+                               for (Speaker speakers : roomSpeakerRel.get(room)) {
                                        // System.out.println("DEBUG: Turn off speaker!");
                                        try {
                                                // Turning off speaker if they are still on
@@ -251,7 +232,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                                if (currPosCount != 0) {
                                        currentPosition = (int)(currPosTotal / currPosCount);
                                }
-                       }
+                       }*/
                }
 
                // a speaker has finished playing and so we should change all the audio buffers
@@ -278,7 +259,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                        if (ringStatus) {
 
                                // Turn off all speakers if ring status is true (phone is ringing)
-                               for (@NonLocalRemote Room room : audioRooms.values()) {
+                               for (RoomSmart room : audioRooms.values()) {
 
                                        // System.out.println("DEBUG: Update status off for speakers! Phone is ringing!!!");
                                        // Turn off speaker
@@ -289,9 +270,9 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                                // Phone is not ringing... just play music on the right speaker
 
                                // Check for every room
-                               for (@NonLocalRemote Room room : audioRooms.values()) {
+                               for (RoomSmart room : audioRooms.values()) {
 
-                                       try {
+                                       //try {
                                                // Turn on the right speaker based on room ID sent from phone app
                                                // Stream audio to a speaker based on room ID
                                                if (room.getRoomID() == roomIdentifier) {
@@ -309,9 +290,9 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                                                        roomSpeakersOnOffStatus.put(room, false);
                                                }
 
-                                       } catch (RemoteException ex) {
-                                               ex.printStackTrace();
-                                       }
+                                       //} catch (RemoteException ex) {
+                                       //      ex.printStackTrace();
+                                       //}
                                }
                        }
                        // Finish processing data - put this back to false
@@ -328,7 +309,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
                System.out.println("Stopping all device playback");
                // stop all devices that are still playing and clear their buffers
                // they are about to end playback anyways
-               for (@NonLocalRemote Speaker speakers : speakerSet.values()) {
+               for (SpeakerSmart speakers : speakerSet.values()) {
                        try {
 
                                if (speakers.getPlaybackState()) {
@@ -390,7 +371,7 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
 
                System.out.println("Loading Speakers");
                // send the new data to all the speakers
-               for (@NonLocalRemote Speaker speakers : speakerSet.values()) {
+               for (SpeakerSmart speakers : speakerSet.values()) {
                        System.out.println("Loading a single speaker with data");
                        try {
                                speakers.loadData(compressedArray, 0, compressedArray.length);
@@ -418,27 +399,27 @@ public class SpeakerController extends UnicastRemoteObject implements GPSGateway
        public void init() throws RemoteException, InterruptedException {
 
                // Initialize the rooms
-               for (@NonLocalRemote Room room : audioRooms.values()) {
+               for (RoomSmart room : audioRooms.values()) {
                        // All rooms start with the speakers turned off
                        roomSpeakersOnOffStatus.put(room, false);
                }
 
                // Setup the cameras, start them all and assign each one a motion detector
-               for (@NonLocalRemote GPSGateway gw : gpsSet.values()) {
+               for (GPSGatewaySmart gw : gpsSet.values()) {
 
-                       try {
+                       //try {
                                // initialize, register callback, and start the gateway
                                gw.init();
                                gw.registerCallback(this);
                                gw.start();
-                       } catch (RemoteException ex) {
-                               ex.printStackTrace();
-                       }
+                       //} catch (RemoteException ex) {
+                       //      ex.printStackTrace();
+                       //}
                }
 
 
                //Initialize the speakers
-               for (@NonLocalRemote Speaker speakers : speakerSet.values()) {
+               for (SpeakerSmart speakers : speakerSet.values()) {
                        speakers.init();
                }