#endif
#include <stdlib.h> // atoi() atof() abs() system()
+#include <signal.h> // signal()
#include <simgear/compiler.h>
}
}
+#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;
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 );
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());
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);
// Must call this before any other net stuff
netInit( &argc,argv );
-
netSocket s;
if ( ! s.open( false ) ) { // open a UDP socket
}
- 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
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) {
}
else if ( testing ) {
- exit( 0 );
+ terminating = true;
} else
ulSleep( 1 );
- } // while true
+ } // while !terminating
return 0;
}