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>
40 #include <plib/netSocket.h>
43 #include <simgear/bucket/newbucket.hxx>
44 #include <simgear/misc/sg_path.hxx>
46 #ifdef HAVE_SVN_CLIENT_H
47 # ifdef HAVE_LIBSVN_CLIENT_1
48 # include <svn_auth.h>
49 # include <svn_client.h>
50 # include <svn_cmdline.h>
51 # include <svn_pools.h>
53 # undef HAVE_SVN_CLIENT_H
61 const char* source_base = NULL;
62 const char* svn_base =
63 "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
64 const char* rsync_base = "scenery.flightgear.org::Scenery";
65 const char* dest_base = "terrasyncdir";
66 const char* rsync_cmd =
67 "rsync --verbose --archive --delete --perms --owner --group";
69 #ifdef HAVE_SVN_CLIENT_H
73 const char* svn_cmd = "svn checkout";
77 static void usage( const string& prog ) {
78 cout << "Usage: " << endl
79 << prog << " -p <port> "
80 << "-R [ -s <rsync_source> ] -d <dest>" << endl
81 << prog << " -p <port> "
82 << "-S [ -s <svn_source> ] -d <dest>" << endl;
83 #ifdef HAVE_SVN_CLIENT_H
84 cout << " (defaults to the built in subversion)" << endl;
86 cout << " (defaults to rsync, using external commands)" << endl;
90 #ifdef HAVE_SVN_CLIENT_H
92 // Things we need for doing subversion checkout - often
93 apr_pool_t *mysvn_pool = NULL;
94 svn_client_ctx_t *mysvn_ctx = NULL;
95 svn_opt_revision_t *mysvn_rev = NULL;
97 static const svn_version_checklist_t mysvn_checklist[] = {
98 { "svn_subr", svn_subr_version },
99 { "svn_client", svn_client_version },
100 { "svn_wc", svn_wc_version },
101 { "svn_ra", svn_ra_version },
102 { "svn_delta", svn_delta_version },
103 { "svn_diff", svn_diff_version },
107 // Configure our subversion session
108 int mysvn_setup(void) {
109 // Are we already prepared?
110 if (mysvn_pool) return EXIT_SUCCESS;
111 // No, so initialize svn internals generally
113 // there is a segfault when providing an error stream.
114 // Apparently, calling setvbuf with a nul buffer is
115 // not supported under msvc 7.1 ( code inside svn_cmdline_init )
116 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
119 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
123 apr_pool_create(&pool, NULL);
124 svn_error_t *err = NULL;
125 SVN_VERSION_DEFINE(mysvn_version);
126 err = svn_ver_check_list(&mysvn_version, mysvn_checklist);
128 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
129 err = svn_ra_initialize(pool);
131 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
132 char *config_dir = NULL;
133 err = svn_config_ensure(config_dir, pool);
135 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
136 err = svn_client_create_context(&mysvn_ctx, pool);
138 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
139 err = svn_config_get_config(&(mysvn_ctx->config),
142 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
144 cfg = ( svn_config_t*) apr_hash_get(
146 SVN_CONFIG_CATEGORY_CONFIG,
147 APR_HASH_KEY_STRING);
149 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
150 svn_auth_baton_t *ab;
151 err = svn_cmdline_setup_auth_baton(&ab,
152 TRUE, NULL, NULL, config_dir, TRUE, cfg,
153 mysvn_ctx->cancel_func, mysvn_ctx->cancel_baton, pool);
155 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
156 mysvn_ctx->auth_baton = ab;
157 mysvn_ctx->conflict_func = NULL;
158 mysvn_ctx->conflict_baton = NULL;
159 mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool,
160 sizeof(svn_opt_revision_t));
163 mysvn_rev->kind = svn_opt_revision_head;
164 // Success if we got this far
171 // sync one directory tree
172 void sync_tree(char* dir) {
175 SGPath path( dest_base );
178 rc = path.create_dir( 0755 );
180 cout << "Return code = " << rc << endl;
185 #ifdef HAVE_SVN_CLIENT_H
186 cout << dir << " ... ";
188 char dest_base_dir[512];
189 snprintf( command, 512,
190 "%s/%s", source_base, dir);
191 snprintf( dest_base_dir, 512,
192 "%s/%s", dest_base, dir);
193 svn_error_t *err = NULL;
194 if (mysvn_setup() != EXIT_SUCCESS)
196 apr_pool_t *subpool = svn_pool_create(mysvn_pool);
197 err = svn_client_checkout(NULL,
205 // Report errors from the checkout attempt
206 cout << "failed: " << endl
207 << err->message << endl;
208 svn_error_clear(err);
211 cout << "done" << endl;
213 svn_pool_destroy(subpool);
217 snprintf( command, 512,
218 "%s %s/%s %s/%s", svn_cmd,
223 snprintf( command, 512,
224 "%s %s/%s/ %s/%s/", rsync_cmd,
228 cout << command << endl;
229 rc = system( command );
231 cout << "Return code = " << rc << endl;
232 if (rc == 5120) exit(1);
237 const int nowhere = -9999;
240 static void parse_message( const string &msg, int *lat, int *lon ) {
244 // find GGA string and advance to start of lat
245 string::size_type pos = text.find( "$GPGGA" );
246 string tmp = text.substr( pos + 7 );
247 pos = text.find( "," );
248 tmp = tmp.substr( pos + 1 );
249 // cout << "-> " << tmp << endl;
251 // find lat then advance to start of hemisphere
252 pos = tmp.find( "," );
253 string lats = tmp.substr( 0, pos );
254 dlat = atof( lats.c_str() ) / 100.0;
255 tmp = tmp.substr( pos + 1 );
257 // find N/S hemisphere and advance to start of lon
258 if ( tmp.substr( 0, 1 ) == "S" ) {
261 pos = tmp.find( "," );
262 tmp = tmp.substr( pos + 1 );
265 pos = tmp.find( "," );
266 string lons = tmp.substr( 0, pos );
267 dlon = atof( lons.c_str() ) / 100.0;
268 tmp = tmp.substr( pos + 1 );
270 // find E/W hemisphere and advance to start of lon
271 if ( tmp.substr( 0, 1 ) == "W" ) {
276 *lat = (int)dlat - 1;
282 *lon = (int)dlon - 1;
287 if ((dlon == 0) && (dlat == 0)) {
295 static void sync_area( int lat, int lon ) {
296 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
299 int baselat, baselon;
302 int base = (int)(lat / 10);
303 if ( lat == base * 10 ) {
306 baselat = (base - 1) * 10;
310 baselat = (int)(lat / 10) * 10;
314 int base = (int)(lon / 10);
315 if ( lon == base * 10 ) {
318 baselon = (base - 1) * 10;
322 baselon = (int)(lon / 10) * 10;
326 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
330 for (tree = &terrainobjects[0]; *tree; tree++) {
331 snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
333 EW, abs(baselon), NS, abs(baselat),
334 EW, abs(lon), NS, abs(lat) );
341 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
342 // do current 1x1 degree area first
343 sync_area( lat, lon );
345 if ( lat_dir == 0 && lon_dir == 0 ) {
346 // now do surrounding 8 1x1 degree areas.
347 for ( int i = lat - 1; i <= lat + 1; ++i ) {
348 for ( int j = lon - 1; j <= lon + 1; ++j ) {
349 if ( i != lat || j != lon ) {
355 if ( lat_dir != 0 ) {
356 sync_area( lat + lat_dir, lon );
357 sync_area( lat + lat_dir, lon - 1 );
358 sync_area( lat + lat_dir, lon + 1 );
360 if ( lon_dir != 0 ) {
361 sync_area( lat, lon + lon_dir );
362 sync_area( lat - 1, lon + lon_dir );
363 sync_area( lat + 1, lon + lon_dir );
369 int main( int argc, char **argv ) {
371 char host[256] = ""; // accept messages from anyone
372 bool testing = false;
377 if ( (string)argv[i] == "-p" ) {
379 port = atoi( argv[i] );
380 } else if ( (string)argv[i] == "-s" ) {
382 source_base = argv[i];
383 } else if ( (string)argv[i] == "-d" ) {
386 } else if ( (string)argv[i] == "-R" ) {
388 } else if ( (string)argv[i] == "-S" ) {
390 } else if ( (string)argv[i] == "-T" ) {
399 // Use the appropriate default for the "-s" flag
400 if (source_base == NULL) {
402 source_base = svn_base;
404 source_base = rsync_base;
407 // We just want one grid square, no FGFS communications
409 sync_areas( 37, -123, 0, 0 );
413 // Must call this before any other net stuff
414 netInit( &argc,argv );
418 if ( ! s.open( false ) ) { // open a UDP socket
419 printf("error opening socket\n");
423 s.setBlocking( false );
425 if ( s.bind( host, port ) == -1 ) {
426 printf("error binding to port %d\n", port);
434 int last_lat = nowhere;
435 int last_lon = nowhere;
436 bool recv_msg = false;
437 int synced_other = 0;
441 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
445 parse_message( msg, &lat, &lon );
446 cout << "pos in msg = " << lat << "," << lon << endl;
450 if ( lat != last_lat || lon != last_lon ) {
451 int lat_dir, lon_dir, dist;
452 if ( last_lat == nowhere || last_lon == nowhere ) {
453 lat_dir = lon_dir = 0;
455 dist = lat - last_lat;
457 lat_dir = dist / abs(dist);
461 dist = lon - last_lon;
463 lon_dir = dist / abs(dist);
468 cout << "lat = " << lat << " lon = " << lon << endl;
469 cout << "lat_dir = " << lat_dir << " "
470 << "lon_dir = " << lon_dir << endl;
471 sync_areas( lat, lon, lat_dir, lon_dir );
472 } else if ( last_lat == nowhere || last_lon == nowhere ) {
473 cout << "Waiting for FGFS to finish startup" << endl;
475 switch (synced_other++) {
477 sync_tree((char*) "Airports/K");
480 sync_tree((char*) "Airports");
483 cout << "Done non-tile syncs" << endl;