]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
More fixes to get the code working when threads are disabled.
[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) && 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
51
52 \f
53 /**
54  * Interface to control environment information for a specific location.
55  */
56 class FGEnvironmentCtrl : public SGSubsystem
57 {
58
59 public:
60
61   FGEnvironmentCtrl ();
62   virtual ~FGEnvironmentCtrl ();
63
64   virtual void setEnvironment (FGEnvironment * environment);
65
66   virtual FGEnvironment * getEnvironment () const { return _environment; }
67
68   virtual void setLongitudeDeg (double lon_deg);
69   virtual void setLatitudeDeg (double lat_deg);
70   virtual void setElevationFt (double elev_ft);
71   virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
72
73   virtual double getLongitudeDeg () const { return _lon_deg; }
74   virtual double getLatitudeDeg () const { return _lat_deg; }
75   virtual double getElevationFt () const { return _elev_ft; }
76
77 protected:
78
79   FGEnvironment * _environment;
80   double _lon_deg;
81   double _lat_deg;
82   double _elev_ft;
83
84 };
85
86
87 \f
88 /**
89  * Environment controller using user-supplied parameters.
90  */
91 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
92 {
93 public:
94   FGUserDefEnvironmentCtrl ();
95   virtual ~FGUserDefEnvironmentCtrl ();
96
97   virtual void init ();
98   virtual void update (double dt);
99
100 private:
101
102   SGPropertyNode * _base_wind_speed_node;
103   SGPropertyNode * _gust_wind_speed_node;
104
105   double _current_wind_speed_kt;
106   double _delta_wind_speed_kt;
107
108 };
109
110
111 \f
112 /**
113  * Interplation controller using user-supplied parameters.
114  */
115 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
116 {
117 public:
118     FGInterpolateEnvironmentCtrl ();
119     virtual ~FGInterpolateEnvironmentCtrl ();
120     
121     virtual void init ();
122     virtual void reinit ();
123     virtual void update (double delta_time_sec);
124
125 private:
126     
127     struct bucket {
128         double altitude_ft;
129         FGEnvironment environment;
130         bool operator< (const bucket &b) const;
131     };
132
133     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
134     void do_interpolate (vector<bucket *> &table, double altitude_ft,
135                          FGEnvironment * environment);
136
137     FGEnvironment env1, env2;   // temporaries
138
139     vector<bucket *> _boundary_table;
140     vector<bucket *> _aloft_table;
141 };
142
143
144 // A convenience wrapper around SGMetar
145 struct FGMetarResult {
146     string icao;
147     SGMetar *m;
148 };
149
150
151 \f
152 /**
153  * Interplation controller using the SGMetar class
154  */
155 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
156 {
157 public:
158     FGMetarEnvironmentCtrl ();
159     virtual ~FGMetarEnvironmentCtrl ();
160
161     virtual void init ();
162     virtual void reinit ();
163     virtual void update (double delta_time_sec);
164     virtual void setEnvironment (FGEnvironment * environment);
165
166 private:
167     FGInterpolateEnvironmentCtrl *env;
168
169     string _icao;
170     float station_elevation_ft;
171     float search_interval_sec;
172     float same_station_interval_sec;
173     float search_elapsed;
174     float fetch_elapsed;
175     FGAirport last_apt;
176     SGPropertyNode *proxy_host;
177     SGPropertyNode *proxy_port;
178     SGPropertyNode *proxy_auth;
179
180     FGMetarResult fetch_data( const string &icao );
181     virtual void update_metar_properties( SGMetar *m );
182     void update_env_config();
183
184 private:
185
186 #if defined(ENABLE_THREADS) && ENABLE_THREADS
187     /**
188      * FIFO queue which holds a pointer to the fetched metar data.
189      */
190     SGBlockingQueue < string > request_queue;
191
192     /**
193      * FIFO queue which holds a pointer to the fetched metar data.
194      */
195     SGBlockingQueue < FGMetarResult > result_queue;
196 #else
197     /**
198      * FIFO queue which holds a pointer to the fetched metar data.
199      */
200     queue < string > request_queue;
201
202     /**
203      * FIFO queue which holds a pointer to the fetched metar data.
204      */
205     queue < FGMetarResult > result_queue;
206 #endif
207
208 #if defined(ENABLE_THREADS) && ENABLE_THREADS
209     /**
210      * This class represents the thread of execution responsible for
211      * fetching the metar data.
212      */
213     class MetarThread : public SGThread
214     {
215     public:
216         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
217         ~MetarThread() {}
218
219         /**
220          * Fetche the metar data from the NOAA.
221          */
222         void run();
223
224     private:
225         FGMetarEnvironmentCtrl *fetcher;
226
227     private:
228         // not implemented.
229         MetarThread();
230         MetarThread( const MetarThread& );
231         MetarThread& operator=( const MetarThread& );
232     };
233
234     friend class MetarThread;
235
236     /**
237      * Metar data fetching thread.
238      */
239     MetarThread* thread;
240
241     /**
242      * Lock and synchronize access to metar queue.
243      */
244     SGMutex mutex;
245     SGPthreadCond metar_cond;
246
247     /**
248      * Thread cleanup handler.
249      */
250     friend void metar_cleanup_handler( void* );
251
252 #endif // ENABLE_THREADS
253
254     int _error_count;
255     double _dt;
256     double _error_dt;
257 };
258
259 #endif // _ENVIRONMENT_CTRL_HXX