1
# Copyright (C) 2010 Sun Microsystems, Inc. All rights reserved.
2
# Use is subject to license terms.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18
package GenTest::IPC::Channel;
31
use constant CHANNEL_IN => 0;
32
use constant CHANNEL_OUT => 1;
33
use constant CHANNEL_PIPE => 2;
34
use constant CHANNEL_EOF => 3;
35
use constant CHANNEL_READER => 4;
40
my $self = $class->SUPER::new({},@_);
42
## open bi-directional pipe
44
$self->[CHANNEL_IN] = IO::Handle->new();
45
$self->[CHANNEL_OUT] = IO::Handle->new();
46
$self->[CHANNEL_PIPE] = IO::Pipe->new($self->[CHANNEL_IN],$self->[CHANNEL_OUT]);
48
$self->[CHANNEL_EOF]= 0;
49
$self->[CHANNEL_READER] = undef;
51
## Turn off buffering of output. Each object is sent as one
53
$self->[CHANNEL_OUT]->autoflush(1);
61
croak "OUT pipe closed" if defined $self->[CHANNEL_READER] and $self->[CHANNEL_READER];
63
## Preliminary save Data::Dumper settings since this is a global setting
64
my $oldindent = $Data::Dumper::Indent;
65
my $oldpurity = $Data::Dumper::Purity;
67
## Make output with no newlines and suitable for eval
68
$Data::Dumper::Indent = 0;
69
$Data::Dumper::Purity = 1;
71
my $msg = Dumper($obj);
73
## Encode newline because that is used as message separator
74
## (readline on the other end)
75
$msg =~ s/\n/&NEWLINE;/g;
77
my $chn = $self->[CHANNEL_OUT];
80
## Reset indent to old value
81
$Data::Dumper::Indent = $oldindent;
82
$Data::Dumper::Purity = $oldpurity;
89
croak "IN pipe closed" if defined $self->[CHANNEL_READER] and !$self->[CHANNEL_READER];
90
## Read until eof or an object that may be evaluated is recieved
91
while (!(defined $obj) and (!$self->[CHANNEL_EOF])) {
92
my $line = readline $self->[CHANNEL_IN];
95
$line =~ s/&NEWLINE;/\n/g;
97
## Turn off strict vars since received message uses variables
104
$self->[CHANNEL_EOF] = eof $self->[CHANNEL_IN];
112
## Readers don't need the output part
113
close $self->[CHANNEL_OUT];
114
$self->[CHANNEL_READER] = 1;
120
## Writers don't need the input part
121
close $self->[CHANNEL_IN];
122
$self->[CHANNEL_READER] = 0;
127
if (not defined $self->[CHANNEL_READER]) {
128
close $self->[CHANNEL_OUT];
129
close $self->[CHANNEL_IN];
130
} elsif ($self->[CHANNEL_READER]) {
131
close $self->[CHANNEL_IN];
133
close $self->[CHANNEL_OUT];
139
return not $self->[CHANNEL_EOF];