IO::Lambda::Message - message passing queue
The module implements a generic message passing protocol, and two generic classes that implement the server and the client functionality. The server code is implemented in a simple, blocking fashion, and is expected to be executed remotely. The client API is written in lambda style, where message completion can be asynchronously awaited for. The communication between server and client is done through two file handles of any type ( stream sockets, pipes, etc ).
use IO::Lambda::Message qw(message); lambda { my $messenger = IO::Lambda::Message-> new( \*READER, \*WRITER); context $messenger-> new_message('hello world'); tail { print "response1: @_, "\n"; context $messenger, 'same thing'; message { print "response2: @_, "\n"; undef $messenger; }}}
The message passing protocol featured here is synchronous, which means that any message initiated either by server or client is expected to be replied to. Both server and client can wait for the message reply, but they cannot communicate while waiting.
Messages are prepended with simple header, that is a 8-digit hexadecimal length of the message, and 1 byte with value 0x0A (newline). After the message another 0x0A byte is followed.
The class implements a generic message passing queue, that allows adding asynchronous messages to the queue, and wait for the response.
Constructs a new object of IO::Lambda::Message class, and attaches to $reader and $writer file handles ( which can be the same object, and in which case $writer can be omitted, but only if %options is empty too). Accepted options:
IO::Lambda::Message
$reader
$writer
%options
Custom reader, sysreader by default.
sysreader
Custom writer, syswriter by default.
syswriter
If $reader handle was used (or will be needed to be used) in buffered I/O, its buffer can be passed along to the object.
If set, the object will listen for incoming messages from the server, otherwise it will only initiate outcoming messages. By default set to 0, and the method incoming that handles incoming messages, dies. This functionality is designed for derived classes, not for the caller.
incoming
Registers a new message in the queue. The message must be delivered and replied to no later than $deadline, and returns a lambda that will be ready when the message is responded to. The lambda returns the response or the error.
$deadline
Upon communication error, all queued messages are discarded. Timeout is regarded as a protocol error too, so use the $deadline option with care. That means, as soon the deadline error is fired, communication is no longer possible; the remote will wait for its eventual response to be read by your program, which no longer listens. And if it tries to write to the socket again, the whole thing will deadlock. Consider using other means to wait for the message with a timeout.
Condition version of new_message.
new_message
Cancels all pending messages, stores @reason in the associated lambdas.
@reason
Returns the last protocol handling error. If set, no new messages are allowed to be registered, and listening will fail too.
If set, object is listening for asynchronous events from server.
If set, object is sending messages to the server.
The class implements a simple generic protocol dispatcher, that executes methods of its own class, and returns the results back to the client. The methods have to be defined in a derived class.
Creates a new object that will communicate with clients using given handles, in a blocking fashion.
Starts the message loop
Signals the loop to stop
IO::Lambda::DBI.
Dmitry Karasik, <dmitry@karasik.eu.org>.
To install IO::Lambda, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::Lambda
CPAN shell
perl -MCPAN -e shell install IO::Lambda
For more information on module installation, please visit the detailed CPAN module installation guide.