=begin nd Provided an alternate version of JSON::RPC::Server::Daemon that can be inturpted using signals. =cut package JSON::RPC::Server::AltDaemon; use strict; use JSON::RPC::Server; # for old Perl 5.005 use base qw(JSON::RPC::Server); $JSON::RPC::Server::AltDaemon::VERSION = '0.03'; use Data::Dumper; =begin nd Create a new instance of JSON::RPC::Server::Daemon::Alt @param $class ref Object reference/static class @param %args hash Arguments (timeout => '10') @return undef =cut sub new { my $class = shift; my %args = @_; my $self = $class->SUPER::new(); my $pkg; if( grep { $_ =~ /^SSL_/ } @_ ){ $self->{_daemon_pkg} = $pkg = 'HTTP::Daemon::SSL'; } else{ $self->{_daemon_pkg} = $pkg = 'HTTP::Daemon'; } eval qq| require $pkg; |; if($@){ die $@ } $self->{_daemon} ||= $pkg->new(@_) or die; $self->{__timeout} = delete $args{timeout} || 10; return $self; } =begin nd Starts the server @param $self ref Object reference/static class @param %args hash Arguments (signal => 'INT') @return undef =cut sub handle { my $self = shift; my %args = @_; my $d = $self->{_daemon} ||= $self->{_daemon_pkg}->new(@_) or die; # Localize and set the signal handler as an exit route my @exit_signals; if (exists $args{signal} and $args{signal} ne 'NONE') { @exit_signals = (ref $args{signal}) ? @{$args{signal}} : $args{signal} } else { push @exit_signals, 'INT'; } my $exit_now; local @SIG{@exit_signals} = (sub { $exit_now++;}); my $timeout = $d->timeout(1); while (! $exit_now) { my $c = $d->accept; if ($exit_now) { last; } if (! $c) { next; } $c->timeout($self->timeout); $self->{con} = $c; while (my $r = $c->get_request) { $self->request($r); $self->path_info($r->url->path); $self->SUPER::handle(); last; } $c->close; undef $c; } return; } sub retrieve_json_from_post { return $_[0]->request->content; } sub retrieve_json_from_get { } sub response { my ($self, $response) = @_; $self->{con}->send_response($response); } =begin nd This sets the timeout for processing connections after a new connection has been accepted. It returns the old timeout value. If you pass in no value, it returns the current timeout. @param $self ref Object reference/static class @param $timeout int New timeout value @return integer Current timeout value =cut sub timeout { my ($self, $timeout) = @_; my $old_timeout = $self->{__timeout}; if ($timeout) { $self->{__timeout} = $timeout; } return $old_timeout; } 1;