| 1 | package Net::MessageBus::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | Net::MessageBus::Server - Pure Perl message bus server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | Version 0.08 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use base qw(Net::MessageBus::Base); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use Net::MessageBus::Message; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | #handle gracefully the death of child ssh processes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use POSIX ":sys_wait_h"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | $| = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | This module creates a new Net::MessageBus server running on the specified address/port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | Usage : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | use Net::MessageBus::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my $MBServer = Net::MessageBus::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | address => '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | port => '15000', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | logger => $logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | authenticate => \&authenticate_method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | $MBServer->start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | $MBServer->daemon() || die "Fork to start Net::MessageBus::Server in background failed!" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | if ( $MBServer->is_running() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | print "Server is alive"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | $MBServer->stop(); #if started as a daemon. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | Creates a new server object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | It does not automatically start the server, you have to start it using the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | start() method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | Arguments : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | =item * address = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | The address on which the server should bind , 127.0.0.1 by dafault | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | =item * port = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | The port on which the server should listen , 4500 by default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | =item * logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | Any object that supports the fallowing methods : debug, info, warn,error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | =item * authenticate = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | A code ref to a method that returns true if the authentication is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | successfull and false otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | B<Example> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | my $MBServer = Net::MessageBus::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | address => '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | port => '15000', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | logger => $logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | authenticate => \&authenticate_method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | B<Example authentication method> : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | sub authenticate_method { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | my ($username, $password, $client_ip) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | return 1 if ($username eq "john" && $password eq "1234"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | my %params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | if ((ref($_[0]) || '') eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $_[0] || ''
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table50%1/2
A
A= ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | %params = %{$_[0]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | %params = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | my $self = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | address => $params{address} || '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | port => $params{port} || '4500', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | logger => $params{logger} || Net::MessageBus::Base::create_default_logger(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | authenticate => $params{autenticate} || sub {return 1}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition40%4/10
$params{'address'} || '127.0.0.1'
$params{'port'} || '4500'
$params{'logger'} || Net::MessageBus::Base::create_default_logger()
$params{'autenticate'} || sub {
return 1;
}
MC/DC0%0/1
$params{'address'} || '127.0.0.1'
$params{'address'}
MC/DC0%0/1
$params{'port'} || '4500'
$params{'port'}
MC/DC0%0/2
$params{'logger'} || Net::MessageBus::Base::create_default_logger()
$params{'logger'}
Net::MessageBus::Base::create_default_logger()
MC/DC0%0/2
$params{'autenticate'} || sub {
return 1;
}
$params{'autenticate'}
sub {
return 1;
}
Truth table50%1/2
A
A= $params{'address'}
Truth table50%1/2
A
A= $params{'port'}
Truth table33%1/3
A || B
A= $params{'logger'}B= Net::MessageBus::Base::create_default_logger()
Truth table33%1/3
A || B
A= $params{'autenticate'}B= sub {
return 1;
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | $self->{subscriptions} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | all => [], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | groups => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | senders => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | $self->{authenticated} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =head2 start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | Starts the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $self->{server_socket} = $self->create_server_socket(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | my $server_sel = IO::Select->new($self->{server_socket}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | $self->{run} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | while ($self->{run} == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my @exceptions = $server_sel->has_exception(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | foreach my $broken_socket (@exceptions) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $server_sel->remove($broken_socket); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | close($broken_socket); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my @ready = $server_sel->can_read(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | next unless scalar(@ready); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | foreach my $fh (@ready) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if( $fh == $self->{server_socket} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Accept the incoming socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | my $new = $fh->accept; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | next unless $new; #in case the ssl connection failed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $straddr = $self->get_peer_address($new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $self->logger->info("Accepted from : $straddr\n"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | $server_sel->add($new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # Process socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | local $\ = "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | local $/ = "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | my $text = readline($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $straddr = $self->get_peer_address($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | if ($text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | chomp($text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | $self->{client_socket} = $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | $self->logger->debug("Request from $straddr : '$text'"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $request = from_json($text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | print $fh to_json({status => 0, status_message => $@ }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | elsif ($request->{type} eq "message") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | print $fh to_json({status => 1}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $message = Net::MessageBus::Message->new($request->{payload}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $self->send_message($message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | elsif ($request->{type} eq "authenticate") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my %data = %{$request->{payload}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | my $auth = $self->{authenticate}->( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | @data{qw/username password/}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | $self->get_peer_address($fh) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $self->{authenticated}->{$fh} = $auth; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | print $fh to_json({status => $auth}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | elsif ($request->{type} eq "subscribe") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $self->subscribe_client($request->{payload}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | print $fh to_json({status => 1}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | print $fh to_json({status => 0, status_message => 'Invalid request!'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | $self->logger->info("Peear $straddr closed connection\n"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $self->unsubscribe_client($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | delete $self->{authenticated}->{$fh}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | $server_sel->remove($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | close ($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | =head2 daemon | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | Starts the server in background | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | ▶ | sub daemon { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
daemon: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | ▶ | if ( defined $self->{pid} && kill(0,$self->{pid}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"pid"} and kill 0, $self->{"pid"}
MC/DC0%0/2
defined $self->{"pid"} and kill 0, $self->{"pid"}
defined $self->{"pid"}
kill 0, $self->{"pid"}
Truth table33%1/3
A and B
A= defined $self->{"pid"}B= kill 0, $self->{"pid"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | $self->logger->error('An instance of the server is already running!'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 2 | ▶ | $SIG{CHLD} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # don't change $! and $? outside handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | local ( $!, $? ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | ▶ | while ( my $pid = waitpid( -1, WNOHANG ) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | #Wait for the child processes to exit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 2 | my $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | ▶ | if ( $pid = fork() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | $self->{pid} = $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | $SIG{INT} = $SIG{HUP} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $self->{run} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | $self->{server_socket}->close(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | $self->start(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | exit(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | =head2 stop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | Stops a previously started daemon | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 2 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=5 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | ▶ | if (! defined $self->{pid} || ! kill(0,$self->{pid}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"pid"} and kill 0, $self->{"pid"}
MC/DC0%0/2
defined $self->{"pid"} and kill 0, $self->{"pid"}
defined $self->{"pid"}
kill 0, $self->{"pid"}
Truth table33%1/3
A and B
A= defined $self->{"pid"}B= kill 0, $self->{"pid"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $self->logger->error('No Net::MessageBus::Server is running (pid : '.$self->{pid}.')!'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 2 | ▶ | if ($^O =~ /Win/i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | #signal 15 not delivered while in IO wait on Windows so we have to take drastic measures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | kill 9, $self->{pid}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | kill 15, $self->{pid}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 2 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 2 | ▶ | if ( kill(0,$self->{pid}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | $self->logger->error('Failed to stop the Net::MessageBus::Server (pid : '.$self->{pid}.')! '); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | delete $self->{pid}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =head2 is_running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | Returns true if the server process is running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 4 | ▶ | sub is_running { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_running: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 4 | ▶ | if (! defined $self->{pid} || ! kill(0,$self->{pid}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"pid"} and kill 0, $self->{"pid"}
MC/DC50%1/2
defined $self->{"pid"} and kill 0, $self->{"pid"}
defined $self->{"pid"}
kill 0, $self->{"pid"}
Truth table66%2/3
A and B
A= defined $self->{"pid"}B= kill 0, $self->{"pid"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | =head1 Private methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | =head2 create_server_socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | Starts the TCP socket that to which the clients will connect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub create_server_socket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_server_socket: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | my $server_sock= IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | LocalHost => $self->{address}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | LocalPort => $self->{port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | Listen => 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | Blocking => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | ) || die "Cannot listen on ".$self->{address}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | ":".$self->{port}.", Error: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
'IO::Socket::INET'->new('LocalHost', $self->{'address'}, 'LocalPort', $self->{'port'}, 'Proto', 'tcp', 'Listen', 10, 'ReuseAddr', 1, 'Blocking', 1) || die('Cannot listen on ' . $self->{'address'} . ':' . $self->{'port'} . ", Error: $!")
MC/DC0%0/1
'IO::Socket::INET'->new('LocalHost', $self->{'address'}, 'LocalPort', $self->{'port'}, 'Proto', 'tcp', 'Listen', 10, 'ReuseAddr', 1, 'Blocking', 1) || die('Cannot listen on ' . $self->{'address'} . ':' . $self->{'port'} . ", Error: $!")
'IO::Socket::INET'->new('LocalHost', $self->{'address'}, 'LocalPort', $self->{'port'}, 'Proto', 'tcp', 'Listen', 10, 'ReuseAddr', 1, 'Blocking', 1)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('LocalHost', $self->{'address'}, 'LocalPort', $self->{'port'}, 'Proto', 'tcp', 'Listen', 10, 'ReuseAddr', 1, 'Blocking', 1)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | $self->logger->info("$0 server v$VERSION - Listening on ". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $self->{address}.":".$self->{port} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | return $server_sock; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | =head2 get_peer_address | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | Returns the ip address for the given connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | sub get_peer_address { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_peer_address: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | my $straddr = 'unknown'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | my $sockaddr = getpeername($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my ($port, $iaddr) = sockaddr_in($sockaddr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | $straddr = inet_ntoa($iaddr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | return $straddr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | =head2 subscribe_client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | Adds the client to the subscription list which he specified | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | sub subscribe_client { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
subscribe_client: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | ▶ | if (defined $data->{all}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | ▶ | $self->{subscriptions}->{all} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"subscriptions"}{"all"} ||= []
MC/DC0%0/1
$self->{"subscriptions"}{"all"} ||= []
$self->{"subscriptions"}{"all"}
Truth table0%0/2
A
A= $self->{"subscriptions"}{"all"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | push @{$self->{subscriptions}->{all}}, $self->{client_socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | elsif (defined $data->{group}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | ▶ | $self->{subscriptions}->{groups}->{$data->{group}} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"subscriptions"}{"groups"}{$data->{"group"}} ||= []
MC/DC0%0/1
$self->{"subscriptions"}{"groups"}{$data->{"group"}} ||= []
$self->{"subscriptions"}{"groups"}{$data->{"group"}}
Truth table0%0/2
A
A= $self->{"subscriptions"}{"groups"}{$data->{"group"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | push @{$self->{subscriptions}->{groups}->{$data->{group}}}, $self->{client_socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | elsif (defined $data->{sender}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | $self->{subscriptions}->{senders}->{$data->{sender}} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"subscriptions"}{"senders"}{$data->{"sender"}} ||= []
MC/DC0%0/1
$self->{"subscriptions"}{"senders"}{$data->{"sender"}} ||= []
$self->{"subscriptions"}{"senders"}{$data->{"sender"}}
Truth table0%0/2
A
A= $self->{"subscriptions"}{"senders"}{$data->{"sender"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | push @{$self->{subscriptions}->{senders}->{$data->{sender}}}, $self->{client_socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | elsif (defined $data->{type}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | $self->{subscriptions}->{types}->{$data->{type}} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"subscriptions"}{"types"}{$data->{"type"}} ||= []
MC/DC0%0/1
$self->{"subscriptions"}{"types"}{$data->{"type"}} ||= []
$self->{"subscriptions"}{"types"}{$data->{"type"}}
Truth table0%0/2
A
A= $self->{"subscriptions"}{"types"}{$data->{"type"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | push @{$self->{subscriptions}->{types}->{$data->{type}}}, $self->{client_socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | elsif (defined $data->{unsubscribe}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | $self->unsubscribe_client($self->{client_socket}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =head2 unsubscribe_client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | Removes the given socket from all subscription lists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | sub unsubscribe_client { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unsubscribe_client: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | $self->{subscriptions}->{all} = [ grep { $_ != $fh } @{$self->{subscriptions}->{all}} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | foreach my $group (keys %{$self->{subscriptions}->{groups}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | $self->{subscriptions}->{groups}->{$group} = [ grep { $_ != $fh } @{$self->{subscriptions}->{groups}->{$group}} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | foreach my $sender (keys %{$self->{subscriptions}->{senders}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | $self->{subscriptions}->{senders}->{$sender} = [ grep { $_ != $fh } @{$self->{subscriptions}->{senders}->{$sender}} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | foreach my $type (keys %{$self->{subscriptions}->{types}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | $self->{subscriptions}->{types}->{$type} = [ grep { $_ != $fh } @{$self->{subscriptions}->{types}->{$type}} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | =head2 clients_registered_for_message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | Returns a list containing all the file handles registered to receive the given message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | sub clients_registered_for_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clients_registered_for_message: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | my $message = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | my @handles = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | ▶ | push @handles, @{ $self->{subscriptions}->{all} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'subscriptions'}{'all'} || []
MC/DC0%0/1
$self->{'subscriptions'}{'all'} || []
$self->{'subscriptions'}{'all'}
Truth table0%0/2
A
A= $self->{'subscriptions'}{'all'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | push @handles, @{ $self->{subscriptions}->{groups}->{$message->group()} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'subscriptions'}{'groups'}{$message->group} || []
MC/DC0%0/1
$self->{'subscriptions'}{'groups'}{$message->group} || []
$self->{'subscriptions'}{'groups'}{$message->group}
Truth table0%0/2
A
A= $self->{'subscriptions'}{'groups'}{$message->group}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | ▶ | push @handles, @{ $self->{subscriptions}->{senders}->{$message->sender()} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'subscriptions'}{'senders'}{$message->sender} || []
MC/DC0%0/1
$self->{'subscriptions'}{'senders'}{$message->sender} || []
$self->{'subscriptions'}{'senders'}{$message->sender}
Truth table0%0/2
A
A= $self->{'subscriptions'}{'senders'}{$message->sender}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | ▶ | push @handles, @{ $self->{subscriptions}->{types}->{$message->type() || ''} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$self->{'subscriptions'}{'types'}{$message->type or ''} || []
$message->type || ''
MC/DC0%0/1
$self->{'subscriptions'}{'types'}{$message->type or ''} || []
$self->{'subscriptions'}{'types'}{$message->type or ''}
MC/DC0%0/1
$message->type || ''
$message->type
Truth table0%0/2
A
A= $self->{'subscriptions'}{'types'}{$message->type or ''}
Truth table0%0/2
A
A= $message->type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my %seen = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | @handles = grep { $_ != $self->{client_socket} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | grep { $self->{authenticated}->{$_} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | grep { ! $seen{$_} ++ } @handles; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | return @handles; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | =head2 send_message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | Sends the given message to the clients that subscribed to the group or sender of the messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | ▶ | sub send_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_message: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | my $message = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | my @recipients = $self->clients_registered_for_message($message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | local $\ = "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | foreach my $client ( @recipients ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | print $client to_json({ type => 'message' , payload => $message->serialize() }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | $self->logger->error('Error sending message to client!'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | Horea Gligan, C<< <gliganh at gmail.com> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | Please report any bugs or feature requests to C<bug-net-MessageBus at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-MessageBus>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | perldoc Net::MessageBus::Server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-MessageBus> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | L<http://annocpan.org/dist/Net-MessageBus> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | L<http://cpanratings.perl.org/d/Net-MessageBus> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | L<http://search.cpan.org/dist/Net-MessageBus/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | Copyright 2012 Horea Gligan. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 4 | 1; # End of Net::MessageBus::Server | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||