-
-
Save rightfold/064abbbd0aa5ae25e816 to your computer and use it in GitHub Desktop.
This file contains 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 perl6 | |
role Monitor { | |
method events returns Channel { ... } | |
method start { ... } | |
method stop { ... } | |
} | |
class PingMonitor does Monitor { | |
has Channel $.events; | |
has Proc::Async $!proc; | |
has @!argv; | |
method new(Str $host, Duration $interval, Duration $timeout) { | |
my @argv = "-i", $interval.Int, "-W", $timeout.Int, $host; | |
self.bless(:@argv); | |
} | |
submethod BUILD(:@!argv) { | |
$!events = Channel.new; | |
} | |
method start { | |
$!proc = Proc::Async.new("ping", @!argv); | |
$!proc.stdout.act: { | |
my $milliseconds = $_ ~~ /time\=((\d|\.)+)/ && $0; | |
$!events.send(Duration.new($milliseconds / 1000)); | |
}; | |
$!proc.start; | |
} | |
method stop { | |
$!proc.kill; | |
} | |
} | |
my $mon = PingMonitor.new("localhost", Duration.new(1), Duration.new(1)); | |
$mon.start; | |
for (1..10) { | |
say $mon.events.receive; | |
} | |
$mon.stop; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment