1   /* Copyright 2002-2021 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.gnss.metric.ntrip;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.net.Authenticator;
24  import java.net.HttpURLConnection;
25  import java.net.InetAddress;
26  import java.net.InetSocketAddress;
27  import java.net.Proxy;
28  import java.net.Proxy.Type;
29  import java.net.SocketAddress;
30  import java.net.URL;
31  import java.net.URLConnection;
32  import java.net.UnknownHostException;
33  import java.nio.charset.StandardCharsets;
34  import java.util.ArrayList;
35  import java.util.Formatter;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  import java.util.concurrent.ExecutorService;
41  import java.util.concurrent.Executors;
42  import java.util.concurrent.TimeUnit;
43  import java.util.concurrent.atomic.AtomicReference;
44  
45  import org.orekit.errors.OrekitException;
46  import org.orekit.errors.OrekitMessages;
47  import org.orekit.gnss.metric.messages.ParsedMessage;
48  
49  /** Source table for ntrip streams retrieval.
50   * <p>
51   * Note that all authentication is performed automatically by just
52   * calling the standard {@link Authenticator#setDefault(Authenticator)}
53   * method to set up an authenticator.
54   * </p>
55   * @author Luc Maisonobe
56   * @since 11.0
57   */
58  public class NtripClient {
59  
60      /** Default timeout for connections and reads (ms). */
61      public static final int DEFAULT_TIMEOUT = 10000;
62  
63      /** Default port for ntrip communication. */
64      public static final int DEFAULT_PORT = 2101;
65  
66      /** Default delay before we reconnect after connection close (s). */
67      public static final double DEFAULT_RECONNECT_DELAY = 1.0;
68  
69      /** Default factor by which reconnection delay is multiplied after each attempt. */
70      public static final double DEFAULT_RECONNECT_DELAY_FACTOR = 1.5;
71  
72      /** Default maximum number of reconnect a attempts without readin any data. */
73      public static final int DEFAULT_MAX_RECONNECT = 20;
74  
75      /** Host header. */
76      private static final String HOST_HEADER_KEY = "Host";
77  
78      /** User-agent header key. */
79      private static final String USER_AGENT_HEADER_KEY = "User-Agent";
80  
81      /** User-agent header value. */
82      private static final String USER_AGENT_HEADER_VALUE = "NTRIP orekit/11.0";
83  
84      /** Version header key. */
85      private static final String VERSION_HEADER_KEY = "Ntrip-Version";
86  
87      /** Version header value. */
88      private static final String VERSION_HEADER_VALUE = "Ntrip/2.0";
89  
90      /** Connection header key. */
91      private static final String CONNECTION_HEADER_KEY = "Connection";
92  
93      /** Connection header value. */
94      private static final String CONNECTION_HEADER_VALUE = "close";
95  
96      /** Flags header key. */
97      private static final String FLAGS_HEADER_KEY = "Ntrip-Flags";
98  
99      /** Content type for source table. */
100     private static final String SOURCETABLE_CONTENT_TYPE = "gnss/sourcetable";
101 
102     /** Degrees to arc minutes conversion factor. */
103     private static final double DEG_TO_MINUTES = 60.0;
104 
105     /** Caster host. */
106     private final String host;
107 
108     /** Caster port. */
109     private final int port;
110 
111     /** Delay before we reconnect after connection close. */
112     private double reconnectDelay;
113 
114     /** Multiplication factor for reconnection delay. */
115     private double reconnectDelayFactor;
116 
117     /** Max number of reconnections. */
118     private int maxRetries;
119 
120     /** Timeout for connections and reads. */
121     private int timeout;
122 
123     /** Proxy to use. */
124     private Proxy proxy;
125 
126     /** NMEA GGA sentence (may be null). */
127     private AtomicReference<String> gga;
128 
129     /** Observers for encoded messages. */
130     private final List<ObserverHolder> observers;
131 
132     /** Monitors for data streams. */
133     private final Map<String, StreamMonitor> monitors;
134 
135     /** Source table. */
136     private SourceTable sourceTable;
137 
138     /** Executor for stream monitoring tasks. */
139     private ExecutorService executorService;
140 
141     /** Build a client for NTRIP.
142      * <p>
143      * The default configuration uses default timeout, default reconnection
144      * parameters, no GPS fix and no proxy.
145      * </p>
146      * @param host caster host providing the source table
147      * @param port port to use for connection
148      * see {@link #DEFAULT_PORT}
149      */
150     public NtripClient(final String host, final int port) {
151         this.host         = host;
152         this.port         = port;
153         this.observers    = new ArrayList<>();
154         this.monitors     = new HashMap<>();
155         setTimeout(DEFAULT_TIMEOUT);
156         setReconnectParameters(DEFAULT_RECONNECT_DELAY,
157                                DEFAULT_RECONNECT_DELAY_FACTOR,
158                                DEFAULT_MAX_RECONNECT);
159         setProxy(Type.DIRECT, null, -1);
160         this.gga             = new AtomicReference<String>(null);
161         this.sourceTable     = null;
162         this.executorService = null;
163     }
164 
165     /** Get the caster host.
166      * @return caster host
167      */
168     public String getHost() {
169         return host;
170     }
171 
172     /** Get the port to use for connection.
173      * @return port to use for connection
174      */
175     public int getPort() {
176         return port;
177     }
178 
179     /** Set timeout for connections and reads.
180      * @param timeout timeout for connections and reads (ms)
181      */
182     public void setTimeout(final int timeout) {
183         this.timeout = timeout;
184     }
185 
186     /** Set Reconnect parameters.
187      * @param delay delay before we reconnect after connection close
188      * @param delayFactor factor by which reconnection delay is multiplied after each attempt
189      * @param max max number of reconnect a attempts without reading any data
190      */
191     public void setReconnectParameters(final double delay,
192                                        final double delayFactor,
193                                        final int max) {
194         this.reconnectDelay       = delay;
195         this.reconnectDelayFactor = delayFactor;
196         this.maxRetries           = max;
197     }
198 
199     /** Set proxy parameters.
200      * @param type proxy type
201      * @param proxyHost host name of the proxy (ignored if {@code type} is {@code Proxy.Type.DIRECT})
202      * @param proxyPort port number of the proxy (ignored if {@code type} is {@code Proxy.Type.DIRECT})
203      */
204     public void setProxy(final Proxy.Type type, final String proxyHost, final int proxyPort) {
205         try {
206             if (type == Proxy.Type.DIRECT) {
207                 // disable proxy
208                 proxy = Proxy.NO_PROXY;
209             } else {
210                 // enable proxy
211                 final InetAddress   hostAddress  = InetAddress.getByName(proxyHost);
212                 final SocketAddress proxyAddress = new InetSocketAddress(hostAddress, proxyPort);
213                 proxy = new Proxy(type, proxyAddress);
214             }
215         } catch (UnknownHostException uhe) {
216             throw new OrekitException(uhe, OrekitMessages.UNKNOWN_HOST, proxyHost);
217         }
218     }
219 
220     /** Get proxy.
221      * @return proxy to use
222      */
223     public Proxy getProxy() {
224         return proxy;
225     }
226 
227     /** Set GPS fix data to send as NMEA sentence to Ntrip caster if required.
228      * @param hour hour of the fix (UTC time)
229      * @param minute minute of the fix (UTC time)
230      * @param second second of the fix (UTC time)
231      * @param latitude latitude (radians)
232      * @param longitude longitude (radians)
233      * @param ellAltitude altitude above ellipsoid (m)
234      * @param undulation height of the geoid above ellipsoid (m)
235      */
236     public void setFix(final int hour, final int minute, final double second,
237                        final double latitude, final double longitude, final double ellAltitude,
238                        final double undulation) {
239 
240         // convert latitude
241         final double latDeg = Math.abs(Math.toDegrees(latitude));
242         final int    dLat   = (int) Math.floor(latDeg);
243         final double mLat   = DEG_TO_MINUTES * (latDeg - dLat);
244         final char   cLat   = latitude >= 0.0 ? 'N' : 'S';
245 
246         // convert longitude
247         final double lonDeg = Math.abs(Math.toDegrees(longitude));
248         final int    dLon   = (int) Math.floor(lonDeg);
249         final double mLon   = DEG_TO_MINUTES * (lonDeg - dLon);
250         final char   cLon   = longitude >= 0.0 ? 'E' : 'W';
251 
252         // build NMEA GGA sentence
253         final StringBuilder builder = new StringBuilder(82);
254         try (Formatter formatter = new Formatter(builder, Locale.US)) {
255 
256             // dummy values
257             final int    fixQuality = 1;
258             final int    nbSat      = 4;
259             final double hdop       = 1.0;
260 
261             // sentence body
262             formatter.format("$GPGGA,%02d%02d%06.3f,%02d%07.4f,%c,%02d%07.4f,%c,%1d,%02d,%3.1f,%.1f,M,%.1f,M,,",
263                              hour, minute, second,
264                              dLat, mLat, cLat, dLon, mLon, cLon,
265                              fixQuality, nbSat, hdop,
266                              ellAltitude, undulation);
267 
268             // checksum
269             byte sum = 0;
270             for (int i = 1; i < builder.length(); ++i) {
271                 sum ^= builder.charAt(i);
272             }
273             formatter.format("*%02X", sum);
274 
275         }
276         gga.set(builder.toString());
277 
278     }
279 
280     /** Get NMEA GGA sentence.
281      * @return NMEA GGA sentence (may be null)
282      */
283     String getGGA() {
284         return gga.get();
285     }
286 
287     /** Add an observer for an encoded messages.
288      * <p>
289      * If messages of the specified type have already been retrieved from
290      * a stream, the observer will be immediately notified with the last
291      * message from each mount point (in unspecified order) as a side effect
292      * of being added.
293      * </p>
294      * @param typeCode code for the message type (if set to 0, notification
295      * will be triggered regardless of message type)
296      * @param mountPoint mountPoint from which data must come (if null, notification
297      * will be triggered regardless of mount point)
298      * @param observer observer for this message type
299      */
300     public void addObserver(final int typeCode, final String mountPoint,
301                             final MessageObserver observer) {
302 
303         // store the observer for future monitored mount points
304         observers.add(new ObserverHolder(typeCode, mountPoint, observer));
305 
306         // check if we should also add it to already monitored mount points
307         for (Map.Entry<String, StreamMonitor> entry : monitors.entrySet()) {
308             if (mountPoint == null || mountPoint.equals(entry.getKey())) {
309                 entry.getValue().addObserver(typeCode, observer);
310             }
311         }
312 
313     }
314 
315     /** Get a sourcetable.
316      * @return source table from the caster
317      */
318     public SourceTable getSourceTable() {
319         if (sourceTable == null) {
320             try {
321 
322                 // perform request
323                 final HttpURLConnection connection = connect("");
324 
325                 final int responseCode = connection.getResponseCode();
326                 if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
327                     throw new OrekitException(OrekitMessages.FAILED_AUTHENTICATION, "caster");
328                 } else if (responseCode != HttpURLConnection.HTTP_OK) {
329                     throw new OrekitException(OrekitMessages.CONNECTION_ERROR, host, connection.getResponseMessage());
330                 }
331 
332                 // for this request, we MUST get a source table
333                 if (!SOURCETABLE_CONTENT_TYPE.equals(connection.getContentType())) {
334                     throw new OrekitException(OrekitMessages.UNEXPECTED_CONTENT_TYPE, connection.getContentType());
335                 }
336 
337                 final SourceTable table = new SourceTable(getHeaderValue(connection, FLAGS_HEADER_KEY));
338 
339                 // parse source table records
340                 try (InputStream is = connection.getInputStream();
341                      InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
342                      BufferedReader br = new BufferedReader(isr)) {
343                     int lineNumber = 0;
344                     for (String line = br.readLine(); line != null; line = br.readLine()) {
345 
346                         ++lineNumber;
347                         line = line.trim();
348                         if (line.length() == 0) {
349                             continue;
350                         }
351 
352                         if (line.startsWith(RecordType.CAS.toString())) {
353                             table.addCasterRecord(new CasterRecord(line));
354                         } else if (line.startsWith(RecordType.NET.toString())) {
355                             table.addNetworkRecord(new NetworkRecord(line));
356                         } else if (line.startsWith(RecordType.STR.toString())) {
357                             table.addDataStreamRecord(new DataStreamRecord(line));
358                         } else if (line.startsWith("ENDSOURCETABLE")) {
359                             // we have reached end of table
360                             break;
361                         } else {
362                             throw new OrekitException(OrekitMessages.SOURCETABLE_PARSE_ERROR,
363                                                       connection.getURL().getHost(), lineNumber, line);
364                         }
365 
366                     }
367                 }
368 
369                 sourceTable = table;
370                 return table;
371 
372             } catch (IOException ioe) {
373                 throw new OrekitException(ioe, OrekitMessages.CANNOT_PARSE_SOURCETABLE, host);
374             }
375         }
376 
377         return sourceTable;
378 
379     }
380 
381     /** Connect to a mount point and start streaming data from it.
382      * <p>
383      * This method sets up an internal dedicated thread for continuously
384      * monitoring data incoming from a mount point. When new complete
385      * {@link ParsedMessage parsed messages} becomes available, the
386      * {@link MessageObserver observers} that have been registered
387      * using {@link #addObserver(int, String, MessageObserver) addObserver()}
388      * method will be notified about the message.
389      * </p>
390      * <p>
391      * This method must be called once for each stream to monitor.
392      * </p>
393      * @param mountPoint mount point providing the stream
394      * @param type messages type of the mount point
395      * @param requiresNMEA if true, the mount point requires a NMEA GGA sentence in the request
396      * @param ignoreUnknownMessageTypes if true, unknown messages types are silently ignored
397      */
398     public void startStreaming(final String mountPoint, final org.orekit.gnss.metric.ntrip.Type type,
399                                final boolean requiresNMEA, final boolean ignoreUnknownMessageTypes) {
400 
401         if (executorService == null) {
402             // lazy creation of executor service, with one thread for each possible data stream
403             executorService = Executors.newFixedThreadPool(getSourceTable().getDataStreams().size());
404         }
405 
406         // safety check
407         if (monitors.containsKey(mountPoint)) {
408             throw new OrekitException(OrekitMessages.MOUNPOINT_ALREADY_CONNECTED, mountPoint);
409         }
410 
411         // create the monitor
412         final StreamMonitor monitor = new StreamMonitor(this, mountPoint, type, requiresNMEA, ignoreUnknownMessageTypes,
413                                                         reconnectDelay, reconnectDelayFactor, maxRetries);
414         monitors.put(mountPoint, monitor);
415 
416         // set up the already known observers
417         for (final ObserverHolder observerHolder : observers) {
418             if (observerHolder.mountPoint == null ||
419                 observerHolder.mountPoint.equals(mountPoint)) {
420                 monitor.addObserver(observerHolder.typeCode, observerHolder.observer);
421             }
422         }
423 
424         // start streaming data
425         executorService.execute(monitor);
426 
427     }
428 
429     /** Check if any of the streaming thread has thrown an exception.
430      * <p>
431      * If a streaming thread has thrown an exception, it will be rethrown here
432      * </p>
433      */
434     public void checkException() {
435         // check if any of the stream got an exception
436         for (final  Map.Entry<String, StreamMonitor> entry : monitors.entrySet()) {
437             final OrekitException exception = entry.getValue().getException();
438             if (exception != null) {
439                 throw exception;
440             }
441         }
442     }
443 
444     /** Stop streaming data from all connected mount points.
445      * <p>
446      * If an exception was encountered during data streaming, it will be rethrown here
447      * </p>
448      * @param time timeout for waiting underlying threads termination (ms)
449      */
450     public void stopStreaming(final int time) {
451 
452         // ask all monitors to stop retrieving data
453         for (final  Map.Entry<String, StreamMonitor> entry : monitors.entrySet()) {
454             entry.getValue().stopMonitoring();
455         }
456 
457         try {
458             // wait for proper ending
459             executorService.awaitTermination(time, TimeUnit.MILLISECONDS);
460         } catch (InterruptedException ie) {
461             // Restore interrupted state...
462             Thread.currentThread().interrupt();
463         }
464 
465         checkException();
466 
467     }
468 
469     /** Connect to caster.
470      * @param mountPoint mount point (empty for getting sourcetable)
471      * @return performed connection
472      * @throws IOException if an I/O exception occurs during connection
473      */
474     HttpURLConnection connect(final String mountPoint)
475         throws IOException {
476 
477         // set up connection
478         final String protocol = "http";
479         final URL casterURL = new URL(protocol, host, port, "/" + mountPoint);
480         final HttpURLConnection connection = (HttpURLConnection) casterURL.openConnection(proxy);
481         connection.setConnectTimeout(timeout);
482         connection.setReadTimeout(timeout);
483 
484         // common headers
485         connection.setRequestProperty(HOST_HEADER_KEY,       host);
486         connection.setRequestProperty(VERSION_HEADER_KEY,    VERSION_HEADER_VALUE);
487         connection.setRequestProperty(USER_AGENT_HEADER_KEY, USER_AGENT_HEADER_VALUE);
488         connection.setRequestProperty(CONNECTION_HEADER_KEY, CONNECTION_HEADER_VALUE);
489 
490         return connection;
491 
492     }
493 
494     /** Get an header from a response.
495      * @param connection connection to analyze
496      * @param key header key
497      * @return header value
498      */
499     private String getHeaderValue(final URLConnection connection, final String key) {
500         final String value = connection.getHeaderField(key);
501         if (value == null) {
502             throw new OrekitException(OrekitMessages.MISSING_HEADER,
503                                       connection.getURL().getHost(), key);
504         }
505         return value;
506     }
507 
508     /** Local holder for observers. */
509     private static class ObserverHolder {
510 
511         /** Code for the message type. */
512         private final int typeCode;
513 
514         /** Mount point. */
515         private final String mountPoint;
516 
517         /** Observer to notify. */
518         private final MessageObserver observer;
519 
520         /** Simple constructor.
521          * @param typeCode code for the message type
522          * @param mountPoint mountPoint from which data must come (if null, notification
523          * will be triggered regardless of mount point)
524          * @param observer observer for this message type
525          */
526         ObserverHolder(final int typeCode, final String mountPoint,
527                             final MessageObserver observer) {
528             this.typeCode   = typeCode;
529             this.mountPoint = mountPoint;
530             this.observer   = observer;
531         }
532 
533     }
534 
535 }