File Coverage

lib/SMB/Agent.pm
Criterion Covered Total %
statement 21 58 36.2
branch 0 10 0.0
condition 0 11 0.0
subroutine 7 14 50.0
pod 1 6 16.6
total 29 99 29.2


line stmt bran cond sub pod time code
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 .
15              
16             package SMB::Agent;
17              
18 1     1   1237 use strict;
  1         1  
  1         31  
19 1     1   5 use warnings;
  1         1  
  1         23  
20              
21 1     1   865 use parent 'SMB';
  1         303  
  1         4  
22              
23 1     1   518 use SMB::Connection;
  1         3  
  1         31  
24 1     1   658 use SMB::Auth;
  1         3  
  1         29  
25 1     1   809 use IO::Socket;
  1         22074  
  1         5  
26 1     1   1317 use IO::Select;
  1         1571  
  1         534  
27              
28             sub new ($%) {
29 0     0 1   my $class = shift;
30 0           my %options = @_;
31              
32 0   0       $options{quiet} ||= 0;
33 0   0       $options{verbose} ||= 0;
34 0   0       $options{unique_conn_addr} ||= 0;
35              
36 0           my $self = $class->SUPER::new(
37             %options,
38             socket_pool => IO::Select->new,
39             connections => {},
40             );
41              
42 0           return $self;
43             }
44              
45             sub get_connection_key ($$) {
46 0     0 0   my $self = shift;
47 0           my $socket = shift;
48              
49 0 0         return $self->unique_conn_addr
50             ? SMB::Connection->get_socket_addr($socket)
51             : $socket->fileno;
52             }
53              
54             sub get_connection ($$) {
55 0     0 0   my $self = shift;
56 0           my $socket = shift;
57              
58 0 0         my $key = $self->get_connection_key($socket)
59             or return;
60              
61 0           return $self->connections->{$key};
62             }
63              
64             sub add_connection ($$$%) {
65 0     0 0   my $self = shift;
66 0           my $socket = shift;
67 0           my $id = shift;
68 0           my %options = @_;
69              
70 0           my $key = $self->get_connection_key($socket);
71 0 0         return $self->connections->{$key} if $self->connections->{$key};
72              
73 0   0       $options{auth} //= SMB::Auth->new;
74              
75 0 0         my $connection = SMB::Connection->new(
76             $socket, $id,
77             quiet => $self->quiet,
78             verbose => $self->verbose,
79             %options,
80             ) or return;
81 0           $self->socket_pool->add($socket);
82 0           $self->connections->{$key} = $connection;
83              
84 0 0         return wantarray ? ($key, $connection) : $connection;
85             }
86              
87             sub delete_connection ($$) {
88 0     0 0   my $self = shift;
89 0   0       my $connection = shift || return;
90              
91 0           my $socket = $connection->socket;
92 0           $self->socket_pool->remove($socket);
93 0           delete $self->connections->{$self->get_connection_key($socket)};
94 0           $connection->close;
95             }
96              
97             sub delete_all_connections ($$) {
98 0     0 0   my $self = shift;
99              
100 0           for (values %{$self->connections}) {
  0            
101 0           $self->delete_connection($_);
102             }
103             }
104              
105             sub DESTROY ($) {
106 0     0     my $self = shift;
107              
108 0           $self->delete_all_connections;
109             }
110              
111             1;