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/Plugin/ScoreAction.pm ↑ Summary MtPolicyd/Plugin/DBL.pm »
1 package Mail::MtPolicyd::Client;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.11'; # VERSION
6 # ABSTRACT: a policyd client class
7  
8  
9 1 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Mail::MtPolicyd::Client::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has 'socket_path' => ( is => 'rw', isa => 'Maybe[Str]' );
15 1 has 'host' => ( is => 'rw', isa => 'Str', default => 'localhost:12345' );
16 1 has 'keepalive' => ( is => 'rw', isa => 'Bool', default => 0 );
17  
18 1 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 1 1;
75  
76  
77 __END__
78 =pod
79  
80 =head1 NAME
81  
82 Mail::MtPolicyd::Client - a policyd client class
83  
84 =head1 VERSION
85  
86 version 1.11
87  
88 =head1 DESCRIPTION
89  
90 Client class to query a policyd server.
91  
92 =head2 SYNOPSIS
93  
94 use Mail::MtPolicyd::Client;
95 use Mail::MtPolicyd::Client::Request;
96  
97 my $client = Mail::MtPolicyd::Client->new(
98 host => 'localhost:12345',
99 keepalive => 1,
100 );
101  
102 my $request = Mail::MtPolicyd::Client::Request->new(
103 'client_address' => '192.168.0.1',
104 );
105  
106 my $response = $client->request( $request );
107 print $response->as_string;
108  
109 =head2 METHODS
110  
111 =over
112  
113 =item request ( $request )
114  
115 Will send a Mail::MtPolicyd::Client::Request to the remote host
116 and return a Mail::MtPolicyd::Client::Response.
117  
118 =back
119  
120 =head2 ATTRIBUTES
121  
122 =over
123  
124 =item socket_path (default: undef)
125  
126 Path of a socket of the policyd server.
127  
128 If defined this socket will be used instead of a tcp connection.
129  
130 =item host (default: localhost:12345)
131  
132 Remote address/port of the policyd server.
133  
134 =item keepalive (default: 0)
135  
136 Keep connection open for multiple requests.
137  
138 =back
139  
140 =head1 AUTHOR
141  
142 Markus Benning <ich@markusbenning.de>
143  
144 =head1 COPYRIGHT AND LICENSE
145  
146 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
147  
148 This is free software, licensed under:
149  
150 The GNU General Public License, Version 2, June 1991
151  
152 =cut
153  
« MtPolicyd/Plugin/ScoreAction.pm ↑ Summary MtPolicyd/Plugin/DBL.pm »