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 if ( pos == string::npos )
252 string tmp = text.substr( pos + 7 );
253 pos = tmp.find( "," );
254 tmp = tmp.substr( pos + 1 );
255 // cout << "-> " << tmp << endl;
257 // find lat then advance to start of hemisphere
258 pos = tmp.find( "," );
259 string lats = tmp.substr( 0, pos );
260 dlat = atof( lats.c_str() ) / 100.0;
261 tmp = tmp.substr( pos + 1 );
263 // find N/S hemisphere and advance to start of lon
264 if ( tmp.substr( 0, 1 ) == "S" ) {
267 pos = tmp.find( "," );
268 tmp = tmp.substr( pos + 1 );
271 pos = tmp.find( "," );
272 string lons = tmp.substr( 0, pos );
273 dlon = atof( lons.c_str() ) / 100.0;
274 tmp = tmp.substr( pos + 1 );
276 // find E/W hemisphere and advance to start of lon
277 if ( tmp.substr( 0, 1 ) == "W" ) {
282 *lat = (int)dlat - 1;
288 *lon = (int)dlon - 1;
293 if ((dlon == 0) && (dlat == 0)) {
301 static void sync_area( int lat, int lon ) {
302 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
305 int baselat, baselon;
308 int base = (int)(lat / 10);
309 if ( lat == base * 10 ) {
312 baselat = (base - 1) * 10;
316 baselat = (int)(lat / 10) * 10;
320 int base = (int)(lon / 10);
321 if ( lon == base * 10 ) {
324 baselon = (base - 1) * 10;
328 baselon = (int)(lon / 10) * 10;
332 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
336 for (tree = &terrainobjects[0]; *tree; tree++) {
337 snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
339 EW, abs(baselon), NS, abs(baselat),
340 EW, abs(lon), NS, abs(lat) );
347 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
348 // do current 1x1 degree area first
349 sync_area( lat, lon );
351 if ( lat_dir == 0 && lon_dir == 0 ) {
352 // now do surrounding 8 1x1 degree areas.
353 for ( int i = lat - 1; i <= lat + 1; ++i ) {
354 for ( int j = lon - 1; j <= lon + 1; ++j ) {
355 if ( i != lat || j != lon ) {
361 if ( lat_dir != 0 ) {
362 sync_area( lat + lat_dir, lon );
363 sync_area( lat + lat_dir, lon - 1 );
364 sync_area( lat + lat_dir, lon + 1 );
366 if ( lon_dir != 0 ) {
367 sync_area( lat, lon + lon_dir );
368 sync_area( lat - 1, lon + lon_dir );
369 sync_area( lat + 1, lon + lon_dir );
375 int main( int argc, char **argv ) {
377 char host[256] = ""; // accept messages from anyone
378 bool testing = false;
383 if ( (string)argv[i] == "-p" ) {
385 port = atoi( argv[i] );
386 } else if ( (string)argv[i] == "-s" ) {
388 source_base = argv[i];
389 } else if ( (string)argv[i] == "-d" ) {
392 } else if ( (string)argv[i] == "-R" ) {
394 } else if ( (string)argv[i] == "-S" ) {
396 } else if ( (string)argv[i] == "-T" ) {
405 // Use the appropriate default for the "-s" flag
406 if (source_base == NULL) {
408 source_base = svn_base;
410 source_base = rsync_base;
413 // We just want one grid square, no FGFS communications
415 sync_areas( 37, -123, 0, 0 );
419 // Must call this before any other net stuff
420 netInit( &argc,argv );
424 if ( ! s.open( false ) ) { // open a UDP socket
425 printf("error opening socket\n");
429 s.setBlocking( false );
431 if ( s.bind( host, port ) == -1 ) {
432 printf("error binding to port %d\n", port);
440 int last_lat = nowhere;
441 int last_lon = nowhere;
442 bool recv_msg = false;
443 int synced_other = 0;
447 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
451 parse_message( msg, &lat, &lon );
452 cout << "pos in msg = " << lat << "," << lon << endl;
456 if ( lat != last_lat || lon != last_lon ) {
457 int lat_dir, lon_dir, dist;
458 if ( last_lat == nowhere || last_lon == nowhere ) {
459 lat_dir = lon_dir = 0;
461 dist = lat - last_lat;
463 lat_dir = dist / abs(dist);
467 dist = lon - last_lon;
469 lon_dir = dist / abs(dist);
474 cout << "lat = " << lat << " lon = " << lon << endl;
475 cout << "lat_dir = " << lat_dir << " "
476 << "lon_dir = " << lon_dir << endl;
477 sync_areas( lat, lon, lat_dir, lon_dir );
478 } else if ( last_lat == nowhere || last_lon == nowhere ) {
479 cout << "Waiting for FGFS to finish startup" << endl;
481 switch (synced_other++) {
483 sync_tree((char*) "Airports/K");
486 sync_tree((char*) "Airports");
489 cout << "Done non-tile syncs" << endl;