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>
41 #include <plib/netSocket.h>
44 #include <simgear/bucket/newbucket.hxx>
45 #include <simgear/misc/sg_path.hxx>
47 #ifdef HAVE_SVN_CLIENT_H
48 # ifdef HAVE_LIBSVN_CLIENT_1
49 # include <svn_auth.h>
50 # include <svn_client.h>
51 # include <svn_cmdline.h>
52 # include <svn_pools.h>
54 # undef HAVE_SVN_CLIENT_H
62 const char* source_base = NULL;
63 const char* svn_base =
64 "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
65 const char* rsync_base = "scenery.flightgear.org::Scenery";
66 const char* dest_base = "terrasyncdir";
67 const char* rsync_cmd =
68 "rsync --verbose --archive --delete --perms --owner --group";
70 #ifdef HAVE_SVN_CLIENT_H
74 const char* svn_cmd = "svn checkout";
78 static void usage( const string& prog ) {
79 cout << "Usage: " << endl
80 << prog << " -p <port> "
81 << "-R [ -s <rsync_source> ] -d <dest>" << endl
82 << prog << " -p <port> "
83 << "-S [ -s <svn_source> ] -d <dest>" << endl;
84 #ifdef HAVE_SVN_CLIENT_H
85 cout << " (defaults to the built in subversion)" << endl;
87 cout << " (defaults to rsync, using external commands)" << endl;
91 std::queue<std::string> waitingTiles;
93 #ifdef HAVE_SVN_CLIENT_H
95 // Things we need for doing subversion checkout - often
96 apr_pool_t *mysvn_pool = NULL;
97 svn_client_ctx_t *mysvn_ctx = NULL;
98 svn_opt_revision_t *mysvn_rev = NULL;
100 static const svn_version_checklist_t mysvn_checklist[] = {
101 { "svn_subr", svn_subr_version },
102 { "svn_client", svn_client_version },
103 { "svn_wc", svn_wc_version },
104 { "svn_ra", svn_ra_version },
105 { "svn_delta", svn_delta_version },
106 { "svn_diff", svn_diff_version },
110 // Configure our subversion session
111 int mysvn_setup(void) {
112 // Are we already prepared?
113 if (mysvn_pool) return EXIT_SUCCESS;
114 // No, so initialize svn internals generally
116 // there is a segfault when providing an error stream.
117 // Apparently, calling setvbuf with a nul buffer is
118 // not supported under msvc 7.1 ( code inside svn_cmdline_init )
119 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
122 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
126 apr_pool_create(&pool, NULL);
127 svn_error_t *err = NULL;
128 SVN_VERSION_DEFINE(mysvn_version);
129 err = svn_ver_check_list(&mysvn_version, mysvn_checklist);
131 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
132 err = svn_ra_initialize(pool);
134 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
135 char *config_dir = NULL;
136 err = svn_config_ensure(config_dir, pool);
138 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
139 err = svn_client_create_context(&mysvn_ctx, pool);
141 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
142 err = svn_config_get_config(&(mysvn_ctx->config),
145 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
147 cfg = ( svn_config_t*) apr_hash_get(
149 SVN_CONFIG_CATEGORY_CONFIG,
150 APR_HASH_KEY_STRING);
152 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
153 svn_auth_baton_t *ab;
154 err = svn_cmdline_setup_auth_baton(&ab,
155 TRUE, NULL, NULL, config_dir, TRUE, cfg,
156 mysvn_ctx->cancel_func, mysvn_ctx->cancel_baton, pool);
158 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
159 mysvn_ctx->auth_baton = ab;
160 mysvn_ctx->conflict_func = NULL;
161 mysvn_ctx->conflict_baton = NULL;
162 mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool,
163 sizeof(svn_opt_revision_t));
166 mysvn_rev->kind = svn_opt_revision_head;
167 // Success if we got this far
174 // sync one directory tree
175 void sync_tree(char* dir) {
178 SGPath path( dest_base );
181 rc = path.create_dir( 0755 );
183 cout << "Return code = " << rc << endl;
188 #ifdef HAVE_SVN_CLIENT_H
189 cout << dir << " ... ";
191 char dest_base_dir[512];
192 snprintf( command, 512,
193 "%s/%s", source_base, dir);
194 snprintf( dest_base_dir, 512,
195 "%s/%s", dest_base, dir);
196 svn_error_t *err = NULL;
197 if (mysvn_setup() != EXIT_SUCCESS)
199 apr_pool_t *subpool = svn_pool_create(mysvn_pool);
200 err = svn_client_checkout(NULL,
208 // Report errors from the checkout attempt
209 cout << "failed: " << endl
210 << err->message << endl;
211 svn_error_clear(err);
214 cout << "done" << endl;
216 svn_pool_destroy(subpool);
220 snprintf( command, 512,
221 "%s %s/%s %s/%s", svn_cmd,
226 snprintf( command, 512,
227 "%s %s/%s/ %s/%s/", rsync_cmd,
231 cout << command << endl;
232 rc = system( command );
234 cout << "Return code = " << rc << endl;
235 if (rc == 5120) exit(1);
240 const int nowhere = -9999;
243 static void parse_message( const string &msg, int *lat, int *lon ) {
247 // find GGA string and advance to start of lat
248 string::size_type pos = text.find( "$GPGGA" );
249 if ( pos == string::npos )
255 string tmp = text.substr( pos + 7 );
256 pos = tmp.find( "," );
257 tmp = tmp.substr( pos + 1 );
258 // cout << "-> " << tmp << endl;
260 // find lat then advance to start of hemisphere
261 pos = tmp.find( "," );
262 string lats = tmp.substr( 0, pos );
263 dlat = atof( lats.c_str() ) / 100.0;
264 tmp = tmp.substr( pos + 1 );
266 // find N/S hemisphere and advance to start of lon
267 if ( tmp.substr( 0, 1 ) == "S" ) {
270 pos = tmp.find( "," );
271 tmp = tmp.substr( pos + 1 );
274 pos = tmp.find( "," );
275 string lons = tmp.substr( 0, pos );
276 dlon = atof( lons.c_str() ) / 100.0;
277 tmp = tmp.substr( pos + 1 );
279 // find E/W hemisphere and advance to start of lon
280 if ( tmp.substr( 0, 1 ) == "W" ) {
285 *lat = (int)dlat - 1;
291 *lon = (int)dlon - 1;
296 if ((dlon == 0) && (dlat == 0)) {
304 static void sync_area( int lat, int lon ) {
305 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
308 int baselat, baselon;
311 int base = (int)(lat / 10);
312 if ( lat == base * 10 ) {
315 baselat = (base - 1) * 10;
319 baselat = (int)(lat / 10) * 10;
323 int base = (int)(lon / 10);
324 if ( lon == base * 10 ) {
327 baselon = (base - 1) * 10;
331 baselon = (int)(lon / 10) * 10;
336 snprintf( dir, 512, "%c%03d%c%02d/%c%03d%c%02d",
337 EW, abs(baselon), NS, abs(baselat),
338 EW, abs(lon), NS, abs(lat) );
340 waitingTiles.push( dir );
345 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
346 // do current 1x1 degree area first
347 sync_area( lat, lon );
349 if ( lat_dir == 0 && lon_dir == 0 ) {
350 // now do surrounding 8 1x1 degree areas.
351 for ( int i = lat - 1; i <= lat + 1; ++i ) {
352 for ( int j = lon - 1; j <= lon + 1; ++j ) {
353 if ( i != lat || j != lon ) {
359 if ( lat_dir != 0 ) {
360 sync_area( lat + lat_dir, lon );
361 sync_area( lat + lat_dir, lon - 1 );
362 sync_area( lat + lat_dir, lon + 1 );
364 if ( lon_dir != 0 ) {
365 sync_area( lat, lon + lon_dir );
366 sync_area( lat - 1, lon + lon_dir );
367 sync_area( lat + 1, lon + lon_dir );
373 int main( int argc, char **argv ) {
375 char host[256] = ""; // accept messages from anyone
376 bool testing = false;
381 if ( (string)argv[i] == "-p" ) {
383 port = atoi( argv[i] );
384 } else if ( (string)argv[i] == "-s" ) {
386 source_base = argv[i];
387 } else if ( (string)argv[i] == "-d" ) {
390 } else if ( (string)argv[i] == "-R" ) {
392 } else if ( (string)argv[i] == "-S" ) {
394 } else if ( (string)argv[i] == "-T" ) {
403 // Use the appropriate default for the "-s" flag
404 if (source_base == NULL) {
406 source_base = svn_base;
408 source_base = rsync_base;
411 // We just want one grid square, no FGFS communications
413 sync_areas( 37, -123, 0, 0 );
417 // Must call this before any other net stuff
418 netInit( &argc,argv );
422 if ( ! s.open( false ) ) { // open a UDP socket
423 printf("error opening socket\n");
427 s.setBlocking( false );
429 if ( s.bind( host, port ) == -1 ) {
430 printf("error binding to port %d\n", port);
438 int last_lat = nowhere;
439 int last_lon = nowhere;
440 bool recv_msg = false;
441 char synced_other = 'K';
445 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
449 parse_message( msg, &lat, &lon );
453 if ( lat != last_lat || lon != last_lon ) {
454 cout << "pos in msg = " << lat << "," << lon << endl;
455 waitingTiles.c.clear();
456 int lat_dir, lon_dir, dist;
457 if ( last_lat == nowhere || last_lon == nowhere ) {
458 lat_dir = lon_dir = 0;
460 dist = lat - last_lat;
462 lat_dir = dist / abs(dist);
466 dist = lon - last_lon;
468 lon_dir = dist / abs(dist);
473 cout << "lat = " << lat << " lon = " << lon << endl;
474 cout << "lat_dir = " << lat_dir << " "
475 << "lon_dir = " << lon_dir << endl;
476 sync_areas( lat, lon, lat_dir, lon_dir );
477 } else if ( last_lat == nowhere || last_lon == nowhere ) {
478 cout << "Waiting for FGFS to finish startup" << endl;
479 } else if ( !waitingTiles.empty() ) {
480 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
484 for (tree = &terrainobjects[0]; *tree; tree++) {
485 snprintf( dir, 512, "%s/%s", *tree, waitingTiles.front().c_str() );
491 while ( !isdigit( c = synced_other++ ) && !isupper( c ) );
494 snprintf( dir, 512, "Airports/%c", c );