]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP/Configuration.pm
Merge Alan's NCIP.pm and NCIPResponder.pm.
[working/NCIPServer.git] / lib / NCIP / Configuration.pm
1 package NCIP::Configuration;
2
3 # Copyright 2013 Catalyst IT <chrisc@catalyst.net.nz>
4
5 # This file is part of NCIPServer
6 #
7 # NCIPServer is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # NCIPServer is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with NCIPServer; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
21 =head1 NAME
22   
23   NCIP::Configuration
24
25 =head1 SYNOPSIS
26
27   use NCIP::Configuration;
28   my $config = NCIP::Configuration->new($config_dir);
29
30 =cut
31
32 use Modern::Perl;
33
34 use NCIP::Configuration::Service;
35 use base qw(Config::Merge);
36
37 sub new {
38     my $class = shift;
39     my $self  = $class->SUPER::new(@_);
40     my @services;
41
42     # we might have a few services set them up safely
43     if ( ref( $self->('NCIP.listeners.service') ) eq 'ARRAY' ) {
44         @services = $self->('NCIP.listeners.service');
45     }
46     else {
47         @services = ( $self->('NCIP.listeners')->{'service'} );
48     }
49     my %listeners;
50     foreach my $service (@services) {
51         my $serv_object = NCIP::Configuration::Service->new($service);
52         $listeners{ lc $service->{'port'} } = $serv_object;
53     }
54     $self->{'listeners'} = \%listeners;
55     return $self;
56 }
57
58 =head1 FUNCTIONS
59
60 =head2 find_service
61
62   my $service = $config->($sockaddr, $port, $proto);
63
64   Used to find which service you should be using to answer an incoming request
65
66 =cut
67
68 sub find_service {
69     my ( $self, $sockaddr, $port, $proto ) = @_;
70     my $portstr;
71     foreach my $addr ( '', '*:', "$sockaddr:" ) {
72         $portstr = sprintf( "%s%s/%s", $addr, $port, lc $proto );
73         Sys::Syslog::syslog( "LOG_DEBUG",
74             "Configuration::find_service: Trying $portstr" );
75         last if ( exists( ( $self->{listeners} )->{$portstr} ) );
76     }
77     return $self->{listeners}->{$portstr};
78 }
79 1;
80