line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mercury::Pattern::Bus; |
2
|
|
|
|
|
|
|
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
|
2
|
|
|
2
|
|
11
|
use Mojo::Base '-base'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
13
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#pod =attr peers |
16
|
|
|
|
|
|
|
#pod |
17
|
|
|
|
|
|
|
#pod The list of peers connected to this bus. |
18
|
|
|
|
|
|
|
#pod |
19
|
|
|
|
|
|
|
#pod =cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has peers => sub { [] }; |
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
|
|
|
|
|
|
|
sub add_peer { |
32
|
6
|
|
|
6
|
1
|
24
|
my ( $self, $tx ) = @_; |
33
|
|
|
|
|
|
|
$tx->on( message => sub { |
34
|
3
|
|
|
3
|
|
18811
|
my ( $tx, $msg ) = @_; |
35
|
3
|
|
|
|
|
8
|
$self->send_message( $msg, $tx ); |
36
|
6
|
|
|
|
|
27
|
} ); |
37
|
|
|
|
|
|
|
$tx->on( finish => sub { |
38
|
6
|
|
|
6
|
|
168
|
my ( $tx ) = @_; |
39
|
6
|
|
|
|
|
13
|
$self->remove_peer( $tx ); |
40
|
6
|
|
|
|
|
69
|
} ); |
41
|
6
|
|
|
|
|
30
|
push @{ $self->peers }, $tx; |
|
6
|
|
|
|
|
12
|
|
42
|
6
|
|
|
|
|
17
|
return; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
#pod =method remove_peer |
46
|
|
|
|
|
|
|
#pod |
47
|
|
|
|
|
|
|
#pod Remove the connection from this bus. Called automatically by the C |
48
|
|
|
|
|
|
|
#pod handler. |
49
|
|
|
|
|
|
|
#pod |
50
|
|
|
|
|
|
|
#pod =cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub remove_peer { |
53
|
6
|
|
|
6
|
1
|
10
|
my ( $self, $tx ) = @_; |
54
|
6
|
|
|
|
|
8
|
my @peers = @{ $self->peers }; |
|
6
|
|
|
|
|
13
|
|
55
|
6
|
|
|
|
|
34
|
for my $i ( 0.. $#peers ) { |
56
|
12
|
100
|
|
|
|
31
|
if ( $peers[$i] eq $tx ) { |
57
|
6
|
|
|
|
|
10
|
splice @peers, $i, 1; |
58
|
6
|
|
|
|
|
16
|
return; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
sub send_message { |
74
|
4
|
|
|
4
|
1
|
32
|
my ( $self, $msg, $from_tx ) = @_; |
75
|
4
|
|
|
|
|
6
|
my @peers = @{ $self->peers }; |
|
4
|
|
|
|
|
8
|
|
76
|
4
|
100
|
|
|
|
33
|
if ( $from_tx ) { |
77
|
3
|
|
|
|
|
6
|
@peers = grep { $_ ne $from_tx } @peers; |
|
12
|
|
|
|
|
27
|
|
78
|
|
|
|
|
|
|
} |
79
|
4
|
|
|
|
|
13
|
$_->send( $msg ) for @peers; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__END__ |