added check for connected when triggered method is called. can't trigger
[urisagit/Stem.git] / lib / Stem / TtySock.pm
1 #  File: Stem/TtySock.pm
2
3 #  This file is part of Stem.
4 #  Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
5
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.
10
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.
15
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
19
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:
23
24 #       Stem Systems, Inc.              781-643-7504
25 #       79 Everett St.                  info@stemsystems.com
26 #       Arlington, MA 02474
27 #       USA
28
29 package Stem::TtySock ;
30
31 use strict ;
32 use Carp ;
33
34 use Stem::AsyncIO ;
35 #use Debug ;
36
37 my $attr_spec = [
38         {
39                 'name'          => 'port',
40                 'default'       => 10_000,
41                 'env'           => 'tty_port',
42                 'help'          => <<HELP,
43 HELP
44         },
45
46         {
47                 'name'          => 'host',
48                 'default'       => 'localhost',
49                 'env'           => 'tty_host',
50                 'help'          => <<HELP,
51 HELP
52         },
53
54 ] ;
55
56 sub new {
57
58         my( $class ) = shift ;
59
60         my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
61         return $self unless ref $self ;
62
63         my $aio = Stem::AsyncIO->new(
64
65                         'object'        => $self,
66                         'read_fh'       => \*STDIN,
67                         'write_fh'      => \*STDOUT,
68                         'read_method'   => 'stdin_read',
69                         'closed_method' => 'stdin_closed',
70         ) ;
71
72         $self->{'aio'} = $aio ;
73
74         my $sock_obj = Stem::Socket->new( 
75                                 'object'        => $self,
76                                 'host'          => $self->{'host'},
77                                 'port'          => $self->{'port'},
78                                 'server'        => $self->{'server'},
79         ) ;
80
81         $self->{'sock_obj'} = $sock_obj ;
82
83 #Debug "TTYSock new" ;
84
85         return( $self ) ;
86 }
87
88
89 sub connected {
90
91         my( $self, $connected_sock ) = @_ ;
92
93         my( $type, $sock_buf ) ;
94
95
96         $self->{'connected'} = 1 ;
97         $self->{'sock'} = $connected_sock ;
98
99         $type = $self->{'sock_obj'}->type() ;
100
101         if ( $type eq 'server' ) {
102
103                 $self->{'sock_obj'}->stop_listening() ;
104         }
105
106         $sock_buf = Stem::AsyncIO->new(
107
108                         'object'        => $self,
109                         'fh'            => $connected_sock,
110                         'read_method'   => 'socket_read',
111                         'closed_method' => 'socket_closed',
112         ) ;
113
114         $self->{'sock_buf'} = $sock_buf ;
115 }
116
117 sub socket_read {
118
119         my( $self, $data_ref ) = @_ ;
120
121         $self->{'aio'}->write( $data_ref ) ;
122 }
123
124 sub socket_closed {
125
126         my( $self ) = @_ ;
127
128         $self->{'connected'} = 0 ;
129
130         $self->{'sock_buf'}->shut_down() ;
131
132         if ( $self->{'sock_obj'}->type() eq 'server' ) {
133
134                 $self->{'sock_obj'}->start_listening() ;
135         }
136         else {
137
138                 $self->{'sock_obj'}->connect_to() ;
139         }
140 }
141
142 sub stdin_read {
143
144         my( $self, $data_ref ) = @_ ;
145
146         unless ( $self->{'connected'} ) {
147
148                 print "TTY::Sock not connected\n" ;
149                 return ;
150         }
151
152         $self->{'sock_buf'}->write( $data_ref ) ;
153 }
154
155 sub stdin_closed {
156
157         my( $self ) = @_ ;
158
159
160 #       print "stdin closed\n" ;
161
162         *STDIN->clearerr() ;
163 }
164
165
166 1 ;