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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include <stdlib.h> // atoi() atof() abs() system()
34 #include <simgear/compiler.h>
39 #include <plib/netSocket.h>
42 #include <simgear/bucket/newbucket.hxx>
48 static string server = "scenery.flightgear.org";
49 static string source_module = "Scenery";
50 static string source_base = server + (string)"::" + source_module;
51 static string dest_base = "/dest/scenery/dir";
55 static void usage( const string& prog ) {
56 cout << "Usage: " << prog
57 << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
62 static void parse_message( const string &msg, int *lat, int *lon ) {
66 // find GGA string and advance to start of lat
67 string::size_type pos = text.find( "$GPGGA" );
68 string tmp = text.substr( pos + 7 );
69 pos = text.find( "," );
70 tmp = tmp.substr( pos + 1 );
71 // cout << "-> " << tmp << endl;
73 // find lat then advance to start of hemisphere
74 pos = tmp.find( "," );
75 string lats = tmp.substr( 0, pos );
76 dlat = atof( lats.c_str() ) / 100.0;
77 tmp = tmp.substr( pos + 1 );
79 // find N/S hemisphere and advance to start of lon
80 if ( tmp.substr( 0, 1 ) == "S" ) {
83 pos = tmp.find( "," );
84 tmp = tmp.substr( pos + 1 );
87 pos = tmp.find( "," );
88 string lons = tmp.substr( 0, pos );
89 dlon = atof( lons.c_str() ) / 100.0;
90 tmp = tmp.substr( pos + 1 );
92 // find E/W hemisphere and advance to start of lon
93 if ( tmp.substr( 0, 1 ) == "W" ) {
104 *lon = (int)dlon - 1;
112 static void sync_area( int lat, int lon ) {
114 int baselat, baselon;
117 int base = (int)(lat / 10);
118 if ( lat == base * 10 ) {
121 baselat = (base - 1) * 10;
125 baselat = (int)(lat / 10) * 10;
129 int base = (int)(lon / 10);
130 if ( lon == base * 10 ) {
133 baselon = (base - 1) * 10;
137 baselon = (int)(lon / 10) * 10;
142 char container_dir[512];
146 snprintf( container_dir, 512, "%s/Terrain/%c%03d%c%02d",
147 dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
148 snprintf( command, 512, "mkdir -p %s", container_dir );
149 cout << command << endl;
152 snprintf( dir, 512, "Terrain/%c%03d%c%02d/%c%03d%c%02d",
153 EW, abs(baselon), NS, abs(baselat),
154 EW, abs(lon), NS, abs(lat) );
156 snprintf( command, 512,
157 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
158 source_base.c_str(), dir, dest_base.c_str(), dir );
159 cout << command << endl;
163 snprintf( container_dir, 512, "%s/Objects/%c%03d%c%02d",
164 dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
165 snprintf( command, 512, "mkdir -p %s", container_dir );
166 cout << command << endl;
169 snprintf( dir, 512, "Objects/%c%03d%c%02d/%c%03d%c%02d",
170 EW, abs(baselon), NS, abs(baselat),
171 EW, abs(lon), NS, abs(lat) );
173 snprintf( command, 512,
174 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
175 source_base.c_str(), dir, dest_base.c_str(), dir );
176 cout << command << endl;
182 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
183 // do current 1x1 degree area first
184 sync_area( lat, lon );
186 if ( lat_dir == 0 && lon_dir == 0 ) {
187 // now do surrounding 8 1x1 degree areas.
188 for ( int i = lat - 1; i <= lat + 1; ++i ) {
189 for ( int j = lon - 1; j <= lon + 1; ++j ) {
190 if ( i != lat || j != lon ) {
196 if ( lat_dir != 0 ) {
197 sync_area( lat + lat_dir, lon );
198 sync_area( lat + lat_dir, lon - 1 );
199 sync_area( lat + lat_dir, lon + 1 );
201 if ( lon_dir != 0 ) {
202 sync_area( lat, lon + lon_dir );
203 sync_area( lat - 1, lon + lon_dir );
204 sync_area( lat + 1, lon + lon_dir );
210 int main( int argc, char **argv ) {
212 char host[256] = ""; // accept messages from anyone
217 if ( (string)argv[i] == "-p" ) {
219 port = atoi( argv[i] );
220 } else if ( (string)argv[i] == "-s" ) {
222 source_base = argv[i];
223 } else if ( (string)argv[i] == "-d" ) {
233 // Must call this before any other net stuff
234 netInit( &argc,argv );
238 if ( ! s.open( false ) ) { // open a UDP socket
239 printf("error opening socket\n");
243 s.setBlocking( false );
245 if ( s.bind( host, port ) == -1 ) {
246 printf("error binding to port %d\n", port);
254 int last_lat = -9999;
255 int last_lon = -9999;
256 bool recv_msg = false;
260 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
264 parse_message( msg, &lat, &lon );
265 cout << "pos = " << lat << "," << lon << endl;
269 if ( lat != last_lat || lon != last_lon ) {
270 int lat_dir, lon_dir, dist;
271 if ( last_lat == -9999 || last_lon == -9999 ) {
272 lat_dir = lon_dir = 0;
274 dist = lat - last_lat;
276 lat_dir = dist / abs(dist);
280 dist = lon - last_lon;
282 lon_dir = dist / abs(dist);
287 cout << "lat = " << lat << " lon = " << lon << endl;
288 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
289 sync_areas( lat, lon, lat_dir, lon_dir );