]> dxcluster.net Git - spider.git/commitdiff
now with async sh/qrz!
authorDirk Koopman <djk@tobit.co.uk>
Tue, 27 Dec 2011 13:59:32 +0000 (13:59 +0000)
committerDirk Koopman <djk@tobit.co.uk>
Tue, 27 Dec 2011 13:59:32 +0000 (13:59 +0000)
There is a new DXCommandmode::http_get() async http get routine
that is callable from a command such as sh/qrz.

cmd/show/qrz.pl
perl/DXChannel.pm
perl/DXCommandmode.pm
perl/Messages
perl/Version.pm
perl/cluster.pl

index 9a3f9c3fc93ed9dd334b64ac259c7e271ac7ae14..a5d14138c38a0a791e909801c181ac3c683cbcf0 100644 (file)
@@ -17,50 +17,54 @@ return (1, "SHOW/QRZ <callsign>, e.g. SH/QRZ g1tlh") unless @list;
 my $target = $Internet::http_proxy || $Internet::qrz_url || 'xml.qrz.com';
 my $port = $Internet::http_proxy_port || 80;
 my $url = '';
-$url = 'http://' . ($Internet::qrz_url | 'xml.qrz.com') if $Internet::http_proxy;
+$url = 'http://' . ($Internet::qrz_url || 'xml.qrz.com') if $Internet::http_proxy;
 
+foreach $l (@list) {
 
-use Net::Telnet;
+       my $host = $url?$url:$target;
+       my $s = "$url/xml?callsign=$l;username=$Internet::qrz_uid;password=$Internet::qrz_pw;agent=dxspider";
+       if (isdbg('qrz')) {
+               dbg("qrz: $host");
+               dbg("qrz: $s");
+       }
 
-my $t = new Net::Telnet;
+       Log('call', "$call: show/qrz \U$l");
+       push @out,  $self->msg('http1', "show/qrz \U$l");
 
-foreach $l (@list) {
-       eval {
-               $t->open(Host     =>  $target,
-                                Port     =>  $port,
-                                Timeout  =>  15);
-       };
+       $self->http_get($host, $s, sub
+                                       {
+                                               my ($response, $header, $body) = @_;
+                                               my @out;
 
-       if (!$t || $@) {
-               push @out, $self->msg('e18', 'QRZ.com');
-       } else {
-               my $s = "GET /xml?callsign=$l;username=$Internet::qrz_uid;password=$Internet::qrz_pw;agent=dxspider HTTP/1.0\n\n";
-               dbg($s) if isdbg('qrz');
-               $t->print($s);
-               Log('call', "$call: show/qrz \U$l");
-               my $state = "blank";
-               while (my $result = eval { $t->getline(Timeout => 30) } || $@) {
-                       dbg($result) if isdbg('qrz') && $result;
-                       if ($@) {
-                               push @out, $self->msg('e18', 'QRZ.com');
-                               last;
-                       }
-                       if ($state eq 'blank' && $result =~ /^<Callsign>/i) {
-                               $state = 'go';
-                       } elsif ($state eq 'go') {
-                               next if $result =~ m|<user>|;
-                               next if $result =~ m|<u_views>|;
-                               next if $result =~ m|<locref>|;
-                               next if $result =~ m|<ccode>|;
-                               next if $result =~ m|<dxcc>|;
-                               last if $result =~ m|</Callsign>|;
-                               my ($tag, $data) = $result =~ m|^\s*<(\w+)>(.*)</|;
-                               push @out, sprintf "%10s: $data", $tag;
-                       }
-               }
-               $t->close;
-               push @out, $self->msg('e3', 'qrz.com', uc $l) unless @out;
-       }
+                                               if (isdbg('qrz')) {
+                                                       dbg("qrz response: $response");
+                                                       dbg("qrz body: $body");
+                                               }
+                                               Log('call', "$call: show/qrz \U$body");
+                                               my $state = "blank";
+                                               foreach my $result (split /\r?\n/, $body) {
+                                                       dbg("qrz: $result") if isdbg('qrz') && $result;
+                                                       if ($state eq 'blank' && $result =~ /^<Callsign>/i) {
+                                                               $state = 'go';
+                                                       } elsif ($state eq 'go') {
+                                                               next if $result =~ m|<user>|;
+                                                               next if $result =~ m|<u_views>|;
+                                                               next if $result =~ m|<locref>|;
+                                                               next if $result =~ m|<ccode>|;
+                                                               next if $result =~ m|<dxcc>|;
+                                                               last if $result =~ m|</Callsign>|;
+                                                               my ($tag, $data) = $result =~ m|^\s*<(\w+)>(.*)</|;
+                                                               push @out, sprintf "%10s: $data", $tag;
+                                                       }
+                                               }
+                                               if (@out) {
+                                                       unshift @out, $self->msg('http2', "show/qrz \U$l");
+                                               } else {
+                                                       push @out, $self->msg('e3', 'show/qrz', uc $l);
+                                               }
+                                               $self->send_ans(@out);
+                                       }
+                                  );
 }
 
 return (1, @out);
index 584a541c1244e44a8b3f1ae90e22b09a8b72e793..64a9a1ae5b7c153608c19416f95d64ebb287fa67 100644 (file)
@@ -125,6 +125,7 @@ $count = 0;
                  inqueue => '9,Input Queue,parray',
                  next_pc92_update => '9,Next PC92 Update,atime',
                  next_pc92_keepalive => '9,Next PC92 KeepAlive,atime',
+                 anyevents => '9,outstanding AnyEvent handles,parray',
                 );
 
 $maxerrors = 20;                               # the maximum number of concurrent errors allowed before disconnection
