]> git.mxchange.org Git - flightgear.git/blobdiff - utils/TerraSync/terrasync.cxx
Alex Perry :
[flightgear.git] / utils / TerraSync / terrasync.cxx
index c25aa13d85b7d3ae084b7c7d06322c4c7eb8bb21..70ebf87aa6c5ce9862704a714630060821953b6a 100644 (file)
@@ -3,6 +3,7 @@
 // Written by Curtis Olson, started November 2002.
 //
 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
+// Copyright (C) 2008  Alexander R. Perry <alex.perry@ieee.org>
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+
 
 #include <stdlib.h>             // atoi() atof() abs() system()
 
 #include <simgear/compiler.h>
 
-#include STL_IOSTREAM
-#include STL_STRING
+#include <iostream>
+#include <string>
 
 #include <plib/netSocket.h>
 #include <plib/ul.h>
 
 #include <simgear/bucket/newbucket.hxx>
+#include <simgear/misc/sg_path.hxx>
+
+#ifdef HAVE_SVN_CLIENT_H
+#  ifdef HAVE_LIBSVN_CLIENT_1
+#    include <svn_auth.h>
+#    include <svn_client.h>
+#    include <svn_cmdline.h>
+#    include <svn_pools.h>
+#  else
+#    undef HAVE_SVN_CLIENT_H
+#  endif
+#endif
+
+using std::string;
+using std::cout;
+using std::endl;
+
+const char* source_base = NULL;
+const char* svn_base =
+  "http://terrascenery.googlecode.com/svn/trunk/data/Scenery";
+const char* rsync_base = "scenery.flightgear.org::Scenery";
+const char* dest_base = "terrasyncdir";
+const char* rsync_cmd = 
+    "rsync --verbose --archive --delete --perms --owner --group";
+
+#ifdef HAVE_SVN_CLIENT_H
+bool use_svn = true;
+#else
+bool use_svn = false;
+const char* svn_cmd = "svn checkout";
+#endif
 
-SG_USING_STD(string);
-SG_USING_STD(cout);
-SG_USING_STD(endl);
+// display usage
+static void usage( const string& prog ) {
+    cout << "Usage: " << endl
+         << prog << " -p <port> "
+        << "-R [ -s <rsync_source> ] -d <dest>" << endl
+         << prog << " -p <port> "
+         << "-S [ -s <svn_source> ] -d <dest>" << endl;
+#ifdef HAVE_SVN_CLIENT_H
+    cout << "    (defaults to the built in subversion)" << endl;
+#else
+    cout << "    (defaults to rsync, using external commands)" << endl;
+#endif
+}
 
-static string server = "scenery.flightgear.org";
-static string source_module = "Scenery-0.9.5";
-static string source_base = server + (string)"::" + source_module;
-static string dest_base = "/dest/scenery/dir";
+#ifdef HAVE_SVN_CLIENT_H
+
+// Things we need for doing subversion checkout - often
+apr_pool_t *mysvn_pool = NULL;
+svn_client_ctx_t *mysvn_ctx = NULL;
+svn_opt_revision_t *mysvn_rev = 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 }
+};
+
+// Configure our subversion session
+int mysvn_setup(void) {
+    // Are we already prepared?
+    if (mysvn_pool) return EXIT_SUCCESS;
+    // No, so initialize svn internals generally
+#ifdef _MSC_VER
+    if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
+        return EXIT_FAILURE;
+#else
+    if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
+        return EXIT_FAILURE;
+#endif
+    apr_pool_t *pool;
+    apr_pool_create(&pool, NULL);
+    svn_error_t *err = NULL;
+    SVN_VERSION_DEFINE(mysvn_version);
+    err = svn_ver_check_list(&mysvn_version, mysvn_checklist);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    err = svn_ra_initialize(pool);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    char *config_dir = NULL;
+    err = svn_config_ensure(config_dir, pool);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    err = svn_client_create_context(&mysvn_ctx, pool);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    err = svn_config_get_config(&(mysvn_ctx->config),
+        config_dir, pool);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    svn_config_t *cfg;
+    cfg = ( svn_config_t*) apr_hash_get(
+        mysvn_ctx->config,
+       SVN_CONFIG_CATEGORY_CONFIG,
+        APR_HASH_KEY_STRING);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    svn_auth_baton_t *ab;
+    err = svn_cmdline_setup_auth_baton(&ab,
+        TRUE, NULL, NULL, config_dir, TRUE, cfg,
+        mysvn_ctx->cancel_func, mysvn_ctx->cancel_baton, pool);
+    if (err)
+        return svn_cmdline_handle_exit_error(err, pool, "terrasync: ");
+    mysvn_ctx->auth_baton = ab;
+    mysvn_ctx->conflict_func = NULL;
+    mysvn_ctx->conflict_baton = NULL;
+    mysvn_rev = (svn_opt_revision_t*) apr_palloc(pool, 
+        sizeof(svn_opt_revision_t));
+    if (!mysvn_rev)
+        return EXIT_FAILURE;
+    mysvn_rev->kind = svn_opt_revision_head;
+    // Success if we got this far
+    mysvn_pool = pool;
+    return EXIT_SUCCESS;
+}
 
