1 // terrasync.cxx -- "JIT" scenery fetcher
3 // Written by Curtis Olson, started November 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
6 // Copyright (C) 2008 Alexander R. Perry <alex.perry@ieee.org>
7 // Copyright (C) 2011 Thorsten Brehm <brehmt@gmail.com>
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/simgear_config.h>
41 #elif defined(_MSC_VER)
45 #include <stdlib.h> // atoi() atof() abs() system()
46 #include <signal.h> // signal()
48 #include <simgear/compiler.h>
54 #include <simgear/io/raw_socket.hxx>
55 #include <simgear/scene/tsync/terrasync.hxx>
57 #if defined(_MSC_VER) || defined(__MINGW32__)
58 typedef void (__cdecl * sighandler_t)(int);
59 #elif defined( __APPLE__ ) || defined (__FreeBSD__)
60 typedef sig_t sighandler_t;
63 int termination_triggering_signals[] = {
64 #if defined(_MSC_VER) || defined(__MINGW32__)
65 SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
67 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM,
69 0}; // zero terminated
73 const char* svn_base = "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
74 const char* rsync_base = "flightgear.mxchange.org::Scenery";
76 bool terminating = false;
77 sighandler_t prior_signal_handlers[32];
79 simgear::Socket theSocket;
80 simgear::SGTerraSync* pTerraSync = NULL;
82 /** display usage information */
84 usage( const string& prog ) {
85 cout << "Usage: terrasync [options]\n"
87 " -d <dest> destination directory [required]\n"
88 " -R transport using pipe to rsync\n"
89 " -S transport using built-in svn\n"
90 " -e transport using external svn client\n"
91 " -p <port> listen on UDP port [default: 5501]\n"
92 " -s <source> source base [default: '']\n"
94 " -pid <pidfile> write PID to file\n"
96 " -v be more verbose\n";
101 " pid=$(cat $pidfile 2>/dev/null)\n"
102 " if test -n \"$pid\" && kill -0 $pid ; then\n"
103 " echo \"terrasync already running: $pid\"\n"
105 " nice /games/sport/fgs/utils/TerraSync/terrasync \\\n"
106 " -v -pid $pidfile -S -p 5500 -d /games/orig/terrasync &\n"
111 /** Signal handler for termination requests (Ctrl-C) */
113 terminate_request_handler(int param)
115 char msg[] = "\nReceived signal XX, intend to exit soon.\n"
116 "Repeat the signal to force immediate termination.\n";
117 msg[17] = '0' + param / 10;
118 msg[18] = '0' + param % 10;
119 if (write(1, msg, sizeof(msg) - 1) == -1)
121 // need to act write's return value to avoid GCC compiler warning
122 // "'write' declared with attribute warn_unused_result"
123 terminating = true; // dummy operation
126 signal(param, prior_signal_handlers[param]);
129 pTerraSync->unbind();
132 /** Parse command-line options. */
134 parseOptions(int argc, char** argv, SGPropertyNode_ptr config,
135 bool& testing, int& verbose, int& port, const char* &pidfn)
138 for (int i=1; i < argc; ++i )
140 string arg = argv[i];
143 port = atoi( argv[i] );
144 } else if ( arg.find("-pid") == 0 ) {
147 cout << "pidfn: " << pidfn << endl;
148 } else if ( arg == "-s" ) {
150 config->setStringValue("svn-server", argv[i]);
151 } else if ( arg == "-d" ) {
153 config->setStringValue("scenery-dir", argv[i]);
154 } else if ( arg == "-R" ) {
155 config->setBoolValue("use-svn", false);
156 } else if ( arg == "-S" ) {
157 config->setBoolValue("use-built-in-svn", true);
158 } else if ( arg == "-e" ) {
159 config->setBoolValue("use-built-in-svn", false);
160 } else if ( arg == "-v" ) {
162 } else if ( arg == "-T" ) {
164 } else if ( arg == "-h" ) {
168 cerr << "Unrecognized command-line option '" << arg << "'" << endl;
175 /** parse a single NMEA-0183 position message */
177 parse_message( int verbose, const string &msg, int *lat, int *lon )
183 cout << "message: '" << text << "'" << endl;
185 // find GGA string and advance to start of lat (see NMEA-0183 for protocol specs)
186 string::size_type pos = text.find( "$GPGGA" );
187 if ( pos == string::npos )
189 *lat = simgear::NOWHERE;
190 *lon = simgear::NOWHERE;
193 string tmp = text.substr( pos + 7 );
194 pos = tmp.find( "," );
195 tmp = tmp.substr( pos + 1 );
196 // cout << "-> " << tmp << endl;
198 // find lat then advance to start of hemisphere
199 pos = tmp.find( "," );
200 string lats = tmp.substr( 0, pos );
201 dlat = atof( lats.c_str() ) / 100.0;
202 tmp = tmp.substr( pos + 1 );
204 // find N/S hemisphere and advance to start of lon
205 if ( tmp.substr( 0, 1 ) == "S" ) {
208 pos = tmp.find( "," );
209 tmp = tmp.substr( pos + 1 );
212 pos = tmp.find( "," );
213 string lons = tmp.substr( 0, pos );
214 dlon = atof( lons.c_str() ) / 100.0;
215 tmp = tmp.substr( pos + 1 );
217 // find E/W hemisphere and advance to start of lon
218 if ( tmp.substr( 0, 1 ) == "W" ) {
223 *lat = (int)dlat - 1;
229 *lon = (int)dlon - 1;
234 if ((dlon == 0) && (dlat == 0)) {
235 *lon = simgear::NOWHERE;
236 *lat = simgear::NOWHERE;
241 syncArea( int lat, int lon )
243 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
246 int baselat, baselon;
249 int base = (int)(lat / 10);
250 if ( lat == base * 10 ) {
253 baselat = (base - 1) * 10;
257 baselat = (int)(lat / 10) * 10;
261 int base = (int)(lon / 10);
262 if ( lon == base * 10 ) {
265 baselon = (base - 1) * 10;
269 baselon = (int)(lon / 10) * 10;
275 << EW << setw(3) << abs(baselon) << NS << setw(2) << abs(baselat) << "/"
276 << EW << setw(3) << abs(lon) << NS << setw(2) << abs(lat);
278 pTerraSync->syncAreaByPath(dir.str());
282 syncAreas( int lat, int lon, int lat_dir, int lon_dir )
284 if ( lat_dir == 0 && lon_dir == 0 ) {
286 // do surrounding 8 1x1 degree areas.
287 for ( int i = lat - 1; i <= lat + 1; ++i ) {
288 for ( int j = lon - 1; j <= lon + 1; ++j ) {
289 if ( i != lat || j != lon ) {
295 if ( lat_dir != 0 ) {
296 syncArea( lat + lat_dir, lon - 1 );
297 syncArea( lat + lat_dir, lon + 1 );
298 syncArea( lat + lat_dir, lon );
300 if ( lon_dir != 0 ) {
301 syncArea( lat - 1, lon + lon_dir );
302 syncArea( lat + 1, lon + lon_dir );
303 syncArea( lat, lon + lon_dir );
307 // do current 1x1 degree area first
308 syncArea( lat, lon );
312 schedulePosition(int lat, int lon)
315 if ((lat == simgear::NOWHERE) || (lon == simgear::NOWHERE)) {
319 static int last_lat = simgear::NOWHERE;
320 static int last_lon = simgear::NOWHERE;
321 if ((lat == last_lat) && (lon == last_lon)) {
329 if ( last_lat != simgear::NOWHERE && last_lon != simgear::NOWHERE )
331 int dist = lat - last_lat;
334 lat_dir = dist / abs(dist);
340 dist = lon - last_lon;
343 lon_dir = dist / abs(dist);
350 cout << "Scenery update for " <<
351 "lat = " << lat << ", lon = " << lon <<
352 ", lat_dir = " << lat_dir << ", " <<
353 "lon_dir = " << lon_dir << endl;
355 syncAreas( lat, lon, lat_dir, lon_dir );
364 /** Monitor UDP socket and process NMEA position updates. */
366 processRequests(SGPropertyNode_ptr config, bool testing, int verbose, int port)
368 const char* host = "localhost";
372 bool connected = false;
374 // Must call this before any other net stuff
375 simgear::Socket::initSockets();
378 if ( !theSocket.open( false ) )
380 cerr << "error opening socket" << endl;
384 if ( theSocket.bind( host, port ) == -1 )
386 cerr << "error binding to port " << port << endl;
390 theSocket.setBlocking(true);
392 while ( (!terminating)&&
393 (!config->getBoolValue("stalled", false)) )
396 cout << "main loop" << endl;
398 bool recv_msg = false;
401 // Testing without FGFS communication
407 if (verbose && pTerraSync->isIdle())
409 cout << "Idle; waiting for FlightGear position data" << endl;
411 len = theSocket.recv(msg, sizeof(msg)-1, 0);
417 cout << "recv length: " << len << endl;
418 parse_message( verbose, msg, &lat, &lon );
420 (lat != simgear::NOWHERE)&&
421 (lon != simgear::NOWHERE))
423 cout << "Valid position data received. Connected successfully." << endl;
431 schedulePosition(lat, lon);
436 if (pTerraSync->isIdle())
439 SGTimeStamp::sleepForMSec(1000);
441 } // while !terminating
447 int main( int argc, char **argv )
452 bool testing = false;
453 const char* pidfn = "";
455 // default configuration
456 sglog().setLogLevels( SG_ALL, SG_ALERT);
457 SGPropertyNode_ptr root = new SGPropertyNode();
458 SGPropertyNode_ptr config = root->getNode("/sim/terrasync", true);
459 config->setStringValue("scenery-dir", "terrasyncdir");
460 config->setStringValue("svn-server", svn_base);
461 config->setStringValue("rsync-server", rsync_base);
462 config->setBoolValue("use-built-in-svn", true);
463 config->setBoolValue("use-svn", true);
464 config->setBoolValue("enabled", true);
465 config->setIntValue("max-errors", -1); // -1 = infinite
467 // parse command-line arguments
468 parseOptions(argc, argv, config, testing, verbose, port, pidfn);
471 sglog().setLogLevels( SG_ALL, SG_INFO);
478 pidstream.open(pidfn);
479 if (!pidstream.good())
481 cerr << "Cannot open pid file '" << pidfn << "': ";
485 pidstream << getpid() << endl;
490 // install signal handlers
491 for (int* sigp=termination_triggering_signals; *sigp; sigp++)
493 prior_signal_handlers[*sigp] =
494 signal(*sigp, *terminate_request_handler);
496 cout << "Intercepting signal " << *sigp << endl;
500 pTerraSync = new simgear::SGTerraSync;
501 pTerraSync->setRoot(root);
505 // now monitor and process position updates
506 exit_code = processRequests(config, testing, verbose, port);
508 pTerraSync->unbind();