Show
Ignore:
Timestamp:
06/20/08 16:31:36 (5 years ago)
Author:
nothingmuch
Message:

errors and generic handler in example

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/POE-Component-Server-JSONRPC/trunk/ex.pl

    r14244 r14291  
    55 
    66use List::Util (); 
     7use Devel::PartialDump; 
    78 
    89POE::Session->create( 
     
    1011        _start => sub { 
    1112            POE::Component::Server::JSONRPC->new( 
    12                 handler => { 
    13                     'echo'    => 'echo', 
    14                     'sum'     => 'sum', 
    15                     'echo_in' => 'echo_in', 
    16                 }, 
     13                handler => "rpc", 
    1714                tcp => { 
    1815                    Port    => 3000, 
     
    2118            ); 
    2219        }, 
    23         echo_in => \&echo_in, 
    24         echo    => \&echo, 
    25         sum     => \&sum, 
    26         do_callback => sub { eval { $_[ARG0]->() } } 
    2720    }, 
     21    package_states => [ 
     22        __PACKAGE__, { map { $_ => $_ } qw( 
     23            rpc 
     24            do_callback 
     25            echo_in 
     26            echo 
     27            sum 
     28            die 
     29        )}, 
     30    ], 
    2831); 
     32 
     33sub do_callback { 
     34    if ( my $cb = $_[ARG0] ) { 
     35        $cb->(); 
     36    } 
     37} 
     38 
     39sub rpc { 
     40    my ($kernel, $cb, $call) = @_[KERNEL, ARG0, ARG1]; 
     41 
     42    my $method = $call->method; 
     43 
     44    if ( __PACKAGE__->can($method) ) { 
     45        # call as a state, so that delays can be set. 
     46        # ordinarily we could just do eval { $cb->( $self->$method ) } 
     47        unless ( eval { $kernel->call( $_[SESSION], $method, @_[ARG0 .. $#_] ); 1 } ) { 
     48            $cb->( $call->return_error($@) ); 
     49        } 
     50    } else { 
     51        $cb->( $call->return_error( message => sprintf 'No such method "%s"', $method ) ); 
     52    } 
     53} 
     54 
     55sub die { 
     56    my ($cb, $call) = @_[ARG0, ARG1]; 
     57    die { message => Devel::PartialDump::dump($call->params_list) }; 
     58} 
    2959 
    3060sub echo_in {