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.
36 #elif defined(_MSC_VER)
40 #include <stdlib.h> // atoi() atof() abs() system()
41 #include <signal.h> // signal()
43 #include <simgear/compiler.h>
49 #include <simgear/io/raw_socket.hxx>
50 #include <simgear/scene/tsync/terrasync.hxx>
52 #if defined(_MSC_VER) || defined(__MINGW32__)
53 typedef void (__cdecl * sighandler_t)(int);
54 #elif defined( __APPLE__ ) || defined (__FreeBSD__)
55 typedef sig_t sighandler_t;
58 int termination_triggering_signals[] = {
59 #if defined(_MSC_VER) || defined(__MINGW32__)
60 SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
62 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM,
64 0}; // zero terminated
68 const char* svn_base =
69 "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
70 const char* rsync_base = "scenery.flightgear.org::Scenery";
72 bool terminating = false;
73 sighandler_t prior_signal_handlers[32];
75 simgear::Socket theSocket;
76 simgear::SGTerraSync* pTerraSync = NULL;
78 /** display usage information */
80 usage( const string& prog ) {
81 cout << "Usage: terrasync [options]\n"
83 " -d <dest> destination directory [required]\n"
84 " -R transport using pipe to rsync\n"
85 " -S transport using built-in svn\n"
86 " -e transport using external svn client\n"
87 " -p <port> listen on UDP port [default: 5501]\n"
88 " -s <source> source base [default: '']\n"
90 " -pid <pidfile> write PID to file\n"
92 " -v be more verbose\n";
97 " pid=$(cat $pidfile 2>/dev/null)\n"
98 " if test -n \"$pid\" && kill -0 $pid ; then\n"
99 " echo \"terrasync already running: $pid\"\n"
101 " nice /games/sport/fgs/utils/TerraSync/terrasync \\\n"
102 " -v -pid $pidfile -S -p 5500 -d /games/orig/terrasync &\n"
107 /** Signal handler for termination requests (Ctrl-C) */
109 terminate_request_handler(int param)
111 char msg[] = "\nReceived signal XX, intend to exit soon.\n"
112 "Repeat the signal to force immediate termination.\n";
113 msg[17] = '0' + param / 10;
114 msg[18] = '0' + param % 10;
115 if (write(1, msg, sizeof(msg) - 1) == -1)
117 // need to act write's return value to avoid GCC compiler warning
118 // "'write' declared with attribute warn_unused_result"
119 terminating = true; // dummy operation
122 signal(param, prior_signal_handlers[param]);
125 pTerraSync->unbind();
128 /** Parse command-line options. */
130 parseOptions(int argc, char** argv, SGPropertyNode_ptr config,
131 bool& testing, int& verbose, int& port, const char* &pidfn)
134 for (int i=1; i < argc; ++i )
136 string arg = argv[i];
139 port = atoi( argv[i] );
140 } else if ( arg.find("-pid") == 0 ) {
143 cout << "pidfn: " << pidfn << endl;
144 } else if ( arg == "-s" ) {
146 config->setStringValue("svn-server", argv[i]);
147 } else if ( arg == "-d" ) {
149 config->setStringValue("scenery-dir", argv[i]);
150 } else if ( arg == "-R" ) {
151 config->setBoolValue("use-svn", false);
152 } else if ( arg == "-S" ) {
153 config->setBoolValue("use-built-in-svn", true);
154 } else if ( arg == "-e" ) {
155 config->setBoolValue("use-built-in-svn", false);
156 } else if ( arg == "-v" ) {
158 } else if ( arg == "-T" ) {
160 } else if ( arg == "-h" ) {
164 cerr << "Unrecognized command-line option '" << arg << "'" << endl;
171 /** parse a single NMEA-0183 position message */
173 parse_message( int verbose, const string &msg, int *lat, int *lon )
179 cout << "message: '" << text << "'" << endl;
181 // find GGA string and advance to start of lat (see NMEA-0183 for protocol specs)
182 string::size_type pos = text.find( "$GPGGA" );
183 if ( pos == string::npos )
185 *lat = simgear::NOWHERE;
186 *lon = simgear::NOWHERE;
189 string tmp = text.substr( pos + 7 );
190 pos = tmp.find( "," );
191 tmp = tmp.substr( pos + 1 );
192 // cout << "-> " << tmp << endl;
194 // find lat then advance to start of hemisphere
195 pos = tmp.find( "," );
196 string lats = tmp.substr( 0, pos );
197 dlat = atof( lats.c_str() ) / 100.0;
198 tmp = tmp.substr( pos + 1 );
200 // find N/S hemisphere and advance to start of lon
201 if ( tmp.substr( 0, 1 ) == "S" ) {
204 pos = tmp.find( "," );
205 tmp = tmp.substr( pos + 1 );
208 pos = tmp.find( "," );
209 string lons = tmp.substr( 0, pos );
210 dlon = atof( lons.c_str() ) / 100.0;
211 tmp = tmp.substr( pos + 1 );
213 // find E/W hemisphere and advance to start of lon
214 if ( tmp.substr( 0, 1 ) == "W" ) {
219 *lat = (int)dlat - 1;
225 *lon = (int)dlon - 1;
230 if ((dlon == 0) && (dlat == 0)) {
231 *lon = simgear::NOWHERE;
232 *lat = simgear::NOWHERE;
236 /** Monitor UDP socket and process NMEA position updates. */
238 processRequests(SGPropertyNode_ptr config, bool testing, int verbose, int port)
240 const char* host = "localhost";
244 bool connected = false;
246 // Must call this before any other net stuff
247 simgear::Socket::initSockets();
250 if ( !theSocket.open( false ) )
252 cerr << "error opening socket" << endl;
256 if ( theSocket.bind( host, port ) == -1 )
258 cerr << "error binding to port " << port << endl;
262 theSocket.setBlocking(true);
264 while ( (!terminating)&&
265 (!config->getBoolValue("stalled", false)) )
268 cout << "main loop" << endl;
270 bool recv_msg = false;
273 // Testing without FGFS communication
279 if (verbose && pTerraSync->isIdle())
281 cout << "Idle; waiting for FlightGear position data" << endl;
283 len = theSocket.recv(msg, sizeof(msg)-1, 0);
289 cout << "recv length: " << len << endl;
290 parse_message( verbose, msg, &lat, &lon );
292 (lat != simgear::NOWHERE)&&
293 (lon != simgear::NOWHERE))
295 cout << "Valid position data received. Connected successfully." << endl;
303 pTerraSync->schedulePosition(lat, lon);
308 if (pTerraSync->isIdle())
311 SGTimeStamp::sleepForMSec(1000);
313 } // while !terminating
319 int main( int argc, char **argv )
324 bool testing = false;
325 const char* pidfn = "";
327 // default configuration
328 sglog().setLogLevels( SG_ALL, SG_ALERT);
329 SGPropertyNode_ptr root = new SGPropertyNode();
330 SGPropertyNode_ptr config = root->getNode("/sim/terrasync", true);
331 config->setStringValue("scenery-dir", "terrasyncdir");
332 config->setStringValue("svn-server", svn_base);
333 config->setStringValue("rsync-server", rsync_base);
334 config->setBoolValue("use-built-in-svn", true);
335 config->setBoolValue("use-svn", true);
336 config->setBoolValue("enabled", true);
337 config->setIntValue("max-errors", -1); // -1 = infinite
339 // parse command-line arguments
340 parseOptions(argc, argv, config, testing, verbose, port, pidfn);
343 sglog().setLogLevels( SG_ALL, SG_INFO);
350 pidstream.open(pidfn);
351 if (!pidstream.good())
353 cerr << "Cannot open pid file '" << pidfn << "': ";
357 pidstream << getpid() << endl;
362 // install signal handlers
363 for (int* sigp=termination_triggering_signals; *sigp; sigp++)
365 prior_signal_handlers[*sigp] =
366 signal(*sigp, *terminate_request_handler);
368 cout << "Intercepting signal " << *sigp << endl;
372 pTerraSync = new simgear::SGTerraSync(root);
376 // now monitor and process position updates
377 exit_code = processRequests(config, testing, verbose, port);
379 pTerraSync->unbind();