und3f / anyevent-ping

Asynchronous ping with AnyEvent
2 stars 4 forks source link

Control socket #3

Closed und3f closed 10 years ago

und3f commented 10 years ago

I'm using AnyEvent :: Ping to test the connection from different interfaces using a work-aroundto change bind address (see the code at bottom).

I had to refer to the private attribute "_socket."

In a future version, you can expose a method to refer to the socket, or a better method to change the address to bind?

Best regards and thanks for your code. Guido Brugnara

Example:

#!/usr/bin/perl

use lib '/opt/heidix/lib/perl';
use AnyEvent;
use AnyEvent::Ping;
use Socket qw( inet_aton inet_ntoa sockaddr_in );

my $ping = AnyEvent::Ping->new;

if($ARGV[0] !~ m/^\d+\.\d+\.\d+\.\d+$/){
  die "No interface address to bind\n";
}
if(!$ARGV[1]){
  die "No address to check\n";
}

$ip = inet_aton($ARGV[0]);
bind $ping->{_socket}, sockaddr_in(0, $ip);

my $cv = AnyEvent->condvar;

$ping->ping($ARGV[1], 1, sub {
  my $result = shift;
  print "Result: ", $result->[0][0],
        " in ", $result->[0][1], " seconds\n";
  $cv->send;
});

$cv->recv;
$ping->end;
und3f commented 10 years ago

it might be good to have on_prepare() method

my $ping = AnyEvent::Ping->new(
    on_prepare => sub {
        my $socket = shift;
        ....
});
GuidoBrugnara commented 10 years ago

Thank you

und3f commented 10 years ago

I've added this feature in 7b9b049b7d324d716f4ca48b96ea1cc3614ed99c