]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
A long, long time ago, a bug was inadvertently introduced into the threaded
[flightgear.git] / src / Environment / environment_ctrl.hxx
1 // environment-ctrl.hxx -- controller for environment information.
2 //
3 // Written by David Megginson, started May 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifndef _ENVIRONMENT_CTRL_HXX
24 #define _ENVIRONMENT_CTRL_HXX
25
26 #include <simgear/compiler.h>
27 #include <simgear/structure/subsystem_mgr.hxx>
28 #include <simgear/environment/metar.hxx>
29
30 #if defined(ENABLE_THREADS)
31 # include <simgear/threads/SGThread.hxx>
32 # include <simgear/threads/SGQueue.hxx>
33 #endif
34
35 #ifdef SG_HAVE_STD_INCLUDES
36 #  include <cmath>
37 #else
38 #  include <math.h>
39 #endif
40
41 #include <queue>
42 #include <vector>
43
44 SG_USING_STD(queue);
45 SG_USING_STD(vector);
46
47 class SGPropertyNode;
48
49 #include "environment.hxx"
50 #include "fgmetar.hxx"
51
52
53 \f
54 /**
55  * Interface to control environment information for a specific location.
56  */
57 class FGEnvironmentCtrl : public SGSubsystem
58 {
59
60 public:
61
62   FGEnvironmentCtrl ();
63   virtual ~FGEnvironmentCtrl ();
64
65   virtual void setEnvironment (FGEnvironment * environment);
66
67   virtual const FGEnvironment * getEnvironment () const { return _environment; }
68
69   virtual void setLongitudeDeg (double lon_deg);
70   virtual void setLatitudeDeg (double lat_deg);
71   virtual void setElevationFt (double elev_ft);
72   virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
73
74   virtual double getLongitudeDeg () const { return _lon_deg; }
75   virtual double getLatitudeDeg () const { return _lat_deg; }
76   virtual double getElevationFt () const { return _elev_ft; }
77
78 protected:
79
80   FGEnvironment * _environment;
81   double _lon_deg;
82   double _lat_deg;
83   double _elev_ft;
84
85 };
86
87
88 \f
89 /**
90  * Environment controller using user-supplied parameters.
91  */
92 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
93 {
94 public:
95   FGUserDefEnvironmentCtrl ();
96   virtual ~FGUserDefEnvironmentCtrl ();
97
98   virtual void init ();
99   virtual void update (double dt);
100
101 private:
102
103   SGPropertyNode * _base_wind_speed_node;
104   SGPropertyNode * _gust_wind_speed_node;
105
106   double _current_wind_speed_kt;
107   double _delta_wind_speed_kt;
108
109 };
110
111
112 \f
113 /**
114  * Interplation controller using user-supplied parameters.
115  */
116 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
117 {
118 public:
119     FGInterpolateEnvironmentCtrl ();
120     virtual ~FGInterpolateEnvironmentCtrl ();
121     
122     virtual void init ();
123     virtual void reinit ();
124     virtual void update (double delta_time_sec);
125
126 private:
127     
128     struct bucket {
129         double altitude_ft;
130         FGEnvironment environment;
131         bool operator< (const bucket &b) const;
132     };
133
134     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
135     void do_interpolate (vector<bucket *> &table, double altitude_ft,
136                          FGEnvironment * environment);
137
138     FGEnvironment env1, env2;   // temporaries
139
140     vector<bucket *> _boundary_table;
141     vector<bucket *> _aloft_table;
142 };
143
144
145 // A convenience wrapper around FGMetar
146 struct FGMetarResult {
147     string icao;
148     FGMetar *m;
149 };
150
151
152 \f
153 /**
154  * Interplation controller using the FGMetar class
155  */
156 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
157 {
158 public:
159     FGMetarEnvironmentCtrl ();
160     virtual ~FGMetarEnvironmentCtrl ();
161
162     virtual void init ();
163     virtual void reinit ();
164     virtual void update (double delta_time_sec);
165     virtual void setEnvironment (FGEnvironment * environment);
166
167 private:
168     FGInterpolateEnvironmentCtrl *env;
169
170     string _icao;
171     float station_elevation_ft;
172     float search_interval_sec;
173     float same_station_interval_sec;
174     float search_elapsed;
175     float fetch_elapsed;
176     FGAirport last_apt;
177     SGPropertyNode *proxy_host;
178     SGPropertyNode *proxy_port;
179     SGPropertyNode *proxy_auth;
180     SGPropertyNode *metar_max_age;
181
182     FGMetarResult fetch_data( const string &icao );
183     virtual void update_metar_properties( const FGMetar *m );
184     void update_env_config();
185
186 private:
187
188 #if defined(ENABLE_THREADS)
189     /**
190      * FIFO queue which holds a pointer to the fetched metar data.
191      */
192     SGBlockingQueue < string > request_queue;
193
194     /**
195      * FIFO queue which holds a pointer to the fetched metar data.
196      */
197     SGBlockingQueue < FGMetarResult > result_queue;
198 #else
199     /**
200      * FIFO queue which holds a pointer to the fetched metar data.
201      */
202     queue < string > request_queue;
203
204     /**
205      * FIFO queue which holds a pointer to the fetched metar data.
206      */
207     queue < FGMetarResult > result_queue;
208 #endif
209
210 #if defined(ENABLE_THREADS)
211     /**
212      * This class represents the thread of execution responsible for
213      * fetching the metar data.
214      */
215     class MetarThread : public SGThread
216     {
217     public:
218         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
219         ~MetarThread() {}
220
221         /**
222          * Fetche the metar data from the NOAA.
223          */
224         void run();
225
226     private:
227         FGMetarEnvironmentCtrl *fetcher;
228
229     private:
230         // not implemented.
231         MetarThread();
232         MetarThread( const MetarThread& );
233         MetarThread& operator=( const MetarThread& );
234     };
235
236     friend class MetarThread;
237
238     /**
239      * Metar data fetching thread.
240      */
241     MetarThread* thread;
242
243     /**
244      * Lock and synchronize access to metar queue.
245      */
246     SGMutex mutex;
247     SGPthreadCond metar_cond;
248
249     /**
250      * Thread cleanup handler.
251      */
252     friend void metar_cleanup_handler( void* );
253
254 #endif // ENABLE_THREADS
255
256     int _error_count;
257     int _stale_count;
258     double _dt;
259     double _error_dt;
260 };
261
262 #endif // _ENVIRONMENT_CTRL_HXX