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 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
116 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
120 apr_pool_create(&pool, NULL);
121 svn_error_t *err = NULL;
122 SVN_VERSION_DEFINE(mysvn_version);
123 err = svn_ver_check_list(&mysvn_version, mysvn_checklist);
125 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
126 err = svn_ra_initialize(pool);
128 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
129 char *config_dir = NULL;
130 err = svn_config_ensure(config_dir, pool);
132 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
133 err = svn_client_create_context(&mysvn_ctx, pool);
135 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
136 err = svn_config_get_config(&(mysvn_ctx->config),
139 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
141 cfg = ( svn_config_t*) apr_hash_get(
143 SVN_CONFIG_CATEGORY_CONFIG,
144 APR_HASH_KEY_STRING);
146 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
147 svn_auth_baton_t *ab;
148 err = svn_cmdline_setup_auth_baton(&ab,
149 TRUE, NULL, NULL, config_dir, TRUE, cfg,
150 mysvn_ctx->cancel_func, mysvn_ctx->cancel_baton, pool);
152 return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
153 mysvn_ctx->auth_baton = ab;
154 mysvn_ctx->conflict_func = NULL;
155 mysvn_ctx->conflict_baton = NULL;
156 mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool,
157 sizeof(svn_opt_revision_t));
160 mysvn_rev->kind = svn_opt_revision_head;
161 // Success if we got this far
168 // sync one directory tree
169 void sync_tree(char* dir) {
172 SGPath path( dest_base );
175 rc = path.create_dir( 0755 );
177 cout << "Return code = " << rc << endl;
182 #ifdef HAVE_SVN_CLIENT_H
183 cout << dir << " ... ";
185 char dest_base_dir[512];
186 snprintf( command, 512,
187 "%s/%s", source_base, dir);
188 snprintf( dest_base_dir, 512,
189 "%s/%s", dest_base, dir);
190 svn_error_t *err = NULL;
191 if (mysvn_setup() != EXIT_SUCCESS)
193 apr_pool_t *subpool = svn_pool_create(mysvn_pool);
194 err = svn_client_checkout(NULL,
202 // Report errors from the checkout attempt
203 cout << "failed: " << endl
204 << err->message << endl;
205 svn_error_clear(err);
208 cout << "done" << endl;
210 svn_pool_destroy(subpool);
214 snprintf( command, 512,
215 "%s %s/%s %s/%s", svn_cmd,
220 snprintf( command, 512,
221 "%s %s/%s/ %s/%s/", rsync_cmd,
225 cout << command << endl;
226 rc = system( command );
228 cout << "Return code = " << rc << endl;
229 if (rc == 5120) exit(1);
234 const int nowhere = -9999;
237 static void parse_message( const string &msg, int *lat, int *lon ) {
241 // find GGA string and advance to start of lat
242 string::size_type pos = text.find( "$GPGGA" );
243 string tmp = text.substr( pos + 7 );
244 pos = text.find( "," );
245 tmp = tmp.substr( pos + 1 );
246 // cout << "-> " << tmp << endl;
248 // find lat then advance to start of hemisphere
249 pos = tmp.find( "," );
250 string lats = tmp.substr( 0, pos );
251 dlat = atof( lats.c_str() ) / 100.0;
252 tmp = tmp.substr( pos + 1 );
254 // find N/S hemisphere and advance to start of lon
255 if ( tmp.substr( 0, 1 ) == "S" ) {
258 pos = tmp.find( "," );
259 tmp = tmp.substr( pos + 1 );
262 pos = tmp.find( "," );
263 string lons = tmp.substr( 0, pos );
264 dlon = atof( lons.c_str() ) / 100.0;
265 tmp = tmp.substr( pos + 1 );
267 // find E/W hemisphere and advance to start of lon
268 if ( tmp.substr( 0, 1 ) == "W" ) {
273 *lat = (int)dlat - 1;
279 *lon = (int)dlon - 1;
284 if ((dlon == 0) && (dlat == 0)) {
292 static void sync_area( int lat, int lon ) {
294 int baselat, baselon;
297 int base = (int)(lat / 10);
298 if ( lat == base * 10 ) {
301 baselat = (base - 1) * 10;
305 baselat = (int)(lat / 10) * 10;
309 int base = (int)(lon / 10);
310 if ( lon == base * 10 ) {
313 baselon = (base - 1) * 10;
317 baselon = (int)(lon / 10) * 10;
321 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
325 for (tree = &terrainobjects[0]; *tree; tree++) {
326 snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
328 EW, abs(baselon), NS, abs(baselat),
329 EW, abs(lon), NS, abs(lat) );
336 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
337 // do current 1x1 degree area first
338 sync_area( lat, lon );
340 if ( lat_dir == 0 && lon_dir == 0 ) {
341 // now do surrounding 8 1x1 degree areas.
342 for ( int i = lat - 1; i <= lat + 1; ++i ) {
343 for ( int j = lon - 1; j <= lon + 1; ++j ) {
344 if ( i != lat || j != lon ) {
350 if ( lat_dir != 0 ) {
351 sync_area( lat + lat_dir, lon );
352 sync_area( lat + lat_dir, lon - 1 );
353 sync_area( lat + lat_dir, lon + 1 );
355 if ( lon_dir != 0 ) {
356 sync_area( lat, lon + lon_dir );
357 sync_area( lat - 1, lon + lon_dir );
358 sync_area( lat + 1, lon + lon_dir );
364 int main( int argc, char **argv ) {
366 char host[256] = ""; // accept messages from anyone
367 bool testing = false;
372 if ( (string)argv[i] == "-p" ) {
374 port = atoi( argv[i] );
375 } else if ( (string)argv[i] == "-s" ) {
377 source_base = argv[i];
378 } else if ( (string)argv[i] == "-d" ) {
381 } else if ( (string)argv[i] == "-R" ) {
383 } else if ( (string)argv[i] == "-S" ) {
385 } else if ( (string)argv[i] == "-T" ) {
394 // Use the appropriate default for the "-s" flag
395 if (source_base == NULL) {
397 source_base = svn_base;
399 source_base = rsync_base;
402 // We just want one grid square, no FGFS communications
404 sync_areas( 37, -123, 0, 0 );
408 // Must call this before any other net stuff
409 netInit( &argc,argv );
413 if ( ! s.open( false ) ) { // open a UDP socket
414 printf("error opening socket\n");
418 s.setBlocking( false );
420 if ( s.bind( host, port ) == -1 ) {
421 printf("error binding to port %d\n", port);
429 int last_lat = nowhere;
430 int last_lon = nowhere;
431 bool recv_msg = false;
432 int synced_other = 0;
436 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
440 parse_message( msg, &lat, &lon );
441 cout << "pos in msg = " << lat << "," << lon << endl;
445 if ( lat != last_lat || lon != last_lon ) {
446 int lat_dir, lon_dir, dist;
447 if ( last_lat == nowhere || last_lon == nowhere ) {
448 lat_dir = lon_dir = 0;
450 dist = lat - last_lat;
452 lat_dir = dist / abs(dist);
456 dist = lon - last_lon;
458 lon_dir = dist / abs(dist);
463 cout << "lat = " << lat << " lon = " << lon << endl;
464 cout << "lat_dir = " << lat_dir << " "
465 << "lon_dir = " << lon_dir << endl;
466 sync_areas( lat, lon, lat_dir, lon_dir );
467 } else if ( last_lat == nowhere || last_lon == nowhere ) {
468 cout << "Waiting for FGFS to finish startup" << endl;
470 switch (synced_other++) {
472 sync_tree((char*) "Airports/K");
475 sync_tree((char*) "Airports");
478 cout << "Done non-tile syncs" << endl;