]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
Durk Talsma:
[flightgear.git] / utils / TerraSync / terrasync.cxx
1 // terrasync.cxx -- "JIT" scenery fetcher
2 //
3 // Written by Curtis Olson, started November 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <stdlib.h>             // atoi() atof() abs() system()
25
26 #include <simgear/compiler.h>
27
28 #include STL_IOSTREAM
29 #include STL_STRING
30
31 #include <plib/netSocket.h>
32 #include <plib/ul.h>
33
34 #include <simgear/bucket/newbucket.hxx>
35
36 SG_USING_STD(string);
37 SG_USING_STD(cout);
38 SG_USING_STD(endl);
39
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";
44
45
46 // display usage
47 static void usage( const string& prog ) {
48     cout << "Usage: " << prog
49          << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
50 }
51
52
53 // parse message
54 static void parse_message( const string &msg, int *lat, int *lon ) {
55     double dlat, dlon;
56     string text = msg;
57
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;
64
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 );
70
71     // find N/S hemisphere and advance to start of lon
72     if ( tmp.substr( 0, 1 ) == "S" ) {
73         dlat = -dlat;
74     }
75     pos = tmp.find( "," );
76     tmp = tmp.substr( pos + 1 );
77
78     // find lon
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 );
83
84     // find E/W hemisphere and advance to start of lon
85     if ( tmp.substr( 0, 1 ) == "W" ) {
86         dlon = -dlon;
87     }
88
89     if ( dlat < 0 ) {
90         *lat = (int)dlat - 1;
91     } else {
92         *lat = (int)dlat;
93     }
94
95     if ( dlon < 0 ) {
96         *lon = (int)dlon - 1;
97     } else {
98         *lon = (int)dlon;
99     }
100 }
101
102
103 // sync area
104 static void sync_area( int lat, int lon ) {
105     char NS, EW;
106     int baselat, baselon;
107
108     if ( lat < 0 ) {
109         int base = (int)(lat / 10);
110         if ( lat == base * 10 ) {
111             baselat = base * 10;
112         } else {
113             baselat = (base - 1) * 10;
114         }
115         NS = 's';
116     } else {
117         baselat = (int)(lat / 10) * 10;
118         NS = 'n';
119     }
120     if ( lon < 0 ) {
121         int base = (int)(lon / 10);
122         if ( lon == base * 10 ) {
123             baselon = base * 10;
124         } else {
125             baselon = (base - 1) * 10;
126         }
127         EW = 'w';
128     } else {
129         baselon = (int)(lon / 10) * 10;
130         EW = 'e';
131     }
132
133     char command[512];
134     char container_dir[512];
135     char dir[512];
136
137     // Sync Terrain
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;
142     system( command );
143
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) );
147
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;
152     system( command );
153
154     // Sync Objects
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;
159     system( command );
160
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) );
164
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;
169     system( command );
170 }
171
172
173 // sync areas
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 );
177
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 ) {
183                     sync_area( i, j );
184                 }
185             }
186         }
187     } else {
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 );
192         }
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 );
197         }
198     }
199 }
200
201
202 int main( int argc, char **argv ) {
203     int port = 5501;
204     char host[256] = "";        // accept messages from anyone
205
206     // parse arguments
207     int i = 1;
208     while ( i < argc ) {
209         if ( (string)argv[i] == "-p" ) {
210             ++i;
211             port = atoi( argv[i] );
212         } else if ( (string)argv[i] == "-s" ) {
213             ++i;
214             source_base = argv[i];
215         } else if ( (string)argv[i] == "-d" ) {
216             ++i;
217             dest_base = argv[i];
218         } else {
219             usage( argv[0] );
220             exit(-1);        
221         }
222         ++i;
223     }
224
225     // Must call this before any other net stuff
226     netInit( &argc,argv );
227
228     netSocket s;
229
230     if ( ! s.open( false ) ) {  // open a UDP socket
231         printf("error opening socket\n");
232         return -1;
233     }
234
235     s.setBlocking( false );
236
237     if ( s.bind( host, port ) == -1 ) {
238         printf("error binding to port %d\n", port);
239         return -1;
240     }
241
242     char msg[256];
243     int maxlen = 256;
244     int len;
245     int lat, lon;
246     int last_lat = -9999;
247     int last_lon = -9999;
248     bool recv_msg = false;
249
250     while ( true ) {
251         recv_msg = false;
252         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
253             msg[len] = '\0';
254             recv_msg = true;
255
256             parse_message( msg, &lat, &lon );
257             cout << "pos = " << lat << "," << lon << endl;
258         }
259
260         if ( recv_msg ) {
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;
265                 } else {
266                     dist = lat - last_lat;
267                     if ( dist != 0 ) {
268                         lat_dir = dist / abs(dist);
269                     } else {
270                         lat_dir = 0;
271                     }
272                     dist = lon - last_lon;
273                     if ( dist != 0 ) {
274                         lon_dir = dist / abs(dist);
275                     } else {
276                         lon_dir = 0;
277                     }
278                 }
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 );
282             }
283
284             last_lat = lat;
285             last_lon = lon;
286         }
287
288         ulSleep( 1 );
289     }
290         
291     return 0;
292 }