| 1 | package POE::Component::Client::BigBrother; | ||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use POE qw< Component::Client::TCP >; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||
| 13 | # send() | ||||||||||||||||||||||||||||||||||||||||
| 14 | # ---- | ||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | sub send { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=15 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | my ($class, %param) = @_; | |||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||
| 18 | # check for mandatory parameters | ||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | my @mandatory = qw< host event command_type command_fields >; | |||||||||||||||||||||||||||||||||||||||
| 20 | 4 | for my $name (@mandatory) { | |||||||||||||||||||||||||||||||||||||||
| 21 | croak "error: $class requires a '$name' parameter" | ||||||||||||||||||||||||||||||||||||||||
| 22 | 16 | ▶ | unless $param{$name}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||
| 25 | # default values | ||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | $param{port} = 1984 unless defined $param{port}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | $param{timeout} = 10 unless defined $param{timeout}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | $param{retry} = 2 unless defined $param{retry}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||
| 30 | # aliases | ||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | my $field = $param{command_fields}; | |||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||
| 33 | # check for mandatory fields common to all commands | ||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | @mandatory = qw< host service message >; | |||||||||||||||||||||||||||||||||||||||
| 35 | 4 | for my $name (@mandatory) { | |||||||||||||||||||||||||||||||||||||||
| 36 | croak "error: '$param{command_type}' command requires a '$name' field" | ||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | ▶ | unless $field->{$name}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||
| 40 | # check and adapt some values to Big Brother message format | ||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | (my $host = $field->{host}) =~ s/\./,/g; | |||||||||||||||||||||||||||||||||||||||
| 42 | 4 | my $service = $field->{service}; | |||||||||||||||||||||||||||||||||||||||
| 43 | 4 | my $color = $field->{color}; | |||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | my $offset = $field->{offset} ? "+$field->{offset}" : ""; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||
| 46 | # construct the Big Brother message | ||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | if ($param{command_type} eq "status" or $param{command_type} eq "page") { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition100%3/3
$param{"command_type"} eq "status" or $param{"command_type"} eq "page"
MC/DC100%2/2
$param{"command_type"} eq "status" or $param{"command_type"} eq "page"
$param{"command_type"} eq "status"
$param{"command_type"} eq "page"
Truth table100%3/3
A or B
A= $param{"command_type"} eq "status"B= $param{"command_type"} eq "page"
| |||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | $param{message} = "$param{command_type}$offset" | |||||||||||||||||||||||||||||||||||||||
| 49 | . " $host.$service $color $field->{message}"; | ||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||
| 51 | elsif ($param{command_type} eq "disable") { | ||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $param{message} = "$param{command_type}$offset" | |||||||||||||||||||||||||||||||||||||||
| 53 | . " $host.$service $field->{duration} $field->{message}"; | ||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||
| 55 | elsif ($param{command_type} eq "enable") { | ||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $param{message} = "$param{command_type}$offset" | |||||||||||||||||||||||||||||||||||||||
| 57 | . " $host.$service $field->{message}"; | ||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||
| 59 | elsif ($param{command_type} eq "event") { | ||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | $field->{activation} ||= time(); | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$field->{"activation"} ||= time
MC/DC0%0/1
$field->{"activation"} ||= time
$field->{"activation"}
Truth table50%1/2
A
A= $field->{"activation"}
| |||||||||||||||||||||||||||||||||||||||||
| 61 | $param{message} = join "", "event\n", | ||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | map { "$_: $field->{$_}\n" } sort keys %$field; | |||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | croak "error: Unknown command type '$param{command_type}'" | |||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||
| 68 | # spawn a PoCo::Client::TCP to send the message | ||||||||||||||||||||||||||||||||||||||||
| 69 | POE::Component::Client::TCP->new( | ||||||||||||||||||||||||||||||||||||||||
| 70 | RemoteAddress => $param{host}, | ||||||||||||||||||||||||||||||||||||||||
| 71 | RemotePort => $param{port}, | ||||||||||||||||||||||||||||||||||||||||
| 72 | ConnectTimeout => $param{timeout}, | ||||||||||||||||||||||||||||||||||||||||
| 73 | Args => [ { %param } ], | ||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | ▶ | Started => sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | my ($kernel, $heap, $sender, $param) | |||||||||||||||||||||||||||||||||||||||
| 77 | = @_[ KERNEL, HEAP, SENDER, ARG0 ]; | ||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | my $sender_id; | |||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||
| 81 | # resolve sender session | ||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | if ($param->{session}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (my $ref = $kernel->alias_resolve($param->{session})) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $sender_id = $ref->ID; | |||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | croak "error: Could not resolve 'session' to a valid " | |||||||||||||||||||||||||||||||||||||||
| 88 | . "POE session"; | ||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | $sender_id = $sender->ID; | |||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||
| 95 | # store some information in the heap | ||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | $heap->{sender_id} = $sender_id; # sender session ID | |||||||||||||||||||||||||||||||||||||||
| 97 | 4 | $heap->{retry} = $param->{retry} + 1; # number of retries left | |||||||||||||||||||||||||||||||||||||||
| 98 | 4 | $heap->{param} = $param; # input parameters | |||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||
| 100 | # store the future response in the heap | ||||||||||||||||||||||||||||||||||||||||
| 101 | $heap->{response} = { | ||||||||||||||||||||||||||||||||||||||||
| 102 | host => $param->{host}, | ||||||||||||||||||||||||||||||||||||||||
| 103 | message => $param->{message}, | ||||||||||||||||||||||||||||||||||||||||
| 104 | context => $param->{context}, | ||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | success => 1, | |||||||||||||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | ▶ | Connected => sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | my ($kernel, $heap) = @_[ KERNEL, HEAP ]; | |||||||||||||||||||||||||||||||||||||||
| 111 | 4 | my $data = $heap->{param}{message}; | |||||||||||||||||||||||||||||||||||||||
| 112 | 4 | $heap->{server}->put($data); # send the message | |||||||||||||||||||||||||||||||||||||||
| 113 | 4 | $kernel->yield("shutdown"); # close the connection | |||||||||||||||||||||||||||||||||||||||
| 114 | }, | ||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | ServerInput => sub {}, # no input is expected from the server | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | ▶ | Disconnected => sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | my ($kernel, $heap) = @_[ KERNEL, HEAP ]; | |||||||||||||||||||||||||||||||||||||||
| 120 | 4 | $kernel->yield("_send_response"); | |||||||||||||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | ConnectError => sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my ($kernel, $heap, $op, $errno, $errstr) | |||||||||||||||||||||||||||||||||||||||
| 125 | = @_[ KERNEL, HEAP, ARG0..ARG2 ]; | ||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $heap->{retry}--; | |||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($heap->{retry} > 0) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $kernel->yield("reconnect"); | |||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $heap->{response}{success} = 0; | |||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $heap->{response}{error} = "$op error $errno: $errstr"; | |||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $kernel->yield("_send_response"); | |||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||
| 137 | }, | ||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||
| 139 | InlineStates => { | ||||||||||||||||||||||||||||||||||||||||
| 140 | 4 | ▶ | _send_response => sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 141 | 4 | my ($kernel, $heap) = @_[ KERNEL, HEAP ]; | |||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||
| 143 | 4 | my $session = $heap->{sender_id}; | |||||||||||||||||||||||||||||||||||||||
| 144 | 4 | my $event = $heap->{param}{event}; | |||||||||||||||||||||||||||||||||||||||
| 145 | 4 | my $response= $heap->{response}; | |||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||
| 147 | # send the response to the send session | ||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | $kernel->post($session, $event, $response); | |||||||||||||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||||||||||||||
| 150 | }, | ||||||||||||||||||||||||||||||||||||||||
| 151 | 4 | ); | |||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||||||||||||||