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;
103 static const svn_version_checklist_t mysvn_checklist[] = {
104 { "svn_subr", svn_subr_version },
105 { "svn_client", svn_client_version },
106 { "svn_wc", svn_wc_version },
107 { "svn_ra", svn_ra_version },
108 { "svn_delta", svn_delta_version },
109 { "svn_diff", svn_diff_version },
113 // Configure our subversion session
114 int mysvn_setup(void) {
115 // Are we already prepared?
116 if (mysvn_pool) return EXIT_SUCCESS;
117 // No, so initialize svn internals generally
119 // there is a segfault when providing an error stream.
120 // Apparently, calling setvbuf with a nul buffer is
121 // not supported under msvc 7.1 ( code inside svn_cmdline_init )
122 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
125 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
129 apr_pool_create(&pool, NULL);
130 svn_error_t *err = NULL;
131 SVN_VERSION_DEFINE(mysvn_version);
132 err = svn_ver_check_list(&mysvn_version, mysvn_checklist);
134 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
135 err = svn_ra_initialize(pool);
137 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
138 char *config_dir = NULL;
139 err = svn_config_ensure(config_dir, pool);
141 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
142 err = svn_client_create_context(&mysvn_ctx, pool);
144 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
145 err = svn_config_get_config(&(mysvn_ctx->config),
148 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
150 cfg = ( svn_config_t*) apr_hash_get(
152 SVN_CONFIG_CATEGORY_CONFIG,
153 APR_HASH_KEY_STRING);
155 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
156 svn_auth_baton_t *ab;
157 err = svn_cmdline_setup_auth_baton(&ab,
158 TRUE, NULL, NULL, config_dir, TRUE, cfg,
159 mysvn_ctx->cancel_func, mysvn_ctx->cancel_baton, pool);
161 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
162 mysvn_ctx->auth_baton = ab;
163 mysvn_ctx->conflict_func = NULL;
164 mysvn_ctx->conflict_baton = NULL;
165 mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool,
166 sizeof(svn_opt_revision_t));
169 mysvn_rev->kind = svn_opt_revision_head;
170 // Success if we got this far
177 // sync one directory tree
178 void sync_tree(const char* dir) {
181 SGPath path( dest_base );
184 rc = path.create_dir( 0755 );
186 cout << "Return code = " << rc << endl;
191 #ifdef HAVE_SVN_CLIENT_H
192 cout << dir << " ... ";
194 char dest_base_dir[512];
195 snprintf( command, 512,
196 "%s/%s", source_base, dir);
197 snprintf( dest_base_dir, 512,
198 "%s/%s", dest_base, dir);
199 svn_error_t *err = NULL;
200 if (mysvn_setup() != EXIT_SUCCESS)
202 apr_pool_t *subpool = svn_pool_create(mysvn_pool);
203 err = svn_client_checkout(NULL,
211 // Report errors from the checkout attempt
212 cout << "failed: " << endl
213 << err->message << endl;
214 svn_error_clear(err);
217 cout << "done" << endl;
219 svn_pool_destroy(subpool);
223 snprintf( command, 512,
224 "%s %s/%s %s/%s", svn_cmd,
229 snprintf( command, 512,
230 "%s %s/%s/ %s/%s/", rsync_cmd,
234 cout << command << endl;
235 rc = system( command );
237 cout << "Return code = " << rc << endl;
238 if (rc == 5120) exit(1);
243 const int nowhere = -9999;
246 static void parse_message( const string &msg, int *lat, int *lon ) {
250 // find GGA string and advance to start of lat
251 string::size_type pos = text.find( "$GPGGA" );
252 if ( pos == string::npos )
258 string tmp = text.substr( pos + 7 );
259 pos = tmp.find( "," );
260 tmp = tmp.substr( pos + 1 );
261 // cout << "-> " << tmp << endl;
263 // find lat then advance to start of hemisphere
264 pos = tmp.find( "," );
265 string lats = tmp.substr( 0, pos );
266 dlat = atof( lats.c_str() ) / 100.0;
267 tmp = tmp.substr( pos + 1 );
269 // find N/S hemisphere and advance to start of lon
270 if ( tmp.substr( 0, 1 ) == "S" ) {
273 pos = tmp.find( "," );
274 tmp = tmp.substr( pos + 1 );
277 pos = tmp.find( "," );
278 string lons = tmp.substr( 0, pos );
279 dlon = atof( lons.c_str() ) / 100.0;
280 tmp = tmp.substr( pos + 1 );
282 // find E/W hemisphere and advance to start of lon
283 if ( tmp.substr( 0, 1 ) == "W" ) {
288 *lat = (int)dlat - 1;
294 *lon = (int)dlon - 1;
299 if ((dlon == 0) && (dlat == 0)) {
307 static void sync_area( int lat, int lon ) {
308 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
311 int baselat, baselon;
314 int base = (int)(lat / 10);
315 if ( lat == base * 10 ) {
318 baselat = (base - 1) * 10;
322 baselat = (int)(lat / 10) * 10;
326 int base = (int)(lon / 10);
327 if ( lon == base * 10 ) {
330 baselon = (base - 1) * 10;
334 baselon = (int)(lon / 10) * 10;
338 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
340 for (const char** tree = &terrainobjects[0]; *tree; tree++) {
342 snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
344 EW, abs(baselon), NS, abs(baselat),
345 EW, abs(lon), NS, abs(lat) );
346 waitingTiles.push_back( dir );
352 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
353 // do current 1x1 degree area first
354 sync_area( lat, lon );
356 if ( lat_dir == 0 && lon_dir == 0 ) {
357 // now do surrounding 8 1x1 degree areas.
358 for ( int i = lat - 1; i <= lat + 1; ++i ) {
359 for ( int j = lon - 1; j <= lon + 1; ++j ) {
360 if ( i != lat || j != lon ) {
366 if ( lat_dir != 0 ) {
367 sync_area( lat + lat_dir, lon );
368 sync_area( lat + lat_dir, lon - 1 );
369 sync_area( lat + lat_dir, lon + 1 );
371 if ( lon_dir != 0 ) {
372 sync_area( lat, lon + lon_dir );
373 sync_area( lat - 1, lon + lon_dir );
374 sync_area( lat + 1, lon + lon_dir );
379 void getWaitingTile() {
380 CompletedTiles::iterator ii = completedTiles.find( waitingTiles.front() );
381 time_t now = time(0);
382 if ( ii == completedTiles.end() || ii->second + 600 < now ) {
383 sync_tree(waitingTiles.front().c_str());
384 completedTiles[ waitingTiles.front() ] = now;
386 waitingTiles.pop_front();
389 int main( int argc, char **argv ) {
391 char host[256] = ""; // accept messages from anyone
392 bool testing = false;
397 if ( (string)argv[i] == "-p" ) {
399 port = atoi( argv[i] );
400 } else if ( (string)argv[i] == "-s" ) {
402 source_base = argv[i];
403 } else if ( (string)argv[i] == "-d" ) {
406 } else if ( (string)argv[i] == "-R" ) {
408 } else if ( (string)argv[i] == "-S" ) {
410 } else if ( (string)argv[i] == "-T" ) {
419 // Use the appropriate default for the "-s" flag
420 if (source_base == NULL) {
422 source_base = svn_base;
424 source_base = rsync_base;
427 // We just want one grid square, no FGFS communications
429 sync_areas( 37, -123, 0, 0 );
433 // Must call this before any other net stuff
434 netInit( &argc,argv );
438 if ( ! s.open( false ) ) { // open a UDP socket
439 printf("error opening socket\n");
443 s.setBlocking( false );
445 if ( s.bind( host, port ) == -1 ) {
446 printf("error binding to port %d\n", port);
454 int last_lat = nowhere;
455 int last_lon = nowhere;
456 bool recv_msg = false;
457 char synced_other = 'K';
461 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
465 parse_message( msg, &lat, &lon );
469 if ( lat != last_lat || lon != last_lon ) {
470 cout << "pos in msg = " << lat << "," << lon << endl;
471 waitingTiles.clear();
472 int lat_dir, lon_dir, dist;
473 if ( last_lat == nowhere || last_lon == nowhere ) {
474 lat_dir = lon_dir = 0;
476 dist = lat - last_lat;
478 lat_dir = dist / abs(dist);
482 dist = lon - last_lon;
484 lon_dir = dist / abs(dist);
489 cout << "lat = " << lat << " lon = " << lon << endl;
490 cout << "lat_dir = " << lat_dir << " "
491 << "lon_dir = " << lon_dir << endl;
492 sync_areas( lat, lon, lat_dir, lon_dir );
493 } else if ( !waitingTiles.empty() ) {
496 if ( last_lat == nowhere || last_lon == nowhere ) {
497 cout << "Waiting for FGFS to finish startup" << endl;
500 while ( !isdigit( c = synced_other++ ) && !isupper( c ) );
503 snprintf( dir, 512, "Airports/%c", c );
504 waitingTiles.push_back( dir );
509 } else if ( !waitingTiles.empty() ) {