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