| 1 | package Lim::RPC::Transports; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Log::Log4perl (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Module::Find qw(findsubmod); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Lim (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =encoding utf8 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Lim::RPC::Transports - Lim's RPC transport loader and container | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | See L<Lim> for version. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 10 | our $VERSION = $Lim::VERSION; | |||||||||||||||||||||||||||
| 25 | 10 | our $INSTANCE; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use Lim::RPC::Transports; | ||||||||||||||||||||||||||||
| 30 | $transport = Lim::RPC::Transports->instance->transport('name'); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =over 4 | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | sub _new { | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 39 | 4 | my $this = shift; | |||||||||||||||||||||||||||
| 40 | 4 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||
| 41 | 4 | my %args = ( @_ ); | |||||||||||||||||||||||||||
| 42 | 4 | my $self = { | |||||||||||||||||||||||||||
| 43 | logger => Log::Log4perl->get_logger($class), | ||||||||||||||||||||||||||||
| 44 | transport => {}, | ||||||||||||||||||||||||||||
| 45 | transport_name => {} | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 48 | 4 | weaken($self->{logger}); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | $self->load; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 4 | $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 57 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 58 | 4 | ▶ | Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | delete $self->{transport}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 10 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 10 | undef($INSTANCE); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item $instance = Lim::RPC::Transports->instance | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Returns the singelton instance of this class. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | sub instance { | ||||||||||||||||||||||||||
|
Subroutine
instance: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 4 | ▶ | $INSTANCE ||= Lim::RPC::Transports->_new; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Lim::RPC::Transports::INSTANCE ||= "Lim::RPC::Transports"->_new
MC/DC0%0/2
$Lim::RPC::Transports::INSTANCE ||= "Lim::RPC::Transports"->_new
$Lim::RPC::Transports::INSTANCE
"Lim::RPC::Transports"->_new
Truth table33%1/3
A ||= B
A= $Lim::RPC::Transports::INSTANCEB= "Lim::RPC::Transports"->_new
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =item $instance->load | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Loads all classes that exists on the system under Lim::RPC::Transport::. Returns | ||||||||||||||||||||||||||||
| 80 | the reference to itself even on error. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=14 SCAR=35.8
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | foreach my $module (findsubmod Lim::RPC::Transport) { | |||||||||||||||||||||||||||
| 88 | 20 | ▶ | if ($module =~ /::Clients?$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 8 | next; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 12 | ▶ | if (exists $self->{transport}->{$module}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | Lim::WARN and $self->{logger}->warn('Transport ', $module, ' already loaded'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | next; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 12 | ▶ | if ($module =~ /^([\w:]+)$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 12 | $module = $1; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 0 | next; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 12 | my $name; | |||||||||||||||||||||||||||
| 104 | 12 | eval { | |||||||||||||||||||||||||||
| 105 | 12 | eval "require $module;"; | |||||||||||||||||||||||||||
| 106 | 12 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 12 | $name = $module->name; | |||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 12 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | Lim::WARN and $self->{logger}->warn('Unable to load transport ', $module, ': ', $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $self->{transport}->{$module} = { | |||||||||||||||||||||||||||
| 113 | name => $name, | ||||||||||||||||||||||||||||
| 114 | module => $module, | ||||||||||||||||||||||||||||
| 115 | loaded => 0, | ||||||||||||||||||||||||||||
| 116 | error => $@ | ||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 0 | next; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 12 | ▶ | unless ($name =~ /^[a-z0-9_\-\.]+$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | Lim::WARN and $self->{logger}->warn('Unable to load transport ', $module, ': Illegal characters in transport name'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->{transport}->{$module} = { | |||||||||||||||||||||||||||
| 124 | module => $module, | ||||||||||||||||||||||||||||
| 125 | loaded => 0, | ||||||||||||||||||||||||||||
| 126 | error => 'Illegal characters in transport name' | ||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||
| 128 | 0 | next; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 12 | ▶ | if (exists $self->{transport_name}->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | Lim::WARN and $self->{logger}->warn('Transport name ', $name, ' already loaded by module ', $self->{transport_name}->{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | next; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 12 | ▶ | Lim::DEBUG and $self->{logger}->debug('Loaded ', $module); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 12 | $self->{transport}->{$module} = { | |||||||||||||||||||||||||||
| 138 | name => $name, | ||||||||||||||||||||||||||||
| 139 | module => $module, | ||||||||||||||||||||||||||||
| 140 | version => $module->VERSION, | ||||||||||||||||||||||||||||
| 141 | loaded => 1 | ||||||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||||||
| 143 | 12 | $self->{transport_name}->{$name} = $module; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 4 | $self; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item $transport = $instance->transport($name, ...) | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | sub transport { | ||||||||||||||||||||||||||
|
Subroutine
transport: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 155 | 4 | my $name = shift; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 4 | ▶ | if (defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 4 | my $module; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 4 | foreach (keys %{$self->{transport}}) { | |||||||||||||||||||||||||||
| 161 | 10 | ▶ | if ($self->{transport}->{$_}->{loaded} and $self->{transport}->{$_}->{name} eq $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name
MC/DC50%1/2
$self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name
$self->{"transport"}{$_}{"loaded"}
$self->{"transport"}{$_}{"name"} eq $name
Truth table66%2/3
A and B
A= $self->{"transport"}{$_}{"loaded"}B= $self->{"transport"}{$_}{"name"} eq $name
| |||||||||||||||||||||||||||||
| 162 | 4 | $module = $self->{transport}->{$_}->{module}; | |||||||||||||||||||||||||||
| 163 | 4 | last; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | ▶ | if (defined $module) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 4 | my $transport; | |||||||||||||||||||||||||||
| 169 | 4 | eval { | |||||||||||||||||||||||||||
| 170 | 4 | $transport = $module->new(@_); | |||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||
| 172 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | Lim::WARN and $self->{logger}->warn('Unable to create new instance of transport ', $name, '(', $module, '): ', $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||
| 176 | 4 | return $transport; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | return; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =back | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 BUGS | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | perldoc Lim::RPC::Transports | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | You can also look for information at: | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =over 4 | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =item * Lim issue tracker (report bugs here) | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | L<https://github.com/jelu/lim/issues> | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =back | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Copyright 2012-2013 Jerry Lundström. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 216 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 217 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 10 | 1; # End of Lim::RPC::Transports | |||||||||||||||||||||||||||