]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
2ffbc52c0af40e9de465c8feea3db7fb013bd691
[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 #include <deque>
40
41 #include <plib/netSocket.h>
42 #include <plib/ul.h>
43
44 #include <simgear/bucket/newbucket.hxx>
45 #include <simgear/misc/sg_path.hxx>
46
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>
53 #  else
54 #    undef HAVE_SVN_CLIENT_H
55 #  endif
56 #endif
57
58 using std::string;
59 using std::cout;
60 using std::endl;
61
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";
69
70 #ifdef HAVE_SVN_CLIENT_H
71 bool use_svn = true;
72 #else
73 bool use_svn = false;
74 const char* svn_cmd = "svn checkout";
75 #endif
76
77 // display usage
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;
86 #else
87     cout << "    (defaults to rsync, using external commands)" << endl;
88 #endif
89 }
90
91 std::deque<std::string> waitingTiles;
92
93 #ifdef HAVE_SVN_CLIENT_H
94
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;
99
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 },
107     { NULL, NULL }
108 };
109
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
115 #ifdef _MSC_VER
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)
120         return EXIT_FAILURE;
121 #else
122     if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
123         return EXIT_FAILURE;
124 #endif
125     apr_pool_t *pool;
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);
130     if (err)
131         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
132     err = svn_ra_initialize(pool);
133     if (err)
134         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
135     char *config_dir = NULL;
136     err = svn_config_ensure(config_dir, pool);
137     if (err)
138         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
139     err = svn_client_create_context(&mysvn_ctx, pool);
140     if (err)
141         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
142     err = svn_config_get_config(&(mysvn_ctx->config),
143         config_dir, pool);
144     if (err)
145         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
146     svn_config_t *cfg;
147     cfg = ( svn_config_t*) apr_hash_get(
148         mysvn_ctx->config,
149         SVN_CONFIG_CATEGORY_CONFIG,
150         APR_HASH_KEY_STRING);
151     if (err)
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);
157     if (err)
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));
164     if (!mysvn_rev)
165         return EXIT_FAILURE;
166     mysvn_rev->kind = svn_opt_revision_head;
167     // Success if we got this far
168     mysvn_pool = pool;
169     return EXIT_SUCCESS;
170 }
171
172 #endif
173
174 // sync one directory tree
175 void sync_tree(char* dir) {
176     int rc;
177     char command[512];
178     SGPath path( dest_base );
179
180     path.append( dir );
181     rc = path.create_dir( 0755 );
182     if (rc) {
183         cout << "Return code = " << rc << endl;
184         exit(1);
185     }
186
187     if (use_svn) {
188 #ifdef HAVE_SVN_CLIENT_H
189         cout << dir << " ... ";
190         cout.flush();
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)
198             exit(1);
199         apr_pool_t *subpool = svn_pool_create(mysvn_pool);
200         err = svn_client_checkout(NULL,
201             command,
202             dest_base_dir,
203             mysvn_rev,
204             1,
205             mysvn_ctx,
206             subpool);
207         if (err) {
208             // Report errors from the checkout attempt
209             cout << "failed: " << endl
210                  << err->message << endl;
211             svn_error_clear(err);
212             return;
213         } else {
214             cout << "done" << endl;
215         }
216         svn_pool_destroy(subpool);
217         return;
218 #else
219
220         snprintf( command, 512,
221             "%s %s/%s %s/%s", svn_cmd,
222             source_base, dir,
223             dest_base, dir );
224 #endif
225     } else {
226         snprintf( command, 512,
227             "%s %s/%s/ %s/%s/", rsync_cmd,
228             source_base, dir,
229             dest_base, dir );
230     }
231     cout << command << endl;
232     rc = system( command );
233     if (rc) {
234         cout << "Return code = " << rc << endl;
235         if (rc == 5120) exit(1);
236     }
237 }
238
239
240 const int nowhere = -9999;
241
242 // parse message
243 static void parse_message( const string &msg, int *lat, int *lon ) {
244     double dlat, dlon;
245     string text = msg;
246
247     // find GGA string and advance to start of lat
248     string::size_type pos = text.find( "$GPGGA" );
249     if ( pos == string::npos )
250     {
251         *lat = -9999.0;
252         *lon = -9999.0;
253         return;
254     }
255     string tmp = text.substr( pos + 7 );
256     pos = tmp.find( "," );
257     tmp = tmp.substr( pos + 1 );
258     // cout << "-> " << tmp << endl;
259
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 );
265
266     // find N/S hemisphere and advance to start of lon
267     if ( tmp.substr( 0, 1 ) == "S" ) {
268         dlat = -dlat;
269     }
270     pos = tmp.find( "," );
271     tmp = tmp.substr( pos + 1 );
272
273     // find lon
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 );
278
279     // find E/W hemisphere and advance to start of lon
280     if ( tmp.substr( 0, 1 ) == "W" ) {
281         dlon = -dlon;
282     }
283
284     if ( dlat < 0 ) {
285         *lat = (int)dlat - 1;
286     } else {
287         *lat = (int)dlat;
288     }
289
290     if ( dlon < 0 ) {
291         *lon = (int)dlon - 1;
292     } else {
293         *lon = (int)dlon;
294     }
295
296     if ((dlon == 0) && (dlat == 0)) {
297       *lon = nowhere;
298       *lat = nowhere;
299     }
300 }
301
302
303 // sync area
304 static void sync_area( int lat, int lon ) {
305     if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
306         return;
307     char NS, EW;
308     int baselat, baselon;
309
310     if ( lat < 0 ) {
311         int base = (int)(lat / 10);
312         if ( lat == base * 10 ) {
313             baselat = base * 10;
314         } else {
315             baselat = (base - 1) * 10;
316         }
317         NS = 's';
318     } else {
319         baselat = (int)(lat / 10) * 10;
320         NS = 'n';
321     }
322     if ( lon < 0 ) {
323         int base = (int)(lon / 10);
324         if ( lon == base * 10 ) {
325             baselon = base * 10;
326         } else {
327             baselon = (base - 1) * 10;
328         }
329         EW = 'w';
330     } else {
331         baselon = (int)(lon / 10) * 10;
332         EW = 'e';
333     }
334
335     char dir[512];
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) );
339
340     waitingTiles.push_back( dir );
341 }
342
343
344 // sync areas
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 );
348
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 ) {
354                     sync_area( i, j );
355                 }
356             }
357         }
358     } else {
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 );
363         }
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 );
368         }
369     }
370 }
371
372
373 int main( int argc, char **argv ) {
374     int port = 5501;
375     char host[256] = "";        // accept messages from anyone
376     bool testing = false;
377
378     // parse arguments
379     int i = 1;
380     while ( i < argc ) {
381         if ( (string)argv[i] == "-p" ) {
382             ++i;
383             port = atoi( argv[i] );
384         } else if ( (string)argv[i] == "-s" ) {
385             ++i;
386             source_base = argv[i];
387         } else if ( (string)argv[i] == "-d" ) {
388             ++i;
389             dest_base = argv[i];
390         } else if ( (string)argv[i] == "-R" ) {
391             use_svn = false;
392         } else if ( (string)argv[i] == "-S" ) {
393             use_svn = true;
394         } else if ( (string)argv[i] == "-T" ) {
395             testing = true;
396         } else {
397             usage( argv[0] );
398             exit(-1);        
399         }
400         ++i;
401     }
402
403     // Use the appropriate default for the "-s" flag
404     if (source_base == NULL) {
405         if (use_svn)
406             source_base = svn_base;
407         else
408             source_base = rsync_base;
409     }
410     
411     // We just want one grid square, no FGFS communications
412     if (testing) {
413           sync_areas( 37, -123, 0, 0 );
414           exit(0);
415     }
416
417     // Must call this before any other net stuff
418     netInit( &argc,argv );
419
420     netSocket s;
421
422     if ( ! s.open( false ) ) {  // open a UDP socket
423         printf("error opening socket\n");
424         return -1;
425     }
426
427     s.setBlocking( false );
428
429     if ( s.bind( host, port ) == -1 ) {
430         printf("error binding to port %d\n", port);
431         return -1;
432     }
433
434     char msg[256];
435     int maxlen = 256;
436     int len;
437     int lat, lon;
438     int last_lat = nowhere;
439     int last_lon = nowhere;
440     bool recv_msg = false;
441     char synced_other = 'K';
442
443     while ( true ) {
444         recv_msg = false;
445         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
446             msg[len] = '\0';
447             recv_msg = true;
448
449             parse_message( msg, &lat, &lon );
450         }
451
452         if ( recv_msg ) {
453             if ( lat != last_lat || lon != last_lon ) {
454                 cout << "pos in msg = " << lat << "," << lon << endl;
455                 waitingTiles.clear();
456                 int lat_dir, lon_dir, dist;
457                 if ( last_lat == nowhere || last_lon == nowhere ) {
458                     lat_dir = lon_dir = 0;
459                 } else {
460                     dist = lat - last_lat;
461                     if ( dist != 0 ) {
462                         lat_dir = dist / abs(dist);
463                     } else {
464                         lat_dir = 0;
465                     }
466                     dist = lon - last_lon;
467                     if ( dist != 0 ) {
468                         lon_dir = dist / abs(dist);
469                     } else {
470                         lon_dir = 0;
471                     }
472                 }
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 };
481                 const char** tree;
482                 char dir[512];
483               
484                 for (tree = &terrainobjects[0]; *tree; tree++) {
485                     snprintf( dir, 512, "%s/%s", *tree, waitingTiles.front().c_str() );
486                     sync_tree(dir);
487                 }
488                 waitingTiles.pop_front();
489             } else {
490                 char c;
491                 while ( !isdigit( c = synced_other++ ) && !isupper( c ) );
492
493                 char dir[512];
494                 snprintf( dir, 512, "Airports/%c", c );
495                 sync_tree( dir );
496             }
497
498             last_lat = lat;
499             last_lon = lon;
500         } 
501
502         ulSleep( 1 );
503     } // while true
504         
505     return 0;
506 }