Distributed::Process::LocalWorker - a base class for Distributed::Process::Worker when running on the client side.


Distributed-Process documentation Contained in the Distributed-Process distribution.

Index


Code Index:

NAME

Top

Distributed::Process::LocalWorker - a base class for Distributed::Process::Worker when running on the client side.

DESCRIPTION

Top

This class implements the methods declared in D::P::BaseWorker as they should work on the client side.

Methods

time NAME, LIST

Runs the method NAME with LIST as arguments, while measuring its run time. Returns whatever the NAME method returns and appends to the result() a string of the form:

    Time for running NAME: n.nnnnn seconds

reset_result

Empties the stack of results.

result LIST
result

When called with a non-empty LIST of arguments, pushes LIST onto the stack of results. These results are meant to be sent back when the server requests them. Each line in LIST is first prepended with a timestamp of the form YYYYMMDD-HHMMSS).

When called without any arguments, returns the list of results.

synchro TOKEN

Sends the command /synchro TOKEN to the server and waits until the server replies with the same /synchro command before returning. The server should reply only when all the connected clients have sent the same /synchro command. TOKEN is only a identification string, mainly useful for logging purposes.

delay TOKEN

This works much the same way as synchro() but the server replies to each client one after the other, and waiting for some (configurable) time between each one. See Distributed::Process::Master for details.

run_on_server NAME, LIST

Sends a /run_method command to let the server invoke the method named NAME with LIST as argument on its instance of the worker. This instance, running on the server, has its inheritance dynamically changed, so that it derives from D::P::RemoteWorker instead of D::P::LocalWorker. Besides this, all its methods are available.

When developping a subclass to D::P::Worker, one should probably create methods that run on the client and methods that run on the server. It is unlikely to need to run one of the methods on both the server side and the client side.

Attributes

The following list describes the attributes of this class. They must only be accessed through their accessors. When called with an argument, the accessor methods set their attribute's value to that argument and return its former value. When called without arguments, they return the current value.

client

The D::P::Client object which handles the network connection for this worker.

SEE ALSO

Top

Distributed::Process::BaseWorker, Distributed::Process::RemoteWorker, Distributed::Process::Worker

AUTHOR

Top

Cédric Bouvier, <cbouvi@cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-distributed-process@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Distributed-Process documentation Contained in the Distributed-Process distribution.
package Distributed::Process::LocalWorker;

use warnings;
use strict;

use POSIX qw/ strftime /;

use Time::HiRes qw/ gettimeofday tv_interval /;

use Distributed::Process;
use Distributed::Process::BaseWorker;
our @ISA = qw/ Distributed::Process::BaseWorker /;

sub time {

    my $self = shift;
    my $method = shift;

    my $t0 = [ gettimeofday ];
    my @result = ($self->$method(@_));
    my $elapsed = tv_interval $t0;
    $self->result(sprintf "Time for running $method: %.5f seconds", $elapsed);
    @result;
}

sub reset_result {

    my $self = shift;
    $self->{_result} = [];
}

sub result {

    my $self = shift;

    if ( @_ ) {
	INFO "adding '@_' to results";
        my $first = shift @_;
        my $time = strftime "%Y%m%d-%H%M%S", localtime;
	#push @{$self->{_result}}, "$time\t$first", @_;
        #$RESULT_QUEUE->enqueue("$time\t$first", @_);
        push @{$self->{_result}}, "$time\t$first", @_;
	return;
    }
    else {
	INFO "returning results";
        return @{$self->{_result} || []};
    }
}

sub synchro {

    my $self = shift;
    my $token = shift;

    $self->client()->send("/synchro $token");
    $self->client()->wait_for_pattern(qr{^/synchro});
}

sub delay {

    my $self = shift;
    my $token = shift;

    $self->client()->send("/delay $token");
    $self->client()->wait_for_pattern(qr{^/delay});
}

sub run_on_server {

    my $self = shift;
    my $method = shift;

    $self->client()->send("/run_method $method @_");
    $self->client()->wait_for_pattern(qr{^/begin_method_result});
    my @res = $self->client()->wait_for_pattern(qr{^ok});
    pop @res;
    INFO "Returned from server: @res";
    wantarray ? @res : $res[0];
}

foreach my $method ( qw/ client / ) {
    no strict 'refs';
    *$method = sub {
	my $self = shift;
	my $old = $self->{"_$method"};
	$self->{"_$method"} = $_[0] if @_;
	return $old;
    };
}

1; # End of Distributed::Process::LocalWorker