[SCM] libmessage-passing-perl Debian packaging branch, master, updated. debian/0.111-3-14-g44f6e88
Tomas Doran
bobtfish at bobtfish.net
Mon May 6 11:56:57 UTC 2013
The following commit has been merged in the master branch:
commit f884d6a6f57beb329bb79884107c1ba4cf8ff254
Author: Tomas Doran <bobtfish at bobtfish.net>
Date: Mon May 21 10:24:20 2012 +0100
AMQP and STOMP both need managed connections, put it in core
diff --git a/lib/Log/Stash/Role/ConnectionManager.pm b/lib/Log/Stash/Role/ConnectionManager.pm
new file mode 100644
index 0000000..a087a4d
--- /dev/null
+++ b/lib/Log/Stash/Role/ConnectionManager.pm
@@ -0,0 +1,64 @@
+package Log::Stash::Role::ConnectionManager;
+use Moose::Role;
+use Scalar::Util qw/ blessed weaken /;
+use namespace::autoclean;
+
+requires '_build_connection';
+
+sub BUILD {
+ my $self = shift;
+ $self->connection;
+}
+
+has connected => (
+ is => 'ro',
+ isa => 'Bool',
+ default => sub { 0 },
+ writer => '_set_connected',
+);
+
+has connection => (
+ is => 'ro',
+ lazy => 1,
+ builder => '_build_connection',
+ clearer => '_clear_connection'
+);
+
+has connect_subscribers => (
+ isa => 'ArrayRef',
+ is => 'ro',
+ default => sub { [] },
+ writer => '_set_connect_subscribers',
+);
+
+sub __clean_subs {
+ my $self = shift;
+ my $subs = [ grep { defined $_ } @{$self->connect_subscribers} ];
+ $self->_set_connect_subscribers($subs);
+}
+
+sub subscribe_to_connect {
+ my ($self, $subscriber) = @_;
+ confess "Subscriber '$subscriber' is not blessed" unless blessed $subscriber;
+ confess "Subscriber '$subscriber' does not have a ->connected method" unless $subscriber->can('connected');
+ $self->__clean_subs;
+ my $subs = $self->connect_subscribers;
+ push(@$subs, $subscriber);
+ weaken(@{$subs}[-1]);
+ if ($self->connected) {
+ warn "Already connected";
+ $subscriber->connect($self->connection);
+ }
+}
+
+after _set_connected => sub {
+ my ($self, $connected) = @_;
+ $self->__clean_subs;
+ my $method = $connected ? 'connected' : 'disconnected';
+ foreach my $sub (@{$self->connect_subscribers}) {
+ $sub->$method($self->connection) if $sub->can($method);
+ }
+ $self->_clear_connection unless $connected;
+};
+
+1;
diff --git a/lib/Log/Stash/Role/HasAConnection.pm b/lib/Log/Stash/Role/HasAConnection.pm
new file mode 100644
index 0000000..637928e
--- /dev/null
+++ b/lib/Log/Stash/Role/HasAConnection.pm
@@ -0,0 +1,64 @@
+package Log::Stash::Role::HasAConnection;
+use Moose::Role;
+use namespace::autoclean;
+
+requires '_build_connection', 'connected';
+
+has connection_manager => (
+ is => 'ro',
+ lazy => 1,
+ #isa => ->can('subscribe_to_connect')
+ builder => '_build_connection',
+);
+
+sub BUILD {}
+after BUILD => sub {
+ my $self = shift;
+ $self->connection_manager->subscribe_to_connect($self);
+};
+
+1;
+
+=head1 NAME
+
+Log::Stash::Role::HasAConnection - Role for components which have a connection
+
+=head1 DESCRIPTION
+
+Provides a standard ->connection_manager attribute for inputs or outputs which need to
+make a network connection before they can send or receive messages.
+
+The connection manager object is assumed to have the C<< ->subscribe_to_connect >> method
+(from L<Log::Stash::Role::Connection>).
+
+=head1 REQUIRED METHODS
+
+=head2 _build_connection
+
+Will be called at BUILD (i.e. object construction) time, should return
+a connection manager object (i.e. an object that C<< ->subscribe_to_connect >>
+can be called on).
+
+=head2 connected ($client)
+
+Called by the connection manager when a connection is made.
+
+Usually used to do things like subscribe to queues..
+
+=head1 OPTIONAL METHODS
+
+=head2 disconnected ($client)
+
+The client recieved an error or otherwise disconnected.
+
+=head1 ATTRIBUTES
+
+=head2 connection_manager
+
+=head1 WRAPPED METHODS
+
+=head2 BUILD
+
+Is wrapped to build the connection manager object.
+
+=cut
--
libmessage-passing-perl Debian packaging
More information about the Pkg-perl-cvs-commits
mailing list