The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

App::Chart::Glib::Ex::DirBroadcast -- broadcast messages through a directory of named pipes

SYNOPSIS

 use App::Chart::Glib::Ex::DirBroadcast;
 App::Chart::Glib::Ex::DirBroadcast->directory ('/my/directory');
 App::Chart::Glib::Ex::DirBroadcast->listen;

 App::Chart::Glib::Ex::DirBroadcast->connect ('my-key', sub { print @_; });

 App::Chart::Glib::Ex::DirBroadcast->send ('my-key', "hello\n");

DESCRIPTION

DirBroadcast is a message broadcasting system based on named pipes in a given directory, with a Glib main loop IO watch listening and calling connected handlers. It's intended for use between multiple running copies of a single application so they can notify each other of changes to files etc.

Messages have a string "key" which is a name or type decided by the application, and then any parameters which Storable can handle (Storable). You can have either a single broadcast directory used for all purposes, or create multiple DirBroadcast objects. The method functions described below take either the class name App::Chart::Glib::Ex::DirBroadcast for the single global, or a DirBroadcast object.

FUNCTIONS

App::Chart::Glib::Ex::DirBroadcast->new ($directory)

Create and return a new DirBroadcast object communicating through the given $directory. $directory is created if it doesn't already exist (with a croak if that fails).

    my $dirb = App::Chart::Glib::Ex::DirBroadcast->new ('/var/run/myapp')
App::Chart::Glib::Ex::DirBroadcast->directory ($directory)
App::Chart::Glib::Ex::DirBroadcast->directory ()
$dirb->directory ($directory)
$dirb->directory ()

Get or set the filesystem directory used for broadcasts.

App::Chart::Glib::Ex::DirBroadcast->send ($key, $data, ...)
App::Chart::Glib::Ex::DirBroadcast->send_locally ($key, $data, ...)
$dirb->send ($key, $data, ...)
$dirb->send_locally ($key, $data, ...)

Send a message of $key and optional $data values. send broadcasts to all processes, including the current process, or send_locally just to the current process.

A send within the current process just means direct calls to functions registered by connect below. This takes place immediately within the send or send_locally, there's no queuing and the current process doesn't have to have a listen active.

The data values can be anything Storable can freeze (see Storable). For send_locally there's no copying, the values are simply passed to the connected functions, so the values can be anything at all.

App::Chart::Glib::Ex::DirBroadcast->listen ()
$dirb->listen ()

Create a named pipe in the broadcast directory to receive messages from other processes, and setup a Glib::IO->add_watch to call the functions registered with connect when a message is received.

App::Chart::Glib::Ex::DirBroadcast->connect ($key, $subr)
$dirb->connect ($key, $subr)

Connect coderef $subr to be called for messages of $key. The arguments to $subr are the data values passed to send.

App::Chart::Glib::Ex::DirBroadcast->connect_for_object ($key, $objsubr, $obj)
$dirb->connect_for_object ($key, $osubr, $obj)

Connect coderef $osubr to be called for notifications of $key, for as long as Perl object $obj exists. $obj is the first argument in each call, followed by the notify data,

    sub my_func {
      my ($obj, $data...) = @_;
    }

If $obj is destroyed then $osubr is no longer called. Only a weak reference to $obj is kept, so just because it wants to hear about some notifications it won't keep it alive forever.

SEE ALSO

Glib, Glib::MainLoop