blib/lib/Ubic/PortMap.pm untested

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