]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
96eb8020004122588e49bd3a367da242cd71e0da
[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  - curt@flightgear.org
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-0.9.2";
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
135     // make container directory
136     char container_dir[512];
137     snprintf( container_dir, 512, "%s/%c%03d%c%02d",
138               dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
139     snprintf( command, 512, "mkdir -p %s", container_dir );
140     cout << command << endl;
141     system( command );
142
143     char dir[512];
144     snprintf( dir, 512, "%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
155
156 // sync areas
157 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
158     // do current 1x1 degree area first
159     sync_area( lat, lon );
160
161     if ( lat_dir == 0 && lon_dir == 0 ) {
162         // now do surrounding 8 1x1 degree areas.
163         for ( int i = lat - 1; i <= lat + 1; ++i ) {
164             for ( int j = lon - 1; j <= lon + 1; ++j ) {
165                 if ( i != lat || j != lon ) {
166                     sync_area( i, j );
167                 }
168             }
169         }
170     } else {
171         if ( lat_dir != 0 ) {
172             sync_area( lat + lat_dir, lon );
173             sync_area( lat + lat_dir, lon - 1 );
174             sync_area( lat + lat_dir, lon + 1 );
175         }
176         if ( lon_dir != 0 ) {
177             sync_area( lat, lon + lon_dir );
178             sync_area( lat - 1, lon + lon_dir );
179             sync_area( lat + 1, lon + lon_dir );
180         }
181     }
182 }
183
184
185 int main( int argc, char **argv ) {
186     int port = 5501;
187     char host[256] = "";        // accept messages from anyone
188
189     // parse arguments
190     int i = 1;
191     while ( i < argc ) {
192         if ( (string)argv[i] == "-p" ) {
193             ++i;
194             port = atoi( argv[i] );
195         } else if ( (string)argv[i] == "-s" ) {
196             ++i;
197             source_base = argv[i];
198         } else if ( (string)argv[i] == "-d" ) {
199             ++i;
200             dest_base = argv[i];
201         } else {
202             usage( argv[0] );
203             exit(-1);        
204         }
205         ++i;
206     }
207
208     // Must call this before any other net stuff
209     netInit( &argc,argv );
210
211     netSocket s;
212
213     if ( ! s.open( false ) ) {  // open a UDP socket
214         printf("error opening socket\n");
215         return -1;
216     }
217
218     s.setBlocking( false );
219
220     if ( s.bind( host, port ) == -1 ) {
221         printf("error binding to port %d\n", port);
222         return -1;
223     }
224
225     char msg[256];
226     int maxlen = 256;
227     int len;
228     int lat, lon;
229     int last_lat = -9999;
230     int last_lon = -9999;
231     bool recv_msg = false;
232
233     while ( true ) {
234         recv_msg = false;
235         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
236             msg[len] = '\0';
237             recv_msg = true;
238
239             parse_message( msg, &lat, &lon );
240             cout << "pos = " << lat << "," << lon << endl;
241         }
242
243         if ( recv_msg ) {
244             if ( lat != last_lat || lon != last_lon ) {
245                 int lat_dir, lon_dir, dist;
246                 if ( last_lat == -9999 || last_lon == -9999 ) {
247                     lat_dir = lon_dir = 0;
248                 } else {
249                     dist = lat - last_lat;
250                     if ( dist != 0 ) {
251                         lat_dir = dist / abs(dist);
252                     } else {
253                         lat_dir = 0;
254                     }
255                     dist = lon - last_lon;
256                     if ( dist != 0 ) {
257                         lon_dir = dist / abs(dist);
258                     } else {
259                         lon_dir = 0;
260                     }
261                 }
262                 cout << "lat = " << lat << " lon = " << lon << endl;
263                 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
264                 sync_areas( lat, lon, lat_dir, lon_dir );
265             }
266
267             last_lat = lat;
268             last_lon = lon;
269         }
270
271         ulSleep( 1 );
272     }
273         
274     return 0;
275 }