Bug 15253: Add Koha::Logger based logging for SIP2
[koha-equinox.git] / C4 / SIP / Sip / Configuration.pm
1 #
2 # parse-config: Parse an XML-format
3 # ACS configuration file and build the configuration
4 # structure.
5 #
6
7 package C4::SIP::Sip::Configuration;
8
9 use strict;
10 use warnings;
11 use XML::Simple qw(:strict);
12
13 use C4::SIP::Sip qw(syslog);
14
15 my $parser = new XML::Simple(
16     KeyAttr => {
17         login       => '+id',
18         institution => '+id',
19         service     => '+port'
20     },
21     GroupTags => {
22         listeners    => 'service',
23         accounts     => 'login',
24         institutions => 'institution',
25     },
26     ForceArray => [ 'service', 'login', 'institution' ],
27     ValueAttr  => {
28         'error-detect' => 'enabled',
29         'min_servers'  => 'value',
30         'max_servers'  => 'value'
31     }
32 );
33
34 sub new {
35     my ( $class, $config_file ) = @_;
36     my $cfg = $parser->XMLin($config_file);
37     my %listeners;
38
39     # The key to the listeners hash is the 'port' component of the
40     # configuration, which is of the form '[host]:[port]/proto', and
41     # the 'proto' component could be upper-, lower-, or mixed-cased.
42     # Regularize it here to lower-case, and then do the same below in
43     # find_server() when building the keys to search the hash.
44
45     foreach my $service ( values %{ $cfg->{listeners} } ) {
46         $listeners{ lc $service->{port} } = $service;
47     }
48     $cfg->{listeners} = \%listeners;
49
50     return bless $cfg, $class;
51 }
52
53 sub error_detect {
54     my $self = shift;
55     return $self->{'error-detect'};
56 }
57
58 sub accounts {
59     my $self = shift;
60     return values %{ $self->{accounts} };
61 }
62
63 sub find_service {
64     my ( $self, $sockaddr, $port, $proto ) = @_;
65     my $portstr;
66     foreach my $addr ( '', '*:', "$sockaddr:", "[$sockaddr]:" ) {
67         $portstr = sprintf( "%s%s/%s", $addr, $port, lc $proto );
68         syslog( "LOG_DEBUG",
69             "Configuration::find_service: Trying $portstr" );
70         last if ( exists( ( $self->{listeners} )->{$portstr} ) );
71     }
72     return $self->{listeners}->{$portstr};
73 }
74
75 1;