1 /******************************************************************************
3 * Written by Durk Talsma, started May 5, 2004.
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.
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.
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.
20 **************************************************************************/
23 * Traffic manager parses airlines timetable-like data and uses this to
24 * determine the approximate position of each AI aircraft in its database.
25 * When an AI aircraft is close to the user's position, a more detailed
26 * AIModels based simulation is set up.
28 * I'm currently assuming the following simplifications:
29 * 1) The earth is a perfect sphere
30 * 2) Each aircraft flies a perfect great circle route.
31 * 3) Each aircraft flies at a constant speed (with infinite accelerations and
33 * 4) Each aircraft leaves at exactly the departure time.
34 * 5) Each aircraft arrives at exactly the specified arrival time.
37 *****************************************************************************/
53 #include <boost/foreach.hpp>
55 #include <simgear/compiler.h>
56 #include <simgear/misc/sg_path.hxx>
57 #include <simgear/misc/sg_dir.hxx>
58 #include <simgear/props/props.hxx>
59 #include <simgear/structure/subsystem_mgr.hxx>
60 #include <simgear/structure/exception.hxx>
61 #include <simgear/timing/sg_time.hxx>
63 #include <simgear/xml/easyxml.hxx>
64 #include <simgear/threads/SGThread.hxx>
65 #include <simgear/threads/SGGuard.hxx>
66 #include <simgear/scene/tsync/terrasync.hxx>
68 #include <AIModel/AIAircraft.hxx>
69 #include <AIModel/AIFlightPlan.hxx>
70 #include <AIModel/AIBase.hxx>
71 #include <AIModel/performancedb.hxx>
73 #include <Airports/airport.hxx>
74 #include <Main/fg_init.hxx>
75 #include <Main/globals.hxx>
76 #include <Main/fg_props.hxx>
78 #include "TrafficMgr.hxx"
87 * Thread encapsulating parsing the traffic schedules.
89 class ScheduleParseThread : public SGThread, public XMLVisitor
92 ScheduleParseThread(FGTrafficManager* traffic) :
93 _trafficManager(traffic),
106 // if we're destroyed while running, ensure the thread exits cleanly
107 ~ScheduleParseThread()
111 _cancelThread = true; // request cancellation so we don't wait ages
119 void setTrafficDirs(const PathList& dirs)
121 _trafficDirPaths = dirs;
124 bool isFinished() const
126 SGGuard<SGMutex> g(_lock);
132 BOOST_FOREACH(SGPath p, _trafficDirPaths) {
139 SGGuard<SGMutex> g(_lock);
145 //cout << "Start XML" << endl;
146 requiredAircraft = "";
152 //cout << "End XML" << endl;
155 void startElement(const char *name,
156 const XMLAttributes & atts)
159 //cout << "Start element " << name << endl;
160 //FGTrafficManager temp;
161 //for (int i = 0; i < atts.size(); i++)
162 // if (string(atts.getName(i)) == string("include"))
163 attval = atts.getValue("include");
165 //cout << "including " << attval << endl;
166 SGPath path = globals->get_fg_root();
167 path.append("/Traffic/");
169 readXML(path.str(), *this);
171 elementValueStack.push_back("");
172 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
175 void endElement(const char *name)
177 //cout << "End element " << name << endl;
178 const string & value = elementValueStack.back();
180 if (!strcmp(name, "model"))
182 else if (!strcmp(name, "livery"))
184 else if (!strcmp(name, "home-port"))
186 else if (!strcmp(name, "registration"))
187 registration = value;
188 else if (!strcmp(name, "airline"))
190 else if (!strcmp(name, "actype"))
192 else if (!strcmp(name, "required-aircraft"))
193 requiredAircraft = value;
194 else if (!strcmp(name, "flighttype"))
196 else if (!strcmp(name, "radius"))
197 radius = atoi(value.c_str());
198 else if (!strcmp(name, "offset"))
199 offset = atoi(value.c_str());
200 else if (!strcmp(name, "performance-class"))
202 else if (!strcmp(name, "heavy")) {
203 if (value == string("true"))
207 } else if (!strcmp(name, "callsign"))
209 else if (!strcmp(name, "fltrules"))
211 else if (!strcmp(name, "port"))
213 else if (!strcmp(name, "time"))
215 else if (!strcmp(name, "departure")) {
216 departurePort = port;
217 departureTime = timeString;
218 } else if (!strcmp(name, "cruise-alt"))
219 cruiseAlt = atoi(value.c_str());
220 else if (!strcmp(name, "arrival")) {
222 arrivalTime = timeString;
223 } else if (!strcmp(name, "repeat"))
225 else if (!strcmp(name, "flight")) {
226 // We have loaded and parsed all the information belonging to this flight
227 // so we temporarily store it.
228 //cerr << "Pusing back flight " << callsign << endl;
229 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
230 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
232 //Prioritize aircraft
233 string apt = fgGetString("/sim/presets/airport-id");
234 //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
235 //if (departurePort == apt) score++;
236 //flights.push_back(new FGScheduledFlight(callsign,
244 if (requiredAircraft == "") {
246 snprintf(buffer, 16, "%d", acCounter);
247 requiredAircraft = buffer;
249 SG_LOG(SG_AI, SG_DEBUG, "Adding flight: " << callsign << " "
251 << departurePort << " "
252 << arrivalPort << " "
254 << departureTime << " "
255 << arrivalTime << " " << repeat << " " << requiredAircraft);
256 // For database maintainance purposes, it may be convenient to
258 if (fgGetBool("/sim/traffic-manager/dumpdata") == true) {
259 SG_LOG(SG_AI, SG_ALERT, "Traffic Dump FLIGHT," << callsign << ","
261 << departurePort << ","
262 << arrivalPort << ","
264 << departureTime << ","
265 << arrivalTime << "," << repeat << "," << requiredAircraft);
268 _trafficManager->flights[requiredAircraft].push_back(new FGScheduledFlight(callsign,
277 requiredAircraft = "";
278 } else if (!strcmp(name, "aircraft")) {
282 elementValueStack.pop_back();
286 void data(const char *s, int len)
288 string token = string(s, len);
289 //cout << "Character data " << string(s,len) << endl;
290 elementValueStack.back() += token;
293 void pi(const char *target, const char *data)
295 //cout << "Processing instruction " << target << ' ' << data << endl;
298 void warning(const char *message, int line, int column)
300 SG_LOG(SG_IO, SG_WARN,
301 "Warning: " << message << " (" << line << ',' << column << ')');
304 void error(const char *message, int line, int column)
306 SG_LOG(SG_IO, SG_ALERT,
307 "Error: " << message << " (" << line << ',' << column << ')');
313 string isHeavy = heavy ? "true" : "false";
315 if (missingModels.find(mdl) != missingModels.end()) {
316 // don't stat() or warn again
317 requiredAircraft = homePort = "";
321 if (!FGAISchedule::validModelPath(mdl)) {
322 missingModels.insert(mdl);
323 #if defined(ENABLE_DEV_WARNINGS)
324 SG_LOG(SG_AI, SG_WARN, "TrafficMgr: Missing model path:" << mdl);
326 requiredAircraft = homePort = "";
331 (int) (fgGetDouble("/sim/traffic-manager/proportion") * 100);
332 int randval = rand() & 100;
333 if (randval > proportion) {
334 requiredAircraft = homePort = "";
338 if (fgGetBool("/sim/traffic-manager/dumpdata") == true) {
339 SG_LOG(SG_AI, SG_ALERT, "Traffic Dump AC," << homePort << "," << registration << "," << requiredAircraft
340 << "," << acType << "," << livery << ","
341 << airline << "," << m_class << "," << offset << "," << radius << "," << flighttype << "," << isHeavy << "," << mdl);
344 if (requiredAircraft == "") {
346 snprintf(buffer, 16, "%d", acCounter);
347 requiredAircraft = buffer;
349 if (homePort == "") {
350 homePort = departurePort;
353 // caution, modifying the scheduled aircraft strucutre from the
354 // 'wrong' thread. This is safe becuase FGTrafficManager won't touch
355 // the structure while we exist.
356 _trafficManager->scheduledAircraft.push_back(new FGAISchedule(mdl,
369 requiredAircraft = "";
374 void parseTrafficDir(const SGPath& path)
379 simgear::Dir trafficDir(path);
380 simgear::PathList d = trafficDir.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
382 BOOST_FOREACH(SGPath p, d) {
384 SG_LOG(SG_AI, SG_INFO, "parsing traffic in:" << p);
385 simgear::PathList trafficFiles = d2.children(simgear::Dir::TYPE_FILE, ".xml");
386 BOOST_FOREACH(SGPath xml, trafficFiles) {
387 readXML(xml.str(), *this);
392 } // of sub-directories iteration
394 SG_LOG(SG_AI, SG_INFO, "parsing traffic schedules took:" << st.elapsedMSec() << "msec");
397 FGTrafficManager* _trafficManager;
398 mutable SGMutex _lock;
401 simgear::PathList _trafficDirPaths;
405 string_list elementValueStack;
406 // record model paths which are missing, to avoid duplicate
407 // warnings when parsing traffic schedules.
408 std::set<std::string> missingModels;
410 std::string mdl, livery, registration, callsign, fltrules,
411 port, timeString, departurePort, departureTime, arrivalPort, arrivalTime,
412 repeat, acType, airline, m_class, flighttype, requiredAircraft, homePort;
414 int score, acCounter;
415 double radius, offset;
420 /******************************************************************************
422 *****************************************************************************/
423 FGTrafficManager::FGTrafficManager() :
426 trafficSyncRequested(false),
427 waitingMetarTime(0.0),
428 enabled("/sim/traffic-manager/enabled"),
429 aiEnabled("/sim/ai/enabled"),
430 realWxEnabled("/environment/realwx/enabled"),
431 metarValid("/environment/metar/valid")
435 FGTrafficManager::~FGTrafficManager()
440 void FGTrafficManager::shutdown()
444 scheduleParser.reset();
451 // Save the heuristics data
452 bool saveData = false;
453 std::ofstream cachefile;
454 if (fgGetBool("/sim/traffic-manager/heuristics")) {
455 SGPath cacheData(globals->get_fg_home());
456 cacheData.append("ai");
457 const string airport = fgGetString("/sim/presets/airport-id");
459 if ((airport) != "") {
461 ::snprintf(buffer, 128, "%c/%c/%c/",
462 airport[0], airport[1], airport[2]);
463 cacheData.append(buffer);
464 cacheData.append(airport + "-cache.txt");
466 // Note: Intuitively, this doesn't make sense, but I do need to create the full file path first
467 // before creating the directories. The SimGear fgpath code has changed so that it first chops off
468 // the trailing dir separator and then determines the directory part of the file path by searching
469 // for the last dir separator. Effecively, this causes a full element of the directory tree to be
471 SG_LOG(SG_GENERAL, SG_DEBUG, "Trying to create dir for : " << cacheData.c_str());
472 if (!cacheData.exists()) {
473 cacheData.create_dir(0755);
476 cachefile.open(cacheData.str().c_str());
477 cachefile << "[TrafficManagerCachedata:ref:2011:09:04]" << endl;
482 BOOST_FOREACH(FGAISchedule* acft, scheduledAircraft) {
484 cachefile << acft->getRegistration() << " "
485 << acft->getRunCount() << " "
486 << acft->getHits() << " "
487 << acft->getLastUsed() << endl;
494 scheduledAircraft.clear();
497 currAircraft = scheduledAircraft.begin();
500 trafficSyncRequested = false;
503 void FGTrafficManager::init()
509 // TorstenD: don't start the traffic manager before the FDM is initialized
510 // The FDM needs the scenery loaded and will wait for our spawned AIModels PagedLOD Nodes
511 // to appear if they are close (less than 1000m) to our position
512 if( !fgGetBool("/sim/signals/fdm-initialized") )
516 simgear::SGTerraSync* terraSync = static_cast<simgear::SGTerraSync*>(globals->get_subsystem("terrasync"));
517 bool doDataSync = fgGetBool("/sim/terrasync/ai-data-enabled");
518 if (doDataSync && terraSync) {
519 if (!trafficSyncRequested) {
520 SG_LOG(SG_AI, SG_INFO, "Sync of AI traffic via TerraSync enabled");
521 terraSync->scheduleDataDir("AI/Traffic");
522 trafficSyncRequested = true;
525 if (terraSync->isDataDirPending("AI/Traffic")) {
526 return; // remain in the init state
529 SG_LOG(SG_AI, SG_INFO, "Traffic files sync complete");
533 if (string(fgGetString("/sim/traffic-manager/datafile")).empty()) {
534 simgear::PathList dirs = globals->get_data_paths("AI/Traffic");
536 // temporary flag to restrict loading while traffic data is found
537 // through terrasync /and/ fgdata. Ultimatley we *do* want to be able to
540 if (dirs.size() > 1) {
541 SGPath p = dirs.back();
542 if (simgear::strutils::starts_with(p.str(), globals->get_fg_root())) {
552 scheduleParser.reset(new ScheduleParseThread(this));
553 scheduleParser->setTrafficDirs(dirs);
554 scheduleParser->start();
556 fgSetBool("/sim/traffic-manager/heuristics", false);
557 SGPath path = string(fgGetString("/sim/traffic-manager/datafile"));
558 string ext = path.extension();
559 if (path.extension() == "xml") {
561 // use a SchedulerParser to parse, but run it in this thread,
562 // i.e don't start it
563 ScheduleParseThread parser(this);
564 readXML(path.str(), parser);
566 } else if (path.extension() == "conf") {
568 readTimeTableFromFile(path);
571 SG_LOG(SG_AI, SG_ALERT,
572 "Unknown data format " << path.str()
581 void FGTrafficManager::finishInit()
584 SG_LOG(SG_AI, SG_INFO, "finishing AI-Traffic init");
586 #if defined(ENABLE_DEV_WARNINGS)
587 PerformanceDB* perfDB = globals->get_subsystem<PerformanceDB>();
589 // Do sorting and scoring separately, to take advantage of the "homeport" variable
590 BOOST_FOREACH(FGAISchedule* schedule, scheduledAircraft) {
591 schedule->setScore();
592 #if defined(ENABLE_DEV_WARNINGS)
593 if (!perfDB->havePerformanceDataForAircraftType(schedule->getAircraft())) {
594 SG_LOG(SG_AI, SG_WARN, "AI-Traffic: schedule aircraft missing performance data:" << schedule->getAircraft());
599 sort(scheduledAircraft.begin(), scheduledAircraft.end(),
601 currAircraft = scheduledAircraft.begin();
602 currAircraftClosest = scheduledAircraft.begin();
608 void FGTrafficManager::loadHeuristics()
610 if (!fgGetBool("/sim/traffic-manager/heuristics")) {
614 HeuristicMap heurMap;
615 //cerr << "Processing Heuristics" << endl;
616 // Load the heuristics data
617 SGPath cacheData(globals->get_fg_home());
618 cacheData.append("ai");
619 string airport = fgGetString("/sim/presets/airport-id");
620 if ((airport) != "") {
622 ::snprintf(buffer, 128, "%c/%c/%c/",
623 airport[0], airport[1], airport[2]);
624 cacheData.append(buffer);
625 cacheData.append(airport + "-cache.txt");
627 if (cacheData.exists()) {
628 std::ifstream data(cacheData.c_str());
630 if (revisionStr != "[TrafficManagerCachedata:ref:2011:09:04]") {
631 SG_LOG(SG_AI, SG_ALERT,"Traffic Manager Warning: discarding outdated cachefile " <<
632 cacheData.c_str() << " for Airport " << airport);
635 Heuristic h; // = new Heuristic;
636 data >> h.registration >> h.runCount >> h.hits >> h.lastRun;
639 HeuristicMapIterator itr = heurMap.find(h.registration);
640 if (itr != heurMap.end()) {
641 #if defined(ENABLE_DEV_WARNINGS)
642 SG_LOG(SG_AI, SG_WARN,"Traffic Manager Warning: found duplicate tailnumber " <<
643 h.registration << " for AI aircraft");
646 heurMap[h.registration] = h;
653 for(currAircraft = scheduledAircraft.begin(); currAircraft != scheduledAircraft.end(); ++currAircraft) {
654 const string& registration = (*currAircraft)->getRegistration();
655 HeuristicMapIterator itr = heurMap.find(registration);
656 if (itr != heurMap.end()) {
657 (*currAircraft)->setrunCount(itr->second.runCount);
658 (*currAircraft)->setHits(itr->second.hits);
659 (*currAircraft)->setLastUsed(itr->second.lastRun);
664 bool FGTrafficManager::metarReady(double dt)
666 // wait for valid METAR (when realWX is enabled only), since we need
667 // to know the active runway
668 if (metarValid || !realWxEnabled)
670 waitingMetarTime = 0.0;
674 // METAR timeout: when running offline, remote server is down etc
675 if (waitingMetarStation != fgGetString("/environment/metar/station-id"))
677 // station has changed: wait for reply, restart timeout
678 waitingMetarTime = 0.0;
679 waitingMetarStation = fgGetString("/environment/metar/station-id");
683 // timeout elapsed (10 seconds)?
684 if (waitingMetarTime > 20.0)
689 waitingMetarTime += dt;
693 void FGTrafficManager::update(double dt)
697 if (inited || doingInit)
707 // traffic depends on AI module
716 if (!doingInit || !scheduleParser->isFinished()) {
724 if (scheduledAircraft.empty()) {
728 SGVec3d userCart = globals->get_aircraft_position_cart();
730 if (currAircraft == scheduledAircraft.end()) {
731 currAircraft = scheduledAircraft.begin();
734 time_t now = globals->get_time_params()->get_cur_time();
736 //cerr << "Processing << " << (*currAircraft)->getRegistration() << " with score " << (*currAircraft)->getScore() << endl;
737 if ((*currAircraft)->update(now, userCart)) {
738 // schedule is done - process another aircraft in next iteration
743 void FGTrafficManager::readTimeTableFromFile(SGPath infileName)
760 vector <string> tokens, depTime,arrTime;
761 vector <string>::iterator it;
762 std::ifstream infile(infileName.str().c_str());
764 infile.getline(buffer, 256);
768 //cerr << "Read line : " << buffer << endl;
769 buffString = string(buffer);
771 Tokenize(buffString, tokens, " \t");
772 //for (it = tokens.begin(); it != tokens.end(); it++) {
773 // cerr << "Tokens: " << *(it) << endl;
776 if (!tokens.empty()) {
777 if (tokens[0] == string("AC")) {
778 if (tokens.size() != 13) {
779 throw sg_io_exception("Error parsing traffic file @ " + buffString, sg_location(infileName.str()));
785 homePort = tokens[1];
786 registration = tokens[2];
787 if (tokens[11] == string("false")) {
794 flightReq = tokens[3] + tokens[5];
795 m_class = tokens[10];
796 FlightType = tokens[9];
797 radius = atof(tokens[8].c_str());
798 offset = atof(tokens[7].c_str());;
800 if (!FGAISchedule::validModelPath(model)) {
801 SG_LOG(SG_AI, SG_WARN, "TrafficMgr: Missing model path:" <<
802 model << " from " << infileName.str());
805 SG_LOG(SG_AI, SG_INFO, "Adding Aircraft" << model << " " << livery << " " << homePort << " "
806 << registration << " " << flightReq << " " << isHeavy
807 << " " << acType << " " << airline << " " << m_class
808 << " " << FlightType << " " << radius << " " << offset);
809 scheduledAircraft.push_back(new FGAISchedule(model,
821 } // of valid model path
823 if (tokens[0] == string("FLIGHT")) {
824 //cerr << "Found flight " << buffString << " size is : " << tokens.size() << endl;
825 if (tokens.size() != 10) {
826 SG_LOG(SG_AI, SG_ALERT, "Error parsing traffic file " << infileName.str() << " at " << buffString);
829 string callsign = tokens[1];
830 string fltrules = tokens[2];
831 string weekdays = tokens[3];
832 string departurePort = tokens[5];
833 string arrivalPort = tokens[7];
834 int cruiseAlt = atoi(tokens[8].c_str());
835 string depTimeGen = tokens[4];
836 string arrTimeGen = tokens[6];
837 string repeat = "WEEK";
838 string requiredAircraft = tokens[9];
840 if (weekdays.size() != 7) {
841 SG_LOG(SG_AI, SG_ALERT, "Found misconfigured weekdays string" << weekdays);
846 Tokenize(depTimeGen, depTime, ":");
847 Tokenize(arrTimeGen, arrTime, ":");
848 double dep = atof(depTime[0].c_str()) + (atof(depTime[1].c_str()) / 60.0);
849 double arr = atof(arrTime[0].c_str()) + (atof(arrTime[1].c_str()) / 60.0);
850 //cerr << "Using " << dep << " " << arr << endl;
851 bool arrivalWeekdayNeedsIncrement = false;
853 arrivalWeekdayNeedsIncrement = true;
855 for (int i = 0; i < 7; i++) {
857 if (weekdays[i] != '.') {
859 snprintf(buffer, 4, "%d/", j);
860 string departureTime = string(buffer) + depTimeGen + string(":00");
862 if (!arrivalWeekdayNeedsIncrement) {
863 arrivalTime = string(buffer) + arrTimeGen + string(":00");
865 if (arrivalWeekdayNeedsIncrement && i != 6 ) {
866 snprintf(buffer, 4, "%d/", j+1);
867 arrivalTime = string(buffer) + arrTimeGen + string(":00");
869 if (arrivalWeekdayNeedsIncrement && i == 6 ) {
870 snprintf(buffer, 4, "%d/", 0);
871 arrivalTime = string(buffer) + arrTimeGen + string(":00");
873 SG_LOG(SG_AI, SG_ALERT, "Adding flight " << callsign << " "
875 << departurePort << " "
876 << arrivalPort << " "
878 << departureTime << " "
879 << arrivalTime << " "
881 << requiredAircraft);
883 flights[requiredAircraft].push_back(new FGScheduledFlight(callsign,
902 void FGTrafficManager::Tokenize(const string& str,
903 vector<string>& tokens,
904 const string& delimiters)
906 // Skip delimiters at beginning.
907 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
908 // Find first "non-delimiter".
909 string::size_type pos = str.find_first_of(delimiters, lastPos);
911 while (string::npos != pos || string::npos != lastPos)
913 // Found a token, add it to the vector.
914 tokens.push_back(str.substr(lastPos, pos - lastPos));
915 // Skip delimiters. Note the "not_of"
916 lastPos = str.find_first_not_of(delimiters, pos);
917 // Find next "non-delimiter"
918 pos = str.find_first_of(delimiters, lastPos);