]> git.mxchange.org Git - flightgear.git/blobdiff - utils/TerraSync/terrasync.cxx
Merge branch 'next' into comm-subsystem
[flightgear.git] / utils / TerraSync / terrasync.cxx
index e586632dda8cd371d52a30e4c95d5ac3f8581bbe..d15078d9a8dccdbcd1e79a2497ae23d7a01dd3d6 100644 (file)
 #ifdef __MINGW32__
 #include <time.h>
 #include <unistd.h>
+#elif defined(_MSC_VER)
+#   include <io.h>
+#   ifndef HAVE_SVN_CLIENT_H
+#       include <time.h>
+#       include <process.h>
+#   endif
 #endif
 
 #include <stdlib.h>             // atoi() atof() abs() system()
+#include <signal.h>             // signal()
 
 #include <simgear/compiler.h>
 
@@ -44,9 +51,7 @@
 #include <deque>
 #include <map>
 
-#include <plib/netSocket.h>
-#include <plib/ul.h>
-
+#include <simgear/io/raw_socket.hxx>
 #include <simgear/bucket/newbucket.hxx>
 #include <simgear/misc/sg_path.hxx>
 
@@ -109,9 +114,10 @@ static void usage( const string& prog ) {
 
 }
 
-std::deque<std::string> waitingTiles;
-typedef std::map<std::string,time_t> CompletedTiles;
+deque<string> waitingTiles;
+typedef map<string,time_t> CompletedTiles;
 CompletedTiles completedTiles;
+simgear::Socket theSocket;
 
 #ifdef HAVE_SVN_CLIENT_H
 
@@ -124,10 +130,6 @@ svn_opt_revision_t *mysvn_rev_peg = NULL;
 static const svn_version_checklist_t mysvn_checklist[] = {
     { "svn_subr",   svn_subr_version },
     { "svn_client", svn_client_version },
-    { "svn_wc",     svn_wc_version },
-    { "svn_ra",     svn_ra_version },
-    { "svn_delta",  svn_delta_version },
-    { "svn_diff",   svn_diff_version },
     { NULL, NULL }
 };
 
@@ -181,8 +183,10 @@ int mysvn_setup(void) {
     if (err)
         return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
     mysvn_ctx->auth_baton = ab;
+#if (SVN_VER_MINOR >= 5)
     mysvn_ctx->conflict_func = NULL;
     mysvn_ctx->conflict_baton = NULL;
+#endif
     // Now our magic revisions
     mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool, 
         sizeof(svn_opt_revision_t));
@@ -227,16 +231,31 @@ void sync_tree(const char* dir) {
        if (mysvn_setup() != EXIT_SUCCESS)
            exit(1);
        apr_pool_t *subpool = svn_pool_create(mysvn_pool);
+       
+#if (SVN_VER_MINOR >= 5)
        err = svn_client_checkout3(NULL,
            command,
            dest_base_dir,
            mysvn_rev_peg,
            mysvn_rev,
            svn_depth_infinity,
-           0,
-           0,
+           0, // ignore-externals = false
+           0, // allow unver obstructions = false
+           mysvn_ctx,
+           subpool);
+#else
+    // version 1.4 API
+    err = svn_client_checkout2(NULL,
+           command,
+           dest_base_dir,
+           mysvn_rev_peg,
+           mysvn_rev,
+           1, // recurse=true - same as svn_depth_infinity for checkout3 above
+           0, // ignore externals = false
            mysvn_ctx,
            subpool);
+#endif
+           
        if (err) {
            // Report errors from the checkout attempt
            cout << "failed: " << endl
@@ -269,9 +288,37 @@ void sync_tree(const char* dir) {
     }
 }
 
+#if defined(_MSC_VER) || defined(__MINGW32__)
+typedef void (__cdecl * sighandler_t)(int);
+#elif defined( __APPLE__ ) || defined (__FreeBSD__)
+typedef sig_t sighandler_t;
+#endif
+
+bool terminating = false;
+sighandler_t prior_signal_handlers[32];
+int termination_triggering_signals[] = {
+#if defined(_MSC_VER) || defined(__MINGW32__)
+    SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
+#else
+    SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM,
+#endif
+    0};  // zero terminated
+
+void terminate_request_handler(int param) {
+    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]);
+    theSocket.close();
+}
+
 
 const int nowhere = -9999;
 
+
 // parse message
 static void parse_message( const string &msg, int *lat, int *lon ) {
     double dlat, dlon;
@@ -281,8 +328,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 +455,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 +470,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);
@@ -484,16 +532,14 @@ int main( int argc, char **argv ) {
     }
     
     // Must call this before any other net stuff
-    netInit( &argc,argv );
-
-    netSocket s;
+    simgear::Socket::initSockets();
 
-    if ( ! s.open( false ) ) {  // open a UDP socket
+    if ( ! theSocket.open( false ) ) {  // open a UDP socket
         printf("error opening socket\n");
         return -1;
     }
 
-    if ( s.bind( host, port ) == -1 ) {
+    if ( theSocket.bind( host, port ) == -1 ) {
         printf("error binding to port %d\n", port);
         return -1;
     }
@@ -524,7 +570,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,8 +585,11 @@ int main( int argc, char **argv ) {
             lon = -123;
             recv_msg = (lat != last_lat) || (lon != last_lon);
         } else {
-            s.setBlocking(waitingTiles.empty());
-            len = s.recv(msg, maxlen, 0);
+            if (verbose && waitingTiles.empty()) {
+                cout << "Idle; waiting for FlightGear position\n";
+            }
+            theSocket.setBlocking(waitingTiles.empty());
+            len = theSocket.recv(msg, maxlen, 0);
             if (len >= 0) {
                 msg[len] = '\0';
                 recv_msg = true;
@@ -546,7 +602,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<std::string> oldRequests;
+               deque<string> oldRequests;
                oldRequests.swap( waitingTiles );
                 int lat_dir, lon_dir, dist;
                 if ( last_lat == nowhere || last_lon == nowhere ) {
@@ -584,11 +640,15 @@ int main( int argc, char **argv ) {
         }
 
        else if ( testing ) {
-           exit( 0 );
+           terminating = true;
        } else
 
-        ulSleep( 1 );
-    } // while true
+        #ifdef _WIN32
+                Sleep(1000);
+#else
+               sleep(1);
+#endif
+    } // while !terminating
         
     return 0;
 }