]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/TrafficMgr.hxx
Use a helper thread to init the traffic-manager.
[flightgear.git] / src / Traffic / TrafficMgr.hxx
1 /* -*- Mode: C++ -*- *****************************************************
2  * TrafficMgr.hxx
3  * Written by Durk Talsma. Started May 5, 2004
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  *
20  **************************************************************************/
21
22 /**************************************************************************
23  * This file contains the class definitions for a (Top Level) traffic
24  * manager for FlightGear. 
25  * 
26  * This is traffic manager version II. The major difference from version 
27  * I is that the Flight Schedules are decoupled from the AIAircraft
28  * entities. This allows for a much greater flexibility in setting up
29  * Irregular schedules. Traffic Manager II also makes no longer use of .xml
30  * based configuration files. 
31  * 
32  * Here is a step plan to achieve the goal of creating Traffic Manager II
33  * 
34  * 1) Read aircraft data from a simple text file, like the one provided by
35  *    Gabor Toth
36  * 2) Create a new database structure of SchedFlights. This new database 
37  *    should not be part of the Schedule class, but of TrafficManager itself
38  * 3) Each aircraft should have a list of possible Flights it can operate
39  *    (i.e. airline and AC type match). 
40  * 4) Aircraft processing proceeds as current. During initialization, we seek 
41  *    the most urgent flight that needs to be operated 
42  * 5) Modify the getNextLeg function so that the next flight is loaded smoothly.
43  
44  **************************************************************************/
45
46 #ifndef _TRAFFICMGR_HXX_
47 #define _TRAFFICMGR_HXX_
48
49 #include <set>
50 #include <memory>
51
52 #include <simgear/structure/subsystem_mgr.hxx>
53 #include <simgear/props/propertyObject.hxx>
54 #include <simgear/xml/easyxml.hxx>
55 #include <simgear/misc/sg_path.hxx>
56 #include <simgear/misc/sg_dir.hxx>
57
58 #include "SchedFlight.hxx"
59 #include "Schedule.hxx"
60
61
62 typedef std::vector<int> IdList;
63 typedef std::vector<int>::iterator IdListIterator;
64
65 class Heuristic
66 {
67 public:
68    std::string registration;
69    unsigned int runCount;
70    unsigned int hits;
71    unsigned int lastRun;
72 };
73
74 typedef std::vector<Heuristic> heuristicsVector;
75 typedef std::vector<Heuristic>::iterator heuristicsVectorIterator;
76
77 typedef std::map < std::string, Heuristic> HeuristicMap;
78 typedef HeuristicMap::iterator             HeuristicMapIterator;
79
80
81
82 class ScheduleParseThread;
83
84 class FGTrafficManager : public SGSubsystem, public XMLVisitor
85 {
86 private:
87   bool inited;
88   bool doingInit;
89   
90   ScheduleVector scheduledAircraft;
91   ScheduleVectorIterator currAircraft, currAircraftClosest;
92   vector<string> elementValueStack;
93
94   // record model paths which are missing, to avoid duplicate
95   // warnings when parsing traffic schedules.
96   std::set<std::string> missingModels;
97     
98   std::string mdl, livery, registration, callsign, fltrules, 
99     port, timeString, departurePort, departureTime, arrivalPort, arrivalTime,
100     repeat, acType, airline, m_class, flighttype, requiredAircraft, homePort;
101   int cruiseAlt;
102   int score, runCount, acCounter;
103   double radius, offset;
104   bool heavy;
105
106   IdList releaseList;
107     
108   FGScheduledFlightMap flights;
109
110   void readTimeTableFromFile(SGPath infilename);
111   void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ");
112
113   simgear::PropertyObject<bool> enabled, aiEnabled, realWxEnabled, metarValid;
114   
115   void loadHeuristics();
116   
117   void finishInit();
118   void shutdown();
119   
120   friend class ScheduleParseThread;
121   std::auto_ptr<ScheduleParseThread> scheduleParser;
122   
123   // helper to read and parse the schedule data.
124   // this is run on a helper thread, so be careful about
125   // accesing properties during parsing
126   void parseSchedule(const SGPath& path);
127   
128 public:
129   FGTrafficManager();
130   ~FGTrafficManager();
131   void init();
132   void update(double time);
133   void release(int ref);
134   bool isReleased(int id);
135
136   FGScheduledFlightVecIterator getFirstFlight(const string &ref) { return flights[ref].begin(); }
137   FGScheduledFlightVecIterator getLastFlight(const string &ref) { return flights[ref].end(); }
138
139   void endAircraft();
140   
141   // Some overloaded virtual XMLVisitor members
142   virtual void startXML (); 
143   virtual void endXML   ();
144   virtual void startElement (const char * name, const XMLAttributes &atts);
145   virtual void endElement (const char * name);
146   virtual void data (const char * s, int len);
147   virtual void pi (const char * target, const char * data);
148   virtual void warning (const char * message, int line, int column);
149   virtual void error (const char * message, int line, int column);
150 };
151
152 #endif