summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-03-27 21:41:40 -0500
committerJesse Luehrs <doy@tozt.net>2010-03-27 21:41:40 -0500
commit342c8bb796dee783df194d8d7c7195e7cc21884d (patch)
tree3f878f2922b76f7dcc0250c28ad47902c79c30bf
parent99672ca05fc56ae74979f0f065f40abe34d73504 (diff)
downloadapp-termcast-342c8bb796dee783df194d8d7c7195e7cc21884d.tar.gz
app-termcast-342c8bb796dee783df194d8d7c7195e7cc21884d.zip
some refactoring
-rw-r--r--lib/App/Termcast.pm116
1 files changed, 85 insertions, 31 deletions
diff --git a/lib/App/Termcast.pm b/lib/App/Termcast.pm
index fb01c6a..63b38f5 100644
--- a/lib/App/Termcast.pm
+++ b/lib/App/Termcast.pm
@@ -74,7 +74,15 @@ has _got_winch => (
init_arg => undef,
);
-sub connect {
+has socket => (
+ traits => ['NoGetopt'],
+ is => 'rw',
+ isa => 'IO::Socket::INET',
+ lazy_build => 1,
+ init_arg => undef,
+);
+
+sub _build_socket {
my $self = shift;
my $socket = IO::Socket::INET->new(PeerAddr => $self->host,
PeerPort => $self->port);
@@ -84,39 +92,81 @@ sub connect {
return $socket;
}
-sub run {
+has pty => (
+ traits => ['NoGetopt'],
+ is => 'rw',
+ isa => 'IO::Pty::Easy',
+ lazy_build => 1,
+ init_arg => undef,
+);
+
+sub _build_pty {
my $self = shift;
my @argv = @{ $self->extra_argv };
push @argv, ($ENV{SHELL} || '/bin/sh') if !@argv;
-
- my $socket = $self->connect;
- my $sockfd = fileno($socket);
-
my $pty = IO::Pty::Easy->new(raw => 0);
$pty->spawn(@argv);
- my $ptyfd = fileno($pty);
+ return $pty;
+}
+
+sub _build_select_args {
+ my $self = shift;
+ my $sockfd = fileno($self->socket);
+ my $ptyfd = fileno($self->pty);
+ my $infd = fileno(STDIN);
- my ($rin, $rout) = '';
- vec($rin, fileno(STDIN) ,1) = 1;
- vec($rin, $ptyfd, 1) = 1;
+ my $rin = '';
+ vec($rin, $infd ,1) = 1;
+ vec($rin, $ptyfd, 1) = 1;
vec($rin, $sockfd, 1) = 1;
- my ($win, $wout) = '';
+
+ my $win = '';
vec($win, $sockfd, 1) = 1;
- my ($ein, $eout) = '';
+
+ my $ein = '';
vec($ein, $sockfd, 1) = 1;
+
+ return ($rin, $win, $ein);
+}
+
+sub _socket_ready {
+ my $self = shift;
+ my ($vec) = @_;
+ vec($vec, fileno($self->socket), 1);
+}
+
+sub _pty_ready {
+ my $self = shift;
+ my ($vec) = @_;
+ vec($vec, fileno($self->pty), 1);
+}
+
+sub _in_ready {
+ my $self = shift;
+ my ($vec) = @_;
+ vec($vec, fileno(STDIN), 1);
+}
+
+sub run {
+ my $self = shift;
+
ReadMode 5;
my $guard = Scope::Guard->new(sub { ReadMode 0 });
+
+ my ($rin, $win, $ein) = $self->_build_select_args;
+ my ($rout, $wout, $eout);
+
local $SIG{WINCH} = sub { $self->_got_winch(1) };
while (1) {
- my $ready = select($rout = $rin, undef, $eout = $ein, undef);
- if (vec($eout, $sockfd, 1)) {
+ select($rout = $rin, undef, $eout = $ein, undef);
+
+ if ($self->_socket_ready($eout)) {
Carp::carp("Lost connection to server ($!), reconnecting...");
- $socket = $self->connect;
- vec($rin, $sockfd, 1) = 0;
- $sockfd = fileno($socket);
- vec($rin, $sockfd, 1) = 1;
+ $self->clear_socket;
+ ($rin, $win, $ein) = $self->_build_select_args;
}
- if (vec($rout, fileno(STDIN), 1)) {
+
+ if ($self->_in_ready($rout)) {
my $buf;
sysread STDIN, $buf, 4096;
if (!defined $buf || length $buf == 0) {
@@ -128,10 +178,12 @@ sub run {
unless defined $buf;
last;
}
- $pty->write($buf);
+
+ $self->pty->write($buf);
}
- if (vec($rout, $ptyfd, 1)) {
- my $buf = $pty->read(0);
+
+ if ($self->_pty_ready($rout)) {
+ my $buf = $self->pty->read(0);
if (!defined $buf || length $buf == 0) {
if ($self->_got_winch) {
$self->_got_winch(0);
@@ -141,20 +193,21 @@ sub run {
unless defined $buf;
last;
}
+
syswrite STDOUT, $buf;
- my $ready = select(undef, $wout = $win, undef, $self->timeout);
- if (!$ready) {
+
+ my $ready = select(undef, $wout = $win, $eout = $ein, $self->timeout);
+ if (!$ready || $self->_socket_ready($eout)) {
Carp::carp("Lost connection to server ($!), reconnecting...");
- $socket = $self->connect;
- vec($rin, $sockfd, 1) = 0;
- $sockfd = fileno($socket);
- vec($rin, $sockfd, 1) = 1;
+ $self->clear_socket;
+ ($rin, $win, $ein) = $self->_build_select_args;
}
- $socket->write($buf);
+ $self->socket->write($buf);
}
- if (vec($rout, $sockfd, 1)) {
+
+ if ($self->_socket_ready($rout)) {
my $buf;
- $socket->recv($buf, 4096);
+ $self->socket->recv($buf, 4096);
if (!defined $buf || length $buf == 0) {
if ($self->_got_winch) {
$self->_got_winch(0);
@@ -164,6 +217,7 @@ sub run {
unless defined $buf;
last;
}
+
if ($self->bell_on_watcher) {
# something better to do here?
syswrite STDOUT, "\a";