Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/064abbbd0aa5ae25e816 to your computer and use it in GitHub Desktop.
Save rightfold/064abbbd0aa5ae25e816 to your computer and use it in GitHub Desktop.
#!/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