2002-12-03 15:40:32 +00:00
|
|
|
// terrasync.cxx -- "JIT" scenery fetcher
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started November 2002.
|
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
|
2008-10-12 07:55:09 +00:00
|
|
|
// Copyright (C) 2008 Alexander R. Perry <alex.perry@ieee.org>
|
2012-02-19 13:33:41 +00:00
|
|
|
// Copyright (C) 2011 Thorsten Brehm <brehmt@gmail.com>
|
2002-12-03 15:40:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-12-03 15:40:32 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
2008-02-04 20:03:52 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2010-01-23 22:26:30 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2010-02-28 22:29:21 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2011-01-05 22:47:08 +00:00
|
|
|
# include <io.h>
|
2010-01-23 22:26:30 +00:00
|
|
|
#endif
|
2002-12-03 15:40:32 +00:00
|
|
|
|
|
|
|
#include <stdlib.h> // atoi() atof() abs() system()
|
2010-02-28 09:09:48 +00:00
|
|
|
#include <signal.h> // signal()
|
2002-12-03 15:40:32 +00:00
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
2008-07-25 18:38:29 +00:00
|
|
|
#include <iostream>
|
2010-01-17 19:41:47 +00:00
|
|
|
#include <fstream>
|
2008-07-25 18:38:29 +00:00
|
|
|
#include <string>
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-24 21:23:15 +00:00
|
|
|
#include <simgear/math/SGMath.hxx>
|
2010-10-23 19:37:26 +00:00
|
|
|
#include <simgear/io/raw_socket.hxx>
|
2012-02-19 13:33:41 +00:00
|
|
|
#include <simgear/scene/tsync/terrasync.hxx>
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2010-05-09 11:23:35 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
2012-02-19 13:33:41 +00:00
|
|
|
typedef void (__cdecl * sighandler_t)(int);
|
2011-02-11 17:53:35 +00:00
|
|
|
#elif defined( __APPLE__ ) || defined (__FreeBSD__)
|
2012-02-19 13:33:41 +00:00
|
|
|
typedef sig_t sighandler_t;
|
2010-02-28 09:09:48 +00:00
|
|
|
#endif
|
|
|
|
|
2010-02-28 22:29:21 +00:00
|
|
|
int termination_triggering_signals[] = {
|
2010-05-09 11:23:35 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
2010-02-28 22:29:21 +00:00
|
|
|
SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
|
2010-05-09 11:23:35 +00:00
|
|
|
#else
|
2010-11-26 02:10:41 +00:00
|
|
|
SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM,
|
2010-02-28 09:09:48 +00:00
|
|
|
#endif
|
2010-02-28 22:29:21 +00:00
|
|
|
0}; // zero terminated
|
2010-02-28 09:09:48 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
const char* svn_base =
|
|
|
|
"http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
|
|
|
|
const char* rsync_base = "scenery.flightgear.org::Scenery";
|
|
|
|
|
|
|
|
bool terminating = false;
|
|
|
|
sighandler_t prior_signal_handlers[32];
|
|
|
|
|
|
|
|
simgear::Socket theSocket;
|
|
|
|
simgear::SGTerraSync* pTerraSync = NULL;
|
|
|
|
|
|
|
|
/** display usage information */
|
|
|
|
static void
|
|
|
|
usage( const string& prog ) {
|
|
|
|
cout << "Usage: terrasync [options]\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -d <dest> destination directory [required]\n"
|
|
|
|
" -R transport using pipe to rsync\n"
|
|
|
|
" -S transport using built-in svn\n"
|
|
|
|
" -e transport using external svn client\n"
|
|
|
|
" -p <port> listen on UDP port [default: 5501]\n"
|
|
|
|
" -s <source> source base [default: '']\n"
|
2012-02-19 21:13:41 +00:00
|
|
|
#ifndef _MSC_VER
|
2012-02-19 13:33:41 +00:00
|
|
|
" -pid <pidfile> write PID to file\n"
|
2012-02-19 21:13:41 +00:00
|
|
|
#endif
|
2012-02-19 13:33:41 +00:00
|
|
|
" -v be more verbose\n";
|
|
|
|
|
2012-02-19 21:13:41 +00:00
|
|
|
#ifndef _MSC_VER
|
2012-02-19 13:33:41 +00:00
|
|
|
cout << "\n"
|
|
|
|
"Example:\n"
|
|
|
|
" pid=$(cat $pidfile 2>/dev/null)\n"
|
|
|
|
" if test -n \"$pid\" && kill -0 $pid ; then\n"
|
|
|
|
" echo \"terrasync already running: $pid\"\n"
|
|
|
|
" else\n"
|
|
|
|
" nice /games/sport/fgs/utils/TerraSync/terrasync \\\n"
|
|
|
|
" -v -pid $pidfile -S -p 5500 -d /games/orig/terrasync &\n"
|
|
|
|
" fi\n";
|
2012-02-19 21:13:41 +00:00
|
|
|
#endif
|
2012-02-19 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Signal handler for termination requests (Ctrl-C) */
|
|
|
|
void
|
|
|
|
terminate_request_handler(int param)
|
|
|
|
{
|
2010-02-28 22:29:21 +00:00
|
|
|
char msg[] = "\nReceived signal XX, intend to exit soon.\n"
|
2012-02-19 13:33:41 +00:00
|
|
|
"Repeat the signal to force immediate termination.\n";
|
2010-02-28 22:29:21 +00:00
|
|
|
msg[17] = '0' + param / 10;
|
|
|
|
msg[18] = '0' + param % 10;
|
2012-02-19 13:33:41 +00:00
|
|
|
if (write(1, msg, sizeof(msg) - 1) == -1)
|
|
|
|
{
|
|
|
|
// need to act write's return value to avoid GCC compiler warning
|
|
|
|
// "'write' declared with attribute warn_unused_result"
|
|
|
|
terminating = true; // dummy operation
|
|
|
|
}
|
2010-02-28 09:09:48 +00:00
|
|
|
terminating = true;
|
|
|
|
signal(param, prior_signal_handlers[param]);
|
2010-03-03 19:35:16 +00:00
|
|
|
theSocket.close();
|
2012-02-19 13:33:41 +00:00
|
|
|
if (pTerraSync)
|
|
|
|
pTerraSync->unbind();
|
2010-02-28 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
/** Parse command-line options. */
|
|
|
|
void
|
|
|
|
parseOptions(int argc, char** argv, SGPropertyNode_ptr config,
|
|
|
|
bool& testing, int& verbose, int& port, const char* &pidfn)
|
|
|
|
{
|
|
|
|
// parse arguments
|
|
|
|
for (int i=1; i < argc; ++i )
|
|
|
|
{
|
|
|
|
string arg = argv[i];
|
|
|
|
if ( arg == "-p" ) {
|
|
|
|
++i;
|
|
|
|
port = atoi( argv[i] );
|
|
|
|
} else if ( arg.find("-pid") == 0 ) {
|
|
|
|
++i;
|
|
|
|
pidfn = argv[i];
|
|
|
|
cout << "pidfn: " << pidfn << endl;
|
|
|
|
} else if ( arg == "-s" ) {
|
|
|
|
++i;
|
|
|
|
config->setStringValue("svn-server", argv[i]);
|
|
|
|
} else if ( arg == "-d" ) {
|
|
|
|
++i;
|
|
|
|
config->setStringValue("scenery-dir", argv[i]);
|
|
|
|
} else if ( arg == "-R" ) {
|
|
|
|
config->setBoolValue("use-svn", false);
|
|
|
|
} else if ( arg == "-S" ) {
|
|
|
|
config->setBoolValue("use-built-in-svn", true);
|
|
|
|
} else if ( arg == "-e" ) {
|
|
|
|
config->setBoolValue("use-built-in-svn", false);
|
|
|
|
} else if ( arg == "-v" ) {
|
|
|
|
verbose++;
|
|
|
|
} else if ( arg == "-T" ) {
|
|
|
|
testing = true;
|
|
|
|
} else if ( arg == "-h" ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
cerr << "Unrecognized command-line option '" << arg << "'" << endl;
|
|
|
|
usage( argv[0] );
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
/** parse a single NMEA-0183 position message */
|
|
|
|
static void
|
|
|
|
parse_message( int verbose, const string &msg, int *lat, int *lon )
|
|
|
|
{
|
2002-12-03 15:40:32 +00:00
|
|
|
double dlat, dlon;
|
|
|
|
string text = msg;
|
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
if (verbose>=3)
|
|
|
|
cout << "message: '" << text << "'" << endl;
|
|
|
|
|
|
|
|
// find GGA string and advance to start of lat (see NMEA-0183 for protocol specs)
|
2002-12-03 15:40:32 +00:00
|
|
|
string::size_type pos = text.find( "$GPGGA" );
|
2009-02-01 18:05:24 +00:00
|
|
|
if ( pos == string::npos )
|
|
|
|
{
|
2012-02-19 13:33:41 +00:00
|
|
|
*lat = simgear::NOWHERE;
|
|
|
|
*lon = simgear::NOWHERE;
|
|
|
|
return;
|
2009-02-01 18:05:24 +00:00
|
|
|
}
|
2002-12-03 15:40:32 +00:00
|
|
|
string tmp = text.substr( pos + 7 );
|
2009-02-01 18:05:24 +00:00
|
|
|
pos = tmp.find( "," );
|
2002-12-03 15:40:32 +00:00
|
|
|
tmp = tmp.substr( pos + 1 );
|
|
|
|
// cout << "-> " << tmp << endl;
|
|
|
|
|
|
|
|
// find lat then advance to start of hemisphere
|
|
|
|
pos = tmp.find( "," );
|
|
|
|
string lats = tmp.substr( 0, pos );
|
|
|
|
dlat = atof( lats.c_str() ) / 100.0;
|
|
|
|
tmp = tmp.substr( pos + 1 );
|
|
|
|
|
|
|
|
// find N/S hemisphere and advance to start of lon
|
|
|
|
if ( tmp.substr( 0, 1 ) == "S" ) {
|
|
|
|
dlat = -dlat;
|
|
|
|
}
|
|
|
|
pos = tmp.find( "," );
|
|
|
|
tmp = tmp.substr( pos + 1 );
|
|
|
|
|
|
|
|
// find lon
|
|
|
|
pos = tmp.find( "," );
|
|
|
|
string lons = tmp.substr( 0, pos );
|
|
|
|
dlon = atof( lons.c_str() ) / 100.0;
|
|
|
|
tmp = tmp.substr( pos + 1 );
|
|
|
|
|
|
|
|
// find E/W hemisphere and advance to start of lon
|
|
|
|
if ( tmp.substr( 0, 1 ) == "W" ) {
|
|
|
|
dlon = -dlon;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( dlat < 0 ) {
|
|
|
|
*lat = (int)dlat - 1;
|
|
|
|
} else {
|
|
|
|
*lat = (int)dlat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( dlon < 0 ) {
|
|
|
|
*lon = (int)dlon - 1;
|
|
|
|
} else {
|
|
|
|
*lon = (int)dlon;
|
|
|
|
}
|
2008-10-12 07:55:09 +00:00
|
|
|
|
|
|
|
if ((dlon == 0) && (dlat == 0)) {
|
2012-02-19 13:33:41 +00:00
|
|
|
*lon = simgear::NOWHERE;
|
|
|
|
*lat = simgear::NOWHERE;
|
2008-10-12 07:55:09 +00:00
|
|
|
}
|
2002-12-03 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
/** Monitor UDP socket and process NMEA position updates. */
|
|
|
|
int
|
|
|
|
processRequests(SGPropertyNode_ptr config, bool testing, int verbose, int port)
|
|
|
|
{
|
|
|
|
const char* host = "localhost";
|
|
|
|
char msg[256];
|
|
|
|
int len;
|
|
|
|
int lat, lon;
|
|
|
|
bool connected = false;
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
// Must call this before any other net stuff
|
|
|
|
simgear::Socket::initSockets();
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
// open UDP socket
|
|
|
|
if ( !theSocket.open( false ) )
|
|
|
|
{
|
|
|
|
cerr << "error opening socket" << endl;
|
|
|
|
return -1;
|
2008-12-21 09:29:33 +00:00
|
|
|
}
|
2009-02-01 19:57:56 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
if ( theSocket.bind( host, port ) == -1 )
|
|
|
|
{
|
|
|
|
cerr << "error binding to port " << port << endl;
|
|
|
|
return -1;
|
2009-02-07 16:10:48 +00:00
|
|
|
}
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
theSocket.setBlocking(true);
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
while ( (!terminating)&&
|
|
|
|
(!config->getBoolValue("stalled", false)) )
|
|
|
|
{
|
|
|
|
if (verbose >= 4)
|
|
|
|
cout << "main loop" << endl;
|
|
|
|
// main loop
|
|
|
|
bool recv_msg = false;
|
|
|
|
if ( testing )
|
|
|
|
{
|
|
|
|
// Testing without FGFS communication
|
|
|
|
lat = 37;
|
|
|
|
lon = -123;
|
|
|
|
recv_msg = true;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (verbose && pTerraSync->isIdle())
|
|
|
|
{
|
|
|
|
cout << "Idle; waiting for FlightGear position data" << endl;
|
|
|
|
}
|
|
|
|
len = theSocket.recv(msg, sizeof(msg)-1, 0);
|
|
|
|
if (len >= 0)
|
|
|
|
{
|
|
|
|
msg[len] = '\0';
|
|
|
|
recv_msg = true;
|
|
|
|
if (verbose>=2)
|
|
|
|
cout << "recv length: " << len << endl;
|
|
|
|
parse_message( verbose, msg, &lat, &lon );
|
|
|
|
if ((!connected)&&
|
|
|
|
(lat != simgear::NOWHERE)&&
|
|
|
|
(lon != simgear::NOWHERE))
|
|
|
|
{
|
|
|
|
cout << "Valid position data received. Connected successfully." << endl;
|
|
|
|
connected = true;
|
2002-12-03 15:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 13:33:41 +00:00
|
|
|
|
|
|
|
if ( recv_msg )
|
|
|
|
{
|
|
|
|
pTerraSync->schedulePosition(lat, lon);
|
2002-12-03 15:40:32 +00:00
|
|
|
}
|
2012-02-19 13:33:41 +00:00
|
|
|
|
|
|
|
if ( testing )
|
|
|
|
{
|
|
|
|
if (pTerraSync->isIdle())
|
|
|
|
terminating = true;
|
|
|
|
else
|
|
|
|
SGTimeStamp::sleepForMSec(1000);
|
2002-12-03 15:40:32 +00:00
|
|
|
}
|
2012-02-19 13:33:41 +00:00
|
|
|
} // while !terminating
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
return 0;
|
2009-02-07 16:10:48 +00:00
|
|
|
}
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
2002-12-03 15:40:32 +00:00
|
|
|
int port = 5501;
|
2012-02-19 13:33:41 +00:00
|
|
|
int verbose = 0;
|
|
|
|
int exit_code = 0;
|
2008-10-19 16:08:29 +00:00
|
|
|
bool testing = false;
|
2010-01-17 19:41:47 +00:00
|
|
|
const char* pidfn = "";
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
// default configuration
|
|
|
|
sglog().setLogLevels( SG_ALL, SG_ALERT);
|
|
|
|
SGPropertyNode_ptr root = new SGPropertyNode();
|
|
|
|
SGPropertyNode_ptr config = root->getNode("/sim/terrasync", true);
|
|
|
|
config->setStringValue("scenery-dir", "terrasyncdir");
|
|
|
|
config->setStringValue("svn-server", svn_base);
|
|
|
|
config->setStringValue("rsync-server", rsync_base);
|
|
|
|
config->setBoolValue("use-built-in-svn", true);
|
|
|
|
config->setBoolValue("use-svn", true);
|
|
|
|
config->setBoolValue("enabled", true);
|
|
|
|
config->setIntValue("max-errors", -1); // -1 = infinite
|
|
|
|
|
|
|
|
// parse command-line arguments
|
|
|
|
parseOptions(argc, argv, config, testing, verbose, port, pidfn);
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
sglog().setLogLevels( SG_ALL, SG_INFO);
|
|
|
|
|
2012-02-19 21:13:41 +00:00
|
|
|
#ifndef _MSC_VER
|
2012-02-19 13:33:41 +00:00
|
|
|
// create PID file
|
|
|
|
if (*pidfn)
|
|
|
|
{
|
|
|
|
ofstream pidstream;
|
|
|
|
pidstream.open(pidfn);
|
|
|
|
if (!pidstream.good())
|
|
|
|
{
|
|
|
|
cerr << "Cannot open pid file '" << pidfn << "': ";
|
|
|
|
perror(0);
|
|
|
|
exit(2);
|
2010-01-17 19:41:47 +00:00
|
|
|
}
|
2012-02-19 13:33:41 +00:00
|
|
|
pidstream << getpid() << endl;
|
|
|
|
pidstream.close();
|
2009-10-10 15:17:21 +00:00
|
|
|
}
|
2012-02-19 21:13:41 +00:00
|
|
|
#endif
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
// install signal handlers
|
|
|
|
for (int* sigp=termination_triggering_signals; *sigp; sigp++)
|
|
|
|
{
|
2010-02-28 09:09:48 +00:00
|
|
|
prior_signal_handlers[*sigp] =
|
|
|
|
signal(*sigp, *terminate_request_handler);
|
2012-02-19 13:33:41 +00:00
|
|
|
if (verbose>=2)
|
|
|
|
cout << "Intercepting signal " << *sigp << endl;
|
2010-02-28 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
{
|
|
|
|
pTerraSync = new simgear::SGTerraSync(root);
|
|
|
|
pTerraSync->bind();
|
|
|
|
pTerraSync->init();
|
2002-12-03 15:40:32 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
// now monitor and process position updates
|
|
|
|
exit_code = processRequests(config, testing, verbose, port);
|
2009-10-10 15:17:21 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
pTerraSync->unbind();
|
|
|
|
delete pTerraSync;
|
|
|
|
pTerraSync = NULL;
|
|
|
|
}
|
2009-10-10 15:17:21 +00:00
|
|
|
|
2012-02-19 13:33:41 +00:00
|
|
|
return exit_code;
|
2002-12-03 15:40:32 +00:00
|
|
|
}
|