Net::muxDaemon -- a generic framework for network daemons.
use Net::muxDaemon;
sub my_cmdprocessor {
return $_[1]."\n";
}
new Net::muxDaemon(PROCESS_COMMAND => \&my_cmdprocessor);
die "Huh? Net::muxDaemons aren't supposed to return!\n$!\n";
Net::muxDaemon implements all the boring, nasty details that go into
making a network daemon. It implements everything about a simple
message-response server except the processing of messages into
responses. This functionality, the guts of the server, is
implemented by the user in a function.
The gist of Net::muxDaemon' usage is simple. See SYNOPSIS for all
the code you would need to implement an echo server. The user's
function will run as a Net::muxDaemon method. It will have all the power
of any of other method with none of the worries.
Note that Net::muxDaemon is really only good for command
processing, almost RPC-like functionality. The same client can't have
more the one message being processed at a time without running some
serious risks. (Actually, the same client could, but not on the
same socket.) These risks can be avoided by designing the protocol
around certain limitations; however, save yourself headaches and
stress by making sure all clients wait for responses after each
command.
The syslog function and $KILLED are exported by default. To
export $LOGTAG and $LOGFACILITY, use the :syslog export tag.
syslog($priority, $format, @args)$Net::muxDaemon::LOGTAG is set to something. This Net::muxDaemon
global is exported, so you can do $LOGTAG = 'myLogLine'; anytime
after useing Net::muxDaemon to start logging. Alternatively,
if you can wait to start syslogging (as is typical if until you launch
the Net::muxDaemon you can likely log to STDERR) you can pass
Net::muxDaemon the LOGTAG option.
Basically, just call it all over the place. Then, if you want to log,
pass Net::muxDaemon a LOGTAG option. Turn logging off from the
command_processor by doing undef($LOGTAG). Turn logging back on by
setting $LOGTAG to something.
Note that if you just want to change your syslog tag, changing $LOGTAG is not enough. Do this:
undef $LOGTAG; syslog(); $LOGTAG = 'newtag';
$LOGTAGsyslog does nothing, otherwise it becomes the
tag used in loglines. May be initialized by the LOGTAG option. See
OPTIONS.
$LOGFACILITYmy($olt) = $LOGTAG; undef $LOGTAG; syslog(); $LOGFACILITY = 'newfacility'; $LOGTAG = $olt;
$KILLED
Net::muxDaemon. Options are passed by name. Never returns.
Net::muxDaemon will
look for a sub named 'process_command', first in the caller's
namespace, then in the Net::muxDaemon namespace. This might be
useful if you want your routine to live in Net::muxDaemon's
namespace, but the benefits of this are obscure. The arguments to this
function will be ($this,$request,$client), where:
$this$request$clientreplyCLOSEBecause perl's anonymous subroutines act as closures with respect to
lexical variables, you could conceivably use them to create your own
static workspace for your command processor. Because Net::muxDaemon
provides you with everything you should need (Ha!), this is is left as
an exercise for the reader.
($ENV{'PWD'} || "/tmp"). This is an
initialize-only option.
$LOGTAG export above.
$LOGFACILITY export above.
Net::muxDaemon, just use HOST and PORT. Takes the form of:
POOL => [ { host => "hostname1",
addr => "1.1.1.1:1234",
sync_to => 10
},
{ host => "hostname2",
port => 1235,
addr => "1.1.1.2",
sync_to => 10
}
]
The contents of {addr} will overwrite those of {host} (and {port}, if
that part of {addr} is supplied.) For Net::muxDaemon, only {host},
{addr}, and {port} are used, and only those of the first locally
bindable spec. However, these keys of the hashes in the aryref are the
same as the Tie::CacheHash option {sync} (See the Tie::CacheHash manpage),
so you may just want to fill it in so you can send it to both modules.
POOL overrides the PORT and HOST options, if present. At
least one of the host specs must be bindable-to. This allows you to
specify more than one server on the same machine. The servers will use
Tie::CacheHash syncing to keep each other up to date with local
changes. Tie::CacheHash syncs on the udp port specified, while
Net::muxDaemon listens on the tcp port of the same number.
These methods are not likely to be called except from the command processing function.
getmore(socket, bytecount, ref_to_scalar)bytecount bytes from socket and places them in the
scalar referenced by ref_to_scalar.
getreq(socket, ref_to_scalar)socket and places it in the scalar
referenced by ref_to_scalar. The request will not be sent to the
command processor after this. (Unless it is pushed into the %ready
attribute manually by the caller.)