I have tested both sessions independently, and they work fine. The
FollowTail watches the file correctly, and its events are working. The TCP
client works fine too, able to connect to the server, receive input from the
server, re-connect when disconnected, and able to send data to the server.
However, making both sessions interacting with each other is proving to be
more difficult that I have anticipated, probably due to my brain's inability
to grasp the POE concepts very well.
In the sub begin_watcher, I create both sessions, the FollowTail and the
Client::TCP
- Why can't I use the "->ID" method on $client_session, the same way I do on
$log_watcher?
In sub current_got_record (called when FollowTail gets new data on the file
watched), I have tried several ways to send the data received to the client
using my Client::TCP session. None of them worked, I have left in comments
what I have tried.
- Any idea how to properly call the clientSend method (sub client_send) so
the FollowTail session can send its output to the server?
Many thanks for your suggestions.
Here is the code below.
#!/usr/local/bin/perl
use warnings;
use strict;
use FileHandle;
use POE qw(Wheel::FollowTail Component::Client::TCP);
my $logfile = "/home/namp/code/client.log";
my $mmsfile = "/namp_log/log/TRmms.log";
my (%wap_logs, %poe_records);
my $PATH = '/namp_log/log';
my $pattern = "mms";
my $MMSC = "10.131.26.164";
my $logPort = "31008";
my $clientID;
my $session_alias;
sub InitWapLog
# Define POE records
{
$poe_records{_start} = \&begin_watchers;
$poe_records{current_record} = \¤t_got_record;
$poe_records{current_log_reset} = \¤t_log_reset;
$poe_records{log_error} = \&generic_log_error;
&Log("POE init complete");
}
sub begin_watchers
# Start POE sessions
{
my $heap = $_[HEAP];
my $logid;
my $log_watcher = POE::Wheel::FollowTail->new
(
Filename => $mmsfile,
InputEvent => "current_record",
ResetEvent => "current_log_reset",
ErrorEvent => "log_error",
);
$logid = $log_watcher->ID;
$heap->{services}->{$logid} = "current";
$heap->{watchers}->{$logid} = $log_watcher;
&Log("Started watch $logid on file $mmsfile");
my $client_session = POE::Component::Client::TCP->new
(
RemoteAddress => $MMSC,
RemotePort => $logPort,
Alias => $session_alias,
ServerInput => \&server_input,
Disconnected => \&handle_disconnect,
ConnectError => \&connect_error,
InlineStates => { clientSend => \&client_send }
);
# $clientID = $session_alias->ID;
# $heap->{client}->{$clientID} = $client_session;
}
sub client_send
# Send to server
{
my ( $heap, $stuff ) = @_[ HEAP, ARG0 ];
&Log("Sending ", $_[SESSION] );
$heap->{client}->put($stuff);
}
sub server_input
# Server spoke
{
my $input = $_[ARG0];
&Log("Client: received $input from server");
}
sub handle_disconnect
# Automatic reconnect
{
$_[KERNEL]->delay(reconnect=>1);
&Log("Reconnecting to server");
}
sub connect_error
# Failure to connect to server
{
&Log("Cannot connect to $MMSC on port $logPort");
}
sub current_log_reset
# Handles log resets for current log, basically do nothing
{
my ($heap, $wheel_id) = @_[HEAP, ARG0];
my $service = $heap->{services}->{$wheel_id};
&Log("POE service $service log reset");
}
sub generic_log_error
# Handles log errors
{
my ($heap, $operation, $errno, $error_string, $wheel_id) = @_[ HEAP,
ARG0, ARG1, ARG2, ARG3 ];
my $service = $heap->{services}->{$wheel_id};
&Log("$service log $operation error $errno: $error_string\n");
&Log("Shutting down $service log watcher.\n");
delete $heap->{services}->{$wheel_id};
delete $heap->{watchers}->{$wheel_id};
}
sub current_got_record
# Handle log events
{
my ($record, $wheel_id, $heap) = @_[ARG0, ARG1, HEAP];
if ($record =~ /$pattern/)
{
my $now = `date +%H:%M:%S`;
chomp($now);
print "[$now]\t$record\n";
# $poe_kernel->post($heap->{client}=>clientSend=>$record);
# $heap->{server}->put($record);
# $poe_kernel->post($heap->{server}=>clientSend=>$record);
# &client_send($heap, $record);
}
}
sub Log
# Write log entries
{
my $text = shift;
my $log = new FileHandle(">>$logfile");
my $now = `date +"%Y%m%d %H:%M:%S"`;
chomp($now);
print $log "[watcher]\t$now\t$text\n";
close($log);
}
# MAIN
&Log("Starting WAP log watch");
&InitWapLog;
POE::Session->create(inline_states => {%poe_records});
$poe_kernel->run();
&Log("All watchers died, exiting");
exit;
--- On Wed, 9/17/08, Emmanuel <gad...@gmail.com> wrote: