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 =
74 "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
75 const char* rsync_base = "scenery.flightgear.org::Scenery";
77 bool terminating = false;
78 sighandler_t prior_signal_handlers[32];
80 simgear::Socket theSocket;
81 simgear::SGTerraSync* pTerraSync = NULL;
83 /** display usage information */
85 usage( const string& prog ) {
86 cout << "Usage: terrasync [options]\n"
88 " -d <dest> destination directory [required]\n"
89 " -R transport using pipe to rsync\n"
90 " -S transport using built-in svn\n"
91 " -e transport using external svn client\n"
92 " -p <port> listen on UDP port [default: 5501]\n"
93 " -s <source> source base [default: '']\n"
95 " -pid <pidfile> write PID to file\n"
97 " -v be more verbose\n";
102 " pid=$(cat $pidfile 2>/dev/null)\n"
103 " if test -n \"$pid\" && kill -0 $pid ; then\n"
104 " echo \"terrasync already running: $pid\"\n"
106 " nice /games/sport/fgs/utils/TerraSync/terrasync \\\n"
107 " -v -pid $pidfile -S -p 5500 -d /games/orig/terrasync &\n"
112 /** Signal handler for termination requests (Ctrl-C) */
114 terminate_request_handler(int param)
116 char msg[] = "\nReceived signal XX, intend to exit soon.\n"
117 "Repeat the signal to force immediate termination.\n";
118 msg[17] = '0' + param / 10;
119 msg[18] = '0' + param % 10;
120 if (write(1, msg, sizeof(msg) - 1) == -1)
122 // need to act write's return value to avoid GCC compiler warning
123 // "'write' declared with attribute warn_unused_result"
124 terminating = true; // dummy operation
127 signal(param, prior_signal_handlers[param]);
130 pTerraSync->unbind();
133 /** Parse command-line options. */
135 parseOptions(int argc, char** argv, SGPropertyNode_ptr config,
136 bool& testing, int& verbose, int& port, const char* &pidfn)
139 for (int i=1; i < argc; ++i )
141 string arg = argv[i];
144 port = atoi( argv[i] );
145 } else if ( arg.find("-pid") == 0 ) {
148 cout << "pidfn: " << pidfn << endl;
149 } else if ( arg == "-s" ) {
151 config->setStringValue("svn-server", argv[i]);
152 } else if ( arg == "-d" ) {
154 config->setStringValue("scenery-dir", argv[i]);
155 } else if ( arg == "-R" ) {
156 config->setBoolValue("use-svn", false);
157 } else if ( arg == "-S" ) {
158 config->setBoolValue("use-built-in-svn", true);
159 } else if ( arg == "-e" ) {
160 config->setBoolValue("use-built-in-svn", false);
161 } else if ( arg == "-v" ) {
163 } else if ( arg == "-T" ) {
165 } else if ( arg == "-h" ) {
169 cerr << "Unrecognized command-line option '" << arg << "'" << endl;
176 /** parse a single NMEA-0183 position message */
178 parse_message( int verbose, const string &msg, int *lat, int *lon )
184 cout << "message: '" << text << "'" << endl;
186 // find GGA string and advance to start of lat (see NMEA-0183 for protocol specs)
187 string::size_type pos = text.find( "$GPGGA" );
188 if ( pos == string::npos )
190 *lat = simgear::NOWHERE;
191 *lon = simgear::NOWHERE;
194 string tmp = text.substr( pos + 7 );
195 pos = tmp.find( "," );
196 tmp = tmp.substr( pos + 1 );
197 // cout << "-> " << tmp << endl;
199 // find lat then advance to start of hemisphere
200 pos = tmp.find( "," );
201 string lats = tmp.substr( 0, pos );
202 dlat = atof( lats.c_str() ) / 100.0;
203 tmp = tmp.substr( pos + 1 );
205 // find N/S hemisphere and advance to start of lon
206 if ( tmp.substr( 0, 1 ) == "S" ) {
209 pos = tmp.find( "," );
210 tmp = tmp.substr( pos + 1 );
213 pos = tmp.find( "," );
214 string lons = tmp.substr( 0, pos );
215 dlon = atof( lons.c_str() ) / 100.0;
216 tmp = tmp.substr( pos + 1 );
218 // find E/W hemisphere and advance to start of lon
219 if ( tmp.substr( 0, 1 ) == "W" ) {
224 *lat = (int)dlat - 1;
230 *lon = (int)dlon - 1;
235 if ((dlon == 0) && (dlat == 0)) {
236 *lon = simgear::NOWHERE;
237 *lat = simgear::NOWHERE;
242 syncArea( int lat, int lon )
244 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
247 int baselat, baselon;
250 int base = (int)(lat / 10);
251 if ( lat == base * 10 ) {
254 baselat = (base - 1) * 10;
258 baselat = (int)(lat / 10) * 10;
262 int base = (int)(lon / 10);
263 if ( lon == base * 10 ) {
266 baselon = (base - 1) * 10;
270 baselon = (int)(lon / 10) * 10;
276 << EW << setw(3) << abs(baselon) << NS << setw(2) << abs(baselat) << "/"
277 << EW << setw(3) << abs(lon) << NS << setw(2) << abs(lat);
279 pTerraSync->syncAreaByPath(dir.str());
283 syncAreas( int lat, int lon, int lat_dir, int lon_dir )
285 if ( lat_dir == 0 && lon_dir == 0 ) {
287 // do surrounding 8 1x1 degree areas.
288 for ( int i = lat - 1; i <= lat + 1; ++i ) {
289 for ( int j = lon - 1; j <= lon + 1; ++j ) {
290 if ( i != lat || j != lon ) {
296 if ( lat_dir != 0 ) {
297 syncArea( lat + lat_dir, lon - 1 );
298 syncArea( lat + lat_dir, lon + 1 );
299 syncArea( lat + lat_dir, lon );
301 if ( lon_dir != 0 ) {
302 syncArea( lat - 1, lon + lon_dir );
303 syncArea( lat + 1, lon + lon_dir );
304 syncArea( lat, lon + lon_dir );
308 // do current 1x1 degree area first
309 syncArea( lat, lon );
313 schedulePosition(int lat, int lon)
316 if ((lat == simgear::NOWHERE) || (lon == simgear::NOWHERE)) {
320 static int last_lat = simgear::NOWHERE;
321 static int last_lon = simgear::NOWHERE;
322 if ((lat == last_lat) && (lon == last_lon)) {
330 if ( last_lat != simgear::NOWHERE && last_lon != simgear::NOWHERE )
332 int dist = lat - last_lat;
335 lat_dir = dist / abs(dist);
341 dist = lon - last_lon;
344 lon_dir = dist / abs(dist);
351 cout << "Scenery update for " <<
352 "lat = " << lat << ", lon = " << lon <<
353 ", lat_dir = " << lat_dir << ", " <<
354 "lon_dir = " << lon_dir << endl;
356 syncAreas( lat, lon, lat_dir, lon_dir );
365 /** Monitor UDP socket and process NMEA position updates. */
367 processRequests(SGPropertyNode_ptr config, bool testing, int verbose, int port)
369 const char* host = "localhost";
373 bool connected = false;
375 // Must call this before any other net stuff
376 simgear::Socket::initSockets();
379 if ( !theSocket.open( false ) )
381 cerr << "error opening socket" << endl;
385 if ( theSocket.bind( host, port ) == -1 )
387 cerr << "error binding to port " << port << endl;
391 theSocket.setBlocking(true);
393 while ( (!terminating)&&
394 (!config->getBoolValue("stalled", false)) )
397 cout << "main loop" << endl;
399 bool recv_msg = false;
402 // Testing without FGFS communication
408 if (verbose && pTerraSync->isIdle())
410 cout << "Idle; waiting for FlightGear position data" << endl;
412 len = theSocket.recv(msg, sizeof(msg)-1, 0);
418 cout << "recv length: " << len << endl;
419 parse_message( verbose, msg, &lat, &lon );
421 (lat != simgear::NOWHERE)&&
422 (lon != simgear::NOWHERE))
424 cout << "Valid position data received. Connected successfully." << endl;
432 schedulePosition(lat, lon);
437 if (pTerraSync->isIdle())
440 SGTimeStamp::sleepForMSec(1000);
442 } // while !terminating
448 int main( int argc, char **argv )
453 bool testing = false;
454 const char* pidfn = "";
456 // default configuration
457 sglog().setLogLevels( SG_ALL, SG_ALERT);
458 SGPropertyNode_ptr root = new SGPropertyNode();
459 SGPropertyNode_ptr config = root->getNode("/sim/terrasync", true);
460 config->setStringValue("scenery-dir", "terrasyncdir");
461 config->setStringValue("svn-server", svn_base);
462 config->setStringValue("rsync-server", rsync_base);
463 config->setBoolValue("use-built-in-svn", true);
464 config->setBoolValue("use-svn", true);
465 config->setBoolValue("enabled", true);
466 config->setIntValue("max-errors", -1); // -1 = infinite
468 // parse command-line arguments
469 parseOptions(argc, argv, config, testing, verbose, port, pidfn);
472 sglog().setLogLevels( SG_ALL, SG_INFO);
479 pidstream.open(pidfn);
480 if (!pidstream.good())
482 cerr << "Cannot open pid file '" << pidfn << "': ";
486 pidstream << getpid() << endl;
491 // install signal handlers
492 for (int* sigp=termination_triggering_signals; *sigp; sigp++)
494 prior_signal_handlers[*sigp] =
495 signal(*sigp, *terminate_request_handler);
497 cout << "Intercepting signal " << *sigp << endl;
501 pTerraSync = new simgear::SGTerraSync;
502 pTerraSync->setRoot(root);
506 // now monitor and process position updates
507 exit_code = processRequests(config, testing, verbose, port);
509 pTerraSync->unbind();