Adding the find service routine and setting up the listeners structure
authorChris Cormack <chrisc@catalyst.net.nz>
Wed, 28 Aug 2013 02:06:32 +0000 (14:06 +1200)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 28 Aug 2013 02:06:32 +0000 (14:06 +1200)
This will allow us to override process_request in the server, and do
the appropriate thing

lib/NCIP/Configuration.pm
lib/NCIP/Configuration/Service.pm [new file with mode: 0644]
t/NCIP_Configuration.t
t/config_sample/NCIP.xml

index 74eee3a..070a2fd 100644 (file)
@@ -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 (file)
index 0000000..cd6e830
--- /dev/null
@@ -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;
index bba7fce..e01cf56 100644 (file)
@@ -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');
+
index 04d6b05..070580d 100644 (file)
       transport="RAW"
       protocol="NCIP/2.00"
       timeout="60" />
+
+<!--    <service
+      port="8023/tcp"
+      transport="RAW"
+      protocol="NCIP/2.00"
+      timeout="60" />
+-->
   </listeners>
 
   <agencies>