APND.pm (969B)
1 package APND::Server; 2 use strict; 3 4 use IPC::Open3; 5 6 sub new { 7 my $class = shift; 8 9 my $self = {}; 10 bless ($self, $class); 11 12 my $socket_path = "apnd_test.socket"; 13 14 my $pid = open3(undef, undef, \*CHLD_ERR, "apnd", "-p", $socket_path); 15 16 $self->{pid} = $pid; 17 $self->{CHLD_ERR} = \*CHLD_ERR; 18 return $self; 19 } 20 21 sub readline { 22 my $self = shift; 23 24 return readline $self->{CHLD_ERR}; 25 } 26 27 sub kill { 28 my $self = shift; 29 30 kill 'TERM', $self->{pid}; 31 waitpid( $self->{pid}, 0 ); 32 } 33 34 1; 35 36 package APND::Socket; 37 use strict; 38 39 use IO::Socket::UNIX; 40 41 sub new { 42 my $class = shift; 43 44 my $self = {}; 45 bless ($self, $class); 46 47 my $socket_path = "apnd_test.socket"; 48 49 my $socket = undef; 50 while (! $socket) { 51 $socket = IO::Socket::UNIX->new( 52 Type => SOCK_STREAM(), 53 Peer => $socket_path 54 ); 55 } 56 die "$socket_path: connect failed: $!\n" unless ($socket); 57 58 $self->{socket} = $socket; 59 return $self; 60 } 61 62 sub write { 63 my ($self, $data) = @_; 64 $self->{socket}->syswrite($data); 65 } 66 67 1;