]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.cxx
eb5fb148c133b288317875eb3402d6eb7e2d1d12
[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
41 static string server = "baron.flightgear.org";
42 static string source_module = "Scenery-0.7.9";
43 static string source_base = server + (string)"::" + source_module;
44 static string dest_base = "/dest/scenery/dir";
45
46
47 // display usage
48 static void usage( const string& prog ) {
49     cout << "Usage: " << prog
50          << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
51 }
52
53
54 // parse message
55 static void parse_message( const string &msg, int *lat, int *lon ) {
56     double dlat, dlon;
57     string text = msg;
58
59     // find GGA string and advance to start of lat
60     string::size_type pos = text.find( "$GPGGA" );
61     string tmp = text.substr( pos + 7 );
62     pos = text.find( "," );
63     tmp = tmp.substr( pos + 1 );
64     // cout << "-> " << tmp << endl;
65
66     // find lat then advance to start of hemisphere
67     pos = tmp.find( "," );
68     string lats = tmp.substr( 0, pos );
69     dlat = atof( lats.c_str() ) / 100.0;
70     tmp = tmp.substr( pos + 1 );
71
72     // find N/S hemisphere and advance to start of lon
73     if ( tmp.substr( 0, 1 ) == "S" ) {
74         dlat = -dlat;
75     }
76     pos = tmp.find( "," );
77     tmp = tmp.substr( pos + 1 );
78
79     // find lon
80     pos = tmp.find( "," );
81     string lons = tmp.substr( 0, pos );
82     dlon = atof( lons.c_str() ) / 100.0;
83     tmp = tmp.substr( pos + 1 );
84
85     // find E/W hemisphere and advance to start of lon
86     if ( tmp.substr( 0, 1 ) == "W" ) {
87         dlon = -dlon;
88     }
89
90     if ( dlat < 0 ) {
91         *lat = (int)dlat - 1;
92     } else {
93         *lat = (int)dlat;
94     }
95
96     if ( dlon < 0 ) {
97         *lon = (int)dlon - 1;
98     } else {
99         *lon = (int)dlon;
100     }
101 }
102
103
104 // sync area
105 static void sync_area( int lat, int lon ) {
106     char NS, EW;
107     int baselat, baselon;
108
109     if ( lat < 0 ) {
110         int base = (int)(lat / 10);
111         if ( lat == base * 10 ) {
112             baselat = base * 10;
113         } else {
114             baselat = (base - 1) * 10;
115         }
116         NS = 's';
117     } else {
118         baselat = (int)(lat / 10) * 10;
119         NS = 'n';
120     }
121     if ( lon < 0 ) {
122         int base = (int)(lon / 10);
123         if ( lon == base * 10 ) {
124             baselon = base * 10;
125         } else {
126             baselon = (base - 1) * 10;
127         }
128         EW = 'w';
129     } else {
130         baselon = (int)(lon / 10) * 10;
131         EW = 'e';
132     }
133
134     char command[512];
135
136     // make container directory
137     char container_dir[512];
138     snprintf( container_dir, 512, "%s/%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     char dir[512];
145     snprintf( dir, 512, "%c%03d%c%02d/%c%03d%c%02d",
146               EW, abs(baselon), NS, abs(baselat),
147               EW, abs(lon), NS, abs(lat) );
148
149     snprintf( command, 512,
150               "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
151               source_base.c_str(), dir, dest_base.c_str(), dir );
152     cout << command << endl;
153     system( command );
154 }
155
156
157 // sync areas
158 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
159     // do current 1x1 degree area first
160     sync_area( lat, lon );
161
162     if ( lat_dir == 0 && lon_dir == 0 ) {
163         // now do surrounding 8 1x1 degree areas.
164         for ( int i = lat - 1; i <= lat + 1; ++i ) {
165             for ( int j = lon - 1; j <= lon + 1; ++j ) {
166                 if ( i != lat || j != lon ) {
167                     sync_area( i, j );
168                 }
169             }
170         }
171     } else {
172         if ( lat_dir != 0 ) {
173             sync_area( lat + lat_dir, lon );
174             sync_area( lat + lat_dir, lon - 1 );
175             sync_area( lat + lat_dir, lon + 1 );
176         }
177         if ( lon_dir != 0 ) {
178             sync_area( lat, lon + lon_dir );
179             sync_area( lat - 1, lon + lon_dir );
180             sync_area( lat + 1, lon + lon_dir );
181         }
182     }
183 }
184
185
186 int main( int argc, char **argv ) {
187     int port = 5501;
188     char host[256] = "";        // accept messages from anyone
189
190     // parse arguments
191     int i = 1;
192     while ( i < argc ) {
193         if ( (string)argv[i] == "-p" ) {
194             ++i;
195             port = atoi( argv[i] );
196         } else if ( (string)argv[i] == "-s" ) {
197             ++i;
198             source_base = argv[i];
199         } else if ( (string)argv[i] == "-d" ) {
200             ++i;
201             dest_base = argv[i];
202         } else {
203             usage( argv[0] );
204             exit(-1);        
205         }
206         ++i;
207     }
208
209     // Must call this before any other net stuff
210     netInit( &argc,argv );
211
212     netSocket s;
213
214     if ( ! s.open( false ) ) {  // open a UDP socket
215         printf("error opening socket\n");
216         return -1;
217     }
218
219     s.setBlocking( false );
220
221     if ( s.bind( host, port ) == -1 ) {
222         printf("error binding to port %d\n", port);
223         return -1;
224     }
225
226     char msg[256];
227     int maxlen = 256;
228     int len;
229     int lat, lon;
230     int last_lat = -9999;
231     int last_lon = -9999;
232     bool recv_msg = false;
233
234     while ( true ) {
235         recv_msg = false;
236         while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
237             msg[len] = '\0';
238             recv_msg = true;
239
240             parse_message( msg, &lat, &lon );
241             cout << "pos = " << lat << "," << lon << endl;
242         }
243
244         if ( recv_msg ) {
245             if ( lat != last_lat || lon != last_lon ) {
246                 int lat_dir, lon_dir, dist;
247                 if ( last_lat == -9999 || last_lon == -9999 ) {
248                     lat_dir = lon_dir = 0;
249                 } else {
250                     dist = lat - last_lat;
251                     if ( dist != 0 ) {
252                         lat_dir = dist / abs(dist);
253                     } else {
254                         lat_dir = 0;
255                     }
256                     dist = lon - last_lon;
257                     if ( dist != 0 ) {
258                         lon_dir = dist / abs(dist);
259                     } else {
260                         lon_dir = 0;
261                     }
262                 }
263                 cout << "lat = " << lat << " lon = " << lon << endl;
264                 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
265                 sync_areas( lat, lon, lat_dir, lon_dir );
266             }
267
268             last_lat = lat;
269             last_lon = lon;
270         }
271
272         ulSleep( 1 );
273     }
274         
275     return 0;
276 }