| 1 | # | ||
| 2 | # Copyright (C) 1998 Ken MacLeod | ||
| 3 | # Frontier::Daemon is free software; you can redistribute it | ||
| 4 | # and/or modify it under the same terms as Perl itself. | ||
| 5 | # | ||
| 6 | # $Id: Daemon.pm,v 1.5 2001/10/03 01:30:54 kmacleod Exp $ | ||
| 7 | # | ||
| 8 | |||
| 9 | # NOTE: see Net::pRPC for a Perl RPC implementation | ||
| 10 | |||
| 11 | ### | ||
| 12 | ### NOTE: $self is inherited from HTTP::Daemon and the weird access | ||
| 13 | ### comes from there (`${*$self}'). | ||
| 14 | ### | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | package Frontier::Daemon; | ||
| 19 | use vars qw{@ISA}; | ||
| 20 | |||
| 21 | @ISA = qw{HTTP::Daemon}; | ||
| 22 | |||
| 23 | use Frontier::RPC2; | ||
| 24 | use HTTP::Daemon; | ||
| 25 | use HTTP::Status; | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my $class = shift; my %args = @_; | ||
| 29 | my $self = $class->SUPER::new(%args); | ||
| 30 | return undef unless $self; | ||
| 31 | |||
| 32 | ${*$self}{'methods'} = $args{'methods'}; | ||
| 33 | ${*$self}{'decode'} = new Frontier::RPC2 'use_objects' => $args{'use_objects'}; | ||
| 34 | ${*$self}{'response'} = new HTTP::Response 200; | ||
| 35 | ${*$self}{'response'}->header('Content-Type' => 'text/xml'); | ||
| 36 | |||
| 37 | my $conn; | ||
| 38 | while ($conn = $self->accept) { | ||
| 39 | my $rq = $conn->get_request; | ||
| 40 | if ($rq) { | ||
| 41 | if ($rq->method eq 'POST' && $rq->url->path eq '/RPC2') { | ||
| 42 | ${*$self}{'response'}->content(${*$self}{'decode'}->serve($rq->content, ${*$self}{'methods'})); | ||
| 43 | $conn->send_response(${*$self}{'response'}); | ||
| 44 | } else { | ||
| 45 | $conn->send_error(RC_FORBIDDEN); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | $conn->close; | ||
| 49 | $conn = undef; # close connection | ||
| 50 | } | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | Frontier::Daemon - receive Frontier XML RPC requests | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | use Frontier::Daemon; | ||
| 62 | |||
| 63 | Frontier::Daemon->new(methods => { | ||
| 64 | 'rpcName' => \&sub_name, | ||
| 65 | ... | ||
| 66 | }); | ||
| 67 | |||
| 68 | =head1 DESCRIPTION | ||
| 69 | |||
| 70 | I<Frontier::Daemon> is an HTTP/1.1 server that listens on a socket for | ||
| 71 | incoming requests containing Frontier XML RPC2 method calls. | ||
| 72 | I<Frontier::Daemon> is a subclass of I<HTTP::Daemon>, which is a | ||
| 73 | subclass of I<IO::Socket::INET>. | ||
| 74 | |||
| 75 | I<Frontier::Daemon> takes a `C<methods>' parameter, a hash that maps | ||
| 76 | an incoming RPC method name to reference to a subroutine. | ||
| 77 | |||
| 78 | I<Frontier::Daemon> takes a `C<use_objects>' parameter that if set to | ||
| 79 | a non-zero value will convert incoming E<lt>intE<gt>, E<lt>i4E<gt>, | ||
| 80 | E<lt>floatE<gt>, and E<lt>stringE<gt> values to objects instead of | ||
| 81 | scalars. See int(), float(), and string() in Frontier::RPC2 for more | ||
| 82 | details. | ||
| 83 | |||
| 84 | =head1 SEE ALSO | ||
| 85 | |||
| 86 | perl(1), HTTP::Daemon(3), IO::Socket::INET(3), Frontier::RPC2(3) | ||
| 87 | |||
| 88 | <http://www.scripting.com/frontier5/xml/code/rpc.html> | ||
| 89 | |||
| 90 | =head1 AUTHOR | ||
| 91 | |||
| 92 | Ken MacLeod <ken@bitsko.slc.ut.us> | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | 1; |