AnyEvent::Finger::Server - Simple asynchronous finger server
version 0.14
use AnyEvent::Finger::Server; my $server = AnyEvent::Finger::Server->new; my %users = ( grimlock => "ME GRIMLOCK HAVE PLAN", optimus => "Freedom is the right of all sentient beings.", ); $server->start(sub { my($tx) = @_; if($tx->req->listing_request) { # respond if remote requests list of users $tx->res->say('users:', keys %users); } else { # respond if user exists if(defined $users{$tx->req->username}) { $tx->res->say($users{$tx->req->username}); } # respond if user does not exist else { $tx->res->say('no such user'); } } # required! done generating the reply, # close the connection with the client. $tx->res->done; });
Provide a simple asynchronous finger server.
my $server = AnyEvent::Finger::Server->new(%args);
The constructor takes the following optional arguments:
The hostname to connect to.
The port to connect to.
A callback subref to be called on error (either connection or transmission error). Passes the error string as the first argument to the callback.
A callback subref to be called when the port number is known. This is useful when ephemeral port is used but other parts of the code depend on it. The first argument to the callback will be the AnyEvent::Finger::Server object.
Deny forward requests, (for example: finger@host1@host2@...
style requests). If neither forward_deny
or forward
is specified then forward requests will be passed on to the callback, like all other requests.
Forward forward requests. This can be set to either 1, or an instance of AnyEvent::Finger::Client which will be used to forward requests. If neither forward_deny
or forward
is specified then forward requests will be passed on to the callback, like all other requests.
$server->start( $callback )
Start the finger server. The callback will be called each time a client connects.
$callback->($tx)
The first argument passed to the callback is the transaction object, which is an instance of AnyEvent::Finger::Transaction. The most important members of these objects that you will want to interact with are $tx->req
for the request (an instance of AnyEvent::Finger::Request) and $tx->res
for the response interface (an instance of AnyEvent::Finger::Response).
With the response object you can return a whole response at one time:
$tx->res->say( "this is the first line", "this is the second line", "there will be no forth line", ); $tx->res->done;
or you can send line one at a time as they become available (possibly asynchronously).
# $dbh is a DBI database handle my $sth = $dbh->prepare("select user_name from user_list"); while(my $h = $sth->fetchrow_hashref) { $tx->res->say($h->{user_name}); } $tx->res->done;
The server will unbind from its port and stop if the server object falls out of scope, or if the stop
method (see below) is called.
$server->bindport
The bind port. If port is set to zero in the constructor or on start, then an ephemeral port will be used, and you can get the port number here. This value is not available until the socket has been allocated and bound to a port, so if you need this value after calling start
but before any clients have connected use the on_bind
callback.
$server->stop
Stop the server and unbind to the port.
Graham Ollis <plicease@cpan.org>
This software is copyright (c) 2012-2022 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.