]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
Merge branch 'maint' into next
[flightgear.git] / utils / TerraSync / terrasync.cxx
1 // terrasync.cxx -- "JIT" scenery fetcher
2 //
3 // Written by Curtis Olson, started November 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
6 // Copyright (C) 2008  Alexander R. Perry <alex.perry@ieee.org>
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #include <windows.h>
30 #endif
31
32
33 #include <stdlib.h>             // atoi() atof() abs() system()
34
35 #include <simgear/compiler.h>
36
37 #include <iostream>
38 #include <string>
39
40 #include <plib/netSocket.h>
41 #include <plib/ul.h>
42
43 #include <simgear/bucket/newbucket.hxx>
44 #include <simgear/misc/sg_path.hxx>
45
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>
52 #  else
53 #    undef HAVE_SVN_CLIENT_H
54 #  endif
55 #endif
56
57 using std::string;
58 using std::cout;
59 using std::endl;
60
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";
68
69 #ifdef HAVE_SVN_CLIENT_H
70 bool use_svn = true;
71 #else
72 bool use_svn = false;
73 const char* svn_cmd = "svn checkout";
74 #endif
75
76 // display usage
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;
85 #else
86     cout << "    (defaults to rsync, using external commands)" << endl;
87 #endif
88 }
89
90 #ifdef HAVE_SVN_CLIENT_H
91
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;
96
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 },
104     { NULL, NULL }
105 };
106
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
112 #ifdef _MSC_VER
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)
117         return EXIT_FAILURE;
118 #else
119     if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
120         return EXIT_FAILURE;
121 #endif
122     apr_pool_t *pool;
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);
127     if (err)
128         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
129     err = svn_ra_initialize(pool);
130     if (err)
131         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
132     char *config_dir = NULL;
133     err = svn_config_ensure(config_dir, pool);
134     if (err)
135         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
136     err = svn_client_create_context(&mysvn_ctx, pool);
137     if (err)
138         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
139     err = svn_config_get_config(&(mysvn_ctx->config),
140         config_dir, pool);
141     if (err)
142         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
143     svn_config_t *cfg;
144     cfg = ( svn_config_t*) apr_hash_get(
145         mysvn_ctx->config,
146         SVN_CONFIG_CATEGORY_CONFIG,
147         APR_HASH_KEY_STRING);
148     if (err)
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);
154     if (err)
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));
161     if (!mysvn_rev)
162         return EXIT_FAILURE;
163     mysvn_rev->kind = svn_opt_revision_head;
164     // Success if we got this far
165     mysvn_pool = pool;
166     return EXIT_SUCCESS;
167 }
168
169 #endif
170
171 // sync one directory tree
172 void sync_tree(char* dir) {
173     int rc;
174     char command[512];
175     SGPath path( dest_base );
176
177     path.append( dir );
178     rc = path.create_dir( 0755 );
179     if (rc) {
180         cout << "Return code = " << rc << endl;
181         exit(1);
182     }
183
184     if (use_svn) {
185 #ifdef HAVE_SVN_CLIENT_H
186         cout << dir << " ... ";
187         cout.flush();
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)
195             exit(1);
196         apr_pool_t *subpool = svn_pool_create(mysvn_pool);
197         err = svn_client_checkout(NULL,
198             command,
199             dest_base_dir,
200             mysvn_rev,
201             1,
202             mysvn_ctx,
203             subpool);
204         if (err) {
205             // Report errors from the checkout attempt
206             cout << "failed: " << endl
207                  << err->message << endl;
208             svn_error_clear(err);
209             return;
210         } else {
211             cout << "done" << endl;
212         }
213         svn_pool_destroy(subpool);
214         return;
215 #else
216
217         snprintf( command, 512,
218             "%s %s/%s %s/%s", svn_cmd,
219             source_base, dir,
220             dest_base, dir );
221 #endif
222     } else {
223         snprintf( command, 512,
224             "%s %s/%s/ %s/%s/", rsync_cmd,
225             source_base, dir,
226             dest_base, dir );
227     }
228     cout << command << endl;
229     rc = system( command );
230     if (rc) {
231         cout << "Return code = " << rc << endl;
232         if (rc == 5120) exit(1);
233     }
234 }
235
236
237 const int nowhere = -9999;
238
239 // parse message
240 static void parse_message( const string &msg, int *lat, int *lon ) {
241     double dlat, dlon;
242     string text = msg;
243
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;
250
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 );
256
257     // find N/S hemisphere and advance to start of lon
258     if ( tmp.substr( 0, 1 ) == "S" ) {
259         dlat = -dlat;
260     }
261     pos = tmp.find( "," );
262     tmp = tmp.substr( pos + 1 );
263
264     // find lon
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 );
269
270     // find E/W hemisphere and advance to start of lon
271     if ( tmp.substr( 0, 1 ) == "W" ) {
272         dlon = -dlon;
273     }
274
275     if ( dlat < 0 ) {
276         *lat = (int)dlat - 1;
277     } else {
278         *lat = (int)dlat;
279     }
280
281     if ( dlon < 0 ) {
282         *lon = (int)dlon - 1;
283     } else {
284         *lon = (int)dlon;
285     }
286
287     if ((dlon == 0) && (dlat == 0)) {
288       *lon = nowhere;
289       *lat = nowhere;
290     }
291 }
292
293
294 // sync area
295 static void sync_area( int lat, int lon ) {
296     if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
297         return;
298     char NS, EW;
299     int baselat, baselon;
300
301     if ( lat < 0 ) {
302         int base = (int)(lat / 10);
303         if ( lat == base * 10 ) {
304             baselat = base * 10;
305         } else {
306             baselat = (base - 1) * 10;
307         }
308         NS = 's';
309     } else {
310         baselat = (int)(lat / 10) * 10;
311         NS = 'n';
312     }
313     if ( lon < 0 ) {
314         int base = (int)(lon / 10);
315         if ( lon == base * 10 ) {
316             baselon = base * 10;
317         } else {
318             baselon = (base - 1) * 10;
319         }
320         EW = 'w';
321     } else {
322         baselon = (int)(lon / 10) * 10;
323         EW = 'e';
324     }
325     
326     const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
327     const char** tree;
328     char dir[512];
329   
330     for (tree = &terrainobjects[0]; *tree; tree++) {
331         snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
332           *tree,
333               EW, abs(baselon), NS, abs(baselat),
334               EW, abs(lon), NS, abs(lat) );
335         sync_tree(dir);
336     }
337 }
338
339
340 // sync areas
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 );
344
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 ) {
350                     sync_area( i, j );
351                 }
352             }
353         }
354     } else {
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 );
359         }
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 );
364         }
365     }
366 }
367
368
369 int main( int argc, char **argv ) {
370     int port = 5501;
371     char host[256] = "";        // accept messages from anyone
372     bool testing = false;
373
374     // parse arguments
375     int i = 1;
376     while ( i < argc ) {
377         if ( (string)argv[i] == "-p" ) {
378             ++i;
379             port = atoi( argv[i] );
380         } else if ( (string)argv[i] == "-s" ) {
381             ++i;
382             source_base = argv[i];
383         } else if ( (string)argv[i] == "-d" ) {
384             ++i;
385             dest_base = argv[i];
386         } else if ( (string)argv[i] == "-R" ) {
387             use_svn = false;
388         } else if ( (string)argv[i] == "-S" ) {
389             use_svn = true;
390         } else if ( (string)argv[i] == "-T" ) {
391             testing = true;
392         } else {
393             usage( argv[0] );
394             exit(-1);        
395         }
396         ++i;
397     }
398
399     // Use the appropriate default for the "-s" flag
400     if (source_base == NULL) {
401         if (use_svn)
402             source_base = svn_base;
403         else
404             source_base = rsync_base;
405     }
406     
407     // We just want one grid square, no FGFS communications
408     if (testing) {
409           sync_areas( 37, -123, 0, 0 );
410           exit(0);
411     }
412
413     // Must call this before any other net stuff
414     netInit( &argc,argv );
415
416     netSocket s;
417
418     if ( ! s.open( false ) ) {  // open a UDP socket
419         printf("error opening socket\n");
420         return -1;
421     }
422
423     s.setBlocking( false );
424
425     if ( s.bind( host, port ) == -1 ) {
426         printf("error binding to port %d\n", port);
427         return -1;
428     }
429
430     char msg[256];
431     int maxlen = 256;
432     int len;
433     int lat, lon;
434     int last_lat = nowhere;
435     int last_lon = nowhere;
436     bool recv_msg = false;
437     int synced_other = 0;
438
439     while ( true ) {
440         recv_msg = false;
441         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
442             msg[len] = '\0';
443             recv_msg = true;
444
445             parse_message( msg, &lat, &lon );
446             cout << "pos in msg = " << lat << "," << lon << endl;
447         }
448
449         if ( recv_msg ) {
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;
454                 } else {
455                     dist = lat - last_lat;
456                     if ( dist != 0 ) {
457                         lat_dir = dist / abs(dist);
458                     } else {
459                         lat_dir = 0;
460                     }
461                     dist = lon - last_lon;
462                     if ( dist != 0 ) {
463                         lon_dir = dist / abs(dist);
464                     } else {
465                         lon_dir = 0;
466                     }
467                 }
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;
474             } else {
475                 switch (synced_other++) {
476                 case 0:
477                     sync_tree((char*) "Airports/K");
478                     break;
479                 case 1:
480                     sync_tree((char*) "Airports");
481                     break;
482                 default:
483                     cout << "Done non-tile syncs" << endl;
484                     break;
485                 }
486             }
487
488             last_lat = lat;
489             last_lon = lon;
490         } 
491
492         ulSleep( 1 );
493     } // while true
494         
495     return 0;
496 }
497