From 6abac3d4df20329e1f1e8967aa2c097fb9303d61 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Fri, 20 Nov 2015 22:56:02 +0100 Subject: [PATCH] Use socket module to provide an IPC mechanism. Guardian now supports inter-process-communication based on an UNIX socket, which is based on the guardians socket module. All recieved messages via socket automatically will be accepted and parsed. If they are valid, the corresponding events will be enqueued into the main event queue of guardian. Signed-off-by: Stefan Schantl --- guardian | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/guardian b/guardian index 62cb9cf..c0773ee 100644 --- a/guardian +++ b/guardian @@ -26,6 +26,7 @@ use Thread::Queue; use Linux::Inotify2; require Guardian::Parser; +require Guardian::Socket; # Array to store the monitored logfiles. my @monitored_files = ( @@ -68,6 +69,9 @@ while(1) { ## is starting. # sub Init () { + # Setup IPC mechanism via Socket in an own thread. + threads->create(\&Socket); + # Loop through the array of which files should be monitored and # create a worker thread for each single one. foreach my $monitored_file (@monitored_files) { @@ -152,6 +156,49 @@ sub Worker ($) { } } +# +## Socket function. +# +## This function uses the Socket module to create and listen to an UNIX socket. +## It automatically accepts all incomming connections and pass the recieved +## data messages to the the Message_Parser function which is also part of the +## socket module. +# +## If a valid command has been sent through the socket, the corresponding event +## will be enqueued into the shared event queue. +# +sub Socket () { + # Create the Server socket by calling the responsible function. + my $server = &Guardian::Socket::Server(); + + # Accept incomming connections from the socket. + while (my $connection = $server->accept()) { + # Autoflush the socket after the data + # has been recieved. + $connection->autoflush(1); + + # Gather all data from the connection. + while (my $message = <$connection>) { + # Remove any newlines. + chomp($message); + + # Send the recieved data message to the + # socket parser. + my $action = &Guardian::Socket::Message_Parser($message); + + # If the parser returns to perform an action, + # add it to the main event queue. + if ($action) { + # Lock the queue. + lock($queue); + + # Enqueue the returned action. + $queue->enqueue($action); + } + } + } +} + # ## Function for fileposition initialization. # -- 2.39.2