Preparing 3rd benchmark for porting with capability-based RMI
[iot2.git] / benchmarks / drivers / GPSPhoneGateway / GPSPhoneGateway.java
1 package iotcode.GPSPhoneGateway;
2
3 // Java standard library
4 import java.util.ArrayList;
5 import java.util.concurrent.atomic.AtomicBoolean;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.net.UnknownHostException;
9
10 // RMI Packages
11 import java.rmi.Remote;
12 import java.rmi.RemoteException;
13
14 // IoTRuntime library
15 import iotruntime.stub.IoTRemoteCall;
16 import iotruntime.slave.IoTSet;
17 import iotruntime.slave.IoTDeviceAddress;
18 import iotcode.annotation.*;
19 import iotcode.interfaces.*;
20
21 // Checker annotations
22 //import iotchecker.qual.*;
23
24 /** GPSPhoneGateway that uses IoTRemoteCall and PhoneInfo class
25  *  to get information from a phone app
26  *
27  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
28  * @version     1.0                
29  * @since       2016-04-27
30  */
31 public class GPSPhoneGateway implements GPSGateway {
32
33         /**
34          * PhoneGateway class properties
35          */
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;
43
44         @config private IoTSet<IoTDeviceAddress> gps_address;
45
46         /**
47          * Constructor
48          */
49         public GPSPhoneGateway() throws RemoteException {
50         }
51
52         /**
53          * Init() function
54          */
55         public void init() {
56
57                 // Get address
58                 Iterator it = gps_address.iterator();
59                 iotDevAdd = (IoTDeviceAddress) it.next();
60 //              try {
61 //                      iotDevAdd = new IoTDeviceAddress("192.168.2.100", 1234, 8000);
62 //              } catch (Exception ex) {
63 //              }
64                 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
65                 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
66                 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
67
68                 // Get server
69                 phoneInfo = new PhoneInfo();
70                 listPGWCallback = new ArrayList<GPSGatewaySmartCallback>();
71                 doEnd = new AtomicBoolean(false);
72
73                 // Threads
74                 callbackThread = null;
75                 workerThread = null;
76         }
77
78         /**
79          * Start() function to start threads
80          */
81         public void start() {
82                 doEnd.set(false);
83
84                 // Launch IoTRemoteCall server in a separate thread
85                 workerThread = new Thread(new Runnable() {
86                         public void run() {
87                                 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class, 
88                                         phoneInfo, iotDevAdd.getDestinationPortNumber(),
89                                         IoTDeviceAddress.getLocalHostAddress());
90                         }
91                 });
92                 workerThread.start();
93                 System.out.println("GPSPhoneGateway: Worker thread started!");
94
95                 callbackThread = new Thread(new Runnable() {
96                         public void run() {
97                                 doCallbacks();
98                         }
99                 });
100                 callbackThread.start();
101                 System.out.println("GPSPhoneGateway: Callback thread started!");
102         }
103
104         /**
105          * Stop() function to stop threads
106          */
107         public void stop() {
108                 doEnd.set(true);
109
110                 try {
111                         callbackThread.join();
112                         workerThread.join();
113                 } catch (Exception e) {
114                         e.printStackTrace();
115                 }
116         }
117
118         /**
119          * Register callbacks
120          */
121         public void registerCallback(GPSGatewaySmartCallback _c) {
122                 listPGWCallback.add(_c);
123         }
124
125         /**
126          * Do callbacks
127          */
128         private void doCallbacks() {
129
130                 while (!doEnd.get()) {
131
132                         for (GPSGatewaySmartCallback c : listPGWCallback) {
133
134                                 //try {
135                                         // Only call back if there is new data  
136                                         if (phoneInfo.isNewRoomIDAvailable()) {
137
138                                                 System.out.println("GPSPhoneGateway: new room ID available - call back!");
139                                                 // Call back!
140                                                 //c.newRoomIDRetrieved(this);
141                                                 c.newRoomIDRetrieved(this.getRoomID());
142                                                 //this.setNewRoomIDAvailable(false);
143
144                                                 // Set back to false after reading
145                                                 phoneInfo.setNewRoomIDAvailable(false);
146
147                                         } else if (phoneInfo.isNewRingStatusAvailable()) {
148
149                                                 System.out.println("GPSPhoneGateway: new ring status available - call back!");
150                                                 // Call back!
151                                                 //c.newRingStatusRetrieved(this);
152                                                 c.newRingStatusRetrieved(this.getRingStatus());
153                                                 //this.setNewRingStatusAvailable(false);
154
155                                                 // Set back to false after reading
156                                                 phoneInfo.setNewRingStatusAvailable(false);
157                                         }
158
159                                 //} catch (RemoteException ex) {
160                                 //      ex.printStackTrace();
161                                 //}
162                         }
163                 }
164         }
165
166         /**
167          * Simply return phoneInfo.iRoomIdentifier
168          */
169         public int getRoomID() {
170
171                 return phoneInfo.getRoomID();
172         }
173
174         /**
175          * Simply return phoneInfo.bRingStatus
176          */
177         public boolean getRingStatus() {
178
179                 return phoneInfo.getRingStatus();
180         }
181
182         /**
183          * Set phoneInfo.bNewRoomIDAvail
184          */
185         public void setNewRoomIDAvailable(boolean bValue) {
186
187                 phoneInfo.setNewRoomIDAvailable(bValue);
188         }
189
190         /**
191          * Set phoneInfo.bNewRingStatusAvail
192          */
193         public void setNewRingStatusAvailable(boolean bValue) {
194
195                 phoneInfo.setNewRingStatusAvailable(bValue);
196         }
197
198 /*      public static void main(String[] args) throws UnknownHostException, RemoteException {
199
200                 @LocalRemote GPSPhoneGateway gpg = new @LocalRemote GPSPhoneGateway();
201                 gpg.init();
202                 gpg.start();
203         }*/
204 }