File Coverage

blib/lib/XAS/Lib/Pipe/Unix.pm
Criterion Covered Total %
statement 12 44 27.2
branch 0 10 0.0
condition n/a
subroutine 4 9 44.4
pod 0 1 0.0
total 16 64 25.0


line stmt bran cond sub pod time code
1             package XAS::Lib::Pipe::Unix;
2              
3             our $VERSION = '0.01';
4              
5 1     1   689 use POE;
  1         1  
  1         4  
6 1     1   203 use POE::Filter::Line;
  1         5  
  1         15  
7 1     1   3 use POE::Wheel::ReadWrite;
  1         1  
  1         31  
8              
9             use XAS::Class
10 1         8 debug => 0,
11             version => $VERSION,
12             base => 'XAS::Base',
13             utils => 'trim dotid',
14             mixins => 'init_pipe _pipe_connect _pipe_input _pipe_output _pipe_error'
15 1     1   4 ;
  1         1  
16              
17             #use Data::Dumper;
18              
19             # ----------------------------------------------------------------------
20             # Public Methods
21             # ----------------------------------------------------------------------
22              
23             # ----------------------------------------------------------------------
24             # Public Events
25             # ----------------------------------------------------------------------
26              
27             # ----------------------------------------------------------------------
28             # Private Events
29             # ----------------------------------------------------------------------
30              
31             sub _pipe_connect {
32 0     0     my ($self) = $_[OBJECT];
33              
34 0           my $alias = $self->alias;
35              
36 0           $self->log->debug("$alias: _pipe_connect()");
37              
38             # Start listening on the pipe.
39              
40 0           $self->{'pipe'} = POE::Wheel::ReadWrite->new(
41             Handle => $self->fifo->open('r+'),
42             Filter => $self->filter,
43             InputEvent => 'pipe_input',
44             ErrorEvent => 'pipe_error'
45             );
46              
47             }
48              
49             sub _pipe_input {
50 0     0     my ($self, $input) = @_[OBJECT,ARG0];
51              
52 0           my $alias = $self->alias;
53              
54 0           $self->log->debug("$alias: _pipe_input()");
55              
56 0           $poe_kernel->post($alias, 'process_input', $input);
57              
58             }
59              
60             sub _pipe_output {
61 0     0     my ($self, $output) = @_[OBJECT,ARG0];
62              
63 0           my @buffer;
64 0           my $alias = $self->alias;
65              
66 0           $self->log->debug("$alias: _pipe_output()");
67              
68 0 0         if (my $wheel = $self->pipe) {
69              
70 0           push(@buffer, $output);
71 0           $wheel->put(@buffer);
72              
73             } else {
74              
75 0           $self->log->error_msg('net_client_nowheel', $alias);
76              
77             }
78              
79             }
80              
81             sub _pipe_error {
82 0     0     my ($self, $syscall, $errnum, $errstr) = @_[OBJECT,ARG0 .. ARG2];
83              
84 0           my $alias = $self->alias;
85              
86 0           $self->log->debug("$alias: _pipe_error()");
87 0           $self->log->debug(sprintf("%s: syscall: %s, errnum: %s, errstr: %s", $alias, $syscall, $errnum, $errstr));
88              
89 0 0         if ($errnum == 0) {
90              
91             # EOF detected.
92              
93 0           $self->log->info_msg('net_client_disconnect', $alias, 'localhost', $self->fifo);
94              
95 0           $poe_kernel->post($alias, 'session_shutdown');
96              
97             } else {
98              
99 0           $poe_kernel->post($alias, 'process_error', $syscall, $errnum, $errstr);
100              
101             }
102              
103             }
104              
105             # ----------------------------------------------------------------------
106             # Private Methods
107             # ----------------------------------------------------------------------
108              
109             sub init_pipe {
110 0     0 0   my $self = shift;
111              
112 0           my $alias = $self->alias;
113 0           my $mkfifo = '/usr/bin/mkfifo -m 666 ';
114              
115 0 0         unless (-p $self->fifo->path) {
116              
117 0 0         system($mkfifo . $self->fifo) && $self->throw_msg(
118             dotid($self->class) . '.nofifo',
119             'net_client_nocreate_fifo',
120             $self->fifo, $!
121             );
122              
123 0           $self->log->info_msg('net_client_create_fifo', $alias, $self->fifo);
124            
125             }
126              
127 0 0         unless (defined($self->filter)) {
128              
129 0           $self->{'filter'} = POE::Filter::Line->new(
130             InputLiteral => $self->eol,
131             OutputLiteral => $self->eol
132             );
133              
134             }
135              
136             }
137              
138             1;
139              
140             __END__
141              
142             =head1 NAME
143              
144             XAS::Lib::Pipe - Interact with named pipes
145              
146             =head1 SYNOPSIS
147              
148             use XAS::Lib::Pipe;
149              
150             my $client = XAS::Lib::Pipe->new(
151             -fifo => File('/var/lib/xas/pipe'),
152             -filter => POE::Filter::Line->new(),
153             -eol => "\n",
154             );
155              
156             $server->run();
157              
158             =head1 DESCRIPTION
159              
160             The module provides a POE based framework for reading and writing to named
161             pipes.
162              
163             =head1 METHODS
164              
165             =head2 new
166              
167             This initializes the module and starts listening on the pipe. The following
168             parametrs are used:
169              
170             =over 4
171              
172             =item B<-alias>
173              
174             The name of the POE session.
175              
176             =item B<-fifo>
177              
178             The name of the pipe to interact with.
179              
180             =item B<-filter>
181              
182             An optional filter to use, defaults to POE::Filter::Line
183              
184             =item B<-eol>
185              
186             An optional EOL, defaults to "\n";
187              
188             =back
189              
190             =head2 process_request($input)
191              
192             This method will process the input from the client. It takes the
193             following parameters:
194              
195             =over 4
196              
197             =item B<$input>
198              
199             The input received from the socket.
200              
201             =back
202              
203             =head2 process_response($output)
204              
205             This method will process the output from the client. It takes the
206             following parameters:
207              
208             =over 4
209              
210             =item B<$output>
211              
212             The output to be sent to the socket.
213              
214             =back
215              
216             =head2 process_errors($error)
217              
218             This method will process the error output from the client. It takes the
219             following parameters:
220              
221             =over 4
222              
223             =item B<$error>
224              
225             The output to be sent to the socket.
226              
227             =back
228              
229             =head1 SEE ALSO
230              
231             =over 4
232              
233             =item L<XAS|XAS>
234              
235             =back
236              
237             =head1 AUTHOR
238              
239             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
240              
241             =head1 COPYRIGHT AND LICENSE
242              
243             Copyright (c) 2012-2016 Kevin L. Esteb
244              
245             This is free software; you can redistribute it and/or modify it under
246             the terms of the Artistic License 2.0. For details, see the full text
247             of the license at http://www.perlfoundation.org/artistic_license_2_0.
248              
249             =cut