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