+#endif
 
-// display usage
-static void usage( const string& prog ) {
-    cout << "Usage: " << prog
-         << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
+// sync one directory tree
+void sync_tree(char* dir) {
+    int rc;
+    char command[512];
+    SGPath path( dest_base );
+
+    path.append( dir );
+    rc = path.create_dir( 0755 );
+    if (rc) {
+        cout << "Return code = " << rc << endl;
+        exit(1);
+    }
+
+    if (use_svn) {
+#ifdef HAVE_SVN_CLIENT_H
+        cout << dir << " ... ";
+       cout.flush();
+        char dest_base_dir[512];
+        snprintf( command, 512,
+            "%s/%s", source_base, dir);
+        snprintf( dest_base_dir, 512,
+            "%s/%s", dest_base, dir);
+       svn_error_t *err = NULL;
+       if (mysvn_setup() != EXIT_SUCCESS)
+           exit(1);
+       apr_pool_t *subpool = svn_pool_create(mysvn_pool);
+       err = svn_client_checkout(NULL,
+           command,
+           dest_base_dir,
+           mysvn_rev,
+           1,
+           mysvn_ctx,
+           subpool);
+       if (err) {
+           // Report errors from the checkout attempt
+           cout << "failed: " << endl
+                << err->message << endl;
+           svn_error_clear(err);
+           return;
+       } else {
+           cout << "done" << endl;
+       }
+       svn_pool_destroy(subpool);
+       return;
+#else
+
+        snprintf( command, 512,
+            "%s %s/%s %s/%s", svn_cmd,
+            source_base, dir,
+           dest_base, dir );
+#endif
+    } else {
+        snprintf( command, 512,
+            "%s %s/%s/ %s/%s/", rsync_cmd,
+            source_base, dir,
+           dest_base, dir );
+    }
+    cout << command << endl;
+    rc = system( command );
+    if (rc) {
+        cout << "Return code = " << rc << endl;
+        if (rc == 5120) exit(1);
+    }
 }
 
 
+const int nowhere = -9999;
+
 // parse message
 static void parse_message( const string &msg, int *lat, int *lon ) {
     double dlat, dlon;
@@ -97,6 +280,11 @@ static void parse_message( const string &msg, int *lat, int *lon ) {
     } else {
         *lon = (int)dlon;
     }
+
+    if ((dlon == 0) && (dlat == 0)) {
+      *lon = nowhere;
+      *lat = nowhere;
+    }
 }
 
 
@@ -126,30 +314,21 @@ static void sync_area( int lat, int lon ) {
         }
         EW = 'w';
     } else {
-        baselon = (int)(lon / 10) * 10;
-        EW = 'e';
-    }
-
-    char command[512];
-
-    // make container directory
-    char container_dir[512];
-    snprintf( container_dir, 512, "%s/%c%03d%c%02d",
-              dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
-    snprintf( command, 512, "mkdir -p %s", container_dir );
-    cout << command << endl;
-    system( command );
-
+            baselon = (int)(lon / 10) * 10;
+            EW = 'e';
+        }
+    
+    const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
+    const char** tree;
     char dir[512];
