1 // terrasync.cxx -- scenery fetcher
3 // Started by Curtis Olson, November 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
6 // Copyright (C) 2008 Alexander R. Perry <alex.perry@ieee.org>
7 // Copyright (C) 2011 Thorsten Brehm <brehmt@gmail.com>
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
38 #elif defined(_MSC_VER)
44 #include <stdlib.h> // atoi() atof() abs() system()
45 #include <signal.h> // signal()
53 #include <simgear/compiler.h>
55 #include "terrasync.hxx"
57 #include <simgear/bucket/newbucket.hxx>
58 #include <simgear/misc/sg_path.hxx>
59 #include <simgear/misc/strutils.hxx>
60 #include <simgear/threads/SGQueue.hxx>
61 #include <simgear/misc/sg_dir.hxx>
62 #include <simgear/debug/BufferedLogCallback.hxx>
63 #include <simgear/props/props_io.hxx>
64 #include <simgear/io/HTTPClient.hxx>
65 #include <simgear/io/SVNRepository.hxx>
66 #include <simgear/structure/exception.hxx>
68 static const bool svn_built_in_available = true;
70 using namespace simgear;
73 const char* rsync_cmd =
74 "rsync --verbose --archive --delete --perms --owner --group";
76 const char* svn_options =
79 namespace UpdateInterval
81 // interval in seconds to allow an update to repeat after a successful update (=daily)
82 static const double SuccessfulAttempt = 24*60*60;
83 // interval in seconds to allow another update after a failed attempt (10 minutes)
84 static const double FailedAttempt = 10*60;
87 typedef map<string,time_t> TileAgeCache;
89 ///////////////////////////////////////////////////////////////////////////////
90 // helper functions ///////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////////////////
92 string stripPath(string path)
94 // svn doesn't like trailing white-spaces or path separators - strip them!
95 path = simgear::strutils::strip(path);
96 size_t slen = path.length();
98 ((path[slen-1]=='/')||(path[slen-1]=='\\')))
102 return path.substr(0,slen);
105 bool hasWhitespace(string path)
107 return path.find(' ')!=string::npos;
110 ///////////////////////////////////////////////////////////////////////////////
111 // SyncItem ////////////////////////////////////////////////////////////
112 ///////////////////////////////////////////////////////////////////////////////
118 Stop = 0, ///< special item indicating to stop the SVNThread
129 Cached, ///< using already cached result
142 SyncItem(string dir, Type ty) :
153 ///////////////////////////////////////////////////////////////////////////////
156 * @brief SyncSlot encapsulates a queue of sync items we will fetch
157 * serially. Multiple slots exist to sync different types of item in
164 isNewDirectory(false),
168 SyncItem currentItem;
170 std::queue<SyncItem> queue;
171 std::auto_ptr<SVNRepository> repository;
173 bool busy; ///< is the slot working or idle
175 unsigned int nextWarnTimeout;
178 static const int SYNC_SLOT_TILES = 0; ///< Terrain and Objects sync
179 static const int SYNC_SLOT_SHARED_DATA = 1; /// shared Models and Airport data
180 static const int SYNC_SLOT_AI_DATA = 2; /// AI traffic and models
181 static const unsigned int NUM_SYNC_SLOTS = 3;
184 * @brief translate a sync item type into one of the available slots.
185 * This provides the scheduling / balancing / prioritising between slots.
187 static unsigned int syncSlotForType(SyncItem::Type ty)
190 case SyncItem::Tile: return SYNC_SLOT_TILES;
191 case SyncItem::SharedModels:
192 case SyncItem::AirportData:
193 return SYNC_SLOT_SHARED_DATA;
194 case SyncItem::AIData:
195 return SYNC_SLOT_AI_DATA;
198 return SYNC_SLOT_SHARED_DATA;
202 ///////////////////////////////////////////////////////////////////////////////
204 ///////////////////////////////////////////////////////////////////////////////
206 class ServerSelectQuery : public HTTP::Request
209 ServerSelectQuery() :
210 HTTP::Request("http://scenery.flightgear.org/svn-server", "GET")
214 std::string svnUrl() const
216 std::string s = simgear::strutils::strip(m_url);
217 if (s.at(s.length() - 1) == '/') {
218 s = s.substr(0, s.length() - 1);
224 virtual void gotBodyData(const char* s, int n)
226 m_url.append(std::string(s, n));
229 virtual void onFail()
231 SG_LOG(SG_TERRAIN, SG_ALERT, "Failed to query TerraSync SVN server");
232 HTTP::Request::onFail();
240 ///////////////////////////////////////////////////////////////////////////////
241 // SGTerraSync::SvnThread /////////////////////////////////////////////////////
242 ///////////////////////////////////////////////////////////////////////////////
243 class SGTerraSync::SvnThread : public SGThread
247 virtual ~SvnThread( ) { stop(); }
252 bool isIdle() {return !_busy; }
253 void request(const SyncItem& dir) {waitingTiles.push_front(dir);}
254 bool isDirty() { bool r = _is_dirty;_is_dirty = false;return r;}
255 bool hasNewTiles() { return !_freshTiles.empty();}
256 SyncItem getNewTile() { return _freshTiles.pop_front();}
258 void setSvnServer(string server) { _svn_server = stripPath(server);}
259 void setSvnDataServer(string server) { _svn_data_server = stripPath(server);}
261 void setExtSvnUtility(string svn_util) { _svn_command = simgear::strutils::strip(svn_util);}
262 void setRsyncServer(string server) { _rsync_server = simgear::strutils::strip(server);}
263 void setLocalDir(string dir) { _local_dir = stripPath(dir);}
264 string getLocalDir() { return _local_dir;}
265 void setUseSvn(bool use_svn) { _use_svn = use_svn;}
266 void setAllowedErrorCount(int errors) {_allowed_errors = errors;}
268 void setCachePath(const SGPath& p) {_persistentCachePath = p;}
269 void setCacheHits(unsigned int hits) {_cache_hits = hits;}
270 void setUseBuiltin(bool built_in) { _use_built_in = built_in;}
272 volatile bool _active;
273 volatile bool _running;
275 volatile bool _stalled;
276 volatile int _fail_count;
277 volatile int _updated_tile_count;
278 volatile int _success_count;
279 volatile int _consecutive_errors;
280 volatile int _allowed_errors;
281 volatile int _cache_hits;
282 volatile int _transfer_rate;
283 // kbytes, not bytes, because bytes might overflow 2^31
284 volatile int _total_kb_downloaded;
289 // external model run and helpers
291 void syncPathExternal(const SyncItem& next);
292 bool runExternalSyncCommand(const char* dir);
294 // internal mode run and helpers
296 void updateSyncSlot(SyncSlot& slot);
298 // commond helpers between both internal and external models
300 SyncItem::Status isPathCached(const SyncItem& next) const;
301 void initCompletedTilesPersistentCache();
302 void writeCompletedTilesPersistentCache() const;
303 void updated(SyncItem item, bool isNewDirectory);
304 void fail(SyncItem failedItem);
305 void notFound(SyncItem notFoundItem);
309 SyncSlot _syncSlots[NUM_SYNC_SLOTS];
311 volatile bool _is_dirty;
313 SGBlockingDeque <SyncItem> waitingTiles;
315 TileAgeCache _completedTiles;
316 TileAgeCache _notFoundItems;
318 SGBlockingDeque <SyncItem> _freshTiles;
321 string _svn_data_server;
323 string _rsync_server;
325 SGPath _persistentCachePath;
328 SGTerraSync::SvnThread::SvnThread() :
334 _updated_tile_count(0),
336 _consecutive_errors(0),
340 _total_kb_downloaded(0),
346 _http.setUserAgent("terrascenery-" SG_STRINGIZE(SG_VERSION));
349 void SGTerraSync::SvnThread::stop()
351 // drop any pending requests
352 waitingTiles.clear();
357 // set stop flag and wake up the thread with an empty request
359 SyncItem w(string(), SyncItem::Stop);
365 bool SGTerraSync::SvnThread::start()
372 SG_LOG(SG_TERRAIN,SG_ALERT,
373 "Cannot start scenery download. Local cache directory is undefined.");
379 SGPath path(_local_dir);
382 SG_LOG(SG_TERRAIN,SG_ALERT,
383 "Cannot start scenery download. Directory '" << _local_dir <<
384 "' does not exist. Set correct directory path or create directory folder.");
390 path.append("version");
393 SG_LOG(SG_TERRAIN,SG_ALERT,
394 "Cannot start scenery download. Directory '" << _local_dir <<
395 "' contains the base package. Use a separate directory.");
401 _use_svn |= _use_built_in;
404 if ((!_use_svn)&&(_rsync_server==""))
406 SG_LOG(SG_TERRAIN,SG_ALERT,
407 "Cannot start scenery download. Rsync scenery server is undefined.");
414 _updated_tile_count = 0;
416 _consecutive_errors = 0;
423 if (_use_svn && _use_built_in)
424 status = "Using built-in SVN support. ";
427 status = "Using external SVN utility '";
428 status += _svn_command;
433 status = "Using RSYNC. ";
436 // not really an alert - but we want to (always) see this message, so user is
437 // aware we're downloading scenery (and using bandwidth).
438 SG_LOG(SG_TERRAIN,SG_ALERT,
439 "Starting automatic scenery download/synchronization. "
441 << "Directory: '" << _local_dir << "'.");
447 bool SGTerraSync::SvnThread::runExternalSyncCommand(const char* dir)
450 SGPath localPath( _local_dir );
451 localPath.append( dir );
455 buf << "\"" << _svn_command << "\" "
456 << svn_options << " "
457 << "\"" << _svn_server << "/" << dir << "\" "
458 << "\"" << localPath.str_native() << "\"";
460 buf << rsync_cmd << " "
461 << "\"" << _rsync_server << "/" << dir << "/\" "
462 << "\"" << localPath.str_native() << "/\"";
467 // windows command line parsing is just lovely...
468 // to allow white spaces, the system call needs this:
469 // ""C:\Program Files\something.exe" somearg "some other arg""
470 // Note: whitespace strings quoted by a pair of "" _and_ the
471 // entire string needs to be wrapped by "" too.
472 // The svn url needs forward slashes (/) as a path separator while
473 // the local path needs windows-native backslash as a path separator.
474 command = "\"" + buf.str() + "\"";
478 SG_LOG(SG_TERRAIN,SG_DEBUG, "sync command '" << command << "'");
481 // tbd: does Windows support "popen"?
482 int rc = system( command.c_str() );
484 FILE* pipe = popen( command.c_str(), "r");
486 // wait for external process to finish
493 SG_LOG(SG_TERRAIN,SG_ALERT,
494 "Failed to synchronize directory '" << dir << "', " <<
495 "error code= " << rc);
501 void SGTerraSync::SvnThread::run()
504 initCompletedTilesPersistentCache();
507 if (_svn_server.empty()) {
508 SG_LOG(SG_TERRAIN,SG_INFO, "Querying closest TerraSync server");
509 ServerSelectQuery* ssq = new ServerSelectQuery;
510 HTTP::Request_ptr req = ssq;
511 _http.makeRequest(req);
512 while (!req->isComplete()) {
516 if (req->readyState() == HTTP::Request::DONE) {
517 _svn_server = ssq->svnUrl();
518 SG_LOG(SG_TERRAIN,SG_INFO, "Closest TerraSync server:" << _svn_server);
520 SG_LOG(SG_TERRAIN,SG_WARN, "Failed to query closest TerraSync server");
523 SG_LOG(SG_TERRAIN,SG_INFO, "Explicit: TerraSync server:" << _svn_server);
526 if (_svn_server.empty()) {
528 _svn_server = "http://foxtrot.mgras.net:8080/terrascenery/trunk/data/Scenery";
543 void SGTerraSync::SvnThread::runExternal()
547 SyncItem next = waitingTiles.pop_front();
551 SyncItem::Status cacheStatus = isPathCached(next);
552 if (cacheStatus != SyncItem::Invalid) {
554 SG_LOG(SG_TERRAIN, SG_DEBUG,
555 "Cache hit for: '" << next._dir << "'");
556 next._status = cacheStatus;
557 _freshTiles.push_back(next);
562 syncPathExternal(next);
564 if ((_allowed_errors >= 0)&&
565 (_consecutive_errors >= _allowed_errors))
570 } // of thread running loop
573 void SGTerraSync::SvnThread::syncPathExternal(const SyncItem& next)
576 SGPath path( _local_dir );
577 path.append( next._dir );
578 bool isNewDirectory = !path.exists();
581 if (isNewDirectory) {
582 int rc = path.create_dir( 0755 );
584 SG_LOG(SG_TERRAIN,SG_ALERT,
585 "Cannot create directory '" << path << "', return code = " << rc );
586 throw sg_exception("Cannot create directory for terrasync", path.str());
590 if (!runExternalSyncCommand(next._dir.c_str())) {
591 throw sg_exception("Running external sync command failed");
593 } catch (sg_exception& e) {
599 updated(next, isNewDirectory);
603 void SGTerraSync::SvnThread::updateSyncSlot(SyncSlot &slot)
605 if (slot.repository.get()) {
606 if (slot.repository->isDoingSync()) {
608 if (slot.stamp.elapsedMSec() > (int)slot.nextWarnTimeout) {
609 SG_LOG(SG_TERRAIN, SG_INFO, "sync taking a long time:" << slot.currentItem._dir << " taken " << slot.stamp.elapsedMSec());
610 SG_LOG(SG_TERRAIN, SG_INFO, "HTTP status:" << _http.hasActiveRequests());
611 slot.nextWarnTimeout += 10000;
614 return; // easy, still working
618 SVNRepository::ResultCode res = slot.repository->failure();
619 if (res == SVNRepository::SVN_ERROR_NOT_FOUND) {
620 notFound(slot.currentItem);
621 } else if (res != SVNRepository::SVN_NO_ERROR) {
622 fail(slot.currentItem);
624 updated(slot.currentItem, slot.isNewDirectory);
625 SG_LOG(SG_TERRAIN, SG_DEBUG, "sync of " << slot.repository->baseUrl() << " finished ("
626 << slot.stamp.elapsedMSec() << " msec");
629 // whatever happened, we're done with this repository instance
631 slot.repository.reset();
634 // init and start sync of the next repository
635 if (!slot.queue.empty()) {
636 slot.currentItem = slot.queue.front();
639 SGPath path(_local_dir);
640 path.append(slot.currentItem._dir);
641 slot.isNewDirectory = !path.exists();
642 if (slot.isNewDirectory) {
643 int rc = path.create_dir( 0755 );
645 SG_LOG(SG_TERRAIN,SG_ALERT,
646 "Cannot create directory '" << path << "', return code = " << rc );
647 fail(slot.currentItem);
650 } // of creating directory step
652 string serverUrl(_svn_server);
653 if (slot.currentItem._type == SyncItem::AIData) {
654 serverUrl = _svn_data_server;
657 slot.repository.reset(new SVNRepository(path, &_http));
658 slot.repository->setBaseUrl(serverUrl + "/" + slot.currentItem._dir);
659 slot.repository->update();
661 slot.nextWarnTimeout = 20000;
664 SG_LOG(SG_TERRAIN, SG_INFO, "sync of " << slot.repository->baseUrl() << " started, queue size is " << slot.queue.size());
668 void SGTerraSync::SvnThread::runInternal()
672 _transfer_rate = _http.transferRateBytesPerSec();
673 // convert from bytes to kbytes
674 _total_kb_downloaded = static_cast<int>(_http.totalBytesDownloaded() / 1024);
679 // drain the waiting tiles queue into the sync slot queues.
680 while (!waitingTiles.empty()) {
681 SyncItem next = waitingTiles.pop_front();
682 SyncItem::Status cacheStatus = isPathCached(next);
683 if (cacheStatus != SyncItem::Invalid) {
685 SG_LOG(SG_TERRAIN, SG_DEBUG, "\nTerraSync Cache hit for: '" << next._dir << "'");
686 next._status = cacheStatus;
687 _freshTiles.push_back(next);
692 unsigned int slot = syncSlotForType(next._type);
693 _syncSlots[slot].queue.push(next);
696 bool anySlotBusy = false;
697 // update each sync slot in turn
698 for (unsigned int slot=0; slot < NUM_SYNC_SLOTS; ++slot) {
699 updateSyncSlot(_syncSlots[slot]);
700 anySlotBusy |= _syncSlots[slot].busy;
705 // wait on the blocking deque here, otherwise we spin
706 // the loop very fast, since _http::update with no connections
707 // active returns immediately.
708 waitingTiles.waitOnNotEmpty();
710 } // of thread running loop
713 SyncItem::Status SGTerraSync::SvnThread::isPathCached(const SyncItem& next) const
715 TileAgeCache::const_iterator ii = _completedTiles.find( next._dir );
716 if (ii == _completedTiles.end()) {
717 ii = _notFoundItems.find( next._dir );
718 // Invalid means 'not cached', otherwise we want to return to
719 // higher levels the cache status
720 return (ii == _notFoundItems.end()) ? SyncItem::Invalid : SyncItem::NotFound;
723 // check if the path still physically exists. This is needed to
724 // cope with the user manipulating our cache dir
725 SGPath p(_local_dir);
728 return SyncItem::Invalid;
731 time_t now = time(0);
732 return (ii->second > now) ? SyncItem::Cached : SyncItem::Invalid;
735 void SGTerraSync::SvnThread::fail(SyncItem failedItem)
737 time_t now = time(0);
738 _consecutive_errors++;
740 failedItem._status = SyncItem::Failed;
741 _freshTiles.push_back(failedItem);
742 SG_LOG(SG_TERRAIN,SG_INFO,
743 "Faield to sync'" << failedItem._dir << "'");
744 _completedTiles[ failedItem._dir ] = now + UpdateInterval::FailedAttempt;
748 void SGTerraSync::SvnThread::notFound(SyncItem item)
750 // treat not found as authorative, so use the same cache expiry
751 // as succesful download. Important for MP models and similar so
752 // we don't spam the server with lookups for models that don't
755 time_t now = time(0);
756 item._status = SyncItem::NotFound;
757 _freshTiles.push_back(item);
759 _notFoundItems[ item._dir ] = now + UpdateInterval::SuccessfulAttempt;
760 writeCompletedTilesPersistentCache();
763 void SGTerraSync::SvnThread::updated(SyncItem item, bool isNewDirectory)
765 time_t now = time(0);
766 _consecutive_errors = 0;
768 SG_LOG(SG_TERRAIN,SG_INFO,
769 "Successfully synchronized directory '" << item._dir << "'");
771 item._status = SyncItem::Updated;
772 if (item._type == SyncItem::Tile) {
773 _updated_tile_count++;
776 _freshTiles.push_back(item);
777 _completedTiles[ item._dir ] = now + UpdateInterval::SuccessfulAttempt;
779 writeCompletedTilesPersistentCache();
782 void SGTerraSync::SvnThread::initCompletedTilesPersistentCache()
784 if (!_persistentCachePath.exists()) {
788 SGPropertyNode_ptr cacheRoot(new SGPropertyNode);
789 time_t now = time(0);
792 readProperties(_persistentCachePath.str(), cacheRoot);
793 } catch (sg_exception& e) {
794 SG_LOG(SG_TERRAIN, SG_INFO, "corrupted persistent cache, discarding");
798 for (int i=0; i<cacheRoot->nChildren(); ++i) {
799 SGPropertyNode* entry = cacheRoot->getChild(i);
800 bool isNotFound = (strcmp(entry->getName(), "not-found") == 0);
801 string tileName = entry->getStringValue("path");
802 time_t stamp = entry->getIntValue("stamp");
808 _completedTiles[tileName] = stamp;
810 _notFoundItems[tileName] = stamp;
815 void SGTerraSync::SvnThread::writeCompletedTilesPersistentCache() const
818 if (_persistentCachePath.isNull()) {
822 std::ofstream f(_persistentCachePath.c_str(), std::ios::trunc);
827 SGPropertyNode_ptr cacheRoot(new SGPropertyNode);
828 TileAgeCache::const_iterator it = _completedTiles.begin();
829 for (; it != _completedTiles.end(); ++it) {
830 SGPropertyNode* entry = cacheRoot->addChild("entry");
831 entry->setStringValue("path", it->first);
832 entry->setIntValue("stamp", it->second);
835 it = _notFoundItems.begin();
836 for (; it != _notFoundItems.end(); ++it) {
837 SGPropertyNode* entry = cacheRoot->addChild("not-found");
838 entry->setStringValue("path", it->first);
839 entry->setIntValue("stamp", it->second);
842 writeProperties(f, cacheRoot, true /* write_all */);
846 ///////////////////////////////////////////////////////////////////////////////
847 // SGTerraSync ////////////////////////////////////////////////////////////////
848 ///////////////////////////////////////////////////////////////////////////////
849 SGTerraSync::SGTerraSync() :
854 _svnThread = new SvnThread();
855 _log = new BufferedLogCallback(SG_TERRAIN, SG_INFO);
856 _log->truncateAt(255);
858 sglog().addCallback(_log);
861 SGTerraSync::~SGTerraSync()
865 sglog().removeCallback(_log);
867 _tiedProperties.Untie();
870 void SGTerraSync::setRoot(SGPropertyNode_ptr root)
872 _terraRoot = root->getNode("/sim/terrasync",true);
875 void SGTerraSync::init()
884 _terraRoot->setBoolValue("built-in-svn-available",svn_built_in_available);
889 void SGTerraSync::shutdown()
894 void SGTerraSync::reinit()
896 // do not reinit when enabled and we're already up and running
897 if ((_terraRoot->getBoolValue("enabled",false))&&
898 (_svnThread->_active && _svnThread->_running))
905 if (_terraRoot->getBoolValue("enabled",false))
907 _svnThread->setSvnServer(_terraRoot->getStringValue("svn-server",""));
908 _svnThread->setSvnDataServer(_terraRoot->getStringValue("svn-data-server",""));
909 _svnThread->setRsyncServer(_terraRoot->getStringValue("rsync-server",""));
910 _svnThread->setLocalDir(_terraRoot->getStringValue("scenery-dir",""));
911 _svnThread->setAllowedErrorCount(_terraRoot->getIntValue("max-errors",5));
912 _svnThread->setUseBuiltin(_terraRoot->getBoolValue("use-built-in-svn",true));
913 _svnThread->setCachePath(SGPath(_terraRoot->getStringValue("cache-path","")));
914 _svnThread->setCacheHits(_terraRoot->getIntValue("cache-hit", 0));
915 _svnThread->setUseSvn(_terraRoot->getBoolValue("use-svn",true));
916 _svnThread->setExtSvnUtility(_terraRoot->getStringValue("ext-svn-utility","svn"));
918 if (_svnThread->start())
920 syncAirportsModels();
924 _stalledNode->setBoolValue(_svnThread->_stalled);
927 void SGTerraSync::bind()
934 _tiedProperties.Tie( _terraRoot->getNode("busy", true), (bool*) &_svnThread->_busy );
935 _tiedProperties.Tie( _terraRoot->getNode("active", true), (bool*) &_svnThread->_active );
936 _tiedProperties.Tie( _terraRoot->getNode("update-count", true), (int*) &_svnThread->_success_count );
937 _tiedProperties.Tie( _terraRoot->getNode("error-count", true), (int*) &_svnThread->_fail_count );
938 _tiedProperties.Tie( _terraRoot->getNode("tile-count", true), (int*) &_svnThread->_updated_tile_count );
939 _tiedProperties.Tie( _terraRoot->getNode("cache-hits", true), (int*) &_svnThread->_cache_hits );
940 _tiedProperties.Tie( _terraRoot->getNode("transfer-rate-bytes-sec", true), (int*) &_svnThread->_transfer_rate );
942 // use kbytes here because propety doesn't support 64-bit and we might conceivably
943 // download more than 2G in a single session
944 _tiedProperties.Tie( _terraRoot->getNode("downloaded-kbytes", true), (int*) &_svnThread->_total_kb_downloaded );
946 _terraRoot->getNode("busy", true)->setAttribute(SGPropertyNode::WRITE,false);
947 _terraRoot->getNode("active", true)->setAttribute(SGPropertyNode::WRITE,false);
948 _terraRoot->getNode("update-count", true)->setAttribute(SGPropertyNode::WRITE,false);
949 _terraRoot->getNode("error-count", true)->setAttribute(SGPropertyNode::WRITE,false);
950 _terraRoot->getNode("tile-count", true)->setAttribute(SGPropertyNode::WRITE,false);
951 _terraRoot->getNode("use-built-in-svn", true)->setAttribute(SGPropertyNode::USERARCHIVE,false);
952 _terraRoot->getNode("use-svn", true)->setAttribute(SGPropertyNode::USERARCHIVE,false);
953 // stalled is used as a signal handler (to connect listeners triggering GUI pop-ups)
954 _stalledNode = _terraRoot->getNode("stalled", true);
955 _stalledNode->setBoolValue(_svnThread->_stalled);
956 _stalledNode->setAttribute(SGPropertyNode::PRESERVE,true);
959 void SGTerraSync::unbind()
962 _tiedProperties.Untie();
967 _stalledNode.clear();
971 void SGTerraSync::update(double)
973 static SGBucket bucket;
974 if (_svnThread->isDirty())
976 if (!_svnThread->_active)
978 if (_svnThread->_stalled)
980 SG_LOG(SG_TERRAIN,SG_ALERT,
981 "Automatic scenery download/synchronization stalled. Too many errors.");
985 // not really an alert - just always show this message
986 SG_LOG(SG_TERRAIN,SG_ALERT,
987 "Automatic scenery download/synchronization has stopped.");
989 _stalledNode->setBoolValue(_svnThread->_stalled);
992 while (_svnThread->hasNewTiles())
994 SyncItem next = _svnThread->getNewTile();
996 if ((next._type == SyncItem::Tile) || (next._type == SyncItem::AIData)) {
997 _activeTileDirs.erase(next._dir);
999 } // of freshly synced items
1003 bool SGTerraSync::isIdle() {return _svnThread->isIdle();}
1005 void SGTerraSync::syncAirportsModels()
1007 static const char* bounds = "MZAJKL"; // airport sync order: K-L, A-J, M-Z
1008 // note "request" method uses LIFO order, i.e. processes most recent request first
1009 for( unsigned i = 0; i < strlen(bounds)/2; i++ )
1011 for ( char synced_other = bounds[2*i]; synced_other <= bounds[2*i+1]; synced_other++ )
1014 dir << "Airports/" << synced_other;
1015 SyncItem w(dir.str(), SyncItem::AirportData);
1016 _svnThread->request( w );
1020 SyncItem w("Models", SyncItem::SharedModels);
1021 _svnThread->request( w );
1024 void SGTerraSync::syncAreaByPath(const std::string& aPath)
1026 const char* terrainobjects[3] = { "Terrain/", "Objects/", 0 };
1027 for (const char** tree = &terrainobjects[0]; *tree; tree++)
1029 std::string dir = string(*tree) + aPath;
1030 if (_activeTileDirs.find(dir) != _activeTileDirs.end()) {
1034 _activeTileDirs.insert(dir);
1035 SyncItem w(dir, SyncItem::Tile);
1036 _svnThread->request( w );
1040 bool SGTerraSync::scheduleTile(const SGBucket& bucket)
1042 std::string basePath = bucket.gen_base_path();
1043 syncAreaByPath(basePath);
1047 bool SGTerraSync::isTileDirPending(const std::string& sceneryDir) const
1049 if (!_svnThread->_running) {
1053 const char* terrainobjects[3] = { "Terrain/", "Objects/", 0 };
1054 for (const char** tree = &terrainobjects[0]; *tree; tree++) {
1055 string s = *tree + sceneryDir;
1056 if (_activeTileDirs.find(s) != _activeTileDirs.end()) {
1064 void SGTerraSync::scheduleDataDir(const std::string& dataDir)
1066 if (_activeTileDirs.find(dataDir) != _activeTileDirs.end()) {
1070 _activeTileDirs.insert(dataDir);
1071 SyncItem w(dataDir, SyncItem::AIData);
1072 _svnThread->request( w );
1076 bool SGTerraSync::isDataDirPending(const std::string& dataDir) const
1078 if (!_svnThread->_running) {
1082 return (_activeTileDirs.find(dataDir) != _activeTileDirs.end());
1085 void SGTerraSync::reposition()