UR::Service::RPC::Server - Class for implementing RPC servers


UR documentation Contained in the UR distribution.

Index


Code Index:

NAME

Top

UR::Service::RPC::Server - Class for implementing RPC servers

SYNOPSIS

Top

  my $executer = Some::Exec::Class->create(fh => $fh);

  my $server = UR::Service::RPC::Server->create();
  $server->add_executer($executer);

  $server->loop(5);  # Process messages for 5 seconds

DESCRIPTION

Top

The RPC server implementation isn't fleshed out very well yet, and may change in the future.

METHODS

Top

add_executer
  $server->add_executer($exec);

Incorporate a new UR::Service::RPC::Executer instance to this server. It adds the Executer's filehandle to its own internal IO::Select object.

loop
  $server->loop();

  $server->loop(0);

  $server->loop($timeout);

Enter the Server's event loop for the given number of seconds. If the timeout is undef, it will stay in the loop forever. If the timeout is 0, it will make a single pass though the readable filehandles and call execute on their Executer objects.

If the return value of an Executer's execute method is false, that Executer's file handle is removed from the internal Select object.

SEE ALSO

Top

UR::Service::RPC::Executer, UR::Service::RPC::Message


UR documentation Contained in the UR distribution.

package UR::Service::RPC::Server;

use UR;
use IO::Select;

use strict;
use warnings;

# We're going to be essentially reimplementing an Event queue here. :(

class UR::Service::RPC::Server {
    has => [
        'select' => { is => 'IO::Select' },
        timeout  => { is => 'Float', default_value => undef },
        executers  => { is => 'HASH', doc => 'maps file handles to the UR::Service::RPC::Executer objects we are working with' },
    ], 
};

sub create {
    my($class, %args) = @_;

    unless ($args{'executers'}) {
        $args{'executers'} = {};
    }
    
    unless ($args{'select'}) {
        my @fh = map { $_->fh } values %{$args{'executers'}};
        $args{'select'} = IO::Select->new(@fh);
    }

    my $self = $class->SUPER::create(%args);

    return $self;
}

sub add_executer {
    my($self,$executer,$fh) = @_;

    unless ($fh) {
        if ($executer->can('fh')) {
            $fh = $executer->fh;
        } else {
            $self->error_message("Cannot determine file handle for RPC executer $executer");
            return;
        }
    }

    $self->{'executers'}->{$fh} = $executer;
    $self->select->add($fh);
}

sub loop {
    my $self = shift;

    my $timeout;
    if (@_) {
        $timeout = shift;
    } else {
         $timeout = $self->timeout;
    }

    my @ready = $self->select->can_read($timeout);

    my $count = 0;
    foreach my $fh ( @ready ) {
        my $executer = $self->{'executers'}->{$fh};
        unless ($executer) {
            $self->error_message("Cannot determine RPC executer for file handle $fh fileno ",$fh->fileno);
            return;
        }

        $count++;
        unless ($executer->execute($self) ) {
            # they told us they were done
            $self->select->remove($fh);
            delete $self->{'executers'}->{$fh};
        }
    }

    return $count;
}

1;