1 | #!/usr/bin/perl |
---|
2 | #----------------------------------------------------------------------------- |
---|
3 | # |
---|
4 | # Tirex Tile Rendering System |
---|
5 | # |
---|
6 | # tirex-send |
---|
7 | # |
---|
8 | #----------------------------------------------------------------------------- |
---|
9 | # See end of this file for documentation. |
---|
10 | #----------------------------------------------------------------------------- |
---|
11 | # |
---|
12 | # Copyright (C) 2010 Frederik Ramm <frederik.ramm@geofabrik.de> and |
---|
13 | # Jochen Topf <jochen.topf@geofabrik.de> |
---|
14 | # |
---|
15 | # This program is free software; you can redistribute it and/or |
---|
16 | # modify it under the terms of the GNU General Public License |
---|
17 | # as published by the Free Software Foundation; either version 2 |
---|
18 | # of the License, or (at your option) any later version. |
---|
19 | # |
---|
20 | # This program is distributed in the hope that it will be useful, |
---|
21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
23 | # GNU General Public License for more details. |
---|
24 | # |
---|
25 | # You should have received a copy of the GNU General Public License |
---|
26 | # along with this program; If not, see <http://www.gnu.org/licenses/>. |
---|
27 | # |
---|
28 | #----------------------------------------------------------------------------- |
---|
29 | |
---|
30 | use strict; |
---|
31 | use warnings; |
---|
32 | |
---|
33 | use Getopt::Long qw( :config gnu_getopt ); |
---|
34 | use IO::Socket; |
---|
35 | use Socket; |
---|
36 | use Pod::Usage; |
---|
37 | use Errno; |
---|
38 | |
---|
39 | use Tirex; |
---|
40 | |
---|
41 | #----------------------------------------------------------------------------- |
---|
42 | # Reading command line and config |
---|
43 | #----------------------------------------------------------------------------- |
---|
44 | |
---|
45 | my %opts = (); |
---|
46 | GetOptions( \%opts, 'help|h', 'debug|d', 'config|c=s', 'to|t=s', 'wait|w=i' ) or exit(2); |
---|
47 | |
---|
48 | if ($opts{'help'}) |
---|
49 | { |
---|
50 | pod2usage( |
---|
51 | -verbose => 1, |
---|
52 | -msg => "tirex-send - send messages to a tirex daemon\n", |
---|
53 | -exitval => 0 |
---|
54 | ); |
---|
55 | } |
---|
56 | |
---|
57 | $Tirex::DEBUG = 1 if ($opts{'debug'}); |
---|
58 | |
---|
59 | my $config_file = $opts{'config'} || $Tirex::TIREX_CONFIGFILE; |
---|
60 | Tirex::Config::init($config_file); |
---|
61 | |
---|
62 | #----------------------------------------------------------------------------- |
---|
63 | |
---|
64 | my %ports = ( |
---|
65 | 'master' => $Tirex::MASTER_UDP_PORT, |
---|
66 | 'syncd' => $Tirex::SYNCD_UDP_PORT, |
---|
67 | 'renderd' => $Tirex::RENDERD_UDP_PORT, |
---|
68 | ); |
---|
69 | |
---|
70 | $opts{'to'} ||= 'master'; |
---|
71 | my $port = ($opts{'to'} =~ /^[0-9]+$/) ? $opts{'to'} : $ports{$opts{'to'}}; |
---|
72 | |
---|
73 | if (!$port) |
---|
74 | { |
---|
75 | print STDERR "Unknown destination for --to option: $opts{'to'}\n"; |
---|
76 | exit(4); |
---|
77 | } |
---|
78 | |
---|
79 | my $socket = IO::Socket::INET->new( LocalAddr => 'localhost', Proto => 'udp' ); |
---|
80 | $socket->connect( Socket::pack_sockaddr_in($port, Socket::inet_aton('localhost')) ); |
---|
81 | |
---|
82 | #----------------------------------------------------------------------------- |
---|
83 | |
---|
84 | my $DEFAULT_TIMEOUT = 5; |
---|
85 | my $timeout = defined $opts{'wait'} ? $opts{'wait'} : |
---|
86 | ($opts{'to'} eq 'syncd' ? 0 : $DEFAULT_TIMEOUT); |
---|
87 | |
---|
88 | #----------------------------------------------------------------------------- |
---|
89 | |
---|
90 | my $type = shift; |
---|
91 | |
---|
92 | if (! $type) |
---|
93 | { |
---|
94 | print STDERR "missing message type\n"; |
---|
95 | exit(4); |
---|
96 | } |
---|
97 | |
---|
98 | my %msg = ( type => $type ); |
---|
99 | |
---|
100 | # don't add id if we are not waiting for response |
---|
101 | $msg{'id'} = "tirex-send.$$" unless ($timeout == 0); |
---|
102 | |
---|
103 | foreach my $field (@ARGV) |
---|
104 | { |
---|
105 | my ($k, $v) = split(/=/, $field, 2); |
---|
106 | $msg{$k} = $v; |
---|
107 | } |
---|
108 | |
---|
109 | #----------------------------------------------------------------------------- |
---|
110 | |
---|
111 | my $request = Tirex::Message->new(%msg); |
---|
112 | print STDERR "sending: ", $request->to_s(), "\n" if ($Tirex::DEBUG); |
---|
113 | $request->send($socket); |
---|
114 | |
---|
115 | exit(0) if ($timeout == 0); |
---|
116 | |
---|
117 | local $SIG{ALRM} = sub { print STDERR "timeout waiting for answer\n"; exit(3) }; |
---|
118 | alarm($timeout); |
---|
119 | |
---|
120 | my $reply = Tirex::Message->new_from_socket($socket); |
---|
121 | if (! defined $reply) |
---|
122 | { |
---|
123 | if ($!{'ECONNREFUSED'}) |
---|
124 | { |
---|
125 | print STDERR "Could not send request to server. Is it running?\n"; |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | print STDERR "Error reading answer: $!\n"; |
---|
130 | } |
---|
131 | exit(3); |
---|
132 | } |
---|
133 | |
---|
134 | print STDERR "got answer: ", $reply->to_s(), "\n" if ($Tirex::DEBUG); |
---|
135 | |
---|
136 | if ($reply->unknown_message_type()) |
---|
137 | { |
---|
138 | print STDERR "unknown message type: $type\n"; |
---|
139 | exit(2); |
---|
140 | } |
---|
141 | |
---|
142 | exit($reply->ok() ? 0 : 1); |
---|
143 | |
---|
144 | |
---|
145 | __END__ |
---|
146 | |
---|
147 | =head1 NAME |
---|
148 | |
---|
149 | tirex-send - send messages to a tirex daemon |
---|
150 | |
---|
151 | =head1 SYNOPSIS |
---|
152 | |
---|
153 | tirex-send [OPTIONS] TYPE [KEY=VALUE ...] |
---|
154 | |
---|
155 | =head1 OPTIONS |
---|
156 | |
---|
157 | =over 8 |
---|
158 | |
---|
159 | =item B<-h>, B<--help> |
---|
160 | |
---|
161 | Display help message. |
---|
162 | |
---|
163 | =item B<-d>, B<--debug> |
---|
164 | |
---|
165 | Run in debug mode. You'll see the actual messages sent and received. |
---|
166 | |
---|
167 | =item B<-c>, B<--config=FILE> |
---|
168 | |
---|
169 | Use the config file FILE instead of /etc/tirex/tirex.conf. |
---|
170 | |
---|
171 | =item B<-t>, B<--to=DESTINATION> |
---|
172 | |
---|
173 | Talk to DESTINATION, which can be 'master' (default), 'syncd', 'renderd' |
---|
174 | or a port number. |
---|
175 | |
---|
176 | =item B<-w>, B<--wait=SECONDS> |
---|
177 | |
---|
178 | Wait SECONDS for the answer after sending the message. Default is 5 seconds. |
---|
179 | If set to 0, don't wait for an answer at all. This is the default when the |
---|
180 | destination is set to syncd. |
---|
181 | |
---|
182 | =back |
---|
183 | |
---|
184 | =head1 DESCRIPTION |
---|
185 | |
---|
186 | tirex-send sends a message through a UDP socket to a tirex daemon running on |
---|
187 | the local machine and waits for the answer. It can be used to check whether a |
---|
188 | daemon is running, activate debugging in the daemon, send a render request, |
---|
189 | etc. |
---|
190 | |
---|
191 | Messages are normally send to the tirex-master daemon, but you can also send |
---|
192 | them to a renderd or sync daemon. |
---|
193 | |
---|
194 | You have to give the TYPE of the message on the command line and optionally |
---|
195 | some fields in the format KEY=VALUE. |
---|
196 | |
---|
197 | =head1 MESSAGES FOR MASTER |
---|
198 | |
---|
199 | The following messages can be send to a master server: |
---|
200 | |
---|
201 | =over 8 |
---|
202 | |
---|
203 | =item ping |
---|
204 | |
---|
205 | Check whether server is alive. |
---|
206 | |
---|
207 | =item debug |
---|
208 | |
---|
209 | Activate debugging in the daemon. |
---|
210 | |
---|
211 | =item nodebug |
---|
212 | |
---|
213 | Deactivate debugging in the daemon. |
---|
214 | |
---|
215 | =item quit |
---|
216 | |
---|
217 | Quit server now (unclean!). |
---|
218 | |
---|
219 | =item shutdown |
---|
220 | |
---|
221 | Clean shutdown (no yet implemented XXX) |
---|
222 | |
---|
223 | =item reset_max_queue_size |
---|
224 | |
---|
225 | Reset max queue size indicator. |
---|
226 | |
---|
227 | =item stop_rendering_bucket |
---|
228 | |
---|
229 | Stop feeding jobs from a bucket to the renderer. Currently rendering jobs are unaffected. |
---|
230 | Give the name of the bucket as "bucket=NAME". |
---|
231 | |
---|
232 | =item continue_rendering_bucket |
---|
233 | |
---|
234 | Continue rendering jobs from this bucket. |
---|
235 | Give the name of the bucket as "bucket=NAME". |
---|
236 | |
---|
237 | =back |
---|
238 | |
---|
239 | =head1 MESSAGES FOR RENDERD |
---|
240 | |
---|
241 | The following messages can be send to a renderd server: |
---|
242 | |
---|
243 | =over 8 |
---|
244 | |
---|
245 | =item metatile_render_request |
---|
246 | |
---|
247 | Renders the metatile requested. |
---|
248 | |
---|
249 | =back |
---|
250 | |
---|
251 | =head1 MESSAGES FOR SYNCD |
---|
252 | |
---|
253 | The following messages can be send to a syncd server: |
---|
254 | |
---|
255 | =over 8 |
---|
256 | |
---|
257 | =item metatile_render_request |
---|
258 | |
---|
259 | Syncs the metatile file. |
---|
260 | |
---|
261 | =back |
---|
262 | |
---|
263 | =head1 FILES |
---|
264 | |
---|
265 | =over 8 |
---|
266 | |
---|
267 | =item F</etc/tirex/tirex.conf> |
---|
268 | |
---|
269 | The configuration file. See tirex.conf(5) for further details. |
---|
270 | |
---|
271 | =back |
---|
272 | |
---|
273 | =head1 DIAGNOSTICS |
---|
274 | |
---|
275 | tirex-send returns |
---|
276 | |
---|
277 | =over 8 |
---|
278 | |
---|
279 | =item 0 if the message was answered with 'ok', |
---|
280 | |
---|
281 | =item 1 if the message was answered with an error, |
---|
282 | |
---|
283 | =item 2 if the error was 'unknown_message_type', |
---|
284 | |
---|
285 | =item 3 if the message could not be sent or if there was a timeout waiting for the answer, |
---|
286 | |
---|
287 | =item 4 if there was an error parsing the command line. |
---|
288 | |
---|
289 | =back |
---|
290 | |
---|
291 | =head1 AUTHORS |
---|
292 | |
---|
293 | Frederik Ramm <frederik.ramm@geofabrik.de>, Jochen Topf |
---|
294 | <jochen.topf@geofabrik.de> and possibly others. |
---|
295 | |
---|
296 | =cut |
---|
297 | |
---|
298 | #-- THE END ---------------------------------------------------------------------------- |
---|