1 // ----------------------------------------------------------------------------
2 // kmlserver.h  --  KML Server
3 //
4 // Copyright (C) 2012
5 //		Remi Chateauneu, F4ECW
6 //
7 // This file is part of fldigi.
8 //
9 // Fldigi is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // Fldigi is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with fldigi.  If not, see <http://www.gnu.org/licenses/>.
21 // ----------------------------------------------------------------------------
22 
23 #ifndef _KMLSERVER_H
24 #define _KMLSERVER_H
25 
26 #include <limits.h>
27 
28 #include <string>
29 #include <vector>
30 #include <sstream>
31 
32 #include <coordinate.h>
33 
34 /// Keyhole Markup Language: This publishes a complete message, localisation+time+weather etc...
35 class KmlServer {
36 protected:
37 	/// Counts the number of complete messages written.
38 	int m_nb_broadcasts ;
39 	int exit_kml_server;
40 	int request_broadcast;
41 public:
42 	/// List of key-value pairs displayed for example in Google Earth balloons.
43 	struct CustomDataT : public std::vector< std::pair< std::string, std::string > > {
44 		/// Also used when reloading a KML file.
45 		void Push( const char * k, const std::string & v );
46 
47 		/// TODO: Most of keys are duplicated. Should store them in a hash to reduce memory footprint.
PushCustomDataT48 		void Push( const char * k, const char * v ) {
49 			Push( k, std::string(v) );
50 		}
51 		/// This helper makes insertions simpler.
52 		template< class Type >
PushCustomDataT53 		void Push( const char * k, const Type & v ) {
54 			std::stringstream strm ;
55 			strm << v ;
56 			Push( k, strm.str() );
57 		}
58 	};
59 
KmlServer()60 	KmlServer() : m_nb_broadcasts(0), exit_kml_server(0), request_broadcast(0) {}
61 
~KmlServer()62 	virtual ~KmlServer() {}
63 
64 	/// BEWARE: Will work until 2038.
65 	static const time_t UniqueEvent = INT_MAX ;
66 
67 	/// This can for example go to a NMEA client.
68 	virtual void Broadcast(
69 		const std::string       & category,
70 		time_t                    evtTim,
71 		const CoordinateT::Pair & refCoo,
72 		double                    altitude,
73 		const std::string       & kml_name,
74 		const std::string       & styleNam,
75 		const std::string       & descrTxt,
76 		const CustomDataT       & custDat ) = 0;
77 
78 	/// Singleton.
79 	static KmlServer * GetInstance(void);
80 
81 	/// Number of calls to Broadcast(). Debugging purpose only.
NbBroadcasts(void)82 	int NbBroadcasts(void) const { return m_nb_broadcasts; }
83 
84 	/// Debugging only, just to check what happens inside.
ResetCounter()85 	void ResetCounter() { m_nb_broadcasts = 0;}
86 
87 	virtual void Reset() = 0;
88 
89 	/// TODO: Maybe have one display style per category, instead of the same for all.
90 	virtual void InitParams(
91 			const std::string & kml_command,
92 			const std::string & kml_dir,
93 			double              kml_merge = 10000,
94 			int                 kml_retention = 0, // Keep all data.
95 			int                 kml_refresh = 120,
96 			int                 kml_balloon_style = 0) = 0;
97 
98 	virtual void ReloadKmlFiles() = 0;
99 
100 	/// Creates the process for the command to be run when KML data is saved.
101 	static void SpawnProcess();
102 
103 	/// Stops the sub-thread, flush KML data to the files.
104 	static void Exit();
105 
106 	static std::string Tm2Time( time_t tim );
107 	/// No value means, now.
108 	static std::string Tm2Time(void);
109 };
110 
111 #endif // _KMLSERVER_H
112