]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
Win32 fix
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #ifdef HAVE_WINDOWS_H
28 #include <windows.h>
29 #endif
30
31
32 #include <stdlib.h>             // atoi() atof() abs() system()
33
34 #include <simgear/compiler.h>
35
36 #include STL_IOSTREAM
37 #include STL_STRING
38
39 #include <plib/netSocket.h>
40 #include <plib/ul.h>
41
42 #include <simgear/bucket/newbucket.hxx>
43
44 SG_USING_STD(string);
45 SG_USING_STD(cout);
46 SG_USING_STD(endl);
47
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";
52
53
54 // display usage
55 static void usage( const string& prog ) {
56     cout << "Usage: " << prog
57          << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
58 }
59
60
61 // parse message
62 static void parse_message( const string &msg, int *lat, int *lon ) {
63     double dlat, dlon;
64     string text = msg;
65
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;
72
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 );
78
79     // find N/S hemisphere and advance to start of lon
80     if ( tmp.substr( 0, 1 ) == "S" ) {
81         dlat = -dlat;
82     }
83     pos = tmp.find( "," );
84     tmp = tmp.substr( pos + 1 );
85
86     // find lon
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 );
91
92     // find E/W hemisphere and advance to start of lon
93     if ( tmp.substr( 0, 1 ) == "W" ) {
94         dlon = -dlon;
95     }
96
97     if ( dlat < 0 ) {
98         *lat = (int)dlat - 1;
99     } else {
100         *lat = (int)dlat;
101     }
102
103     if ( dlon < 0 ) {
104         *lon = (int)dlon - 1;
105     } else {
106         *lon = (int)dlon;
107     }
108 }
109
110
111 // sync area
112 static void sync_area( int lat, int lon ) {
113     char NS, EW;
114     int baselat, baselon;
115
116     if ( lat < 0 ) {
117         int base = (int)(lat / 10);
118         if ( lat == base * 10 ) {
119             baselat = base * 10;
120         } else {
121             baselat = (base - 1) * 10;
122         }
123         NS = 's';
124     } else {
125         baselat = (int)(lat / 10) * 10;
126         NS = 'n';
127     }
128     if ( lon < 0 ) {
129         int base = (int)(lon / 10);
130         if ( lon == base * 10 ) {
131             baselon = base * 10;
132         } else {
133             baselon = (base - 1) * 10;
134         }
135         EW = 'w';
136     } else {
137         baselon = (int)(lon / 10) * 10;
138         EW = 'e';
139     }
140
141     char command[512];
142     char container_dir[512];
143     char dir[512];
144
145     // Sync Terrain
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;
150     system( command );
151
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) );
155
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;
160     system( command );
161
162     // Sync Objects
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;
167     system( command );
168
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) );
172
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;
177     system( command );
178 }
179
180
181 // sync areas
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 );
185
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 ) {
191                     sync_area( i, j );
192                 }
193             }
194         }
195     } else {
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 );
200         }
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 );
205         }
206     }
207 }
208
209
210 int main( int argc, char **argv ) {
211     int port = 5501;
212     char host[256] = "";        // accept messages from anyone
213
214     // parse arguments
215     int i = 1;
216     while ( i < argc ) {
217         if ( (string)argv[i] == "-p" ) {
218             ++i;
219             port = atoi( argv[i] );
220         } else if ( (string)argv[i] == "-s" ) {
221             ++i;
222             source_base = argv[i];
223         } else if ( (string)argv[i] == "-d" ) {
224             ++i;
225             dest_base = argv[i];
226         } else {
227             usage( argv[0] );
228             exit(-1);        
229         }
230         ++i;
231     }
232
233     // Must call this before any other net stuff
234     netInit( &argc,argv );
235
236     netSocket s;
237
238     if ( ! s.open( false ) ) {  // open a UDP socket
239         printf("error opening socket\n");
240         return -1;
241     }
242
243     s.setBlocking( false );
244
245     if ( s.bind( host, port ) == -1 ) {
246         printf("error binding to port %d\n", port);
247         return -1;
248     }
249
250     char msg[256];
251     int maxlen = 256;
252     int len;
253     int lat, lon;
254     int last_lat = -9999;
255     int last_lon = -9999;
256     bool recv_msg = false;
257
258     while ( true ) {
259         recv_msg = false;
260         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
261             msg[len] = '\0';
262             recv_msg = true;
263
264             parse_message( msg, &lat, &lon );
265             cout << "pos = " << lat << "," << lon << endl;
266         }
267
268         if ( recv_msg ) {
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;
273                 } else {
274                     dist = lat - last_lat;
275                     if ( dist != 0 ) {
276                         lat_dir = dist / abs(dist);
277                     } else {
278                         lat_dir = 0;
279                     }
280                     dist = lon - last_lon;
281                     if ( dist != 0 ) {
282                         lon_dir = dist / abs(dist);
283                     } else {
284                         lon_dir = 0;
285                     }
286                 }
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 );
290             }
291
292             last_lat = lat;
293             last_lon = lon;
294         }
295
296         ulSleep( 1 );
297     }
298         
299     return 0;
300 }