1 // terrasync.cxx -- "JIT" scenery fetcher
3 // Written by Curtis Olson, started November 2002.
5 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <stdlib.h> // atoi() atof() abs() system()
26 #include <simgear/compiler.h>
31 #include <plib/netSocket.h>
34 #include <simgear/bucket/newbucket.hxx>
37 #if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
42 static string server = "baron.flightgear.org";
43 static string source_module = "Scenery-0.7.9";
44 static string source_base = server + (string)"::" + source_module;
45 static string dest_base = "/dest/scenery/dir";
49 static void usage( const string& prog ) {
50 cout << "Usage: " << prog
51 << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
56 static void parse_message( const string &msg, int *lat, int *lon ) {
60 // find GGA string and advance to start of lat
61 string::size_type pos = text.find( "$GPGGA" );
62 string tmp = text.substr( pos + 7 );
63 pos = text.find( "," );
64 tmp = tmp.substr( pos + 1 );
65 // cout << "-> " << tmp << endl;
67 // find lat then advance to start of hemisphere
68 pos = tmp.find( "," );
69 string lats = tmp.substr( 0, pos );
70 dlat = atof( lats.c_str() ) / 100.0;
71 tmp = tmp.substr( pos + 1 );
73 // find N/S hemisphere and advance to start of lon
74 if ( tmp.substr( 0, 1 ) == "S" ) {
77 pos = tmp.find( "," );
78 tmp = tmp.substr( pos + 1 );
81 pos = tmp.find( "," );
82 string lons = tmp.substr( 0, pos );
83 dlon = atof( lons.c_str() ) / 100.0;
84 tmp = tmp.substr( pos + 1 );
86 // find E/W hemisphere and advance to start of lon
87 if ( tmp.substr( 0, 1 ) == "W" ) {
106 static void sync_area( int lat, int lon ) {
108 int baselat, baselon;
111 int base = (int)(lat / 10);
112 if ( lat == base * 10 ) {
115 baselat = (base - 1) * 10;
119 baselat = (int)(lat / 10) * 10;
123 int base = (int)(lon / 10);
124 if ( lon == base * 10 ) {
127 baselon = (base - 1) * 10;
131 baselon = (int)(lon / 10) * 10;
137 // make container directory
138 char container_dir[512];
139 snprintf( container_dir, 512, "%s/%c%03d%c%02d",
140 dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
141 snprintf( command, 512, "mkdir -p %s", container_dir );
142 cout << command << endl;
146 snprintf( dir, 512, "%c%03d%c%02d/%c%03d%c%02d",
147 EW, abs(baselon), NS, abs(baselat),
148 EW, abs(lon), NS, abs(lat) );
150 snprintf( command, 512,
151 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
152 source_base.c_str(), dir, dest_base.c_str(), dir );
153 cout << command << endl;
159 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
160 // do current 1x1 degree area first
161 sync_area( lat, lon );
163 if ( lat_dir == 0 && lon_dir == 0 ) {
164 // now do surrounding 8 1x1 degree areas.
165 for ( int i = lat - 1; i <= lat + 1; ++i ) {
166 for ( int j = lon - 1; j <= lon + 1; ++j ) {
167 if ( i != lat || j != lon ) {
173 if ( lat_dir != 0 ) {
174 sync_area( lat + lat_dir, lon );
175 sync_area( lat + lat_dir, lon - 1 );
176 sync_area( lat + lat_dir, lon + 1 );
178 if ( lon_dir != 0 ) {
179 sync_area( lat, lon + lon_dir );
180 sync_area( lat - 1, lon + lon_dir );
181 sync_area( lat + 1, lon + lon_dir );
187 int main( int argc, char **argv ) {
189 char host[256] = ""; // accept messages from anyone
194 if ( (string)argv[i] == "-p" ) {
196 port = atoi( argv[i] );
197 } else if ( (string)argv[i] == "-s" ) {
199 source_base = argv[i];
200 } else if ( (string)argv[i] == "-d" ) {
210 // Must call this before any other net stuff
211 netInit( &argc,argv );
215 if ( ! s.open( false ) ) { // open a UDP socket
216 printf("error opening socket\n");
220 s.setBlocking( false );
222 if ( s.bind( host, port ) == -1 ) {
223 printf("error binding to port %d\n", port);
231 int last_lat = -9999;
232 int last_lon = -9999;
233 bool recv_msg = false;
237 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
241 parse_message( msg, &lat, &lon );
242 cout << "pos = " << lat << "," << lon << endl;
246 if ( lat != last_lat || lon != last_lon ) {
247 int lat_dir, lon_dir, dist;
248 if ( last_lat == -9999 || last_lon == -9999 ) {
249 lat_dir = lon_dir = 0;
251 dist = lat - last_lat;
253 lat_dir = dist / abs(dist);
257 dist = lon - last_lon;
259 lon_dir = dist / abs(dist);
264 cout << "lat = " << lat << " lon = " << lon << endl;
265 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
266 sync_areas( lat, lon, lat_dir, lon_dir );