| 1 | package Message::Passing::Role::ConnectionManager; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use MooX::Types::MooseLike::Base qw/ Bool ArrayRef /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Scalar::Util qw/ blessed weaken /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw/ confess /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Message::Passing::Exception::ConnectionDisconnected; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Message::Passing::Exception::ConnectionTimeout; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | requires '_build_connection'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub BUILD { | ||||||||||||||||||||||||||||
| 13 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 2 | $self->connection; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | with qw/ | |||||||||||||||||||||||||||
| 18 | Message::Passing::Role::HasTimeoutAndReconnectAfter | ||||||||||||||||||||||||||||
| 19 | Message::Passing::Role::HasErrorChain | ||||||||||||||||||||||||||||
| 20 | /; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | has _timeout_timer => ( | |||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has connected => ( | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | isa => Bool, | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | writer => '_set_connected', | |||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | has connection => ( | |||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | lazy => 1, | ||||||||||||||||||||||||||||
| 36 | predicate => '_has_connection', | ||||||||||||||||||||||||||||
| 37 | builder => '_build_connection', | ||||||||||||||||||||||||||||
| 38 | clearer => '_clear_connection', | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | after _build_connection => sub { | ||||||||||||||||||||||||||||
| 42 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | weaken($self); | |||||||||||||||||||||||||||
| 44 | 3 | $self->_timeout_timer($self->_build_timeout_timer); | |||||||||||||||||||||||||||
| 45 | 2 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub _build_timeout_timer { | ||||||||||||||||||||||||||||
| 48 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_timeout_timer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 3 | weaken($self); | |||||||||||||||||||||||||||
| 50 | AnyEvent->timer( | ||||||||||||||||||||||||||||
| 51 | after => $self->timeout, | ||||||||||||||||||||||||||||
| 52 | cb => sub { | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | $self->error->consume(Message::Passing::Exception::ConnectionTimeout->new( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | after => $self->timeout, | ||||||||||||||||||||||||||||
| 55 | )); | ||||||||||||||||||||||||||||
| 56 | 1 | $self->_timeout_timer(undef); | |||||||||||||||||||||||||||
| 57 | 1 | $self->_set_connected(0); # Use public API, causing reconnect timer to be built | |||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | 3 | ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub _build_reconnect_timer { | ||||||||||||||||||||||||||||
| 63 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_reconnect_timer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 1 | weaken($self); | |||||||||||||||||||||||||||
| 65 | AnyEvent->timer( | ||||||||||||||||||||||||||||
| 66 | after => $self->reconnect_after, | ||||||||||||||||||||||||||||
| 67 | cb => sub { | ||||||||||||||||||||||||||||
| 68 | # $self->error->consume("Reconnecting to ..."); | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | $self->_timeout_timer(undef); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 1 | $self->connection; # Just rebuild the connection object | |||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||
| 72 | 1 | ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | before _clear_connection => sub { | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | return unless $self->_has_connection; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | $self->_timeout_timer($self->_build_reconnect_timer); | |||||||||||||||||||||||||||
| 79 | 2 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | has _connect_subscribers => ( | ||||||||||||||||||||||||||||
| 82 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 83 | is => 'ro', | ||||||||||||||||||||||||||||
| 84 | 2 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 2 | writer => '_set_connect_subscribers', | |||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub __clean_subs { | ||||||||||||||||||||||||||||
| 89 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__clean_subs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 5 | my $subs = [ grep { weaken($_); defined $_ } @{$self->_connect_subscribers} ]; | |||||||||||||||||||||||||||
| 91 | 5 | $self->_set_connect_subscribers($subs); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub subscribe_to_connect { | ||||||||||||||||||||||||||||
| 95 | 2 | ▶ | my ($self, $subscriber) = @_; | ||||||||||||||||||||||||||
|
Subroutine
subscribe_to_connect: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | confess "Subscriber '$subscriber' is not blessed" unless blessed $subscriber; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | confess "Subscriber '$subscriber' does not have a ->connected method" unless $subscriber->can('connected'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 2 | $self->__clean_subs; | |||||||||||||||||||||||||||
| 99 | 2 | my $subs = $self->_connect_subscribers; | |||||||||||||||||||||||||||
| 100 | 2 | push(@$subs, $subscriber); | |||||||||||||||||||||||||||
| 101 | 2 | ▶ | if ($self->connected) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | $subscriber->connected($self->connection); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | after _set_connected => sub { | ||||||||||||||||||||||||||||
| 107 | 3 | ▶ | my ($self, $connected) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 108 | 3 | $self->__clean_subs; | |||||||||||||||||||||||||||
| 109 | 3 | ▶ | my $method = $connected ? 'connected' : 'disconnected'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 3 | foreach my $sub (@{$self->_connect_subscribers}) { | |||||||||||||||||||||||||||
| 111 | 1 | ▶ | $sub->$method($self->connection) if $sub->can($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 3 | ▶ | $self->_timeout_timer(undef) if $connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | if (!$connected && $self->_has_connection) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $connected and $self->_has_connection
MC/DC50%1/2
not $connected and $self->_has_connection
not $connected
$self->_has_connection
Truth table66%2/3
not(A) and B
A= not $connectedB= $self->_has_connection
| |||||||||||||||||||||||||||||
| 115 | 1 | $self->error->consume(Message::Passing::Exception::ConnectionDisconnected->new); | |||||||||||||||||||||||||||
| 116 | 1 | $self->_clear_connection; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 2 | }; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Message::Passing::Role::ConnectionManager - A simple manager for inputs and outputs that need to make network connections. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | This role is for components which make network connections, and need to handle the connection not starting, | ||||||||||||||||||||||||||||
| 129 | timeouts, disconnects etc. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | It provides a simple abstraction for multiple other classes to be able to use the same connection manager, and | ||||||||||||||||||||||||||||
| 132 | a notifies | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 _build_connection | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Build and return the connection we're managing, start the connection | ||||||||||||||||||||||||||||
| 139 | process. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Your connection should use the API as documented below to achieve notification of connect and disconnect events. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 API FOR CONNECTIONS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head2 _set_connected (1) | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Notify clients that the connection is now ready for use. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 _set_connected (0) | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Notify clients that the connection is no longer ready for use. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Will cause the connection to be terminated and retried. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 API FOR CLIENTS | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | To use a connection manager, you should register yourself like this: | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $manager->subscribe_to_connect($self); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | The manager will call C<< $self->connected($connection) >> and C<< $self->disconnected() >> when appropriate. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | If the manager is already connected when you subscribe, it will immediately call back into your | ||||||||||||||||||||||||||||
| 164 | C<< connected >> method, if it is not already connected then this will happen at a later point | ||||||||||||||||||||||||||||
| 165 | once the connection is established. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | See L<Message::Passing::Role::HasAConnection> for a role to help with dealing with a connection manager. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 connected | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | A Boolean indicating if the connection is currently considered fully connected | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 connection | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | The connection object (if we are connected, or connecting currently) - can | ||||||||||||||||||||||||||||
| 178 | be undefined if we are during a reconnect timeout. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 timeout | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Connections will be timed out and aborted after this time if they haven't | ||||||||||||||||||||||||||||
| 183 | successfully connected. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Defaults to 30s | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 reconnect_after | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | The number of seconds to wait before starting a reconnect after a connection has timed out | ||||||||||||||||||||||||||||
| 190 | or been aborted. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Defaults to 2s | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 METHODS | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 subscribe_to_connect ($subscriber) | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | This is called by your Input or Output, as C<< $self->connection_manager->subscribe_to_connect($self) >>. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | This is done for you by L<Message::Passing::Role::HasAConnection> usually.. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | This arranges to store a weak reference to your component, allowing the | ||||||||||||||||||||||||||||
| 203 | connection manager to call the C<< ->connect >> | ||||||||||||||||||||||||||||
| 204 | or C<< ->disconnect >> methods for any components registered when a connection is established or destroyed. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Note that if the connection manager is already connected, it will B<immediately> call the C<< ->connect >> method. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 SPONSORSHIP | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This module exists due to the wonderful people at Suretec Systems Ltd. | ||||||||||||||||||||||||||||
| 211 | <http://www.suretecsystems.com/> who sponsored its development for its | ||||||||||||||||||||||||||||
| 212 | VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with | ||||||||||||||||||||||||||||
| 213 | the SureVoIP API - | ||||||||||||||||||||||||||||
| 214 | <http://www.surevoip.co.uk/support/wiki/api_documentation> | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | ==head1 AUTHOR, COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | See L<Message::Passing>. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||