Agent
view release on metacpan or search on metacpan
package Agent;
use strict;
use UNIVERSAL;
use Class::Tom qw( cc repair );
use Data::Dumper; # for argument passing
use Agent::Message; # load message handling routines
use Agent::Transport; # load the autoloader
use vars qw($VERSION $MAJORVERSION $MINORVERSION $MICROVERSION $thread $Debug);
$MAJORVERSION = '3';
$MINORVERSION = '20';
$MICROVERSION = '00'; # aka patchlevel
# I realize with this scheme it's possible to have conflicting version
# numbers, but CPAN doesn't like tuples. Solution, MINOR < 100. If it
# hits 100 it's prolly time for an increase in MAJOR anyway.
$VERSION = "$MAJORVERSION.$MINORVERSION$MICROVERSION";
BEGIN {
# Check for Thread.pm...
eval "use Thread qw( async );";
if ($@) { $Agent::thread = 0; }
else { $Agent::thread = 1; }
}
sub new {
my ($class, %args) = @_;
my $self = {};
my ($stored, $fh, $name, $code, $cpt, $tom, $method) =
delete @args{'Stored', 'File', 'Name', 'Code', 'Compartment'};
##
# Inherited methods safe for use by agent objects.
##
sub run {
my ($self, %args) = @_;
if (delete $args{Thread}) {
if ($Agent::thread) {
return async { _run($self, %args); };
} else {
print "Threads not available on this system!\n" if $Debug;
}
}
_run($self, %args);
}
sub store {
my $self = shift;
=over 4
=item store()
Returns the agent object in stringified form, suitable for network
transfer or storage.
=item run()
Executes the agent. If the I<Thread> argument is passed and your system has
Thread.pm, run() tries to execute the agent in an asynchronous thread via
Thread's async() command (see the Thread pod for more details). Additional
arguments are passed to the agent being run.
=item identity()
Returns a unique string identifying the agent I<in its present state>.
=back
=head1 SEE ALSO
network to perform some task for a client.]
Prerequisites
-------------
Required Modules:
Class::Tom 2.04+ (3.x not supported!)
MD5
Optional module for *alpha* threads support:
Thread
Platforms:
U*nix & Win32
Mac? OS/2?
Using Agents
------------
Thread.readme view on Meta::CPAN
Thread Suppport
---------------
Agent threads are still considered alpha at the moment. So far, they have
worked for me, but I haven't done anything complicated with them yet. :)
I would _like_ to make sending and recieving messages near-transparent by,
for example, having a queue of incoming/outgoing messages, assigning one
thread to handle all incoming data and another to handle all outgoing.
And I've got some other ideas too, but they are all just dreams at the
moment.
--Steve Purkis
October 5, 1998
examples/Static.pa view on Meta::CPAN
static agent a message containing its store()'d self, which the static agent
then unpacks and executes.
=head1 PARAMETERS
The following arguments may be passed when creating a new static agent:
Address => address to [attempt to] register
Medium => address medium
Cpt => use Safe compartments?
Thread => attempt to run agents in asynchronous threads
verbose => verbose
All arguments are optional. The static agent will try to register
TCP / 127.0.0.1:24368 if no medium/address is passed.
=head2
=head1 NOTES
A static agent will accept messages of the form:
examples/dcalc.pl view on Meta::CPAN
use Agent;
$usage = <<USAGE;
Usage:
perl dcalc.pl [-v] [-t] serv:port host1:port [host2:port ...]
-v = verbose mode
-t = use threads
hostN:port = ip:port of remote hosts (ie: Static agents)
serv:port = server address to bind to
USAGE
# if you want to see lots of meaningless output :-), uncomment these:
#$Agent::Transport::TCP::Debug = 1;
#$Agent::Debug = 1;
#$Class::Tom::debug = 2;
( run in 0.342 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )