]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
e0f867ebf0485942613a5a92aacf11a6e2bb2df3
[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     if ( pos == string::npos )
247     {
248         *lat = -9999.0;
249         *lon = -9999.0;
250         return;
251     }
252     string tmp = text.substr( pos + 7 );
253     pos = tmp.find( "," );
254     tmp = tmp.substr( pos + 1 );
255     // cout << "-> " << tmp << endl;
256
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 );
262
263     // find N/S hemisphere and advance to start of lon
264     if ( tmp.substr( 0, 1 ) == "S" ) {
265         dlat = -dlat;
266     }
267     pos = tmp.find( "," );
268     tmp = tmp.substr( pos + 1 );
269
270     // find lon
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 );
275
276     // find E/W hemisphere and advance to start of lon
277     if ( tmp.substr( 0, 1 ) == "W" ) {
278         dlon = -dlon;
279     }
280
281     if ( dlat < 0 ) {
282         *lat = (int)dlat - 1;
283     } else {
284         *lat = (int)dlat;
285     }
286
287     if ( dlon < 0 ) {
288         *lon = (int)dlon - 1;
289     } else {
290         *lon = (int)dlon;
291     }
292
293     if ((dlon == 0) && (dlat == 0)) {
294       *lon = nowhere;
295       *lat = nowhere;
296     }
297 }
298
299
300 // sync area
301 static void sync_area( int lat, int lon ) {
302     if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
303         return;
304     char NS, EW;
305     int baselat, baselon;
306
307     if ( lat < 0 ) {
308         int base = (int)(lat / 10);
309         if ( lat == base * 10 ) {
310             baselat = base * 10;
311         } else {
312             baselat = (base - 1) * 10;
313         }
314         NS = 's';
315     } else {
316         baselat = (int)(lat / 10) * 10;
317         NS = 'n';
318     }
319     if ( lon < 0 ) {
320         int base = (int)(lon / 10);
321         if ( lon == base * 10 ) {
322             baselon = base * 10;
323         } else {
324             baselon = (base - 1) * 10;
325         }
326         EW = 'w';
327     } else {
328         baselon = (int)(lon / 10) * 10;
329         EW = 'e';
330     }
331     
332     const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
333     const char** tree;
334     char dir[512];
335   
336     for (tree = &terrainobjects[0]; *tree; tree++) {
337         snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
338           *tree,
339               EW, abs(baselon), NS, abs(baselat),
340               EW, abs(lon), NS, abs(lat) );
341         sync_tree(dir);
342     }
343 }
344
345
346 // sync areas
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 );
350
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 ) {
356                     sync_area( i, j );
357                 }
358             }
359         }
360     } else {
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 );
365         }
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 );
370         }
371     }
372 }
373
374
375 int main( int argc, char **argv ) {
376     int port = 5501;
377     char host[256] = "";        // accept messages from anyone
378     bool testing = false;
379
380     // parse arguments
381     int i = 1;
382     while ( i < argc ) {
383         if ( (string)argv[i] == "-p" ) {
384             ++i;
385             port = atoi( argv[i] );
386         } else if ( (string)argv[i] == "-s" ) {
387             ++i;
388             source_base = argv[i];
389         } else if ( (string)argv[i] == "-d" ) {
390             ++i;
391             dest_base = argv[i];
392         } else if ( (string)argv[i] == "-R" ) {
393             use_svn = false;
394         } else if ( (string)argv[i] == "-S" ) {
395             use_svn = true;
396         } else if ( (string)argv[i] == "-T" ) {
397             testing = true;
398         } else {
399             usage( argv[0] );
400             exit(-1);        
401         }
402         ++i;
403     }
404
405     // Use the appropriate default for the "-s" flag
406     if (source_base == NULL) {
407         if (use_svn)
408             source_base = svn_base;
409         else
410             source_base = rsync_base;
411     }
412     
413     // We just want one grid square, no FGFS communications
414     if (testing) {
415           sync_areas( 37, -123, 0, 0 );
416           exit(0);
417     }
418
419     // Must call this before any other net stuff
420     netInit( &argc,argv );
421
422     netSocket s;
423
424     if ( ! s.open( false ) ) {  // open a UDP socket
425         printf("error opening socket\n");
426         return -1;
427     }
428
429     s.setBlocking( false );
430
431     if ( s.bind( host, port ) == -1 ) {
432         printf("error binding to port %d\n", port);
433         return -1;
434     }
435
436     char msg[256];
437     int maxlen = 256;
438     int len;
439     int lat, lon;
440     int last_lat = nowhere;
441     int last_lon = nowhere;
442     bool recv_msg = false;
443     int synced_other = 0;
444
445     while ( true ) {
446         recv_msg = false;
447         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
448             msg[len] = '\0';
449             recv_msg = true;
450
451             parse_message( msg, &lat, &lon );
452             cout << "pos in msg = " << lat << "," << lon << endl;
453         }
454
455         if ( recv_msg ) {
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;
460                 } else {
461                     dist = lat - last_lat;
462                     if ( dist != 0 ) {
463                         lat_dir = dist / abs(dist);
464                     } else {
465                         lat_dir = 0;
466                     }
467                     dist = lon - last_lon;
468                     if ( dist != 0 ) {
469                         lon_dir = dist / abs(dist);
470                     } else {
471                         lon_dir = 0;
472                     }
473                 }
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;
480             } else {
481                 switch (synced_other++) {
482                 case 0:
483                     sync_tree((char*) "Airports/K");
484                     break;
485                 case 1:
486                     sync_tree((char*) "Airports");
487                     break;
488                 default:
489                     cout << "Done non-tile syncs" << endl;
490                     break;
491                 }
492             }
493
494             last_lat = lat;
495             last_lon = lon;
496         } 
497
498         ulSleep( 1 );
499     } // while true
500         
501     return 0;
502 }
503