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)
40 # ifndef HAVE_SVN_CLIENT_H
46 #include <stdlib.h> // atoi() atof() abs() system()
47 #include <signal.h> // signal()
55 #include <simgear/compiler.h>
57 #include "terrasync.hxx"
58 #include <simgear/bucket/newbucket.hxx>
59 #include <simgear/misc/sg_path.hxx>
60 #include <simgear/misc/strutils.hxx>
61 #include <simgear/threads/SGQueue.hxx>
62 #include <simgear/misc/sg_dir.hxx>
63 #include <simgear/debug/BufferedLogCallback.hxx>
66 # include <simgear/io/HTTPClient.hxx>
67 # include <simgear/io/SVNRepository.hxx>
70 #ifdef HAVE_SVN_CLIENT_H
71 # ifdef HAVE_LIBSVN_CLIENT_1
72 # include <svn_version.h>
73 # include <svn_auth.h>
74 # include <svn_client.h>
75 # include <svn_cmdline.h>
76 # include <svn_pools.h>
78 # undef HAVE_SVN_CLIENT_H
82 #ifdef HAVE_SVN_CLIENT_H
83 static const svn_version_checklist_t mysvn_checklist[] = {
84 { "svn_subr", svn_subr_version },
85 { "svn_client", svn_client_version },
90 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
91 static const bool svn_built_in_available = true;
93 static const bool svn_built_in_available = false;
96 using namespace simgear;
99 const char* rsync_cmd =
100 "rsync --verbose --archive --delete --perms --owner --group";
102 const char* svn_options =
105 namespace UpdateInterval
107 // interval in seconds to allow an update to repeat after a successful update (=daily)
108 static const double SuccessfulAttempt = 24*60*60;
109 // interval in seconds to allow another update after a failed attempt (10 minutes)
110 static const double FailedAttempt = 10*60;
113 typedef map<string,time_t> CompletedTiles;
115 ///////////////////////////////////////////////////////////////////////////////
116 // helper functions ///////////////////////////////////////////////////////////
117 ///////////////////////////////////////////////////////////////////////////////
118 string stripPath(string path)
120 // svn doesn't like trailing white-spaces or path separators - strip them!
121 path = simgear::strutils::strip(path);
122 size_t slen = path.length();
124 ((path[slen-1]=='/')||(path[slen-1]=='\\')))
128 return path.substr(0,slen);
131 bool hasWhitespace(string path)
133 return path.find(' ')!=string::npos;
136 ///////////////////////////////////////////////////////////////////////////////
137 // WaitingTile ////////////////////////////////////////////////////////////////
138 ///////////////////////////////////////////////////////////////////////////////
142 WaitingTile(string dir,bool refresh) :
143 _dir(dir), _refreshScenery(refresh) {}
145 bool _refreshScenery;
148 ///////////////////////////////////////////////////////////////////////////////
149 // SGTerraSync::SvnThread /////////////////////////////////////////////////////
150 ///////////////////////////////////////////////////////////////////////////////
151 class SGTerraSync::SvnThread : public SGThread
155 virtual ~SvnThread( ) { stop(); }
160 bool isIdle() {return waitingTiles.empty();}
161 void request(const WaitingTile& dir) {waitingTiles.push_front(dir);}
162 bool isDirty() { bool r = _is_dirty;_is_dirty = false;return r;}
163 bool hasNewTiles() { return !_freshTiles.empty();}
164 WaitingTile getNewTile() { return _freshTiles.pop_front();}
166 void setSvnServer(string server) { _svn_server = stripPath(server);}
167 void setExtSvnUtility(string svn_util) { _svn_command = simgear::strutils::strip(svn_util);}
168 void setRsyncServer(string server) { _rsync_server = simgear::strutils::strip(server);}
169 void setLocalDir(string dir) { _local_dir = stripPath(dir);}
170 string getLocalDir() { return _local_dir;}
171 void setUseSvn(bool use_svn) { _use_svn = use_svn;}
172 void setAllowedErrorCount(int errors) {_allowed_errors = errors;}
174 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
175 void setUseBuiltin(bool built_in) { _use_built_in = built_in;}
178 volatile bool _active;
179 volatile bool _running;
181 volatile bool _stalled;
182 volatile int _fail_count;
183 volatile int _updated_tile_count;
184 volatile int _success_count;
185 volatile int _consecutive_errors;
186 volatile int _allowed_errors;
190 bool syncTree(const char* dir, bool& isNewDirectory);
191 bool syncTreeExternal(const char* dir);
194 #if defined(SG_SVN_CLIENT)
195 bool syncTreeInternal(const char* dir);
198 std::auto_ptr<SVNRepository> _repository;
199 #elif defined(HAVE_SVN_CLIENT_H)
200 static int svnClientSetup(void);
201 bool syncTreeInternal(const char* dir);
205 // Things we need for doing subversion checkout - often
206 static apr_pool_t *_svn_pool;
207 static svn_client_ctx_t *_svn_ctx;
208 static svn_opt_revision_t *_svn_rev;
209 static svn_opt_revision_t *_svn_rev_peg;
212 volatile bool _is_dirty;
214 SGBlockingDeque <WaitingTile> waitingTiles;
215 CompletedTiles _completedTiles;
216 SGBlockingDeque <WaitingTile> _freshTiles;
220 string _rsync_server;
224 #ifdef HAVE_SVN_CLIENT_H
225 apr_pool_t* SGTerraSync::SvnThread::_svn_pool = NULL;
226 svn_client_ctx_t* SGTerraSync::SvnThread::_svn_ctx = NULL;
227 svn_opt_revision_t* SGTerraSync::SvnThread::_svn_rev = NULL;
228 svn_opt_revision_t* SGTerraSync::SvnThread::_svn_rev_peg = NULL;
231 SGTerraSync::SvnThread::SvnThread() :
237 _updated_tile_count(0),
239 _consecutive_errors(0),
241 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
248 #ifdef HAVE_SVN_CLIENT_H
249 int errCode = SGTerraSync::SvnThread::svnClientSetup();
250 if (errCode != EXIT_SUCCESS)
252 SG_LOG(SG_TERRAIN,SG_ALERT,
253 "Failed to initialize built-in SVN client, error = " << errCode);
258 void SGTerraSync::SvnThread::stop()
260 // drop any pending requests
261 waitingTiles.clear();
266 // set stop flag and wake up the thread with an empty request
268 WaitingTile w("",false);
274 bool SGTerraSync::SvnThread::start()
281 SG_LOG(SG_TERRAIN,SG_ALERT,
282 "Cannot start scenery download. Local cache directory is undefined.");
288 SGPath path(_local_dir);
291 SG_LOG(SG_TERRAIN,SG_ALERT,
292 "Cannot start scenery download. Directory '" << _local_dir <<
293 "' does not exist. Set correct directory path or create directory folder.");
299 path.append("version");
302 SG_LOG(SG_TERRAIN,SG_ALERT,
303 "Cannot start scenery download. Directory '" << _local_dir <<
304 "' contains the base package. Use a separate directory.");
310 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
311 _use_svn |= _use_built_in;
314 if ((_use_svn)&&(_svn_server==""))
316 SG_LOG(SG_TERRAIN,SG_ALERT,
317 "Cannot start scenery download. Subversion scenery server is undefined.");
322 if ((!_use_svn)&&(_rsync_server==""))
324 SG_LOG(SG_TERRAIN,SG_ALERT,
325 "Cannot start scenery download. Rsync scenery server is undefined.");
332 _updated_tile_count = 0;
334 _consecutive_errors = 0;
340 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
341 if (_use_svn && _use_built_in)
342 status = "Using built-in SVN support. ";
347 status = "Using external SVN utility '";
348 status += _svn_command;
353 status = "Using RSYNC. ";
356 // not really an alert - but we want to (always) see this message, so user is
357 // aware we're downloading scenery (and using bandwidth).
358 SG_LOG(SG_TERRAIN,SG_ALERT,
359 "Starting automatic scenery download/synchronization. "
361 << "Directory: '" << _local_dir << "'.");
367 // sync one directory tree
368 bool SGTerraSync::SvnThread::syncTree(const char* dir, bool& isNewDirectory)
371 SGPath path( _local_dir );
374 isNewDirectory = !path.exists();
377 rc = path.create_dir( 0755 );
380 SG_LOG(SG_TERRAIN,SG_ALERT,
381 "Cannot create directory '" << dir << "', return code = " << rc );
386 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
388 return syncTreeInternal(dir);
392 return syncTreeExternal(dir);
396 #if defined(SG_SVN_CLIENT)
398 bool SGTerraSync::SvnThread::syncTreeInternal(const char* dir)
400 ostringstream command;
401 command << _svn_server << "/" << dir;
403 SGPath path(_local_dir);
405 _repository.reset(new SVNRepository(path, &_http));
406 _repository->setBaseUrl(command.str());
410 SG_LOG(SG_IO, SG_DEBUG, "terrasync: will sync " << command.str());
411 _repository->update();
414 while (!_stop && _repository->isDoingSync()) {
418 if (_repository->failure() == SVNRepository::ERROR_NOT_FOUND) {
419 // this is fine, but maybe we should use a different return code
420 // in the future to higher layers can distuinguish this case
421 } else if (_repository->failure() != SVNRepository::NO_ERROR) {
424 SG_LOG(SG_IO, SG_DEBUG, "sync of " << command.str() << " finished ("
425 << st.elapsedMSec() << " msec");
432 #elif defined(HAVE_SVN_CLIENT_H)
434 bool SGTerraSync::SvnThread::syncTreeInternal(const char* dir)
436 SG_LOG(SG_TERRAIN,SG_DEBUG, "Synchronizing scenery directory " << dir);
439 SG_LOG(SG_TERRAIN,SG_ALERT,
440 "Built-in SVN client failed to initialize.");
444 ostringstream command;
445 command << _svn_server << "/" << dir;
447 ostringstream dest_base_dir;
448 dest_base_dir << _local_dir << "/" << dir;
450 apr_pool_t *subpool = svn_pool_create(_svn_pool);
452 svn_error_t *err = NULL;
453 #if (SVN_VER_MINOR >= 5)
454 err = svn_client_checkout3(NULL,
455 command.str().c_str(),
456 dest_base_dir.str().c_str(),
460 0, // ignore-externals = false
461 0, // allow unver obstructions = false
466 err = svn_client_checkout2(NULL,
467 command.str().c_str(),
468 dest_base_dir.str().c_str(),
471 1, // recurse=true - same as svn_depth_infinity for checkout3 above
472 0, // ignore externals = false
477 bool ReturnValue = true;
480 // Report errors from the checkout attempt
481 if (err->apr_err == SVN_ERR_RA_ILLEGAL_URL)
483 // ignore errors when remote path doesn't exist (no scenery data for ocean areas)
487 SG_LOG(SG_TERRAIN,SG_ALERT,
488 "Failed to synchronize directory '" << dir << "', " <<
489 err->message << " (code " << err->apr_err << ").");
490 svn_error_clear(err);
492 err = svn_client_cleanup(dest_base_dir.str().c_str(),
496 SG_LOG(SG_TERRAIN,SG_ALERT,
497 "SVN repository cleanup successful for '" << dir << "'.");
503 SG_LOG(SG_TERRAIN,SG_DEBUG, "Done with scenery directory " << dir);
505 svn_pool_destroy(subpool);
508 #endif // of HAVE_SVN_CLIENT_H
510 bool SGTerraSync::SvnThread::syncTreeExternal(const char* dir)
513 SGPath localPath( _local_dir );
514 localPath.append( dir );
518 buf << "\"" << _svn_command << "\" "
519 << svn_options << " "
520 << "\"" << _svn_server << "/" << dir << "\" "
521 << "\"" << localPath.str_native() << "\"";
523 buf << rsync_cmd << " "
524 << "\"" << _rsync_server << "/" << dir << "/\" "
525 << "\"" << localPath.str_native() << "/\"";
530 // windows command line parsing is just lovely...
531 // to allow white spaces, the system call needs this:
532 // ""C:\Program Files\something.exe" somearg "some other arg""
533 // Note: whitespace strings quoted by a pair of "" _and_ the
534 // entire string needs to be wrapped by "" too.
535 // The svn url needs forward slashes (/) as a path separator while
536 // the local path needs windows-native backslash as a path separator.
537 command = "\"" + buf.str() + "\"";
541 SG_LOG(SG_TERRAIN,SG_DEBUG, "sync command '" << command << "'");
544 // tbd: does Windows support "popen"?
545 int rc = system( command.c_str() );
547 FILE* pipe = popen( command.c_str(), "r");
549 // wait for external process to finish
556 SG_LOG(SG_TERRAIN,SG_ALERT,
557 "Failed to synchronize directory '" << dir << "', " <<
558 "error code= " << rc);
564 void SGTerraSync::SvnThread::run()
569 WaitingTile next = waitingTiles.pop_front();
573 CompletedTiles::iterator ii =
574 _completedTiles.find( next._dir );
575 time_t now = time(0);
576 if ((ii == _completedTiles.end())||
579 bool isNewDirectory = false;
582 if (!syncTree(next._dir.c_str(),isNewDirectory))
584 _consecutive_errors++;
586 _completedTiles[ next._dir ] = now + UpdateInterval::FailedAttempt;
590 _consecutive_errors = 0;
592 SG_LOG(SG_TERRAIN,SG_INFO,
593 "Successfully synchronized directory '" << next._dir << "'");
594 if (next._refreshScenery)
597 _updated_tile_count++;
600 // for now only report new directories to refresh display
601 // (i.e. only when ocean needs to be replaced with actual data)
602 _freshTiles.push_back(next);
606 _completedTiles[ next._dir ] = now + UpdateInterval::SuccessfulAttempt;
611 if ((_allowed_errors >= 0)&&
612 (_consecutive_errors >= _allowed_errors))
624 #if defined(HAVE_SVN_CLIENT_H)
625 // Configure our subversion session
626 int SGTerraSync::SvnThread::svnClientSetup(void)
628 // Are we already prepared?
629 if (_svn_pool) return EXIT_SUCCESS;
630 // No, so initialize svn internals generally
633 // there is a segfault when providing an error stream.
634 // Apparently, calling setvbuf with a nul buffer is
635 // not supported under msvc 7.1 ( code inside svn_cmdline_init )
636 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
639 // revert locale setting
640 setlocale(LC_ALL,"C");
642 /* svn_cmdline_init configures the locale. Setup environment to ensure the
643 * default "C" locale remains active, since fgfs isn't locale aware - especially
644 * requires "." as decimal point in strings containing floating point varibales. */
645 setenv("LC_ALL", "C", 1);
647 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
651 apr_pool_t *pool = NULL;
653 apr_allocator_t* allocator = NULL;
654 int aprErr = apr_allocator_create(&allocator);
655 if (aprErr != APR_SUCCESS)
658 apr_pool_create_ex(&pool, NULL /* parent pool */, NULL /* abort func */, allocator);
660 svn_error_t *err = NULL;
661 SVN_VERSION_DEFINE(_svn_version);
662 err = svn_ver_check_list(&_svn_version, mysvn_checklist);
664 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
665 err = svn_ra_initialize(pool);
667 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
668 char *config_dir = NULL;
669 err = svn_config_ensure(config_dir, pool);
671 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
672 err = svn_client_create_context(&_svn_ctx, pool);
674 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
675 err = svn_config_get_config(&(_svn_ctx->config),
678 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
680 cfg = ( svn_config_t*) apr_hash_get(
682 SVN_CONFIG_CATEGORY_CONFIG,
683 APR_HASH_KEY_STRING);
685 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
687 svn_auth_baton_t *ab=NULL;
689 #if (SVN_VER_MINOR >= 6)
690 err = svn_cmdline_create_auth_baton (&ab,
691 TRUE, NULL, NULL, config_dir, TRUE, FALSE, cfg,
692 _svn_ctx->cancel_func, _svn_ctx->cancel_baton, pool);
694 err = svn_cmdline_setup_auth_baton(&ab,
695 TRUE, NULL, NULL, config_dir, TRUE, cfg,
696 _svn_ctx->cancel_func, _svn_ctx->cancel_baton, pool);
700 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
702 _svn_ctx->auth_baton = ab;
703 #if (SVN_VER_MINOR >= 5)
704 _svn_ctx->conflict_func = NULL;
705 _svn_ctx->conflict_baton = NULL;
708 // Now our magic revisions
709 _svn_rev = (svn_opt_revision_t*) apr_palloc(pool,
710 sizeof(svn_opt_revision_t));
713 _svn_rev_peg = (svn_opt_revision_t*) apr_palloc(pool,
714 sizeof(svn_opt_revision_t));
717 _svn_rev->kind = svn_opt_revision_head;
718 _svn_rev_peg->kind = svn_opt_revision_unspecified;
719 // Success if we got this far
723 #endif // of defined(HAVE_SVN_CLIENT_H)
725 ///////////////////////////////////////////////////////////////////////////////
726 // SGTerraSync ////////////////////////////////////////////////////////////////
727 ///////////////////////////////////////////////////////////////////////////////
728 SGTerraSync::SGTerraSync(SGPropertyNode_ptr root) :
732 _terraRoot(root->getNode("/sim/terrasync",true)),
736 _svnThread = new SvnThread();
737 _log = new BufferedLogCallback(SG_TERRAIN, SG_INFO);
738 _log->truncateAt(255);
740 sglog().addCallback(_log);
743 SGTerraSync::~SGTerraSync()
745 _tiedProperties.Untie();
748 sglog().removeCallback(_log);
752 void SGTerraSync::init()
754 _refreshDisplay = _terraRoot->getNode("refresh-display",true);
755 _terraRoot->setBoolValue("built-in-svn-available",svn_built_in_available);
759 void SGTerraSync::reinit()
761 // do not reinit when enabled and we're already up and running
762 if ((_terraRoot->getBoolValue("enabled",false))&&
763 (_svnThread->_active && _svnThread->_running))
768 if (_terraRoot->getBoolValue("enabled",false))
770 _svnThread->setSvnServer(_terraRoot->getStringValue("svn-server",""));
771 _svnThread->setRsyncServer(_terraRoot->getStringValue("rsync-server",""));
772 _svnThread->setLocalDir(_terraRoot->getStringValue("scenery-dir",""));
773 _svnThread->setAllowedErrorCount(_terraRoot->getIntValue("max-errors",5));
775 #if defined(HAVE_SVN_CLIENT_H) or defined(SG_SVN_CLIENT)
776 _svnThread->setUseBuiltin(_terraRoot->getBoolValue("use-built-in-svn",true));
778 _terraRoot->setBoolValue("use-built-in-svn",false);
780 _svnThread->setUseSvn(_terraRoot->getBoolValue("use-svn",true));
781 _svnThread->setExtSvnUtility(_terraRoot->getStringValue("ext-svn-utility","svn"));
783 if (_svnThread->start())
785 syncAirportsModels();
786 if (last_lat != NOWHERE && last_lon != NOWHERE)
788 // reschedule most recent position
793 schedulePosition(lat, lon);
798 _stalledNode->setBoolValue(_svnThread->_stalled);
801 void SGTerraSync::bind()
803 _tiedProperties.Tie( _terraRoot->getNode("busy", true), (bool*) &_svnThread->_busy );
804 _tiedProperties.Tie( _terraRoot->getNode("active", true), (bool*) &_svnThread->_active );
805 _tiedProperties.Tie( _terraRoot->getNode("update-count", true), (int*) &_svnThread->_success_count );
806 _tiedProperties.Tie( _terraRoot->getNode("error-count", true), (int*) &_svnThread->_fail_count );
807 _tiedProperties.Tie( _terraRoot->getNode("tile-count", true), (int*) &_svnThread->_updated_tile_count );
808 _terraRoot->getNode("busy", true)->setAttribute(SGPropertyNode::WRITE,false);
809 _terraRoot->getNode("active", true)->setAttribute(SGPropertyNode::WRITE,false);
810 _terraRoot->getNode("update-count", true)->setAttribute(SGPropertyNode::WRITE,false);
811 _terraRoot->getNode("error-count", true)->setAttribute(SGPropertyNode::WRITE,false);
812 _terraRoot->getNode("tile-count", true)->setAttribute(SGPropertyNode::WRITE,false);
813 _terraRoot->getNode("use-built-in-svn", true)->setAttribute(SGPropertyNode::USERARCHIVE,false);
814 _terraRoot->getNode("use-svn", true)->setAttribute(SGPropertyNode::USERARCHIVE,false);
815 // stalled is used as a signal handler (to connect listeners triggering GUI pop-ups)
816 _stalledNode = _terraRoot->getNode("stalled", true);
817 _stalledNode->setBoolValue(_svnThread->_stalled);
818 _stalledNode->setAttribute(SGPropertyNode::PRESERVE,true);
821 void SGTerraSync::unbind()
824 _tiedProperties.Untie();
827 void SGTerraSync::update(double)
829 static SGBucket bucket;
830 if (_svnThread->isDirty())
832 if (!_svnThread->_active)
834 if (_svnThread->_stalled)
836 SG_LOG(SG_TERRAIN,SG_ALERT,
837 "Automatic scenery download/synchronization stalled. Too many errors.");
841 // not really an alert - just always show this message
842 SG_LOG(SG_TERRAIN,SG_ALERT,
843 "Automatic scenery download/synchronization has stopped.");
845 _stalledNode->setBoolValue(_svnThread->_stalled);
848 if (!_refreshDisplay->getBoolValue())
851 while (_svnThread->hasNewTiles())
853 WaitingTile next = _svnThread->getNewTile();
854 if (next._refreshScenery)
856 refreshScenery(_svnThread->getLocalDir(),next._dir);
862 void SGTerraSync::refreshScenery(SGPath path,const string& relativeDir)
864 // find tiles to be refreshed
867 path.append(relativeDir);
870 simgear::Dir dir(path);
871 //TODO need to be smarter here. only update tiles which actually
872 // changed recently. May also be possible to use information from the
873 // built-in SVN client directly (instead of checking directory contents).
874 PathList tileList = dir.children(simgear::Dir::TYPE_FILE, ".stg");
875 for (unsigned int i=0; i<tileList.size(); ++i)
877 // reload scenery tile
878 long index = atoi(tileList[i].file().c_str());
879 _refreshCb(_userCbData, index);
885 bool SGTerraSync::isIdle() {return _svnThread->isIdle();}
887 void SGTerraSync::setTileRefreshCb(SGTerraSyncCallback refreshCb, void* userCbData)
889 _refreshCb = refreshCb;
890 _userCbData = userCbData;
893 void SGTerraSync::syncAirportsModels()
895 static const char* bounds = "MZAJKL"; // airport sync order: K-L, A-J, M-Z
896 // note "request" method uses LIFO order, i.e. processes most recent request first
897 for( unsigned i = 0; i < strlen(bounds)/2; i++ )
899 for ( char synced_other = bounds[2*i]; synced_other <= bounds[2*i+1]; synced_other++ )
902 dir << "Airports/" << synced_other;
903 WaitingTile w(dir.str(),false);
904 _svnThread->request( w );
907 WaitingTile w("Models",false);
908 _svnThread->request( w );
912 void SGTerraSync::syncArea( int lat, int lon )
914 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
917 int baselat, baselon;
920 int base = (int)(lat / 10);
921 if ( lat == base * 10 ) {
924 baselat = (base - 1) * 10;
928 baselat = (int)(lat / 10) * 10;
932 int base = (int)(lon / 10);
933 if ( lon == base * 10 ) {
936 baselon = (base - 1) * 10;
940 baselon = (int)(lon / 10) * 10;
944 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
946 for (const char** tree = &terrainobjects[0]; *tree; tree++)
949 dir << *tree << "/" << setfill('0')
950 << EW << setw(3) << abs(baselon) << NS << setw(2) << abs(baselat) << "/"
951 << EW << setw(3) << abs(lon) << NS << setw(2) << abs(lat);
952 WaitingTile w(dir.str(),refresh);
953 _svnThread->request( w );
959 void SGTerraSync::syncAreas( int lat, int lon, int lat_dir, int lon_dir )
961 if ( lat_dir == 0 && lon_dir == 0 ) {
962 // do surrounding 8 1x1 degree areas.
963 for ( int i = lat - 1; i <= lat + 1; ++i ) {
964 for ( int j = lon - 1; j <= lon + 1; ++j ) {
965 if ( i != lat || j != lon ) {
971 if ( lat_dir != 0 ) {
972 syncArea( lat + lat_dir, lon - 1 );
973 syncArea( lat + lat_dir, lon + 1 );
974 syncArea( lat + lat_dir, lon );
976 if ( lon_dir != 0 ) {
977 syncArea( lat - 1, lon + lon_dir );
978 syncArea( lat + 1, lon + lon_dir );
979 syncArea( lat, lon + lon_dir );
983 // do current 1x1 degree area first
984 syncArea( lat, lon );
988 bool SGTerraSync::schedulePosition(int lat, int lon)
992 // Ignore messages where the location does not change
993 if ( lat != last_lat || lon != last_lon )
995 if (_svnThread->_running)
997 SG_LOG(SG_TERRAIN,SG_DEBUG, "Requesting scenery update for position " <<
1001 if ( last_lat != NOWHERE && last_lon != NOWHERE )
1003 int dist = lat - last_lat;
1006 lat_dir = dist / abs(dist);
1012 dist = lon - last_lon;
1015 lon_dir = dist / abs(dist);
1022 SG_LOG(SG_TERRAIN,SG_DEBUG, "Scenery update for " <<
1023 "lat = " << lat << ", lon = " << lon <<
1024 ", lat_dir = " << lat_dir << ", " <<
1025 "lon_dir = " << lon_dir);
1027 syncAreas( lat, lon, lat_dir, lon_dir );