]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
Alex Perry :
[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     if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
114         return EXIT_FAILURE;
115 #else
116     if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
117         return EXIT_FAILURE;
118 #endif
119     apr_pool_t *pool;
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);
124     if (err)
125         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
126     err = svn_ra_initialize(pool);
127     if (err)
128         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
129     char *config_dir = NULL;
130     err = svn_config_ensure(config_dir, pool);
131     if (err)
132         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
133     err = svn_client_create_context(&mysvn_ctx, pool);
134     if (err)
135         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
136     err = svn_config_get_config(&(mysvn_ctx->config),
137         config_dir, pool);
138     if (err)
139         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
140     svn_config_t *cfg;
141     cfg = ( svn_config_t*) apr_hash_get(
142         mysvn_ctx->config,
143         SVN_CONFIG_CATEGORY_CONFIG,
144         APR_HASH_KEY_STRING);
145     if (err)
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);
151     if (err)
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));
158     if (!mysvn_rev)
159         return EXIT_FAILURE;
160     mysvn_rev->kind = svn_opt_revision_head;
161     // Success if we got this far
162     mysvn_pool = pool;
163     return EXIT_SUCCESS;
164 }
165
166 #endif
167
168 // sync one directory tree
169 void sync_tree(char* dir) {
170     int rc;
171     char command[512];
172     SGPath path( dest_base );
173
174     path.append( dir );
175     rc = path.create_dir( 0755 );
176     if (rc) {
177         cout << "Return code = " << rc << endl;
178         exit(1);
179     }
180
181     if (use_svn) {
182 #ifdef HAVE_SVN_CLIENT_H
183         cout << dir << " ... ";
184         cout.flush();
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)
192             exit(1);
193         apr_pool_t *subpool = svn_pool_create(mysvn_pool);
194         err = svn_client_checkout(NULL,
195             command,
196             dest_base_dir,
197             mysvn_rev,
198             1,
199             mysvn_ctx,
200             subpool);
201         if (err) {
202             // Report errors from the checkout attempt
203             cout << "failed: " << endl
204                  << err->message << endl;
205             svn_error_clear(err);
206             return;
207         } else {
208             cout << "done" << endl;
209         }
210         svn_pool_destroy(subpool);
211         return;
212 #else
213
214         snprintf( command, 512,
215             "%s %s/%s %s/%s", svn_cmd,
216             source_base, dir,
217             dest_base, dir );
218 #endif
219     } else {
220         snprintf( command, 512,
221             "%s %s/%s/ %s/%s/", rsync_cmd,
222             source_base, dir,
223             dest_base, dir );
224     }
225     cout << command << endl;
226     rc = system( command );
227     if (rc) {
228         cout << "Return code = " << rc << endl;
229         if (rc == 5120) exit(1);
230     }
231 }
232
233
234 const int nowhere = -9999;
235
236 // parse message
237 static void parse_message( const string &msg, int *lat, int *lon ) {
238     double dlat, dlon;
239     string text = msg;
240
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;
247
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 );
253
254     // find N/S hemisphere and advance to start of lon
255     if ( tmp.substr( 0, 1 ) == "S" ) {
256         dlat = -dlat;
257     }
258     pos = tmp.find( "," );
259     tmp = tmp.substr( pos + 1 );
260
261     // find lon
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 );
266
267     // find E/W hemisphere and advance to start of lon
268     if ( tmp.substr( 0, 1 ) == "W" ) {
269         dlon = -dlon;
270     }
271
272     if ( dlat < 0 ) {
273         *lat = (int)dlat - 1;
274     } else {
275         *lat = (int)dlat;
276     }
277
278     if ( dlon < 0 ) {
279         *lon = (int)dlon - 1;
280     } else {
281         *lon = (int)dlon;
282     }
283
284     if ((dlon == 0) && (dlat == 0)) {
285       *lon = nowhere;
286       *lat = nowhere;
287     }
288 }
289
290
291 // sync area
292 static void sync_area( int lat, int lon ) {
293     char NS, EW;
294     int baselat, baselon;
295
296     if ( lat < 0 ) {
297         int base = (int)(lat / 10);
298         if ( lat == base * 10 ) {
299             baselat = base * 10;
300         } else {
301             baselat = (base - 1) * 10;
302         }
303         NS = 's';
304     } else {
305         baselat = (int)(lat / 10) * 10;
306         NS = 'n';
307     }
308     if ( lon < 0 ) {
309         int base = (int)(lon / 10);
310         if ( lon == base * 10 ) {
311             baselon = base * 10;
312         } else {
313             baselon = (base - 1) * 10;
314         }
315         EW = 'w';
316     } else {
317             baselon = (int)(lon / 10) * 10;
318             EW = 'e';
319         }
320     
321     const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
322     const char** tree;
323     char dir[512];
324   
325     for (tree = &terrainobjects[0]; *tree; tree++) {
326         snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
327               *tree,
328               EW, abs(baselon), NS, abs(baselat),
329               EW, abs(lon), NS, abs(lat) );
330         sync_tree(dir);
331     }
332 }
333
334
335 // sync areas
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 );
339
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 ) {
345                     sync_area( i, j );
346                 }
347             }
348         }
349     } else {
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 );
354         }
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 );
359         }
360     }
361 }
362
363
364 int main( int argc, char **argv ) {
365     int port = 5501;
366     char host[256] = "";        // accept messages from anyone
367     bool testing = false;
368
369     // parse arguments
370     int i = 1;
371     while ( i < argc ) {
372         if ( (string)argv[i] == "-p" ) {
373             ++i;
374             port = atoi( argv[i] );
375         } else if ( (string)argv[i] == "-s" ) {
376             ++i;
377             source_base = argv[i];
378         } else if ( (string)argv[i] == "-d" ) {
379             ++i;
380             dest_base = argv[i];
381         } else if ( (string)argv[i] == "-R" ) {
382             use_svn = false;
383         } else if ( (string)argv[i] == "-S" ) {
384             use_svn = true;
385         } else if ( (string)argv[i] == "-T" ) {
386             testing = true;
387         } else {
388             usage( argv[0] );
389             exit(-1);        
390         }
391         ++i;
392     }
393
394     // Use the appropriate default for the "-s" flag
395     if (source_base == NULL) {
396         if (use_svn)
397             source_base = svn_base;
398         else
399             source_base = rsync_base;
400     }
401     
402     // We just want one grid square, no FGFS communications
403     if (testing) {
404           sync_areas( 37, -123, 0, 0 );
405           exit(0);
406     }
407
408     // Must call this before any other net stuff
409     netInit( &argc,argv );
410
411     netSocket s;
412
413     if ( ! s.open( false ) ) {  // open a UDP socket
414         printf("error opening socket\n");
415         return -1;
416     }
417
418     s.setBlocking( false );
419
420     if ( s.bind( host, port ) == -1 ) {
421         printf("error binding to port %d\n", port);
422         return -1;
423     }
424
425     char msg[256];
426     int maxlen = 256;
427     int len;
428     int lat, lon;
429     int last_lat = nowhere;
430     int last_lon = nowhere;
431     bool recv_msg = false;
432     int synced_other = 0;
433
434     while ( true ) {
435         recv_msg = false;
436         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
437             msg[len] = '\0';
438             recv_msg = true;
439
440             parse_message( msg, &lat, &lon );
441             cout << "pos in msg = " << lat << "," << lon << endl;
442         }
443
444         if ( recv_msg ) {
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;
449                 } else {
450                     dist = lat - last_lat;
451                     if ( dist != 0 ) {
452                         lat_dir = dist / abs(dist);
453                     } else {
454                         lat_dir = 0;
455                     }
456                     dist = lon - last_lon;
457                     if ( dist != 0 ) {
458                         lon_dir = dist / abs(dist);
459                     } else {
460                         lon_dir = 0;
461                     }
462                 }
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;
469             } else {
470                 switch (synced_other++) {
471                 case 0:
472                     sync_tree((char*) "Airports/K");
473                     break;
474                 case 1:
475                     sync_tree((char*) "Airports");
476                     break;
477                 default:
478                     cout << "Done non-tile syncs" << endl;
479                     break;
480                 }
481             }
482
483             last_lat = lat;
484             last_lon = lon;
485         } 
486
487         ulSleep( 1 );
488     } // while true
489         
490     return 0;
491 }
492