Agent
view release on metacpan or search on metacpan
examples/Loop.pa view on Meta::CPAN
#!/usr/bin/perl
##
# The Loop agent - a basic, recursive sample agent.
# Steve Purkis <spurkis@engsoc.carleton.ca>
# Jan 18, 1998.
##
package Agent::Loop;
@ISA = qw( Agent );
sub new {
my ($class, %args) = @_;
my $self = {};
foreach (keys(%args)) { $self->{"$_"} = $args{"$_"}; }
bless $self, $class;
}
sub agent_main {
my ($self, @args) = @_;
# first, get a Transport address:
my $tcp = new Agent::Transport(
Medium => 'TCP',
Cycle => 1
) or die "Couldn't get a tcp transport address: $!!\n";
print "Got tcp address: " . $tcp->address . "\n" if $self->{verbose};
# start the message...
my $msg = new Agent::Message(
Body => [ $tcp->address . "\n", 'hi' ]
);
print "Started Loop agent.\n";
# should we start the loop, or wait for the remote to start?
if ($self->{Tell}) {
print "Initiating loop...\n";
$msg->add_dest( 'TCP', $self->{Tell} ) or die "Error: $!\n";
unless ($msg->send) {
print "Couldn't send message!\n";
return;
}
}
while (1) { # loop till user/remote breaks
my @incoming = $tcp->recv(
Timeout => 120
) or die "Error: $!\n";
chop (my $remote = $incoming[0]);
print "got message from $remote.\n" if $self->{verbose};
$msg->add_dest( 'TCP', $remote) or die "Error: $!\n";
unless ($msg->send) {
print "Remote end stopped receiving\n";
return;
}
}
}
1;
__END__
=head1 NAME
Agent::Loop - a basic, recursive sample agent.
=head1 SYNOPSIS
use Agent;
my $agent = new Agent( Name => 'Loop', %args );
$agent->run;
=head1 DESCRIPTION
( run in 2.812 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )