Reverting local HTTP gateway back from HTTPS to HTTP; there was a change regarding...
[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 // 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                         /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
87                         This disrupts the usual setting for self-signed certificate
88                         See this link for more info: https://github.com/owntracks/android/issues/481
89                         public void run() {
90                                 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class, 
91                                         phoneInfo, iotDevAdd.getDestinationPortNumber(),
92                                         IoTDeviceAddress.getLocalHostAddress());
93                         }*/
94                         public void run() {
95                                 iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class, 
96                                         phoneInfo, iotDevAdd.getDestinationPortNumber());
97                         }
98                 });
99                 workerThread.start();
100                 System.out.println("GPSPhoneGateway: Worker thread started!");
101
102                 callbackThread = new Thread(new Runnable() {
103                         public void run() {
104                                 doCallbacks();
105                         }
106                 });
107                 callbackThread.start();
108                 System.out.println("GPSPhoneGateway: Callback thread started!");
109         }
110
111         /**
112          * Stop() function to stop threads
113          */
114         public void stop() {
115                 doEnd.set(true);
116
117                 try {
118                         callbackThread.join();
119                         workerThread.join();
120                 } catch (Exception e) {
121                         e.printStackTrace();
122                 }
123         }
124
125         /**
126          * Register callbacks
127          */
128         public void registerCallback(GPSGatewaySmartCallback _c) {
129                 listPGWCallback.add(_c);
130         }
131
132         /**
133          * Do callbacks
134          */
135         private void doCallbacks() {
136
137                 while (!doEnd.get()) {
138
139                         for (GPSGatewaySmartCallback c : listPGWCallback) {
140
141                                 //try {
142                                         // Only call back if there is new data  
143                                         if (phoneInfo.isNewRoomIDAvailable()) {
144
145                                                 System.out.println("GPSPhoneGateway: new room ID available - call back!");
146                                                 // Call back!
147                                                 //c.newRoomIDRetrieved(this);
148                                                 c.newRoomIDRetrieved(this.getRoomID());
149                                                 //this.setNewRoomIDAvailable(false);
150
151                                                 // Set back to false after reading
152                                                 phoneInfo.setNewRoomIDAvailable(false);
153
154                                         } else if (phoneInfo.isNewRingStatusAvailable()) {
155
156                                                 System.out.println("GPSPhoneGateway: new ring status available - call back!");
157                                                 // Call back!
158                                                 //c.newRingStatusRetrieved(this);
159                                                 c.newRingStatusRetrieved(this.getRingStatus());
160                                                 //this.setNewRingStatusAvailable(false);
161
162                                                 // Set back to false after reading
163                                                 phoneInfo.setNewRingStatusAvailable(false);
164                                         }
165
166                                 //} catch (RemoteException ex) {
167                                 //      ex.printStackTrace();
168                                 //}
169                         }
170                 }
171         }
172
173         /**
174          * Simply return phoneInfo.iRoomIdentifier
175          */
176         public int getRoomID() {
177
178                 return phoneInfo.getRoomID();
179         }
180
181         /**
182          * Simply return phoneInfo.bRingStatus
183          */
184         public boolean getRingStatus() {
185
186                 return phoneInfo.getRingStatus();
187         }
188
189         /**
190          * Set phoneInfo.bNewRoomIDAvail
191          */
192         public void setNewRoomIDAvailable(boolean bValue) {
193
194                 phoneInfo.setNewRoomIDAvailable(bValue);
195         }
196
197         /**
198          * Set phoneInfo.bNewRingStatusAvail
199          */
200         public void setNewRingStatusAvailable(boolean bValue) {
201
202                 phoneInfo.setNewRingStatusAvailable(bValue);
203         }
204
205 /*      public static void main(String[] args) throws UnknownHostException, RemoteException {
206
207                 @LocalRemote GPSPhoneGateway gpg = new @LocalRemote GPSPhoneGateway();
208                 gpg.init();
209                 gpg.start();
210         }*/
211 }