IOMux
view release on metacpan or search on metacpan
examples/echo-daemon.pl view on Meta::CPAN
#!/usr/bin/env perl
# This script can be used as template for daemons using IOMux.
# The code is more verbose than needed in the common case.
#
# Purpose: the daemon returns the text it receives.
# You may run the test with
# ls | netcat localhost 5422 || echo 'not running'
use warnings;
use strict;
use Log::Report;
use Any::Daemon;
#use IOMux::Select;
use IOMux::Poll;
use IOMux::Service::TCP;
use Getopt::Long qw/GetOptions :config no_ignore_case bundling/;
use File::Basename qw/basename/;
#use IO::Socket::SSL; # when SSL is used anywhere
#
## get command-line options
#
my $mode = 0; # increase output
my %os_opts =
( pid_file => basename($0). '.pid' # usually in /var/run
, user => undef
, group => undef
);
my %run_opts =
( background => 1
, max_childs => 1 # all done in 1 task
);
my %net_opts =
( host => 'localhost:5422'
, port => undef
);
GetOptions
'background|bg!' => \$run_opts{background}
, 'group|g=s' => \$os_opts{group}
, 'host|h=s' => \$net_opts{host}
, 'pid-file|p=s' => \$os_opts{pid_file}
, 'port|p=s' => \$net_opts{port}
, 'user|u=s' => \$os_opts{user}
, 'v+' => \$mode # -v -vv -vvv
or exit 1;
unless(defined $net_opts{port})
{ my $port = $net_opts{port} = $1
if $net_opts{host} =~ s/\:([0-9]+)$//;
defined $port or error "no port specified";
}
#
## initialize the daemon activities
#
# From now on, all errors and warnings are also sent to syslog,
# provided by Log::Report. Output still also to the screen.
dispatcher SYSLOG => 'syslog', accept => 'INFO-'
, identity => 'iomux', facility => 'local0';
dispatcher mode => $mode, 'ALL'
if $mode;
# close output to stderr/die/warn when in background
dispatcher close => 'default'
if $run_opts{background};
my $daemon = Any::Daemon->new(%os_opts);
$daemon->run
( child_task => \&run_multiplexer
, %run_opts
( run in 2.472 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )