2 # This class is the internal subclass that does various Async connects and
3 # retreivals of info. Typical uses (and specific support) include http get and
6 # This merely starts up a Msg handler (and no DXChannel) ($conn in other words)
7 # does the GET, parses out the result and the data and then (assuming a positive
8 # result and that the originating callsign is still online) punts out the data
11 # It isn't designed to be very clever.
13 # Copyright (c) 2013 - Dirk Koopman G1TLH
23 use vars qw(@ISA $deftimeout);
31 # standard http get handler
38 my $state = $conn->{state};
40 dbg("asyncmsg: $msg") if isdbg('async');
42 # no point in going on if there is no-one wanting the output anymore
43 my $dxchan = DXChannel::get($conn->{caller});
49 if ($state eq 'waitreply') {
50 # look at the reply code and decide whether it is a success
51 my ($http, $code, $ascii) = $msg =~ m|(HTTP/\d\.\d)\s+(\d+)\s+(.*)|;
54 $conn->{state} = 'waitblank';
56 $dxchan->send("$code $ascii");
59 } elsif ($state eq 'waitblank') {
61 $conn->{state} = 'indata';
64 if (my $filter = $conn->{filter}) {
66 # this will crash if the command has been redefined and the filter is a
67 # function defined there whilst the request is in flight,
68 # but this isn't exactly likely in a production environment.
69 $filter->($conn, $msg, $dxchan);
71 my $prefix = $conn->{prefix} || '';
72 $dxchan->send("$prefix$msg");
80 # Just outputs everything
87 # no point in going on if there is no-one wanting the output anymore
88 my $dxchan = DXChannel::get($conn->{caller});
95 my $prefix = $conn->{prefix} || '';
96 $dxchan->send("$prefix$msg");
105 my $conn = $pkg->SUPER::new($handler);
106 $conn->{caller} = ref $call ? $call->call : $call;
109 $outstanding{$conn} = $conn;
114 # This does a http get on a path on a host and
115 # returns the result (through an optional filter)
117 # expects to be called something like from a cmd.pl file:
119 # AsyncMsg->get($self, <host>, <port>, <path>, [<key=>value>...]
121 # Standard key => value pairs are:
123 # filter => CODE ref (e.g. sub { ... })
124 # prefix => <string> prefix output with this string
126 # Anything else is taken and sent as (extra) http header stuff e.g:
128 # 'User-Agent' => qq{DXSpider;$main::version;$main::build;$^O}
129 # 'Content-Type' => q{text/xml; charset=utf-8}
130 # 'Content-Length' => $lth
132 # Host: is always set to the name of the host (unless overridden)
133 # User-Agent: is set to default above (unless overridden)
146 my $conn = $pkg->new($call, \&handle_get);
147 $conn->{state} = 'waitreply';
148 $conn->{filter} = delete $args{filter} if exists $args{filter};
149 $conn->{prefix} = delete $args{prefix} if exists $args{prefix};
150 $conn->{path} = $path;
152 $r = $conn->connect($host, $port);
154 dbg("Sending 'GET $path HTTP/1.0'") if isdbg('async');
155 $conn->send_later("GET $path HTTP/1.0\n");
156 my $h = delete $args{Host} || $host;
157 my $u = delete $args{'User-Agent'} || "DxSpider;$main::version;$main::build;$^O;$main::mycall";
158 $conn->send_later("Host: $h\n");
159 $conn->send_later("User-Agent: $u\n");
160 while (my ($k,$v) = each %args) {
161 $conn->send_later("$k: $v\n");
163 $conn->send_later("\n");
166 return $r ? $conn : undef;
169 # do a raw connection
171 # Async->raw($self, <host>, <port>, [handler => CODE ref], [prefix => <string>]);
173 # With no handler defined, everything sent by the connection will be sent to
176 # One can send stuff out on the connection by doing a standard "$conn->send_later(...)"
177 # inside the (custom) handler.
188 my $handler = delete $args{handler} || \&handle_raw;
189 my $conn = $pkg->new($call, $handler);
190 $conn->{prefix} = delete $args{prefix} if exists $args{prefix};
191 $r = $conn->connect($host, $port);
192 return $r ? $conn : undef;
202 my $r = $conn->SUPER::connect($host, $port);
204 dbg("HTTPMsg: Connected $conn->{cnum} to $host $port") if isdbg('async');
206 dbg("HTTPMsg: ***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('async');
215 delete $outstanding{$conn};
216 $conn->SUPER::disconnect;
222 delete $outstanding{$conn};
223 $conn->SUPER::DESTROY;