Cleaning up benchmarks and drivers code.
[iot2.git] / benchmarks / drivers / Java / 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
22 /** GPSPhoneGateway that uses IoTRemoteCall and PhoneInfo class
23  *  to get information from a phone app
24  *
25  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
26  * @version     1.0                
27  * @since       2016-04-27
28  */
29 public class GPSPhoneGateway implements GPSGateway {
30
31         /**
32          * PhoneGateway class properties
33          */
34         private PhoneInfo phoneInfo;
35         private IoTRemoteCall iotRemCall;
36         private List<GPSGatewaySmartCallback> listPGWCallback;
37         private AtomicBoolean doEnd;
38         private Thread callbackThread;
39         private Thread workerThread;
40         private IoTDeviceAddress iotDevAdd;
41
42         @config private IoTSet<IoTDeviceAddress> gps_address;
43
44         /**
45          * Constructor
46          */
47         public GPSPhoneGateway() throws RemoteException {
48         }
49
50         /**
51          * Init() function
52          */
53         public void init() {
54
55                 // Get address
56                 Iterator it = gps_address.iterator();
57                 iotDevAdd = (IoTDeviceAddress) it.next();
58                 System.out.println("Address: " + iotDevAdd.getCompleteAddress());
59                 System.out.println("Source port: " + iotDevAdd.getSourcePortNumber());
60                 System.out.println("Destination port: " + iotDevAdd.getDestinationPortNumber());
61
62                 // Get server
63                 phoneInfo = new PhoneInfo();
64                 listPGWCallback = new ArrayList<GPSGatewaySmartCallback>();
65                 doEnd = new AtomicBoolean(false);
66
67                 // Threads
68                 callbackThread = null;
69                 workerThread = null;
70         }
71
72         /**
73          * Start() function to start threads
74          */
75         public void start() {
76                 doEnd.set(false);
77
78                 // Launch IoTRemoteCall server in a separate thread
79                 workerThread = new Thread(new Runnable() {
80                         /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
81                         This disrupts the usual setting for self-signed certificate
82                         See this link for more info: https://github.com/owntracks/android/issues/481
83                         public void run() {
84                                 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class, 
85                                         phoneInfo, iotDevAdd.getDestinationPortNumber(),
86                                         IoTDeviceAddress.getLocalHostAddress());
87                         }*/
88                         public void run() {
89                                 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class, 
90                                         phoneInfo, iotDevAdd.getDestinationPortNumber());
91                         }
92                 });
93                 workerThread.start();
94                 System.out.println("GPSPhoneGateway: Worker thread started!");
95
96                 callbackThread = new Thread(new Runnable() {
97                         public void run() {
98                                 doCallbacks();
99                         }
100                 });
101                 callbackThread.start();
102                 System.out.println("GPSPhoneGateway: Callback thread started!");
103         }
104
105         /**
106          * Stop() function to stop threads
107          */
108         public void stop() {
109                 doEnd.set(true);
110
111                 try {
112                         callbackThread.join();
113                         workerThread.join();
114                 } catch (Exception e) {
115                         e.printStackTrace();
116                 }
117         }
118
119         /**
120          * Register callbacks
121          */
122         public void registerCallback(GPSGatewaySmartCallback _c) {
123                 listPGWCallback.add(_c);
124         }
125
126         /**
127          * Do callbacks
128          */
129         private void doCallbacks() {
130
131                 while (!doEnd.get()) {
132
133                         for (GPSGatewaySmartCallback c : listPGWCallback) {
134
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.getRingStatus());
152
153                                         // Set back to false after reading
154                                         phoneInfo.setNewRingStatusAvailable(false);
155                                 }
156                         }
157                 }
158         }
159
160         /**
161          * Simply return phoneInfo.iRoomIdentifier
162          */
163         public int getRoomID() {
164
165                 return phoneInfo.getRoomID();
166         }
167
168         /**
169          * Simply return phoneInfo.bRingStatus
170          */
171         public boolean getRingStatus() {
172
173                 return phoneInfo.getRingStatus();
174         }
175
176         /**
177          * Set phoneInfo.bNewRoomIDAvail
178          */
179         public void setNewRoomIDAvailable(boolean bValue) {
180
181                 phoneInfo.setNewRoomIDAvailable(bValue);
182         }
183
184         /**
185          * Set phoneInfo.bNewRingStatusAvail
186          */
187         public void setNewRingStatusAvailable(boolean bValue) {
188
189                 phoneInfo.setNewRingStatusAvailable(bValue);
190         }
191 }
192