| 1 | package Mercury::Pattern::Bus; | ||||||||||
| 2 | 6 | our $VERSION = '0.016'; | |||||||||
| 3 | # ABSTRACT: A messaging pattern where all peers share messages | ||||||||||
| 4 | |||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||
| 6 | #pod | ||||||||||
| 7 | #pod =head1 DESCRIPTION | ||||||||||
| 8 | #pod | ||||||||||
| 9 | #pod =head1 SEE ALSO | ||||||||||
| 10 | #pod | ||||||||||
| 11 | #pod =cut | ||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use Mojo::Base '-base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | #pod =attr peers | ||||||||||
| 16 | #pod | ||||||||||
| 17 | #pod The list of peers connected to this bus. | ||||||||||
| 18 | #pod | ||||||||||
| 19 | #pod =cut | ||||||||||
| 20 | |||||||||||
| 21 | 3 | ▶ | has peers => sub { [] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | #pod =method add_peer | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod $pat->add_peer( $tx ) | ||||||||||
| 26 | #pod | ||||||||||
| 27 | #pod Add the given connection as a peer to this bus. | ||||||||||
| 28 | #pod | ||||||||||
| 29 | #pod =cut | ||||||||||
| 30 | |||||||||||
| 31 | 6 | ▶ | sub add_peer { | ||||||||
|
Subroutine
add_peer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 6 | my ( $self, $tx ) = @_; | |||||||||
| 33 | 3 | ▶ | $tx->on( message => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 3 | my ( $tx, $msg ) = @_; | |||||||||
| 35 | 3 | $self->send_message( $msg, $tx ); | |||||||||
| 36 | 6 | } ); | |||||||||
| 37 | 6 | ▶ | $tx->on( finish => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 6 | my ( $tx ) = @_; | |||||||||
| 39 | 6 | $self->remove_peer( $tx ); | |||||||||
| 40 | 6 | } ); | |||||||||
| 41 | 6 | push @{ $self->peers }, $tx; | |||||||||
| 42 | 6 | return; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | #pod =method remove_peer | ||||||||||
| 46 | #pod | ||||||||||
| 47 | #pod Remove the connection from this bus. Called automatically by the C<finish> | ||||||||||
| 48 | #pod handler. | ||||||||||
| 49 | #pod | ||||||||||
| 50 | #pod =cut | ||||||||||
| 51 | |||||||||||
| 52 | 6 | ▶ | sub remove_peer { | ||||||||
|
Subroutine
remove_peer: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 53 | 6 | my ( $self, $tx ) = @_; | |||||||||
| 54 | 6 | my @peers = @{ $self->peers }; | |||||||||
| 55 | 6 | for my $i ( 0.. $#peers ) { | |||||||||
| 56 | 12 | ▶ | if ( $peers[$i] eq $tx ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 6 | splice @peers, $i, 1; | |||||||||
| 58 | 6 | return; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | 0 | return; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | #pod =method send_message | ||||||||||
| 65 | #pod | ||||||||||
| 66 | #pod $pat->send_message( $message, $from ) | ||||||||||
| 67 | #pod | ||||||||||
| 68 | #pod Send a message to all the peers on this bus. If a C<$from> websocket is | ||||||||||
| 69 | #pod specified, will not send to that peer (they should know what they sent). | ||||||||||
| 70 | #pod | ||||||||||
| 71 | #pod =cut | ||||||||||
| 72 | |||||||||||
| 73 | 4 | ▶ | sub send_message { | ||||||||
|
Subroutine
send_message: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 74 | 4 | my ( $self, $msg, $from_tx ) = @_; | |||||||||
| 75 | 4 | my @peers = @{ $self->peers }; | |||||||||
| 76 | 4 | ▶ | if ( $from_tx ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 3 | @peers = grep { $_ ne $from_tx } @peers; | |||||||||
| 78 | } | ||||||||||
| 79 | 4 | $_->send( $msg ) for @peers; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 6 | 1; | |||||||||
| 84 | |||||||||||
| 85 | __END__ | ||||||||||