@@ -179,6 +180,7 @@ sub alloc
                $self->{cq} = $dxcc[1]->cq;                                             
        }
        $self->{inqueue} = [];
+       $self->{anyevents} = [];
 
        $count++;
        dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
@@ -752,6 +754,25 @@ sub handle_xml
        return $r;
 }
 
+sub anyevent_add
+{
+       my $self = shift;
+       my $handle = shift;
+       my $sort = shift || "unknown";
+
+       push @{$self->{anyevents}}, $handle;
+       dbg("anyevent: add $sort handle making " . scalar @{$self->{anyevents}} . " handles in use") if isdbg('anyevent');
+}
+
+sub anyevent_del
+{
+       my $self = shift;
+       my $handle = shift;
+       my $sort = shift || "unknown";
+       $self->{anyevents} = [ grep {$_ != $handle} @{$self->{anyevents}} ];
+       dbg("anyevent: delete $sort handle making " . scalar @{$self->{anyevents}} . " handles in use") if isdbg('anyevent');
+}
+
 #no strict;
 sub AUTOLOAD
 {
index 798351773c998c62f603facd84c093c782873d34..a9777cbf0929b2da31cc5d3b76fa30929e6e55cb 100644 (file)
@@ -13,6 +13,10 @@ package DXCommandmode;
 
 @ISA = qw(DXChannel);
 
+use AnyEvent;
+use AnyEvent::Handle;
+use AnyEvent::Socket;
+
 use POSIX qw(:math_h);
 use DXUtil;
 use DXChannel;
@@ -1233,5 +1237,50 @@ sub send_motd
        }
        $self->send_file($motd) if -e $motd;
 }
+
+sub http_get
+{
+       my $self = shift;
+       my ($host, $uri, $cb) = @_;
+
+       # store results here
+       my ($response, $header, $body);
+
+       my $handle;
+       $handle = AnyEvent::Handle->new(
+                                                                       connect  => [$host => 'http'],
+                                                                       on_error => sub {
+                                                                               $cb->("HTTP/1.0 500 $!");
+                                                                               $self->anyevent_del($handle);
+                                                                               $handle->destroy; # explicitly destroy handle
+                                                                       },
+                                                                       on_eof   => sub {
+                                                                               $cb->($response, $header, $body);
+                                                                               $self->anyevent_del($handle);
+                                                                               $handle->destroy; # explicitly destroy handle
+                                                                       }
+                                                                  );
+       $self->anyevent_add($handle);
+       $handle->push_write ("GET $uri HTTP/1.0\015\012\015\012");
+
+       # now fetch response status line
+       $handle->push_read (line => sub {
+                                                       my ($handle, $line) = @_;
+                                                       $response = $line;
+                                               });
+
+       # then the headers
+       $handle->push_read (line => "\015\012\015\012", sub {
+                                                       my ($handle, $line) = @_;
+                                                       $header = $line;
+                                               });
+
+       # and finally handle any remaining data as body
+       $handle->on_read (sub {
+                                                 $body .= $_[0]->rbuf;
+                                                 $_[0]->rbuf = "";
+                                         });
+}
+
 1;
 __END__
index 911f7c89f61d4b49f3d82b455ecd9e9191a717cb..22551f00595c6b87e573f1b2dd6ebddb5b59ce5d 100644 (file)
@@ -150,6 +150,8 @@ package DXM;
                                hnodee1 => 'Please enter your Home Node, set/homenode <your home DX Cluster>',
                                hnodee2 => 'Failed to set homenode on $_[0]',
                                hnode => 'Your Homenode is now \"$_[0]\"',
+                               http1 => '$_[0] working ...',
+                               http2 => '$_[0] returned:',
                                init1 => 'sent initialisation message to $_[0]',
                                iso => '$_[0] Isolated',
                                isou => '$_[0] UnIsolated',
index 3c7bad08888e93f71bc132ccde0b50083aa0a19f..3088501f0c37b5d933c93f398755742e6b5bb3a6 100644 (file)
@@ -11,7 +11,7 @@ use vars qw($version $subversion $build $gitversion);
 
 $version = '1.56';
 $subversion = '0';
-$build = '9';
-$gitversion = 'a22dbff';
+$build = '10';
+$gitversion = '370d356';
 
 1;
index a40a5aa5c51c274f2690984526a0de43fd5cecc8..43828974d05c00f10d7936c8815b3df914fa8500 100755 (executable)
@@ -492,6 +492,8 @@ my ($sigint, $sigterm);
 unless ($DB::VERSION) {
        $sigint = AnyEvent->signal(signal=>'INT', cb=> sub{$decease->send});
        $sigterm = AnyEvent->signal(signal=>'TERM', cb=> sub{$decease->send});
+#      $sigint = AnyEvent->signal(signal=>'INT', cb=> sub{AnyEvent->unloop});
+#      $sigterm = AnyEvent->signal(signal=>'TERM', cb=> sub{AnyEvent->unloop});
 }
 
 unless ($is_win) {
@@ -584,6 +586,7 @@ my $per_sec = AnyEvent->timer(after => 0, interval => 0.010, cb => sub{idle_loop
 
 # main loop
 $decease->recv;
+#AnyEvent->loop;
 
 idle_loop() for (1..25);
 cease(0);