Created
January 20, 2011 08:30
-
-
Save saillinux/787581 to your computer and use it in GitHub Desktop.
anyevent non-blocking port check template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use AnyEvent::Socket; | |
use constant INTERVAL => 1; | |
my ($host, $port) = @ARGV; | |
die "No host name is given" unless defined $host; | |
die "No port numb is given" unless defined $port; | |
my $cv = AnyEvent->condvar; | |
my $once_per_second; | |
$once_per_second = AnyEvent->timer(interval => INTERVAL, | |
cb => \&port_check, | |
); | |
$cv->recv; | |
sub port_check { | |
tcp_connect $host, $port, sub { | |
my ($fh) = @_ or do { | |
die "The host or service is down\n"; # print 'timeout' at most every second | |
}; | |
warn "Host is alive"; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment