1 # File: Stem/TtySock.pm
3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
6 # Stem is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Stem is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Stem; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # For a license to use the Stem under conditions other than those
21 # described here, to purchase support for this software, or to purchase a
22 # commercial warranty contract, please contact Stem Systems at:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
29 package Stem::TtySock ;
48 'default' => 'localhost',
58 my( $class ) = shift ;
60 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
61 return $self unless ref $self ;
63 my $aio = Stem::AsyncIO->new(
67 'write_fh' => \*STDOUT,
68 'read_method' => 'stdin_read',
69 'closed_method' => 'stdin_closed',
72 $self->{'aio'} = $aio ;
74 my $sock_obj = Stem::Socket->new(
76 'host' => $self->{'host'},
77 'port' => $self->{'port'},
78 'server' => $self->{'server'},
81 $self->{'sock_obj'} = $sock_obj ;
83 #Debug "TTYSock new" ;
91 my( $self, $connected_sock ) = @_ ;
93 my( $type, $sock_buf ) ;
96 $self->{'connected'} = 1 ;
97 $self->{'sock'} = $connected_sock ;
99 $type = $self->{'sock_obj'}->type() ;
101 if ( $type eq 'server' ) {
103 $self->{'sock_obj'}->stop_listening() ;
106 $sock_buf = Stem::AsyncIO->new(
109 'fh' => $connected_sock,
110 'read_method' => 'socket_read',
111 'closed_method' => 'socket_closed',
114 $self->{'sock_buf'} = $sock_buf ;
119 my( $self, $data_ref ) = @_ ;
121 $self->{'aio'}->write( $data_ref ) ;
128 $self->{'connected'} = 0 ;
130 $self->{'sock_buf'}->shut_down() ;
132 if ( $self->{'sock_obj'}->type() eq 'server' ) {
134 $self->{'sock_obj'}->start_listening() ;
138 $self->{'sock_obj'}->connect_to() ;
144 my( $self, $data_ref ) = @_ ;
146 unless ( $self->{'connected'} ) {
148 print "TTY::Sock not connected\n" ;
152 $self->{'sock_buf'}->write( $data_ref ) ;
160 # print "stdin closed\n" ;