From d1d7e6ad387184476ded9423d41adba24f97e3a2 Mon Sep 17 00:00:00 2001 From: fredb Date: Sun, 28 Feb 2010 09:09:48 +0000 Subject: [PATCH 1/4] Alex Perry : Patch to protect terrasync SVN from ^C Me : make it compile under MSVC. Works with fgrun. --- utils/TerraSync/terrasync.cxx | 49 +++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/utils/TerraSync/terrasync.cxx b/utils/TerraSync/terrasync.cxx index e586632dd..f26cb96b6 100644 --- a/utils/TerraSync/terrasync.cxx +++ b/utils/TerraSync/terrasync.cxx @@ -35,6 +35,7 @@ #endif #include // atoi() atof() abs() system() +#include // signal() #include @@ -269,9 +270,31 @@ void sync_tree(const char* dir) { } } +#ifdef _MSC_VER +typedef void (__cdecl * sighandler_t)(int); +#endif + +bool terminating = false; +sighandler_t prior_signal_handlers[32]; +int termination_triggering_signals[] = +#ifndef _MSC_VER + {SIGHUP, SIGINT, SIGQUIT, SIGKILL, 0}; // zero terminated +#else + {SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT, 0}; // zero terminated +#endif + +void terminate_request_handler(int param) { + cout << "\nReceived signal " << param << ", " + << "intend to terminate soon, " + << "repeat to force an immediate effect.\n"; + terminating = true; + signal(param, prior_signal_handlers[param]); +} + const int nowhere = -9999; + // parse message static void parse_message( const string &msg, int *lat, int *lon ) { double dlat, dlon; @@ -281,8 +304,8 @@ static void parse_message( const string &msg, int *lat, int *lon ) { string::size_type pos = text.find( "$GPGGA" ); if ( pos == string::npos ) { - *lat = -9999.0; - *lon = -9999.0; + *lat = nowhere; + *lon = nowhere; return; } string tmp = text.substr( pos + 7 ); @@ -408,7 +431,8 @@ static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) { void getWaitingTile() { while ( !waitingTiles.empty() ) { - CompletedTiles::iterator ii = completedTiles.find( waitingTiles.front() ); + CompletedTiles::iterator ii = + completedTiles.find( waitingTiles.front() ); time_t now = time(0); if ( ii == completedTiles.end() || ii->second + 600 < now ) { sync_tree(waitingTiles.front().c_str()); @@ -422,7 +446,7 @@ void getWaitingTile() { int main( int argc, char **argv ) { int port = 5501; - char host[256] = ""; // accept messages from anyone + char host[256] = "localhost"; bool testing = false; bool do_checkout(true); int verbose(0); @@ -485,7 +509,6 @@ int main( int argc, char **argv ) { // Must call this before any other net stuff netInit( &argc,argv ); - netSocket s; if ( ! s.open( false ) ) { // open a UDP socket @@ -524,7 +547,14 @@ int main( int argc, char **argv ) { } - while ( true ) { // main loop + for (int* sigp=termination_triggering_signals; *sigp; sigp++) { + prior_signal_handlers[*sigp] = + signal(*sigp, *terminate_request_handler); + if (verbose) cout << "Intercepted signal " << *sigp << endl; + } + + while ( !terminating ) { + // main loop recv_msg = false; if ( testing ) { // No FGFS communications @@ -532,6 +562,9 @@ int main( int argc, char **argv ) { lon = -123; recv_msg = (lat != last_lat) || (lon != last_lon); } else { + if (verbose && waitingTiles.empty()) { + cout << "Idle; waiting for FlightGear position\n"; + } s.setBlocking(waitingTiles.empty()); len = s.recv(msg, maxlen, 0); if (len >= 0) { @@ -584,11 +617,11 @@ int main( int argc, char **argv ) { } else if ( testing ) { - exit( 0 ); + terminating = true; } else ulSleep( 1 ); - } // while true + } // while !terminating return 0; } From 3a6587ae1659cb84990843933bfc4101b86c1e7b Mon Sep 17 00:00:00 2001 From: fredb Date: Sun, 28 Feb 2010 22:29:21 +0000 Subject: [PATCH 2/4] Alex Perry : Don't call stream functions in signal handlers Me : close socket to force exit when termination signal is received while idle. --- utils/TerraSync/terrasync.cxx | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/utils/TerraSync/terrasync.cxx b/utils/TerraSync/terrasync.cxx index f26cb96b6..d79c59fa6 100644 --- a/utils/TerraSync/terrasync.cxx +++ b/utils/TerraSync/terrasync.cxx @@ -32,6 +32,8 @@ #ifdef __MINGW32__ #include #include +#elif defined(_MSC_VER) +#include #endif #include // atoi() atof() abs() system() @@ -110,9 +112,10 @@ static void usage( const string& prog ) { } -std::deque waitingTiles; -typedef std::map CompletedTiles; +deque waitingTiles; +typedef map CompletedTiles; CompletedTiles completedTiles; +netSocket socket; #ifdef HAVE_SVN_CLIENT_H @@ -276,19 +279,23 @@ typedef void (__cdecl * sighandler_t)(int); bool terminating = false; sighandler_t prior_signal_handlers[32]; -int termination_triggering_signals[] = +int termination_triggering_signals[] = { #ifndef _MSC_VER - {SIGHUP, SIGINT, SIGQUIT, SIGKILL, 0}; // zero terminated + SIGHUP, SIGINT, SIGQUIT, SIGKILL, #else - {SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT, 0}; // zero terminated + SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT, #endif + 0}; // zero terminated void terminate_request_handler(int param) { - cout << "\nReceived signal " << param << ", " - << "intend to terminate soon, " - << "repeat to force an immediate effect.\n"; + char msg[] = "\nReceived signal XX, intend to exit soon.\n" + "repeat the signal to force immediate termination.\n"; + msg[17] = '0' + param / 10; + msg[18] = '0' + param % 10; + write(1, msg, sizeof(msg) - 1); terminating = true; signal(param, prior_signal_handlers[param]); + socket.close(); } @@ -509,14 +516,13 @@ int main( int argc, char **argv ) { // Must call this before any other net stuff netInit( &argc,argv ); - netSocket s; - if ( ! s.open( false ) ) { // open a UDP socket + if ( ! socket.open( false ) ) { // open a UDP socket printf("error opening socket\n"); return -1; } - if ( s.bind( host, port ) == -1 ) { + if ( socket.bind( host, port ) == -1 ) { printf("error binding to port %d\n", port); return -1; } @@ -565,8 +571,8 @@ int main( int argc, char **argv ) { if (verbose && waitingTiles.empty()) { cout << "Idle; waiting for FlightGear position\n"; } - s.setBlocking(waitingTiles.empty()); - len = s.recv(msg, maxlen, 0); + socket.setBlocking(waitingTiles.empty()); + len = socket.recv(msg, maxlen, 0); if (len >= 0) { msg[len] = '\0'; recv_msg = true; @@ -579,7 +585,7 @@ int main( int argc, char **argv ) { // Ignore messages where the location does not change if ( lat != last_lat || lon != last_lon ) { cout << "pos in msg = " << lat << "," << lon << endl; - std::deque oldRequests; + deque oldRequests; oldRequests.swap( waitingTiles ); int lat_dir, lon_dir, dist; if ( last_lat == nowhere || last_lon == nowhere ) { From 51e0e490f03495aa2d4cd6b165cac20167396884 Mon Sep 17 00:00:00 2001 From: fredb Date: Wed, 3 Mar 2010 19:35:16 +0000 Subject: [PATCH 3/4] Remove a name conflict under Unix --- utils/TerraSync/terrasync.cxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/TerraSync/terrasync.cxx b/utils/TerraSync/terrasync.cxx index d79c59fa6..91017fb34 100644 --- a/utils/TerraSync/terrasync.cxx +++ b/utils/TerraSync/terrasync.cxx @@ -115,7 +115,7 @@ static void usage( const string& prog ) { deque waitingTiles; typedef map CompletedTiles; CompletedTiles completedTiles; -netSocket socket; +netSocket theSocket; #ifdef HAVE_SVN_CLIENT_H @@ -295,7 +295,7 @@ void terminate_request_handler(int param) { write(1, msg, sizeof(msg) - 1); terminating = true; signal(param, prior_signal_handlers[param]); - socket.close(); + theSocket.close(); } @@ -517,12 +517,12 @@ int main( int argc, char **argv ) { // Must call this before any other net stuff netInit( &argc,argv ); - if ( ! socket.open( false ) ) { // open a UDP socket + if ( ! theSocket.open( false ) ) { // open a UDP socket printf("error opening socket\n"); return -1; } - if ( socket.bind( host, port ) == -1 ) { + if ( theSocket.bind( host, port ) == -1 ) { printf("error binding to port %d\n", port); return -1; } @@ -571,8 +571,8 @@ int main( int argc, char **argv ) { if (verbose && waitingTiles.empty()) { cout << "Idle; waiting for FlightGear position\n"; } - socket.setBlocking(waitingTiles.empty()); - len = socket.recv(msg, maxlen, 0); + theSocket.setBlocking(waitingTiles.empty()); + len = theSocket.recv(msg, maxlen, 0); if (len >= 0) { msg[len] = '\0'; recv_msg = true; From 0429b711585a60c2cbca9fd52efd98f2d3ef81eb Mon Sep 17 00:00:00 2001 From: fredb Date: Wed, 3 Mar 2010 19:38:17 +0000 Subject: [PATCH 4/4] =?UTF-8?q?Jari=20H=E4kkinen:=20compile=20on=20Mac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/TerraSync/terrasync.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/TerraSync/terrasync.cxx b/utils/TerraSync/terrasync.cxx index 91017fb34..f6c33c9b6 100644 --- a/utils/TerraSync/terrasync.cxx +++ b/utils/TerraSync/terrasync.cxx @@ -275,6 +275,8 @@ void sync_tree(const char* dir) { #ifdef _MSC_VER typedef void (__cdecl * sighandler_t)(int); +#elif defined( __APPLE__ ) +typedef sig_t sighandler_t; #endif bool terminating = false;