1 package iotcode.GPSPhoneGateway;
3 // Java standard library
4 import java.util.ArrayList;
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.Iterator;
8 import java.net.UnknownHostException;
11 import java.rmi.Remote;
12 import java.rmi.RemoteException;
15 import iotruntime.stub.IoTRemoteCall;
16 import iotruntime.slave.IoTSet;
17 import iotruntime.slave.IoTDeviceAddress;
18 import iotcode.annotation.*;
19 import iotcode.interfaces.*;
21 // Checker annotations
22 //import iotchecker.qual.*;
24 /** GPSPhoneGateway that uses IoTRemoteCall and PhoneInfo class
25 * to get information from a phone app
27 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
31 public class GPSPhoneGateway implements GPSGateway {
34 * PhoneGateway class properties
36 private PhoneInfo phoneInfo;
37 private IoTRemoteCall iotRemCall;
38 private List<GPSGatewaySmartCallback> listPGWCallback;
39 private AtomicBoolean doEnd;
40 private Thread callbackThread;
41 private Thread workerThread;
42 private IoTDeviceAddress iotDevAdd;
44 @config private IoTSet<IoTDeviceAddress> gps_address;
49 public GPSPhoneGateway() throws RemoteException {
58 Iterator it = gps_address.iterator();
59 iotDevAdd = (IoTDeviceAddress) it.next();
61 // iotDevAdd = new IoTDeviceAddress("192.168.2.100", 1234, 8000);
62 // } catch (Exception ex) {
64 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
65 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
66 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
69 phoneInfo = new PhoneInfo();
70 listPGWCallback = new ArrayList<GPSGatewaySmartCallback>();
71 doEnd = new AtomicBoolean(false);
74 callbackThread = null;
79 * Start() function to start threads
84 // Launch IoTRemoteCall server in a separate thread
85 workerThread = new Thread(new Runnable() {
87 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class,
88 phoneInfo, iotDevAdd.getDestinationPortNumber(),
89 IoTDeviceAddress.getLocalHostAddress());
93 System.out.println("GPSPhoneGateway: Worker thread started!");
95 callbackThread = new Thread(new Runnable() {
100 callbackThread.start();
101 System.out.println("GPSPhoneGateway: Callback thread started!");
105 * Stop() function to stop threads
111 callbackThread.join();
113 } catch (Exception e) {
121 public void registerCallback(GPSGatewaySmartCallback _c) {
122 listPGWCallback.add(_c);
128 private void doCallbacks() {
130 while (!doEnd.get()) {
132 for (GPSGatewaySmartCallback c : listPGWCallback) {
135 // Only call back if there is new data
136 if (phoneInfo.isNewRoomIDAvailable()) {
138 System.out.println("GPSPhoneGateway: new room ID available - call back!");
140 //c.newRoomIDRetrieved(this);
141 c.newRoomIDRetrieved(this.getRoomID());
142 //this.setNewRoomIDAvailable(false);
144 // Set back to false after reading
145 phoneInfo.setNewRoomIDAvailable(false);
147 } else if (phoneInfo.isNewRingStatusAvailable()) {
149 System.out.println("GPSPhoneGateway: new ring status available - call back!");
151 //c.newRingStatusRetrieved(this);
152 c.newRingStatusRetrieved(this.getRingStatus());
153 //this.setNewRingStatusAvailable(false);
155 // Set back to false after reading
156 phoneInfo.setNewRingStatusAvailable(false);
159 //} catch (RemoteException ex) {
160 // ex.printStackTrace();
167 * Simply return phoneInfo.iRoomIdentifier
169 public int getRoomID() {
171 return phoneInfo.getRoomID();
175 * Simply return phoneInfo.bRingStatus
177 public boolean getRingStatus() {
179 return phoneInfo.getRingStatus();
183 * Set phoneInfo.bNewRoomIDAvail
185 public void setNewRoomIDAvailable(boolean bValue) {
187 phoneInfo.setNewRoomIDAvailable(bValue);
191 * Set phoneInfo.bNewRingStatusAvail
193 public void setNewRingStatusAvailable(boolean bValue) {
195 phoneInfo.setNewRingStatusAvailable(bValue);
198 /* public static void main(String[] args) throws UnknownHostException, RemoteException {
200 @LocalRemote GPSPhoneGateway gpg = new @LocalRemote GPSPhoneGateway();