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;
241 /** Monitor UDP socket and process NMEA position updates. */
243 processRequests(SGPropertyNode_ptr config, bool testing, int verbose, int port)
245 const char* host = "localhost";
249 bool connected = false;
251 // Must call this before any other net stuff
252 simgear::Socket::initSockets();
255 if ( !theSocket.open( false ) )
257 cerr << "error opening socket" << endl;
261 if ( theSocket.bind( host, port ) == -1 )
263 cerr << "error binding to port " << port << endl;
267 theSocket.setBlocking(true);
269 while ( (!terminating)&&
270 (!config->getBoolValue("stalled", false)) )
273 cout << "main loop" << endl;
275 bool recv_msg = false;
278 // Testing without FGFS communication
284 if (verbose && pTerraSync->isIdle())
286 cout << "Idle; waiting for FlightGear position data" << endl;
288 len = theSocket.recv(msg, sizeof(msg)-1, 0);
294 cout << "recv length: " << len << endl;
295 parse_message( verbose, msg, &lat, &lon );
297 (lat != simgear::NOWHERE)&&
298 (lon != simgear::NOWHERE))
300 cout << "Valid position data received. Connected successfully." << endl;
308 pTerraSync->schedulePosition(lat, lon);
313 if (pTerraSync->isIdle())
316 SGTimeStamp::sleepForMSec(1000);
318 } // while !terminating
324 int main( int argc, char **argv )
329 bool testing = false;
330 const char* pidfn = "";
332 // default configuration
333 sglog().setLogLevels( SG_ALL, SG_ALERT);
334 SGPropertyNode_ptr root = new SGPropertyNode();
335 SGPropertyNode_ptr config = root->getNode("/sim/terrasync", true);
336 config->setStringValue("scenery-dir", "terrasyncdir");
337 config->setStringValue("svn-server", svn_base);
338 config->setStringValue("rsync-server", rsync_base);
339 config->setBoolValue("use-built-in-svn", true);
340 config->setBoolValue("use-svn", true);
341 config->setBoolValue("enabled", true);
342 config->setIntValue("max-errors", -1); // -1 = infinite
344 // parse command-line arguments
345 parseOptions(argc, argv, config, testing, verbose, port, pidfn);
348 sglog().setLogLevels( SG_ALL, SG_INFO);
355 pidstream.open(pidfn);
356 if (!pidstream.good())
358 cerr << "Cannot open pid file '" << pidfn << "': ";
362 pidstream << getpid() << endl;
367 // install signal handlers
368 for (int* sigp=termination_triggering_signals; *sigp; sigp++)
370 prior_signal_handlers[*sigp] =
371 signal(*sigp, *terminate_request_handler);
373 cout << "Intercepting signal " << *sigp << endl;
377 pTerraSync = new simgear::SGTerraSync(root);
381 // now monitor and process position updates
382 exit_code = processRequests(config, testing, verbose, port);
384 pTerraSync->unbind();