]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
Provide an explanation to why a special case is needed
[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     char NS, EW;
297     int baselat, baselon;
298
299     if ( lat < 0 ) {
300         int base = (int)(lat / 10);
301         if ( lat == base * 10 ) {
302             baselat = base * 10;
303         } else {
304             baselat = (base - 1) * 10;
305         }
306         NS = 's';
307     } else {
308         baselat = (int)(lat / 10) * 10;
309         NS = 'n';
310     }
311     if ( lon < 0 ) {
312         int base = (int)(lon / 10);
313         if ( lon == base * 10 ) {
314             baselon = base * 10;
315         } else {
316             baselon = (base - 1) * 10;
317         }
318         EW = 'w';
319     } else {
320             baselon = (int)(lon / 10) * 10;
321             EW = 'e';
322         }
323     
324     const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
325     const char** tree;
326     char dir[512];
327   
328     for (tree = &terrainobjects[0]; *tree; tree++) {
329         snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
330               *tree,
331               EW, abs(baselon), NS, abs(baselat),
332               EW, abs(lon), NS, abs(lat) );
333         sync_tree(dir);
334     }
335 }
336
337
338 // sync areas
339 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
340     // do current 1x1 degree area first
341     sync_area( lat, lon );
342
343     if ( lat_dir == 0 && lon_dir == 0 ) {
344         // now do surrounding 8 1x1 degree areas.
345         for ( int i = lat - 1; i <= lat + 1; ++i ) {
346             for ( int j = lon - 1; j <= lon + 1; ++j ) {
347                 if ( i != lat || j != lon ) {
348                     sync_area( i, j );
349                 }
350             }
351         }
352     } else {
353         if ( lat_dir != 0 ) {
354             sync_area( lat + lat_dir, lon );
355             sync_area( lat + lat_dir, lon - 1 );
356             sync_area( lat + lat_dir, lon + 1 );
357         }
358         if ( lon_dir != 0 ) {
359             sync_area( lat, lon + lon_dir );
360             sync_area( lat - 1, lon + lon_dir );
361             sync_area( lat + 1, lon + lon_dir );
362         }
363     }
364 }
365
366
367 int main( int argc, char **argv ) {
368     int port = 5501;
369     char host[256] = "";        // accept messages from anyone
370     bool testing = false;
371
372     // parse arguments
373     int i = 1;
374     while ( i < argc ) {
375         if ( (string)argv[i] == "-p" ) {
376             ++i;
377             port = atoi( argv[i] );
378         } else if ( (string)argv[i] == "-s" ) {
379             ++i;
380             source_base = argv[i];
381         } else if ( (string)argv[i] == "-d" ) {
382             ++i;
383             dest_base = argv[i];
384         } else if ( (string)argv[i] == "-R" ) {
385             use_svn = false;
386         } else if ( (string)argv[i] == "-S" ) {
387             use_svn = true;
388         } else if ( (string)argv[i] == "-T" ) {
389             testing = true;
390         } else {
391             usage( argv[0] );
392             exit(-1);        
393         }
394         ++i;
395     }
396
397     // Use the appropriate default for the "-s" flag
398     if (source_base == NULL) {
399         if (use_svn)
400             source_base = svn_base;
401         else
402             source_base = rsync_base;
403     }
404     
405     // We just want one grid square, no FGFS communications
406     if (testing) {
407           sync_areas( 37, -123, 0, 0 );
408           exit(0);
409     }
410
411     // Must call this before any other net stuff
412     netInit( &argc,argv );
413
414     netSocket s;
415
416     if ( ! s.open( false ) ) {  // open a UDP socket
417         printf("error opening socket\n");
418         return -1;
419     }
420
421     s.setBlocking( false );
422
423     if ( s.bind( host, port ) == -1 ) {
424         printf("error binding to port %d\n", port);
425         return -1;
426     }
427
428     char msg[256];
429     int maxlen = 256;
430     int len;
431     int lat, lon;
432     int last_lat = nowhere;
433     int last_lon = nowhere;
434     bool recv_msg = false;
435     int synced_other = 0;
436
437     while ( true ) {
438         recv_msg = false;
439         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
440             msg[len] = '\0';
441             recv_msg = true;
442
443             parse_message( msg, &lat, &lon );
444             cout << "pos in msg = " << lat << "," << lon << endl;
445         }
446
447         if ( recv_msg ) {
448             if ( lat != last_lat || lon != last_lon ) {
449                 int lat_dir, lon_dir, dist;
450                 if ( last_lat == nowhere || last_lon == nowhere ) {
451                     lat_dir = lon_dir = 0;
452                 } else {
453                     dist = lat - last_lat;
454                     if ( dist != 0 ) {
455                         lat_dir = dist / abs(dist);
456                     } else {
457                         lat_dir = 0;
458                     }
459                     dist = lon - last_lon;
460                     if ( dist != 0 ) {
461                         lon_dir = dist / abs(dist);
462                     } else {
463                         lon_dir = 0;
464                     }
465                 }
466                 cout << "lat = " << lat << " lon = " << lon << endl;
467                 cout << "lat_dir = " << lat_dir << "  "
468                      << "lon_dir = " << lon_dir << endl;
469                 sync_areas( lat, lon, lat_dir, lon_dir );
470             } else if ( last_lat == nowhere || last_lon == nowhere ) {
471                 cout << "Waiting for FGFS to finish startup" << endl;
472             } else {
473                 switch (synced_other++) {
474                 case 0:
475                     sync_tree((char*) "Airports/K");
476                     break;
477                 case 1:
478                     sync_tree((char*) "Airports");
479                     break;
480                 default:
481                     cout << "Done non-tile syncs" << endl;
482                     break;
483                 }
484             }
485
486             last_lat = lat;
487             last_lon = lon;
488         } 
489
490         ulSleep( 1 );
491     } // while true
492         
493     return 0;
494 }
495