-    snprintf( dir, 512, "%c%03d%c%02d/%c%03d%c%02d",
-              EW, abs(baselon), NS, abs(baselat),
-              EW, abs(lon), NS, abs(lat) );
-
-    snprintf( command, 512,
-              "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
-              source_base.c_str(), dir, dest_base.c_str(), dir );
-    cout << command << endl;
-    system( command );
+  
+    for (tree = &terrainobjects[0]; *tree; tree++) {
+        snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
+             *tree,
+             EW, abs(baselon), NS, abs(baselat),
+             EW, abs(lon), NS, abs(lat) );
+       sync_tree(dir);
+    }
 }
 
 
@@ -185,6 +364,7 @@ static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
 int main( int argc, char **argv ) {
     int port = 5501;
     char host[256] = "";        // accept messages from anyone
+    bool testing = false;
 
     // parse arguments
     int i = 1;
@@ -198,6 +378,12 @@ int main( int argc, char **argv ) {
         } else if ( (string)argv[i] == "-d" ) {
             ++i;
             dest_base = argv[i];
+        } else if ( (string)argv[i] == "-R" ) {
+           use_svn = false;
+        } else if ( (string)argv[i] == "-S" ) {
+           use_svn = true;
+        } else if ( (string)argv[i] == "-T" ) {
+           testing = true;
         } else {
             usage( argv[0] );
             exit(-1);        
@@ -205,6 +391,20 @@ int main( int argc, char **argv ) {
         ++i;
     }
 
+    // Use the appropriate default for the "-s" flag
+    if (source_base == NULL) {
+        if (use_svn)
+           source_base = svn_base;
+       else
+           source_base = rsync_base;
+    }
+    
+    // We just want one grid square, no FGFS communications
+    if (testing) {
+          sync_areas( 37, -123, 0, 0 );
+         exit(0);
+    }
+
     // Must call this before any other net stuff
     netInit( &argc,argv );
 
@@ -226,9 +426,10 @@ int main( int argc, char **argv ) {
     int maxlen = 256;
     int len;
     int lat, lon;
-    int last_lat = -9999;
-    int last_lon = -9999;
+    int last_lat = nowhere;
+    int last_lon = nowhere;
     bool recv_msg = false;
+    int synced_other = 0;
 
     while ( true ) {
         recv_msg = false;
@@ -237,13 +438,13 @@ int main( int argc, char **argv ) {
             recv_msg = true;
 
             parse_message( msg, &lat, &lon );
-            cout << "pos = " << lat << "," << lon << endl;
+            cout << "pos in msg = " << lat << "," << lon << endl;
         }
 
         if ( recv_msg ) {
             if ( lat != last_lat || lon != last_lon ) {
                 int lat_dir, lon_dir, dist;
-                if ( last_lat == -9999 || last_lon == -9999 ) {
+                if ( last_lat == nowhere || last_lon == nowhere ) {
                     lat_dir = lon_dir = 0;
                 } else {
                     dist = lat - last_lat;
@@ -260,16 +461,32 @@ int main( int argc, char **argv ) {
                     }
                 }
                 cout << "lat = " << lat << " lon = " << lon << endl;
-                cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
+                cout << "lat_dir = " << lat_dir << "  "
+                    << "lon_dir = " << lon_dir << endl;
                 sync_areas( lat, lon, lat_dir, lon_dir );
-            }
+            } else if ( last_lat == nowhere || last_lon == nowhere ) {
+               cout << "Waiting for FGFS to finish startup" << endl;
+           } else {
+               switch (synced_other++) {
+               case 0:
+                   sync_tree((char*) "Airports/K");
+                   break;
+               case 1:
+                   sync_tree((char*) "Airports");
+                   break;
+               default:
+                   cout << "Done non-tile syncs" << endl;
+                   break;
+               }
+           }
 
             last_lat = lat;
             last_lon = lon;
-        }
+        } 
 
         ulSleep( 1 );
-    }
+    } // while true
         
     return 0;
 }
+