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>
41 static string server = "baron.flightgear.org";
42 static string source_module = "Scenery-0.7.9";
43 static string source_base = server + (string)"::" + source_module;
44 static string dest_base = "/dest/scenery/dir";
48 static void usage( const string& prog ) {
49 cout << "Usage: " << prog
50 << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
55 static void parse_message( const string &msg, int *lat, int *lon ) {
59 // find GGA string and advance to start of lat
60 string::size_type pos = text.find( "$GPGGA" );
61 string tmp = text.substr( pos + 7 );
62 pos = text.find( "," );
63 tmp = tmp.substr( pos + 1 );
64 // cout << "-> " << tmp << endl;
66 // find lat then advance to start of hemisphere
67 pos = tmp.find( "," );
68 string lats = tmp.substr( 0, pos );
69 dlat = atof( lats.c_str() ) / 100.0;
70 tmp = tmp.substr( pos + 1 );
72 // find N/S hemisphere and advance to start of lon
73 if ( tmp.substr( 0, 1 ) == "S" ) {
76 pos = tmp.find( "," );
77 tmp = tmp.substr( pos + 1 );
80 pos = tmp.find( "," );
81 string lons = tmp.substr( 0, pos );
82 dlon = atof( lons.c_str() ) / 100.0;
83 tmp = tmp.substr( pos + 1 );
85 // find E/W hemisphere and advance to start of lon
86 if ( tmp.substr( 0, 1 ) == "W" ) {
105 static void sync_area( int lat, int lon ) {
107 int baselat, baselon;
110 int base = (int)(lat / 10);
111 if ( lat == base * 10 ) {
114 baselat = (base - 1) * 10;
118 baselat = (int)(lat / 10) * 10;
122 int base = (int)(lon / 10);
123 if ( lon == base * 10 ) {
126 baselon = (base - 1) * 10;
130 baselon = (int)(lon / 10) * 10;
136 // make container directory
137 char container_dir[512];
138 snprintf( container_dir, 512, "%s/%c%03d%c%02d",
139 dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
140 snprintf( command, 512, "mkdir -p %s", container_dir );
141 cout << command << endl;
145 snprintf( dir, 512, "%c%03d%c%02d/%c%03d%c%02d",
146 EW, abs(baselon), NS, abs(baselat),
147 EW, abs(lon), NS, abs(lat) );
149 snprintf( command, 512,
150 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
151 source_base.c_str(), dir, dest_base.c_str(), dir );
152 cout << command << endl;
158 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
159 // do current 1x1 degree area first
160 sync_area( lat, lon );
162 if ( lat_dir == 0 && lon_dir == 0 ) {
163 // now do surrounding 8 1x1 degree areas.
164 for ( int i = lat - 1; i <= lat + 1; ++i ) {
165 for ( int j = lon - 1; j <= lon + 1; ++j ) {
166 if ( i != lat || j != lon ) {
172 if ( lat_dir != 0 ) {
173 sync_area( lat + lat_dir, lon );
174 sync_area( lat + lat_dir, lon - 1 );
175 sync_area( lat + lat_dir, lon + 1 );
177 if ( lon_dir != 0 ) {
178 sync_area( lat, lon + lon_dir );
179 sync_area( lat - 1, lon + lon_dir );
180 sync_area( lat + 1, lon + lon_dir );
186 int main( int argc, char **argv ) {
188 char host[256] = ""; // accept messages from anyone
193 if ( (string)argv[i] == "-p" ) {
195 port = atoi( argv[i] );
196 } else if ( (string)argv[i] == "-s" ) {
198 source_base = argv[i];
199 } else if ( (string)argv[i] == "-d" ) {
209 // Must call this before any other net stuff
210 netInit( &argc,argv );
214 if ( ! s.open( false ) ) { // open a UDP socket
215 printf("error opening socket\n");
219 s.setBlocking( false );
221 if ( s.bind( host, port ) == -1 ) {
222 printf("error binding to port %d\n", port);
230 int last_lat = -9999;
231 int last_lon = -9999;
232 bool recv_msg = false;
236 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
240 parse_message( msg, &lat, &lon );
241 cout << "pos = " << lat << "," << lon << endl;
245 if ( lat != last_lat || lon != last_lon ) {
246 int lat_dir, lon_dir, dist;
247 if ( last_lat == -9999 || last_lon == -9999 ) {
248 lat_dir = lon_dir = 0;
250 dist = lat - last_lat;
252 lat_dir = dist / abs(dist);
256 dist = lon - last_lon;
258 lon_dir = dist / abs(dist);
263 cout << "lat = " << lat << " lon = " << lon << endl;
264 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
265 sync_areas( lat, lon, lat_dir, lon_dir );