package Security::Monitoring::Messaging::Messenger; use 5.14.2; use strict; use warnings; use Carp qw(carp croak); use IO::Socket::INET; use IO::Socket::UNIX; =head1 NAME Security::Monitoring::Messaging::Messenger - The great new Security::Monitoring::Messaging::Messenger! =head1 VERSION Version 0.01 =cut our $VERSION = '0.01'; =head1 DESCRIPTION this module provides the messenger class, instances of this class will be used in small daemon to handle message passing between the main units of the program, thus allowing each unit to interact as if on the same machine of each other. the Main Messaging daemon creates smaller daemons when asked to do so (at start time such order will come from the config files, during run time it will listen to a named pipe for new orders) =head1 SUBROUTINES/METHODS =head2 new summons a new instance of the Messenger class =head3 use my %outputs = ( $whatevername => $filehandle, $whatevername2=>$anotherfilehandle, ); my %params = ( input=>$filehandle, output = \%output; ); my $messenger = $class->new(\%params); =cut sub new { my ($class,$params) = @_; my $self = {}; bless $self, $class; if (!defined $params){ croak "no params, cant create new messenger\n"; } $self->_init($params); return $self; } =head2 _init init function for messenger instances =cut sub _init { my($self,$params) = @_; $self->{input}->{type} = $params->{input}->{type}; $self->{input}->{fh} = $params->{input}->{fh}; my @outputs = keys $params->{output}; foreach my $key (@outputs){ $self->{output}->{$key}->{type} = $params->{output}->{$key}->{type}; $self->{output}->{$key}->{fh} = $params->{output}->{$key}->{fh}; } } =head2 _read this function is used by the relay sub to start doing the messaging job =cut sub _read{ my $self = shift; if(!ref $self){ croak "can not do that as a class!"; } my $handle = $self->{input}->{fh}; given ($self->{input}->{type}){ when ('named_pipe'){ local $/; while(<$handle>){ my $output = $_; return $output; } } when('socket'){ my $socket = $handle->accept or croak ("could not accept connection"); local $/; while(<$socket>){ my $output=$_; return $output; } } } } =head2 _write this function is used by the relay sub for the messaging job =cut sub _write{ my $self = shift; if(!ref $self){ croak "can not do that as a class!"; } my $input = shift; my @keys = keys $self->{output}; foreach my $key (@keys){ my $handle = $self->{output}->{$key}->{fh}; if(!defined($handle)){ print "undefined fh for key $key\n"; } print $handle $input; } } =head2 relay this function is called just after forking : it starts the actual messaging, the daemon waits for input and transmit to all its outputs =head3 use $messenger->relay; =cut sub relay{ my $self = shift; if(!ref($self)){ croak "can not call relay from a class"; } $self->_write($self->_read); } 1; # End of Security::Monitoring::Messaging::Messenger