File Coverage

lib/Ubic/PortMap.pm
Criterion Covered Total %
statement 21 55 38.1
branch 0 12 0.0
condition 0 3 0.0
subroutine 7 11 63.6
pod 2 2 100.0
total 30 83 36.1


line stmt bran cond sub pod time code
1             package Ubic::PortMap;
2             $Ubic::PortMap::VERSION = '1.59';
3 1     1   1134 use strict;
  1         2  
  1         23  
4 1     1   3 use warnings;
  1         1  
  1         19  
5              
6             # ABSTRACT: update and read mapping of ports to service names.
7              
8              
9 1     1   3 use Try::Tiny;
  1         1  
  1         40  
10 1     1   4 use Params::Validate qw(:all);
  1         1  
  1         110  
11              
12 1     1   223 use Ubic::Logger;
  1         2  
  1         42  
13 1     1   4 use Ubic::Persistent;
  1         1  
  1         13  
14 1     1   3 use Ubic;
  1         1  
  1         291  
15              
16             sub _portmap_file {
17 0     0     my $ubic_dir = Ubic->get_data_dir;
18 0           my $PORTMAP_FILE = $ubic_dir.'/portmap';
19 0           return $PORTMAP_FILE;
20             }
21              
22             sub update {
23 0     0 1   validate_pos(@_);
24              
25 0           my $portmap = Ubic::Persistent->new(_portmap_file());
26 0           my %port2service;
27              
28             my $process_tree;
29             $process_tree = sub {
30 0   0 0     my $service = shift() || Ubic->root_service;
31 0           for $_ ($service->services) {
32 0 0         if ($_->isa('Ubic::Multiservice')) {
33             # multiservice
34 0           $process_tree->($_);
35             }
36             else {
37             try {
38 0           my $port = $_->port;
39 0 0         if ($port) {
40 0           push @{ $portmap->{$port} }, $_->full_name;
  0            
41             }
42             }
43             catch {
44 0           ERROR $_;
45 0           };
46             }
47             }
48 0           return;
49 0           };
50              
51 0           for (keys %{$portmap}) {
  0            
52 0 0         next unless /^\d+$/;
53 0           delete $portmap->{$_};
54             }
55 0           $process_tree->();
56 0           $portmap->commit();
57 0           undef $portmap; # fighting memory leaks - closure doesn't allow local variable to be destroyed
58              
59 0           return;
60             }
61              
62             sub port2name($) {
63 0     0 1   my ($port) = validate_pos(@_, { regex => qr/^\d+$/ });
64 0           my $portmap = Ubic::Persistent->load(_portmap_file());
65 0 0         return unless $portmap->{$port};
66 0           my @names = @{ $portmap->{$port} };
  0            
67 0           while (my $name = shift @names) {
68 0 0         return $name unless @names; # return last service even if it's disabled
69 0 0         return $name if Ubic->is_enabled($name);
70             }
71             }
72              
73              
74             1;
75              
76             __END__