AnyEvent::mDNS - Multicast DNS in AnyEvent style
use AnyEvent::mDNS; my $cv = AnyEvent->condvar; AnyEvent::mDNS::discover '_http._tcp', $cv; my @services = $cv->recv; for my $service (@_) { warn "Found $service->{name} ($service->{proto}) running on $service->{host}:$service->{port}\n"; }
AnyEvent::mDNS is a multicast DNS resolver using AnyEvent framework.
Run multicast DNS query and receive the services discovered with the callback. The callback is passed with the service as a hash reference with keys: host, port, proto and name.
host
port
proto
name
The UDP socket for the DNS query times out in 3 seconds by default, which you can change with timeout parameter, and all the services found are passed to the callback after the timeout.
timeout
# receive all services in one shot, after 5 sec timeout my $cv = AnyEvent->condvar; AnyEvent::mDNS::discover $proto, timeout => 5, $cv; my @all_services = $cv->recv;
Although the timeout is done in a non-blocking way, you might want to retrieve the service as soon as possible, in which case you specify another callback with the key on_found, then each service will be passed to the callback as it's found.
on_found
# receive service as it's found (faster) AnyEvent::mDNS::discover $proto, on_found => sub { my $service = shift; # ... }, $cv; $cv->recv;
You can obviously write your own AnyEvent timer loop to run this mDNS query from time to time with smart interval (See the Multicast DNS Internet Draft for details), to keep the discovered list up-to-date.
Tatsuhiko Miyagawa <miyagawa@bulknews.net>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AnyEvent::DNS http://files.multicastdns.org/draft-cheshire-dnsext-multicastdns.txt
To install AnyEvent::mDNS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::mDNS
CPAN shell
perl -MCPAN -e shell install AnyEvent::mDNS
For more information on module installation, please visit the detailed CPAN module installation guide.