1 // terrasync.cxx -- "JIT" scenery fetcher
3 // Written by Curtis Olson, started November 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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>
40 static string server = "scenery.flightgear.org";
41 static string source_module = "Scenery";
42 static string source_base = server + (string)"::" + source_module;
43 static string dest_base = "/dest/scenery/dir";
47 static void usage( const string& prog ) {
48 cout << "Usage: " << prog
49 << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
54 static void parse_message( const string &msg, int *lat, int *lon ) {
58 // find GGA string and advance to start of lat
59 string::size_type pos = text.find( "$GPGGA" );
60 string tmp = text.substr( pos + 7 );
61 pos = text.find( "," );
62 tmp = tmp.substr( pos + 1 );
63 // cout << "-> " << tmp << endl;
65 // find lat then advance to start of hemisphere
66 pos = tmp.find( "," );
67 string lats = tmp.substr( 0, pos );
68 dlat = atof( lats.c_str() ) / 100.0;
69 tmp = tmp.substr( pos + 1 );
71 // find N/S hemisphere and advance to start of lon
72 if ( tmp.substr( 0, 1 ) == "S" ) {
75 pos = tmp.find( "," );
76 tmp = tmp.substr( pos + 1 );
79 pos = tmp.find( "," );
80 string lons = tmp.substr( 0, pos );
81 dlon = atof( lons.c_str() ) / 100.0;
82 tmp = tmp.substr( pos + 1 );
84 // find E/W hemisphere and advance to start of lon
85 if ( tmp.substr( 0, 1 ) == "W" ) {
104 static void sync_area( int lat, int lon ) {
106 int baselat, baselon;
109 int base = (int)(lat / 10);
110 if ( lat == base * 10 ) {
113 baselat = (base - 1) * 10;
117 baselat = (int)(lat / 10) * 10;
121 int base = (int)(lon / 10);
122 if ( lon == base * 10 ) {
125 baselon = (base - 1) * 10;
129 baselon = (int)(lon / 10) * 10;
134 char container_dir[512];
138 snprintf( container_dir, 512, "%s/Terrain/%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;
144 snprintf( dir, 512, "Terrain/%c%03d%c%02d/%c%03d%c%02d",
145 EW, abs(baselon), NS, abs(baselat),
146 EW, abs(lon), NS, abs(lat) );
148 snprintf( command, 512,
149 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
150 source_base.c_str(), dir, dest_base.c_str(), dir );
151 cout << command << endl;
155 snprintf( container_dir, 512, "%s/Objects/%c%03d%c%02d",
156 dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
157 snprintf( command, 512, "mkdir -p %s", container_dir );
158 cout << command << endl;
161 snprintf( dir, 512, "Objects/%c%03d%c%02d/%c%03d%c%02d",
162 EW, abs(baselon), NS, abs(baselat),
163 EW, abs(lon), NS, abs(lat) );
165 snprintf( command, 512,
166 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
167 source_base.c_str(), dir, dest_base.c_str(), dir );
168 cout << command << endl;
174 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
175 // do current 1x1 degree area first
176 sync_area( lat, lon );
178 if ( lat_dir == 0 && lon_dir == 0 ) {
179 // now do surrounding 8 1x1 degree areas.
180 for ( int i = lat - 1; i <= lat + 1; ++i ) {
181 for ( int j = lon - 1; j <= lon + 1; ++j ) {
182 if ( i != lat || j != lon ) {
188 if ( lat_dir != 0 ) {
189 sync_area( lat + lat_dir, lon );
190 sync_area( lat + lat_dir, lon - 1 );
191 sync_area( lat + lat_dir, lon + 1 );
193 if ( lon_dir != 0 ) {
194 sync_area( lat, lon + lon_dir );
195 sync_area( lat - 1, lon + lon_dir );
196 sync_area( lat + 1, lon + lon_dir );
202 int main( int argc, char **argv ) {
204 char host[256] = ""; // accept messages from anyone
209 if ( (string)argv[i] == "-p" ) {
211 port = atoi( argv[i] );
212 } else if ( (string)argv[i] == "-s" ) {
214 source_base = argv[i];
215 } else if ( (string)argv[i] == "-d" ) {
225 // Must call this before any other net stuff
226 netInit( &argc,argv );
230 if ( ! s.open( false ) ) { // open a UDP socket
231 printf("error opening socket\n");
235 s.setBlocking( false );
237 if ( s.bind( host, port ) == -1 ) {
238 printf("error binding to port %d\n", port);
246 int last_lat = -9999;
247 int last_lon = -9999;
248 bool recv_msg = false;
252 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
256 parse_message( msg, &lat, &lon );
257 cout << "pos = " << lat << "," << lon << endl;
261 if ( lat != last_lat || lon != last_lon ) {
262 int lat_dir, lon_dir, dist;
263 if ( last_lat == -9999 || last_lon == -9999 ) {
264 lat_dir = lon_dir = 0;
266 dist = lat - last_lat;
268 lat_dir = dist / abs(dist);
272 dist = lon - last_lon;
274 lon_dir = dist / abs(dist);
279 cout << "lat = " << lat << " lon = " << lon << endl;
280 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
281 sync_areas( lat, lon, lat_dir, lon_dir );