App-BCVI-NotifyClient

 view release on metacpan or  search on metacpan

lib/App/BCVI/NotifyClient.pm  view on Meta::CPAN

                $self->_notify_wait_for_output(\%opt);
                $self->SUPER::send_command("output received");
                $self->_notify_exit if $opt{once};
                $self->_notify_wait_for_idle(\%opt);
            }
        });
    }
    elsif($opt{kill}) {
        $self->App::BCVI::Server::kill_current_listener();
        exit;
    }
}


sub _notify_wait_for_idle {
    my($self, $opt) = @_;

    my $sleep_time = $opt->{idle};
    while(1) {
        sleep($sleep_time);
        my $mtime = $self->_notify_tty_mtime($opt);
        my $idle = time() - $mtime;
        $sleep_time = $opt->{idle} - $idle;
        return if $sleep_time <= 0;
    }
}


sub _notify_wait_for_output {
    my($self, $opt) = @_;

    my $mtime = $self->_notify_tty_mtime($opt);
    while(1) {
        sleep($opt->{output});
        return if $self->_notify_tty_mtime($opt) != $mtime;
    }
}


sub _notify_tty_mtime {
    my($self, $opt) = @_;

    my @stat = stat($opt->{tty}) or die "stat($opt->{tty}): $!";
    return $stat[9];
}


sub _notify_current_tty {
    my $tty = readlink("/proc/self/fd/0");
    if(!$tty) {
        chomp( $tty = `tty` );
    }
    die "Unable to determine TTY\n" unless $tty;
    return $tty;
}


sub _notify_get_options {
    my($self, @args) = @_;

    local(@ARGV) = @args;
    my %opt = ();
    Getopt::Long::GetOptions(\%opt,
        '--idle|i:i',
        '--output|o:i',
        '--tty|t=s',
        '--once|1',
        '--kill|k',
    ) or exit;

    if(defined($opt{idle})) {
        $opt{idle} ||= $default_idle_time;
    }

    if(defined($opt{output})) {
        $opt{output} ||= $default_poll_interval;
    }

    if(defined($opt{tty}) and ! -e $opt{tty}) {
        die "No such file or device: $opt{tty}\n";
    }

    return %opt;
}


sub _notify_fork_bg_tty_monitor {
    my($self, $monitor_sub) = @_;

    fork() && exit;   # Parent retuns to shell child continues in background
    setsid();

    # We have this code lying around - might as well abuse it
    $self->App::BCVI::Server::save_pid();

    $monitor_sub->();

    exit;  # Shouldn't be reached
}


sub _notify_exit {
    my($self) = @_;

    unlink $self->pid_file;
    exit;
}


sub pid_file {
    my($self) = @_;

    my $path = $self->_notify_current_tty;
    $path =~ s{^.*(?=tty|pty|pts)}{}i;
    $path =~ s{^/}{};
    $path =~ s{\W+}{-}g;

    return File::Spec->catfile($self->conf_directory(), "notify-$path.pid");
}


 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.355 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-48ebf85a1963 )