File Coverage

blib/lib/Tak/Router.pm
Criterion Covered Total %
statement 12 34 35.2
branch 0 8 0.0
condition n/a
subroutine 4 13 30.7
pod 0 7 0.0
total 16 62 25.8


line stmt bran cond sub pod time code
1             package Tak::Router;
2              
3 1     1   463 use Tak::MetaService;
  1         3  
  1         36  
4 1     1   5 use Scalar::Util qw(weaken);
  1         2  
  1         44  
5 1     1   5 use Log::Contextual qw(:log);
  1         2  
  1         3  
6 1     1   1223 use Moo;
  1         2  
  1         4  
7              
8             has services => (is => 'ro', default => sub { {} });
9              
10             sub BUILD {
11 0     0 0   my ($self) = @_;
12 0           $self->register(meta => Tak::MetaService->new(router => $self));
13             }
14              
15             sub start_request {
16 0     0 0   my ($self, $req, $target, @payload) = @_;
17 0 0         return $req->mistake("Reached router with no target")
18             unless $target;
19             return $req->failure("Reached router with invalid target ${target}")
20 0 0         unless my $next = $self->services->{$target};
21 0           $next->start_request($req, @payload);
22             }
23              
24             sub receive {
25 0     0 0   my ($self, $target, @payload) = @_;
26 0 0         return unless $target;
27 0     0     log_debug { "Message received for ${target}" };
  0            
28 0     0     return log_info { "Discarded message to ${target}" }
29 0 0         unless my $next = $self->services->{$target};
30 0           $next->receive(@payload);
31             }
32              
33             sub register {
34 0     0 0   my ($self, $name, $service) = @_;
35 0           $self->services->{$name} = $service;
36             }
37              
38             sub register_weak {
39 0     0 0   my ($self, $name, $service) = @_;
40 0           weaken($self->services->{$name} = $service);
41             }
42              
43             sub deregister {
44 0     0 0   my ($self, $name) = @_;
45 0           delete $self->services->{$name}
46             }
47              
48             sub clone_or_self {
49 0     0 0   my ($self) = @_;
50 0           (ref $self)->new(services => { %{$self->services} });
  0            
51             }
52              
53             1;