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/math/SGMath.hxx>
50 #include <simgear/io/raw_socket.hxx>
51 #include <simgear/scene/tsync/terrasync.hxx>
53 #if defined(_MSC_VER) || defined(__MINGW32__)
54 typedef void (__cdecl * sighandler_t)(int);
55 #elif defined( __APPLE__ ) || defined (__FreeBSD__)
56 typedef sig_t sighandler_t;
59 int termination_triggering_signals[] = {
60 #if defined(_MSC_VER) || defined(__MINGW32__)
61 SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
63 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM,
65 0}; // zero terminated
69 const char* svn_base =
70 "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
71 const char* rsync_base = "scenery.flightgear.org::Scenery";
73 bool terminating = false;
74 sighandler_t prior_signal_handlers[32];
76 simgear::Socket theSocket;
77 simgear::SGTerraSync* pTerraSync = NULL;
79 /** display usage information */
81 usage( const string& prog ) {
82 cout << "Usage: terrasync [options]\n"
84 " -d <dest> destination directory [required]\n"
85 " -R transport using pipe to rsync\n"
86 " -S transport using built-in svn\n"
87 " -e transport using external svn client\n"
88 " -p <port> listen on UDP port [default: 5501]\n"
89 " -s <source> source base [default: '']\n"
91 " -pid <pidfile> write PID to file\n"
93 " -v be more verbose\n";
98 " pid=$(cat $pidfile 2>/dev/null)\n"
99 " if test -n \"$pid\" && kill -0 $pid ; then\n"
100 " echo \"terrasync already running: $pid\"\n"
102 " nice /games/sport/fgs/utils/TerraSync/terrasync \\\n"
103 " -v -pid $pidfile -S -p 5500 -d /games/orig/terrasync &\n"
108 /** Signal handler for termination requests (Ctrl-C) */
110 terminate_request_handler(int param)
112 char msg[] = "\nReceived signal XX, intend to exit soon.\n"
113 "Repeat the signal to force immediate termination.\n";
114 msg[17] = '0' + param / 10;
115 msg[18] = '0' + param % 10;
116 if (write(1, msg, sizeof(msg) - 1) == -1)
118 // need to act write's return value to avoid GCC compiler warning
119 // "'write' declared with attribute warn_unused_result"
120 terminating = true; // dummy operation
123 signal(param, prior_signal_handlers[param]);
126 pTerraSync->unbind();
129 /** Parse command-line options. */
131 parseOptions(int argc, char** argv, SGPropertyNode_ptr config,
132 bool& testing, int& verbose, int& port, const char* &pidfn)
135 for (int i=1; i < argc; ++i )
137 string arg = argv[i];
140 port = atoi( argv[i] );
141 } else if ( arg.find("-pid") == 0 ) {
144 cout << "pidfn: " << pidfn << endl;
145 } else if ( arg == "-s" ) {
147 config->setStringValue("svn-server", argv[i]);
148 } else if ( arg == "-d" ) {
150 config->setStringValue("scenery-dir", argv[i]);
151 } else if ( arg == "-R" ) {
152 config->setBoolValue("use-svn", false);
153 } else if ( arg == "-S" ) {
154 config->setBoolValue("use-built-in-svn", true);
155 } else if ( arg == "-e" ) {
156 config->setBoolValue("use-built-in-svn", false);
157 } else if ( arg == "-v" ) {
159 } else if ( arg == "-T" ) {
161 } else if ( arg == "-h" ) {
165 cerr << "Unrecognized command-line option '" << arg << "'" << endl;
172 /** parse a single NMEA-0183 position message */
174 parse_message( int verbose, const string &msg, int *lat, int *lon )
180 cout << "message: '" << text << "'" << endl;
182 // find GGA string and advance to start of lat (see NMEA-0183 for protocol specs)
183 string::size_type pos = text.find( "$GPGGA" );
184 if ( pos == string::npos )
186 *lat = simgear::NOWHERE;
187 *lon = simgear::NOWHERE;
190 string tmp = text.substr( pos + 7 );
191 pos = tmp.find( "," );
192 tmp = tmp.substr( pos + 1 );
193 // cout << "-> " << tmp << endl;
195 // find lat then advance to start of hemisphere
196 pos = tmp.find( "," );
197 string lats = tmp.substr( 0, pos );
198 dlat = atof( lats.c_str() ) / 100.0;
199 tmp = tmp.substr( pos + 1 );
201 // find N/S hemisphere and advance to start of lon
202 if ( tmp.substr( 0, 1 ) == "S" ) {
205 pos = tmp.find( "," );
206 tmp = tmp.substr( pos + 1 );
209 pos = tmp.find( "," );
210 string lons = tmp.substr( 0, pos );
211 dlon = atof( lons.c_str() ) / 100.0;
212 tmp = tmp.substr( pos + 1 );
214 // find E/W hemisphere and advance to start of lon
215 if ( tmp.substr( 0, 1 ) == "W" ) {
220 *lat = (int)dlat - 1;
226 *lon = (int)dlon - 1;
231 if ((dlon == 0) && (dlat == 0)) {
232 *lon = simgear::NOWHERE;
233 *lat = simgear::NOWHERE;
237 /** Monitor UDP socket and process NMEA position updates. */
239 processRequests(SGPropertyNode_ptr config, bool testing, int verbose, int port)
241 const char* host = "localhost";
245 bool connected = false;
247 // Must call this before any other net stuff
248 simgear::Socket::initSockets();
251 if ( !theSocket.open( false ) )
253 cerr << "error opening socket" << endl;
257 if ( theSocket.bind( host, port ) == -1 )
259 cerr << "error binding to port " << port << endl;
263 theSocket.setBlocking(true);
265 while ( (!terminating)&&
266 (!config->getBoolValue("stalled", false)) )
269 cout << "main loop" << endl;
271 bool recv_msg = false;
274 // Testing without FGFS communication
280 if (verbose && pTerraSync->isIdle())
282 cout << "Idle; waiting for FlightGear position data" << endl;
284 len = theSocket.recv(msg, sizeof(msg)-1, 0);
290 cout << "recv length: " << len << endl;
291 parse_message( verbose, msg, &lat, &lon );
293 (lat != simgear::NOWHERE)&&
294 (lon != simgear::NOWHERE))
296 cout << "Valid position data received. Connected successfully." << endl;
304 pTerraSync->schedulePosition(lat, lon);
309 if (pTerraSync->isIdle())
312 SGTimeStamp::sleepForMSec(1000);
314 } // while !terminating
320 int main( int argc, char **argv )
325 bool testing = false;
326 const char* pidfn = "";
328 // default configuration
329 sglog().setLogLevels( SG_ALL, SG_ALERT);
330 SGPropertyNode_ptr root = new SGPropertyNode();
331 SGPropertyNode_ptr config = root->getNode("/sim/terrasync", true);
332 config->setStringValue("scenery-dir", "terrasyncdir");
333 config->setStringValue("svn-server", svn_base);
334 config->setStringValue("rsync-server", rsync_base);
335 config->setBoolValue("use-built-in-svn", true);
336 config->setBoolValue("use-svn", true);
337 config->setBoolValue("enabled", true);
338 config->setIntValue("max-errors", -1); // -1 = infinite
340 // parse command-line arguments
341 parseOptions(argc, argv, config, testing, verbose, port, pidfn);
344 sglog().setLogLevels( SG_ALL, SG_INFO);
351 pidstream.open(pidfn);
352 if (!pidstream.good())
354 cerr << "Cannot open pid file '" << pidfn << "': ";
358 pidstream << getpid() << endl;
363 // install signal handlers
364 for (int* sigp=termination_triggering_signals; *sigp; sigp++)
366 prior_signal_handlers[*sigp] =
367 signal(*sigp, *terminate_request_handler);
369 cout << "Intercepting signal " << *sigp << endl;
373 pTerraSync = new simgear::SGTerraSync(root);
377 // now monitor and process position updates
378 exit_code = processRequests(config, testing, verbose, port);
380 pTerraSync->unbind();