Net::MessageBus - Pure Perl simple message bus
Version 0.08
This module implements the client side of the Message Bus.
use Net::MessageBus; my $MessageBus = Net::MessageBus->new( server => '127.0.0.1', group => 'backend', sender => 'machine1', username => 'user', password => 'password', logger => $logger_object, blocking => 0, timeout => 0.01 );
On initialization the client authenticates with the Net::MessageBus::Server after which it can start pushing messages to the bus.
In order to receive any messages from the bus the client must subscribe to :
one or more groups
one or more senders
one or more message types
all messages
#each can be called multiple times $MessageBus->subscribe(group => 'test'); $MessageBus->subscribe(sender => 'test_process_1'); $MessageBus->subscribe(type => 'test_message_type'); or $MessageBus->subscribe_to_all();
The client can unsubscribe at any time by calling the unsubscribe method
unsubscribe
$MessageBus->unsubscribe();
To retrive the messages received from the bus, the client can call one of this methods :
my @messages = $MessageBus->pending_messages(); or my $message = $MessageBus->next_message();
use Net::MessageBus; my $MessageBus = Net::MessageBus->new(server => '127.0.0.1', group => 'backend', sender => 'machine1'); $MessageBus->subscribe_to_all(); or $MessageBus->subscribe(group => 'test'); $MessageBus->subscribe(sender => 'test_process_1'); ... my @messages = $MessageBus->pending_messages(); or while (my $message = $MessageBus->next_message()) { print $message->type(); }
Creates a new New::MessageBus object
Arguments
server = The ip address of the server
port = The port on which the server is listening for connections
group = The group to which this client belogs to
sender = A name for the current client
username = User name that will be sent to the server for authentication
password = The password that will be sent to the server for authentication
logger = A object on which we can call the fallowing methods debug,info,warn,error
debug,info,warn,error
block = if we don't have any unread messages from the server we will block until the server sends something. If block is true timeout will be ignored.
timeout = the maximum ammount of time we should wait for a message from the server before returning undef for next_message() or an empty list for pending_messages()
undef
next_message()
pending_messages()
Example
my $MessageBus = Net::MessageBus->new( server => '127.0.0.1', group => 'backend', sender => 'machine1', username => 'user', password => 'password', logger => $logger_object, );
Subscribes the current Net::MessageBus client to the messages from the specified category. It can be called multiple times
Example :
$MessageBus->subscribe(group => 'test'); or $MessageBus->subscribe(sender => 'test_process_1');
Subscribes the current Net::MessageBus client to all the messages the server receives
$MessageBus->subscribe_all;
Unsubscribes current Net::MessageBus client from all the messages it previously subscribed to
Send a new messge to the message queue. It has two forms in which it can be called :
type = The message type
payload = The actual information we want to send with the message. It can be a scalar, array ref or hash ref and it cannot contain any objects
$MessageBus->send( $message ); #message must be a Net::MessageBus::Message object or $MessageBus->send( type => 'alert', payload => { a => 1, b => 2 } );
Returns the next message from the queue of messages we received from the server. The message is a Net::MessageBus::Message object.
Returns all the messages received until now from the server. Each message is a Net::MessageBus::Message object.
Argumens :
force_read_queue = forces a read of everyting the server might have sent and we have't processed yet
Note: Forcing a read of the message queue when block mode is on will block the call until we received something from the server
Getter/Setter for the blocking setting of the client. If set to true, when waiting for server messages, the client will block until it receives something
Examples : my $blocking = $MessageBus->blocking(); or $MessageBus->blocking(1);
Getter/Setter for the timeout when waiting for server messages. It can have subunitary value (eg. 0.01).
Note1 : When blocking is set to a true value, the timeout is ignored Note2 : When timeout is set to 0 the effect is the same as setting blocking to a true value.
Example : my $timeout = $MessageBus->timeout(); or $MessageBus->timeout(0.01);
This methods are for internal use and should not be called manually
Creates a connection to the Net::MessageBus server and authenticates the user
Handles the actual comunication with the server
Sends a authenication request to the server and waits for the response
Returns the response received from the server for the last request
Reads all the messages received from the server and adds the to the internal message queue
Check out Net::MessageBus::Server which implements the server of the MessageBus and Net::MessageBus::Message which is the OO inteface for the messages passwed between the client and the server
Horea Gligan, <gliganh at gmail.com>
<gliganh at gmail.com>
Please report any bugs or feature requests to bug-net-MessageBus at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-MessageBus. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-net-MessageBus at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Net::MessageBus
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-MessageBus
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Net-MessageBus
CPAN Ratings
http://cpanratings.perl.org/d/Net-MessageBus
Search CPAN
http://search.cpan.org/dist/Net-MessageBus/
Thanks to Manol Roujinov for helping to improve this module
Copyright 2012 Horea Gligan.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Net::MessageBus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::MessageBus
CPAN shell
perl -MCPAN -e shell install Net::MessageBus
For more information on module installation, please visit the detailed CPAN module installation guide.