Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.94 KB | None | 0 0
  1. use Downtime::Monitor;
  2.  
  3. my sub keep-line(Str $line) { $line !~~ /^PING/ }
  4.  
  5. my sub parse-line(Str $line) {
  6.     given $line {
  7.         when /time\=([\d|\.]+)/ { Duration.new($0.Real / 1000) }
  8.         when /timeout/ { "error: timeout" }
  9.         default { "error: $_" }
  10.     }
  11. }
  12.  
  13. class PingMonitor does Monitor {
  14.     has Supply $.events;
  15.     has Str @!args;
  16.     has Proc::Async $!proc;
  17.  
  18.     method new(Str $host, Duration $interval, Duration $timeout) {
  19.         my @args = (
  20.             "-i", $interval.Str,
  21.             "-W", ($timeout * 1000).Str,
  22.             $host,
  23.         );
  24.         self.bless(:@args);
  25.     }
  26.  
  27.     submethod BUILD(:@!args) {
  28.         $!events = Supply.new;
  29.     }
  30.  
  31.     method start {
  32.         $!proc = Proc::Async.new("stdbuf", "-oL", "-eL", "ping", @!args);
  33.         $!proc.stdout.grep(&keep-line).act: { $!events.emit(parse-line($_)) };
  34.         $!proc.start;
  35.     }
  36.  
  37.     method stop {
  38.         $!proc.kill;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement