source: subversion/applications/utils/tirex/bin/tirex-send @ 21037

Last change on this file since 21037 was 20996, checked in by jochen, 10 years ago

optionally allow destination to be a port number

  • Property svn:executable set to *
File size: 6.8 KB
Line 
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
30use strict;
31use warnings;
32
33use Getopt::Long qw( :config gnu_getopt );
34use IO::Socket;
35use Socket;
36use Pod::Usage;
37use Errno;
38
39use Tirex;
40
41#-----------------------------------------------------------------------------
42# Reading command line and config
43#-----------------------------------------------------------------------------
44
45my %opts = ();
46GetOptions( \%opts, 'help|h', 'debug|d', 'config|c=s', 'to|t=s', 'wait|w=i' ) or exit(2);
47
48if ($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
59my $config_file = $opts{'config'} || $Tirex::TIREX_CONFIGFILE;
60Tirex::Config::init($config_file);
61
62#-----------------------------------------------------------------------------
63
64my %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';
71my $port = ($opts{'to'} =~ /^[0-9]+$/) ? $opts{'to'} : $ports{$opts{'to'}};
72
73if (!$port)
74{
75    print STDERR "Unknown destination for --to option: $opts{'to'}\n";
76    exit(4);
77}
78
79my $socket = IO::Socket::INET->new( LocalAddr => 'localhost', Proto => 'udp' );
80$socket->connect( Socket::pack_sockaddr_in($port, Socket::inet_aton('localhost')) );
81
82#-----------------------------------------------------------------------------
83
84my $DEFAULT_TIMEOUT = 5;
85my $timeout = defined $opts{'wait'} ? $opts{'wait'} :
86                    ($opts{'to'} eq 'syncd' ? 0 : $DEFAULT_TIMEOUT);
87
88#-----------------------------------------------------------------------------
89
90my $type = shift;
91
92if (! $type)
93{
94    print STDERR "missing message type\n";
95    exit(4);
96}
97
98my %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
103foreach my $field (@ARGV)
104{
105    my ($k, $v) = split(/=/, $field, 2);
106    $msg{$k} = $v;
107}
108
109#-----------------------------------------------------------------------------
110
111my $request = Tirex::Message->new(%msg);
112print STDERR "sending: ", $request->to_s(), "\n" if ($Tirex::DEBUG);
113$request->send($socket);
114
115exit(0) if ($timeout == 0);
116
117local $SIG{ALRM} = sub { print STDERR "timeout waiting for answer\n"; exit(3) };
118alarm($timeout);
119
120my $reply = Tirex::Message->new_from_socket($socket);
121if (! 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
134print STDERR "got answer: ", $reply->to_s(), "\n" if ($Tirex::DEBUG);
135
136if ($reply->unknown_message_type())
137{
138    print STDERR "unknown message type: $type\n";
139    exit(2);
140}
141
142exit($reply->ok() ? 0 : 1);
143
144
145__END__
146
147=head1 NAME
148
149tirex-send - send messages to a tirex daemon
150
151=head1 SYNOPSIS
152
153tirex-send [OPTIONS] TYPE [KEY=VALUE ...]
154
155=head1 OPTIONS
156
157=over 8
158
159=item B<-h>, B<--help>
160
161Display help message.
162
163=item B<-d>, B<--debug>
164
165Run in debug mode. You'll see the actual messages sent and received.
166
167=item B<-c>, B<--config=FILE>
168
169Use the config file FILE instead of /etc/tirex/tirex.conf.
170
171=item B<-t>, B<--to=DESTINATION>
172
173Talk to DESTINATION, which can be 'master' (default), 'syncd', 'renderd'
174or a port number.
175
176=item B<-w>, B<--wait=SECONDS>
177
178Wait SECONDS for the answer after sending the message. Default is 5 seconds.
179If set to 0, don't wait for an answer at all. This is the default when the
180destination is set to syncd.
181
182=back
183
184=head1 DESCRIPTION
185
186tirex-send sends a message through a UDP socket to a tirex daemon running on
187the local machine and waits for the answer. It can be used to check whether a
188daemon is running, activate debugging in the daemon, send a render request,
189etc.
190
191Messages are normally send to the tirex-master daemon, but you can also send
192them to a renderd or sync daemon.
193
194You have to give the TYPE of the message on the command line and optionally
195some fields in the format KEY=VALUE.
196
197=head1 MESSAGES FOR MASTER
198
199The following messages can be send to a master server:
200
201=over 8
202
203=item ping
204
205Check whether server is alive.
206
207=item debug
208
209Activate debugging in the daemon.
210
211=item nodebug
212
213Deactivate debugging in the daemon.
214
215=item quit
216
217Quit server now (unclean!).
218
219=item shutdown
220
221Clean shutdown (no yet implemented XXX)
222
223=item reset_max_queue_size
224
225Reset max queue size indicator.
226
227=item stop_rendering_bucket
228
229Stop feeding jobs from a bucket to the renderer. Currently rendering jobs are unaffected.
230Give the name of the bucket as "bucket=NAME".
231
232=item continue_rendering_bucket
233
234Continue rendering jobs from this bucket.
235Give the name of the bucket as "bucket=NAME".
236
237=back
238
239=head1 MESSAGES FOR RENDERD
240
241The following messages can be send to a renderd server:
242
243=over 8
244
245=item metatile_render_request
246
247Renders the metatile requested.
248
249=back
250
251=head1 MESSAGES FOR SYNCD
252
253The following messages can be send to a syncd server:
254
255=over 8
256
257=item metatile_render_request
258
259Syncs the metatile file.
260
261=back
262
263=head1 FILES
264
265=over 8
266
267=item F</etc/tirex/tirex.conf>
268
269The configuration file. See tirex.conf(5) for further details.
270
271=back
272
273=head1 DIAGNOSTICS
274
275tirex-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
293Frederik Ramm <frederik.ramm@geofabrik.de>, Jochen Topf
294<jochen.topf@geofabrik.de> and possibly others.
295
296=cut
297
298#-- THE END ----------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.