File: HasAConnection.pm

package info (click to toggle)
libmessage-passing-perl 0.117-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 544 kB
  • sloc: perl: 2,742; makefile: 2; sh: 1
file content (105 lines) | stat: -rw-r--r-- 2,695 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package Message::Passing::Role::HasAConnection;
use Moo::Role;
use Module::Runtime qw/ require_module /;
use Carp qw/ confess /;
use namespace::clean -except => 'meta';

with qw/
    Message::Passing::Role::HasTimeoutAndReconnectAfter
    Message::Passing::Role::HasErrorChain
/;

# requires qw/ _connection_manager_attributes _connection_manager_class /;
requires 'connected';

has connection_manager => (
    is => 'ro',
    lazy => 1,
#    isa => duck_type([qw/subscribe_to_connect/]),
    builder => '_build_connection_manager',
);

sub _build_connection_manager {
    my $self = shift;
    confess "Cannot auto-build this connection manager"
        unless $self->can('_connection_manager_attributes')
            && $self->can('_connection_manager_class');
    my %attrs = map { $_ => $self->$_ } (@{ $self->_connection_manager_attributes }, qw/timeout reconnect_after error/);
    my $class = $self->_connection_manager_class;
    require_module($class);
    $class->new(%attrs);
}

sub BUILD {}
after BUILD => sub {
    my $self = shift;
    $self->connection_manager->subscribe_to_connect($self);
};

1;

=head1 NAME

Message::Passing::Role::HasAConnection - Role for components which have a connection

=head1 DESCRIPTION

Provides a standard ->connection_manager attribute for inputs or outputs which need to
make a network connection before they can send or receive messages.

The connection manager object is assumed to have the C<< ->subscribe_to_connect >> method
(from L<Message::Passing::Role::Connection>).

=head1 REQUIRED METHODS

=head2 _build_connection_manager

Will be called at BUILD (i.e. object construction) time, should return
a connection manager object (i.e. an object that C<< ->subscribe_to_connect >>
can be called on).

=head2 connected ($client)

Called by the connection manager when a connection is made.

Usually used to do things like subscribe to queues..

=head1 OPTIONAL METHODS

=head2 disconnected ($client)

The client received an error or otherwise disconnected.

=head1 ATTRIBUTES

=head2 connection_manager

Holds the connection manger returned by the C<_build_connection_manager> method.

=head1 WRAPPED METHODS

=head2 BUILD

Is wrapped to build the connection manager object.

=head1 SEE ALSO

=over

=item L<Message::Passing::Role::ConenctionManager>.

=back

=head1 SPONSORSHIP

This module exists due to the wonderful people at Suretec Systems Ltd.
<http://www.suretecsystems.com/> who sponsored its development for its
VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
the SureVoIP API - 
<http://www.surevoip.co.uk/support/wiki/api_documentation>

=head1 AUTHOR, COPYRIGHT AND LICENSE

See L<Message::Passing>.

=cut