File Coverage

blib/lib/Message/Passing/Input/Socket/UDP.pm
Criterion Covered Total %
statement 15 27 55.5
branch n/a
condition n/a
subroutine 5 11 45.4
pod 0 1 0.0
total 20 39 51.2


line stmt bran cond sub pod time code
1             package Message::Passing::Input::Socket::UDP;
2 2     2   2119 use Moo;
  2         6  
  2         15  
3 2     2   653 use AnyEvent;
  2         5  
  2         59  
4 2     2   1150 use AnyEvent::Handle::UDP;
  2         54750  
  2         91  
5 2     2   21 use Scalar::Util qw/ weaken /;
  2         5  
  2         125  
6 2     2   16 use namespace::clean -except => 'meta';
  2         4  
  2         19  
7              
8             with qw/
9             Message::Passing::Role::Input
10             Message::Passing::Role::HasHostnameAndPort
11             Message::Passing::Role::HasErrorChain
12             /;
13              
14             has '+hostname' => (
15             default => sub { 'localhost' },
16             );
17              
18             has '+port' => (
19             required => 1,
20             );
21              
22 0     0     sub _default_port { die "You must supply a port #" }
23              
24             has handle => (
25             is => 'ro',
26             builder => '_build_handle',
27             lazy => 1,
28             );
29              
30             sub BUILD {
31 0     0 0   my $self = shift;
32 0           $self->handle;
33             }
34              
35             sub _send_data {
36 0     0     my ($self, $data, $from_addr) = @_;
37 0           $self->output_to->consume($data);
38             }
39              
40             sub _build_handle {
41 0     0     my $self = shift;
42 0           weaken($self);
43             AnyEvent::Handle::UDP->new(
44             bind => [ $self->hostname, $self->port ],
45             on_recv => sub {
46 0     0     my ($data, $h, $from_addr) = @_;
47             # The output can optionally drop from addr.
48 0           $self->_send_data($data, $from_addr);
49             },
50             on_error => sub {
51 0     0     my ($h, $fatal, $msg) = @_;
52 0           $self->error->consume($msg);
53             },
54 0           );
55             }
56              
57             1;
58              
59             =head1 NAME
60              
61             Message::Passing::Input::Socket::UDP - UDP input
62              
63             =head1 DESCRIPTION
64              
65             An input which gets messages from a UDP network socket using
66             L<AnyEvent::Handle::UDP>.
67              
68             =head1 ATTRIBUTES
69              
70             =head2 hostname
71              
72             The hostname L<AnyEvent::Handle::UDP/new> will bind to.
73              
74             =head2 port
75              
76             The port L<AnyEvent::Handle::UDP/new> will bind to.
77              
78             =head1 SEE ALSO
79              
80             L<Message::Passing>
81              
82             =head1 SPONSORSHIP
83              
84             This module exists due to the wonderful people at Suretec Systems Ltd.
85             <http://www.suretecsystems.com/> who sponsored its development for its
86             VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
87             the SureVoIP API -
88             <http://www.surevoip.co.uk/support/wiki/api_documentation>
89              
90             =head1 AUTHOR, COPYRIGHT AND LICENSE
91              
92             See L<Message::Passing>.
93              
94             =cut
95