blib/lib/SMB/Agent.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 100 SCAR 30.0
CC
4
Cov
0%
add_connection
12.0
get_connection_key
6.0
new
2.0
CRAP 20.0
1 # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::Agent;
17  
18 use strict;
19 use warnings;
20  
21 use parent 'SMB';
22  
23 use SMB::Connection;
24 use SMB::Auth;
25 use IO::Socket;
26 use IO::Select;
27  
28 sub new ($%) {
29 my $class = shift;
30 my %options = @_;
31  
32 $options{quiet} ||= 0;
33 $options{verbose} ||= 0;
34 $options{unique_conn_addr} ||= 0;
35  
36 my $self = $class->SUPER::new(
37 %options,
38 socket_pool => IO::Select->new,
39 connections => {},
40 );
41  
42 return $self;
43 }
44  
45 sub get_connection_key ($$) {
46 my $self = shift;
47 my $socket = shift;
48  
49 return $self->unique_conn_addr
50 ? SMB::Connection->get_socket_addr($socket)
51 : $socket->fileno;
52 }
53  
54 sub get_connection ($$) {
55 my $self = shift;
56 my $socket = shift;
57  
58 my $key = $self->get_connection_key($socket)
59 or return;
60  
61 return $self->connections->{$key};
62 }
63  
64 sub add_connection ($$$%) {
65 my $self = shift;
66 my $socket = shift;
67 my $id = shift;
68 my %options = @_;
69  
70 my $key = $self->get_connection_key($socket);
71 return $self->connections->{$key} if $self->connections->{$key};
72  
73 $options{auth} //= SMB::Auth->new;
74  
75 my $connection = SMB::Connection->new(
76 $socket, $id,
77 quiet => $self->quiet,
78 verbose => $self->verbose,
79 %options,
80 ) or return;
81 $self->socket_pool->add($socket);
82 $self->connections->{$key} = $connection;
83  
84 return wantarray ? ($key, $connection) : $connection;
85 }
86  
87 sub delete_connection ($$) {
88 my $self = shift;
89 my $connection = shift || return;
90  
91 my $socket = $connection->socket;
92 $self->socket_pool->remove($socket);
93 delete $self->connections->{$self->get_connection_key($socket)};
94 $connection->close;
95 }
96  
97 sub delete_all_connections ($$) {
98 my $self = shift;
99  
100 for (values %{$self->connections}) {
101 $self->delete_connection($_);
102 }
103 }
104  
105 sub DESTROY ($) {
106 my $self = shift;
107  
108 $self->delete_all_connections;
109 }
110  
111 1;