1 // terrasync.cxx -- "JIT" scenery fetcher
3 // Written by Curtis Olson, started 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>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <stdlib.h> // atoi() atof() abs() system()
35 #include <simgear/compiler.h>
42 #include <plib/netSocket.h>
45 #include <simgear/bucket/newbucket.hxx>
46 #include <simgear/misc/sg_path.hxx>
48 #ifdef HAVE_SVN_CLIENT_H
49 # ifdef HAVE_LIBSVN_CLIENT_1
50 # include <svn_auth.h>
51 # include <svn_client.h>
52 # include <svn_cmdline.h>
53 # include <svn_pools.h>
55 # undef HAVE_SVN_CLIENT_H
63 const char* source_base = NULL;
64 const char* svn_base =
65 "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
66 const char* rsync_base = "scenery.flightgear.org::Scenery";
67 const char* dest_base = "terrasyncdir";
68 const char* rsync_cmd =
69 "rsync --verbose --archive --delete --perms --owner --group";
71 #ifdef HAVE_SVN_CLIENT_H
75 const char* svn_cmd = "svn checkout";
79 static void usage( const string& prog ) {
80 cout << "Usage: " << endl
81 << prog << " -p <port> "
82 << "-R [ -s <rsync_source> ] -d <dest>" << endl
83 << prog << " -p <port> "
84 << "-S [ -s <svn_source> ] -d <dest>" << endl;
85 #ifdef HAVE_SVN_CLIENT_H
86 cout << " (defaults to the built in subversion)" << endl;
88 cout << " (defaults to rsync, using external commands)" << endl;
92 std::deque<std::string> waitingTiles;
93 typedef std::map<std::string,time_t> CompletedTiles;
94 CompletedTiles completedTiles;
96 #ifdef HAVE_SVN_CLIENT_H
98 // Things we need for doing subversion checkout - often
99 apr_pool_t *mysvn_pool = NULL;
100 svn_client_ctx_t *mysvn_ctx = NULL;
101 svn_opt_revision_t *mysvn_rev = NULL;
102 svn_opt_revision_t *mysvn_rev_peg = NULL;
104 static const svn_version_checklist_t mysvn_checklist[] = {
105 { "svn_subr", svn_subr_version },
106 { "svn_client", svn_client_version },
107 { "svn_wc", svn_wc_version },
108 { "svn_ra", svn_ra_version },
109 { "svn_delta", svn_delta_version },
110 { "svn_diff", svn_diff_version },
114 // Configure our subversion session
115 int mysvn_setup(void) {
116 // Are we already prepared?
117 if (mysvn_pool) return EXIT_SUCCESS;
118 // No, so initialize svn internals generally
120 // there is a segfault when providing an error stream.
121 // Apparently, calling setvbuf with a nul buffer is
122 // not supported under msvc 7.1 ( code inside svn_cmdline_init )
123 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
126 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
130 apr_pool_create(&pool, NULL);
131 svn_error_t *err = NULL;
132 SVN_VERSION_DEFINE(mysvn_version);
133 err = svn_ver_check_list(&mysvn_version, mysvn_checklist);
135 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
136 err = svn_ra_initialize(pool);
138 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
139 char *config_dir = NULL;
140 err = svn_config_ensure(config_dir, pool);
142 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
143 err = svn_client_create_context(&mysvn_ctx, pool);
145 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
146 err = svn_config_get_config(&(mysvn_ctx->config),
149 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
151 cfg = ( svn_config_t*) apr_hash_get(
153 SVN_CONFIG_CATEGORY_CONFIG,
154 APR_HASH_KEY_STRING);
156 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
157 svn_auth_baton_t *ab;
158 err = svn_cmdline_setup_auth_baton(&ab,
159 TRUE, NULL, NULL, config_dir, TRUE, cfg,
160 mysvn_ctx->cancel_func, mysvn_ctx->cancel_baton, pool);
162 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
163 mysvn_ctx->auth_baton = ab;
164 mysvn_ctx->conflict_func = NULL;
165 mysvn_ctx->conflict_baton = NULL;
166 // Now our magic revisions
167 mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool,
168 sizeof(svn_opt_revision_t));
171 mysvn_rev_peg = (svn_opt_revision_t*) apr_palloc(pool,
172 sizeof(svn_opt_revision_t));
175 mysvn_rev->kind = svn_opt_revision_head;
176 mysvn_rev_peg->kind = svn_opt_revision_unspecified;
177 // Success if we got this far
184 // sync one directory tree
185 void sync_tree(const char* dir) {
188 SGPath path( dest_base );
191 rc = path.create_dir( 0755 );
193 cout << "Return code = " << rc << endl;
198 #ifdef HAVE_SVN_CLIENT_H
199 cout << dir << " ... ";
201 char dest_base_dir[512];
202 snprintf( command, 512,
203 "%s/%s", source_base, dir);
204 snprintf( dest_base_dir, 512,
205 "%s/%s", dest_base, dir);
206 svn_error_t *err = NULL;
207 if (mysvn_setup() != EXIT_SUCCESS)
209 apr_pool_t *subpool = svn_pool_create(mysvn_pool);
210 err = svn_client_checkout3(NULL,
221 // Report errors from the checkout attempt
222 cout << "failed: " << endl
223 << err->message << endl;
224 svn_error_clear(err);
227 cout << "done" << endl;
229 svn_pool_destroy(subpool);
233 snprintf( command, 512,
234 "%s %s/%s %s/%s", svn_cmd,
239 snprintf( command, 512,
240 "%s %s/%s/ %s/%s/", rsync_cmd,
244 cout << command << endl;
245 rc = system( command );
247 cout << "Return code = " << rc << endl;
248 if (rc == 5120) exit(1);
253 const int nowhere = -9999;
256 static void parse_message( const string &msg, int *lat, int *lon ) {
260 // find GGA string and advance to start of lat
261 string::size_type pos = text.find( "$GPGGA" );
262 if ( pos == string::npos )
268 string tmp = text.substr( pos + 7 );
269 pos = tmp.find( "," );
270 tmp = tmp.substr( pos + 1 );
271 // cout << "-> " << tmp << endl;
273 // find lat then advance to start of hemisphere
274 pos = tmp.find( "," );
275 string lats = tmp.substr( 0, pos );
276 dlat = atof( lats.c_str() ) / 100.0;
277 tmp = tmp.substr( pos + 1 );
279 // find N/S hemisphere and advance to start of lon
280 if ( tmp.substr( 0, 1 ) == "S" ) {
283 pos = tmp.find( "," );
284 tmp = tmp.substr( pos + 1 );
287 pos = tmp.find( "," );
288 string lons = tmp.substr( 0, pos );
289 dlon = atof( lons.c_str() ) / 100.0;
290 tmp = tmp.substr( pos + 1 );
292 // find E/W hemisphere and advance to start of lon
293 if ( tmp.substr( 0, 1 ) == "W" ) {
298 *lat = (int)dlat - 1;
304 *lon = (int)dlon - 1;
309 if ((dlon == 0) && (dlat == 0)) {
317 static void sync_area( int lat, int lon ) {
318 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
321 int baselat, baselon;
324 int base = (int)(lat / 10);
325 if ( lat == base * 10 ) {
328 baselat = (base - 1) * 10;
332 baselat = (int)(lat / 10) * 10;
336 int base = (int)(lon / 10);
337 if ( lon == base * 10 ) {
340 baselon = (base - 1) * 10;
344 baselon = (int)(lon / 10) * 10;
348 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
350 for (const char** tree = &terrainobjects[0]; *tree; tree++) {
352 snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
354 EW, abs(baselon), NS, abs(baselat),
355 EW, abs(lon), NS, abs(lat) );
356 waitingTiles.push_back( dir );
362 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
363 // do current 1x1 degree area first
364 sync_area( lat, lon );
366 if ( lat_dir == 0 && lon_dir == 0 ) {
367 // now do surrounding 8 1x1 degree areas.
368 for ( int i = lat - 1; i <= lat + 1; ++i ) {
369 for ( int j = lon - 1; j <= lon + 1; ++j ) {
370 if ( i != lat || j != lon ) {
376 if ( lat_dir != 0 ) {
377 sync_area( lat + lat_dir, lon );
378 sync_area( lat + lat_dir, lon - 1 );
379 sync_area( lat + lat_dir, lon + 1 );
381 if ( lon_dir != 0 ) {
382 sync_area( lat, lon + lon_dir );
383 sync_area( lat - 1, lon + lon_dir );
384 sync_area( lat + 1, lon + lon_dir );
389 void getWaitingTile() {
390 while ( !waitingTiles.empty() ) {
391 CompletedTiles::iterator ii = completedTiles.find( waitingTiles.front() );
392 time_t now = time(0);
393 if ( ii == completedTiles.end() || ii->second + 600 < now ) {
394 sync_tree(waitingTiles.front().c_str());
395 completedTiles[ waitingTiles.front() ] = now;
396 waitingTiles.pop_front();
399 waitingTiles.pop_front();
403 int main( int argc, char **argv ) {
405 char host[256] = ""; // accept messages from anyone
406 bool testing = false;
411 if ( (string)argv[i] == "-p" ) {
413 port = atoi( argv[i] );
414 } else if ( (string)argv[i] == "-s" ) {
416 source_base = argv[i];
417 } else if ( (string)argv[i] == "-d" ) {
420 } else if ( (string)argv[i] == "-R" ) {
422 } else if ( (string)argv[i] == "-S" ) {
424 } else if ( (string)argv[i] == "-T" ) {
433 // Use the appropriate default for the "-s" flag
434 if (source_base == NULL) {
436 source_base = svn_base;
438 source_base = rsync_base;
441 // Must call this before any other net stuff
442 netInit( &argc,argv );
446 if ( ! s.open( false ) ) { // open a UDP socket
447 printf("error opening socket\n");
451 s.setBlocking( false );
453 if ( s.bind( host, port ) == -1 ) {
454 printf("error binding to port %d\n", port);
462 int last_lat = nowhere;
463 int last_lon = nowhere;
464 bool recv_msg = false;
467 for ( synced_other = 'K'; synced_other <= 'Z'; synced_other++ ) {
469 snprintf( dir, 512, "Airports/%c", synced_other );
470 waitingTiles.push_back( dir );
472 for ( synced_other = 'A'; synced_other <= 'J'; synced_other++ ) {
474 snprintf( dir, 512, "Airports/%c", synced_other );
475 waitingTiles.push_back( dir );
478 waitingTiles.push_back( "Models" );
484 // No FGFS communications
487 recv_msg = (lat != last_lat) || (lon != last_lon);
489 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
493 parse_message( msg, &lat, &lon );
497 // Ignore messages where the location does not change
498 if ( lat != last_lat || lon != last_lon ) {
499 cout << "pos in msg = " << lat << "," << lon << endl;
500 std::deque<std::string> oldRequests;
501 oldRequests.swap( waitingTiles );
502 int lat_dir, lon_dir, dist;
503 if ( last_lat == nowhere || last_lon == nowhere ) {
504 lat_dir = lon_dir = 0;
506 dist = lat - last_lat;
508 lat_dir = dist / abs(dist);
512 dist = lon - last_lon;
514 lon_dir = dist / abs(dist);
519 cout << "lat = " << lat << " lon = " << lon << endl;
520 cout << "lat_dir = " << lat_dir << " "
521 << "lon_dir = " << lon_dir << endl;
522 sync_areas( lat, lon, lat_dir, lon_dir );
523 while ( !oldRequests.empty() ) {
524 waitingTiles.push_back( oldRequests.front() );
525 oldRequests.pop_front();
532 // No messages inbound, so process some pending work
533 if ( !waitingTiles.empty() ) {
541 if ( last_lat == nowhere || last_lon == nowhere ) {
542 cout << "FlightGear is not running, exiting." << endl;