From 399cb7e0767cc8276468f775e86fa0c49292f0a4 Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Wed, 28 Aug 2013 14:06:32 +1200 Subject: [PATCH] Adding the find service routine and setting up the listeners structure This will allow us to override process_request in the server, and do the appropriate thing --- lib/NCIP/Configuration.pm | 35 +++++++++++++++++++++++++++++++ lib/NCIP/Configuration/Service.pm | 17 +++++++++++++++ t/NCIP_Configuration.t | 4 +++- t/config_sample/NCIP.xml | 7 +++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 lib/NCIP/Configuration/Service.pm diff --git a/lib/NCIP/Configuration.pm b/lib/NCIP/Configuration.pm index 74eee3a..070a2fd 100644 --- a/lib/NCIP/Configuration.pm +++ b/lib/NCIP/Configuration.pm @@ -29,7 +29,42 @@ package NCIP::Configuration; use Modern::Perl; +use NCIP::Configuration::Service; use base qw(Config::Merge); +sub new { + my $class = shift; + my $self = $class->SUPER::new(@_); + my @services; + + # we might have a few services set them up safely + if ( ref( $self->('NCIP.listeners.service') ) eq 'ARRAY' ) { + @services = $self->('NCIP.listeners.service'); + } + else { + @services = ( $self->('NCIP.listeners')->{'service'} ); + } + my %listeners; + foreach my $service (@services) { + my $serv_object = NCIP::Configuration::Service->new($service); + $listeners{ lc $service->{'port'} } = $serv_object; + } + $self->{'listeners'} = \%listeners; + return $self; +} + +sub find_service { + my ( $self, $sockaddr, $port, $proto ) = @_; + my $portstr; + foreach my $addr ( '', '*:', "$sockaddr:" ) { + $portstr = sprintf( "%s%s/%s", $addr, $port, lc $proto ); + + # Sys::Syslog::syslog( "LOG_DEBUG", + # "Configuration::find_service: Trying $portstr" ); + # print "Configuration::find_service: Trying $portstr"; + last if ( exists( ( $self->{listeners} )->{$portstr} ) ); + } + return $self->{listeners}->{$portstr}; +} 1; diff --git a/lib/NCIP/Configuration/Service.pm b/lib/NCIP/Configuration/Service.pm new file mode 100644 index 0000000..cd6e830 --- /dev/null +++ b/lib/NCIP/Configuration/Service.pm @@ -0,0 +1,17 @@ +package NCIP::Configuration::Service; + +use Modern::Perl; + +sub new { + my ($class, $obj) = @_; + my $type = ref($class) || $class; + + if (ref($obj) eq "HASH") { + # Just bless the object + return bless $obj, $type; + } + + return bless {}, $type; +} + +1; diff --git a/t/NCIP_Configuration.t b/t/NCIP_Configuration.t index bba7fce..e01cf56 100644 --- a/t/NCIP_Configuration.t +++ b/t/NCIP_Configuration.t @@ -18,7 +18,7 @@ use strict; use warnings; -use Test::More tests => 4; # last test to print +use Test::More tests => 5; # last test to print use_ok('NCIP::Configuration'); @@ -30,3 +30,5 @@ ok( my $server_params = $config->('NCIP.server-params'), 'Get server-params' ); is( $server_params->{'min_servers'}, 1, 'Do we have a minimum of one server' ); +ok ($config->find_service('127.0.0.1','6001','tcp'),'Testing find_service'); + diff --git a/t/config_sample/NCIP.xml b/t/config_sample/NCIP.xml index 04d6b05..070580d 100644 --- a/t/config_sample/NCIP.xml +++ b/t/config_sample/NCIP.xml @@ -15,6 +15,13 @@ transport="RAW" protocol="NCIP/2.00" timeout="60" /> + + -- 2.43.2