MtPolicyd/Client.pm

filter: Statementstmt 45.0% 18 / 40 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 4 / 8 Podpod 100% 1 / 1 totaltotal 34.8% 23 / 66 CC 8 SCAR 24.7
CC
8
Cov
33%
_connect
20.0
request
20.0
_is_connected
6.0
CRAP 11.9
« MtPolicyd/SessionCache.pm ↑ Summary MtPolicyd/Client/App.pm »
1 package Mail::MtPolicyd::Client;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '2.01'; # VERSION
6 # ABSTRACT: a policyd client class
7  
8  
9 2 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Mail::MtPolicyd::Client::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 has 'socket_path' => ( is => 'rw', isa => 'Maybe[Str]' );
15 2 has 'host' => ( is => 'rw', isa => 'Str', default => 'localhost:12345' );
16 2 has 'keepalive' => ( is => 'rw', isa => 'Bool', default => 0 );
17  
18 2 has '_fh' => ( is => 'rw', isa => 'Maybe[IO::Handle]' );
19  
20 sub _connect {
21 0 ▶ my $self = shift;
Subroutine _connect: not called CC=4 SCAR=30.0
22 0 my $fh;
23 0 ▶ if( defined $self->socket_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->socket_path) { } 0 0 0
24 0 ▶ $fh = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Socket::UNIX"->new("Peer", $self->socket_path, "autoflush", 0) or die 'could not connect to socket: ' . $! 0 0 0
25 Peer => $self->socket_path,
26 autoflush => 0,
27 ) or die('could not connect to socket: '.$!);
28 } else {
29 0 ▶ $fh = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Socket::INET"->new("PeerAddr", $self->host, "Proto", "tcp", "autoflush", 0) or die 'could not connect to host: ' . $! 0 0 0
30 PeerAddr => $self->host,
31 Proto => 'tcp',
32 autoflush => 0,
33 ) or die('could not connect to host: '.$!);
34 }
35 0 $self->_fh( $fh );
36 }
37  
38 sub _disconnect {
39 0 ▶ my $self = shift;
Subroutine _disconnect: not called CC=1 SCAR=6.9
40  
41 0 $self->_fh->close;
42 0 $self->_fh( undef );
43 }
44  
45 sub _is_connected {
46 0 ▶ my $self = shift;
Subroutine _is_connected: not called CC=2 SCAR=17.9
47 0 ▶ if( defined $self->_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_fh) 0 0 0
48 0 return(1);
49 }
50 0 return(0);
51 }
52  
53 sub request {
54 0 ▶ my ( $self, $request ) = @_;
Subroutine request: not called CC=4 SCAR=30.0
Pod documented
55  
56 0 ▶ if( ! $self->_is_connected ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_connected) 0 0 0
57 0 $self->_connect;
58 }
59  
60 0 $self->_fh->print( $request->as_string );
61 0 $self->_fh->flush;
62  
63 0 my $response = Mail::MtPolicyd::Client::Response->new_from_fh( $self->_fh );
64  
65 # close connection we're not doing keepalive
66 # or if the server already closed connection (server side keepalive off)
67 0 ▶ if( ! $self->keepalive || $self->_fh->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->keepalive or $self->_fh->eof) 0 0 0
Condition0%0/3
not $self->keepalive or $self->_fh->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->keepalive or $self->_fh->eof
not $self->keepalive $self->_fh->eof
Truth table0%0/3
not(A) or B
A= not $self->keepaliveB= $self->_fh->eof
ABresult
1X1
011
000
68 0 $self->_disconnect;
69 }
70  
71 0 return $response;
72 }
73  
74 2 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 Mail::MtPolicyd::Client - a policyd client class
85  
86 =head1 VERSION
87  
88 version 2.01
89  
90 =head1 DESCRIPTION
91  
92 Client class to query a policyd server.
93  
94 =head2 SYNOPSIS
95  
96 use Mail::MtPolicyd::Client;
97 use Mail::MtPolicyd::Client::Request;
98  
99 my $client = Mail::MtPolicyd::Client->new(
100 host => 'localhost:12345',
101 keepalive => 1,
102 );
103  
104 my $request = Mail::MtPolicyd::Client::Request->new(
105 'client_address' => '192.168.0.1',
106 );
107  
108 my $response = $client->request( $request );
109 print $response->as_string;
110  
111 =head2 METHODS
112  
113 =over
114  
115 =item request ( $request )
116  
117 Will send a Mail::MtPolicyd::Client::Request to the remote host
118 and return a Mail::MtPolicyd::Client::Response.
119  
120 =back
121  
122 =head2 ATTRIBUTES
123  
124 =over
125  
126 =item socket_path (default: undef)
127  
128 Path of a socket of the policyd server.
129  
130 If defined this socket will be used instead of a tcp connection.
131  
132 =item host (default: localhost:12345)
133  
134 Remote address/port of the policyd server.
135  
136 =item keepalive (default: 0)
137  
138 Keep connection open for multiple requests.
139  
140 =back
141  
142 =head1 AUTHOR
143  
144 Markus Benning <ich@markusbenning.de>
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
149  
150 This is free software, licensed under:
151  
152 The GNU General Public License, Version 2, June 1991
153  
154 =cut
« MtPolicyd/SessionCache.pm ↑ Summary MtPolicyd/Client/App.pm »