File Coverage

blib/lib/Log/UDP/Server.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1 2     2   49911 use strict;
  2         4  
  2         64  
2 2     2   12 use warnings;
  2         4  
  2         50  
3 2     2   39 use 5.006; # Found with Perl::MinimumVersion
  2         11  
  2         126  
4              
5             package Log::UDP::Server;
6             BEGIN {
7 2     2   53 $Log::UDP::Server::VERSION = '0.40.0';
8             }
9 2     2   895 use MooseX::POE;
  0            
  0            
10             with 'Data::Serializable' => { -version => '0.40.0' };
11              
12             # ABSTRACT: A simple way to receive and handle structured messages via UDP
13              
14             use IO::Socket::INET ();
15             use Readonly;
16              
17              
18             Readonly::Scalar our $DATAGRAM_MAXLEN => 8192;
19              
20              
21             has 'handler' => (
22             is => 'rw',
23             isa => 'CodeRef',
24             required => 1,
25             );
26              
27              
28             has 'server_address' => (
29             is => 'ro',
30             isa => 'Str',
31             default => sub { '127.0.0.1'; },
32             );
33              
34              
35             has 'server_port' => (
36             is => 'ro',
37             isa => 'Int',
38             default => sub { 9999; },
39             );
40              
41              
42             has 'server_socket' => (
43             is => 'rw',
44             isa => 'IO::Socket::INET',
45             lazy => 1,
46             builder => '_build_server_socket',
47             );
48              
49             sub _build_server_socket { ## no critic qw(Subroutines::ProhibitUnusedPrivateSubroutines)
50             my ($self) = @_;
51              
52             # Create socket
53             my $socket = IO::Socket::INET->new(
54             Proto => 'udp',
55             LocalPort => $self->server_port,
56             LocalAddr => $self->server_address,
57             );
58              
59             # Croak on error
60             unless ( $socket ) {
61             die("Unable to bind to " . $self->server_address . ":" . $self->server_port . ": $!\n");
62             }
63              
64             return $socket;
65             }
66              
67              
68             sub run {
69             POE::Kernel->run();
70             return 1; # OK
71             }
72              
73              
74             sub START {
75             my ($self) = @_;
76             POE::Kernel->select_read( $self->server_socket, "get_datagram" );
77             return 1; # OK
78             }
79              
80              
81             event get_datagram => sub {
82             my ($self) = @_;
83              
84             my $remote_address = recv( $self->server_socket, my $message = "", $DATAGRAM_MAXLEN, 0 );
85             return unless defined $remote_address;
86              
87             my ( $peer_port, $peer_addr ) = IO::Socket::INET::unpack_sockaddr_in($remote_address);
88             my $human_addr = IO::Socket::INET::inet_ntoa($peer_addr);
89              
90             # Deserialize and call handler
91             $self->handler->(
92             $self->deserialize($message)
93             );
94             };
95              
96             1;
97              
98              
99              
100             =pod
101              
102             =encoding utf-8
103              
104             =head1 NAME
105              
106             Log::UDP::Server - A simple way to receive and handle structured messages via UDP
107              
108             =head1 VERSION
109              
110             version 0.40.0
111              
112             =head1 SYNOPSIS
113              
114             use Log::UDP::Server;
115              
116             my $server = Log::UDP::Server->new( handler => sub { warn( $_[0] ); } );
117             $server->run();
118              
119             =head1 DESCRIPTION
120              
121             This module enables you to receive a message (simple string or complicated object)
122             over a UDP socket. An easy way to send a structured message is to use Log::UDP::Client.
123             The message received will automatically be handled by the specified callback.
124              
125             =head1 ATTRIBUTES
126              
127             =head2 handler : CodeRef
128              
129             The handler that is used to process each message as it is received.
130              
131             =head2 server_address : Str
132              
133             The address you want to listen on.
134              
135             =head2 server_port : Int
136              
137             The port you want to listen on.
138              
139             =head2 server_socket : IO::Socket::INET
140              
141             The listening socket used for communication.
142              
143             =head1 METHODS
144              
145             =head2 run
146              
147             Starts the server and listens for incoming datagrams on the specified socket.
148              
149             =head2 START
150              
151             Initializes the C<get_datagram> event on C<server_socket>.
152              
153             =head1 EVENTS
154              
155             =head2 get_datagram
156              
157             Will execute the coderef in C<handler> with the deserialized message as the
158             first argument.
159              
160             =head1 CONSTANTS
161              
162             =head2 $DATAGRAM_MAXLEN : Int
163              
164             Maximum UDP packet size. Set to 8192 bytes.
165              
166             =head1 INHERITED METHODS
167              
168             =over 4
169              
170             =item *
171              
172             deserialize
173              
174             =item *
175              
176             deserializer
177              
178             =item *
179              
180             serialize
181              
182             =item *
183              
184             serializer
185              
186             =item *
187              
188             serializer_module
189              
190             =item *
191              
192             throws_exception
193              
194             =back
195              
196             All of these methods are inherited from L<Data::Serializable>. Read more about them there.
197              
198             =for :stopwords CPAN AnnoCPAN RT CPANTS Kwalitee diff
199              
200             =head1 SUPPORT
201              
202             You can find documentation for this module with the perldoc command.
203              
204             perldoc Log::UDP::Server
205              
206             =head2 Websites
207              
208             =over 4
209              
210             =item *
211              
212             Search CPAN
213              
214             L<http://search.cpan.org/dist/Log-UDP-Server>
215              
216             =item *
217              
218             AnnoCPAN: Annotated CPAN documentation
219              
220             L<http://annocpan.org/dist/Log-UDP-Server>
221              
222             =item *
223              
224             CPAN Ratings
225              
226             L<http://cpanratings.perl.org/d/Log-UDP-Server>
227              
228             =item *
229              
230             CPAN Forum
231              
232             L<http://cpanforum.com/dist/Log-UDP-Server>
233              
234             =item *
235              
236             RT: CPAN's Bug Tracker
237              
238             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-UDP-Server>
239              
240             =item *
241              
242             CPANTS Kwalitee
243              
244             L<http://cpants.perl.org/dist/overview/Log-UDP-Server>
245              
246             =item *
247              
248             CPAN Testers Results
249              
250             L<http://cpantesters.org/distro/L/Log-UDP-Server.html>
251              
252             =item *
253              
254             CPAN Testers Matrix
255              
256             L<http://matrix.cpantesters.org/?dist=Log-UDP-Server>
257              
258             =item *
259              
260             Source Code Repository
261              
262             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
263             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
264             from your repository :)
265              
266             L<git://github.com/robinsmidsrod/Log-UDP-Server.git>
267              
268             =back
269              
270             =head2 Bugs
271              
272             Please report any bugs or feature requests to C<bug-log-udp-server at rt.cpan.org>, or through
273             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-UDP-Server>. I will be
274             notified, and then you'll automatically be notified of progress on your bug as I make changes.
275              
276             =head1 AUTHOR
277              
278             Robin Smidsrød <robin@smidsrod.no>
279              
280             =head1 COPYRIGHT AND LICENSE
281              
282             This software is copyright (c) 2010 by Robin Smidsrød.
283              
284             This is free software; you can redistribute it and/or modify it under
285             the same terms as the Perl 5 programming language system itself.
286              
287             =cut
288              
289              
290             __END__
291