| 1 | package ControlFreak::Console; | ||||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 8 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 8 | ▶ | use AnyEvent::Socket(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 8 | ▶ | use AnyEvent::Handle(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 8 | ▶ | use Scalar::Util(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 8 | our $CRLF = "\015\12"; | |||||||||||
| 11 | |||||||||||||
| 12 | 8 | ▶ | use Object::Tiny qw{ | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | host | ||||||||||||
| 14 | service | ||||||||||||
| 15 | full | ||||||||||||
| 16 | started | ||||||||||||
| 17 | 8 | }; | |||||||||||
| 18 | |||||||||||||
| 19 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 20 | 1 | my $console = shift->SUPER::new(@_); | |||||||||||
| 21 | 1 | my %param = @_; | |||||||||||
| 22 | $console->{ctrl} = $param{ctrl} | ||||||||||||
| 23 | 1 | ▶ | or croak "Console requires a controller"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | 1 | Scalar::Util::weaken($console->{ctrl}); | |||||||||||
| 25 | 1 | $console->{started} = 0; | |||||||||||
| 26 | 1 | $param{ctrl}->set_console($console); | |||||||||||
| 27 | 1 | $console->{full} = 1; | |||||||||||
| 28 | 1 | return $console; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | =head1 NAME | ||||||||||||
| 32 | |||||||||||||
| 33 | ControlFreak::Console - Handles all communications with ControlFreak | ||||||||||||
| 34 | |||||||||||||
| 35 | =cut | ||||||||||||
| 36 | |||||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||||
| 38 | |||||||||||||
| 39 | $con = ControlFreak::Console->new( | ||||||||||||
| 40 | host => $host, | ||||||||||||
| 41 | service => $service, | ||||||||||||
| 42 | full => 1, | ||||||||||||
| 43 | ctrl => $ctrl, | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | $con->start; | ||||||||||||
| 46 | |||||||||||||
| 47 | ## return all the current connection handles | ||||||||||||
| 48 | @hdls = $con->conns; | ||||||||||||
| 49 | |||||||||||||
| 50 | $con->add_conn($hdl); | ||||||||||||
| 51 | |||||||||||||
| 52 | $ok = $con->process_command($string); | ||||||||||||
| 53 | |||||||||||||
| 54 | $con->stop; | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 METHODS | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 start | ||||||||||||
| 59 | |||||||||||||
| 60 | Starts the console | ||||||||||||
| 61 | |||||||||||||
| 62 | =cut | ||||||||||||
| 63 | |||||||||||||
| 64 | 1 | ▶ | sub start { | ||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 65 | 1 | my $console = shift; | |||||||||||
| 66 | 1 | my %param = @_; | |||||||||||
| 67 | 1 | my $ctrl = $console->{ctrl}; | |||||||||||
| 68 | |||||||||||||
| 69 | 1 | my $service = $console->service; | |||||||||||
| 70 | 1 | ▶ | my $accept_cb = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 71 | 1 | my ($fh, $host, $port) = @_; | |||||||||||
| 72 | 1 | ▶ | my $addr = $host eq 'unix/' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | ? "$host:$service" | ||||||||||||
| 74 | : AnyEvent::Socket::format_hostport($host, $port); | ||||||||||||
| 75 | 1 | $ctrl->log->info("new connection to admin from $addr"); | |||||||||||
| 76 | 1 | $console->accept_connection($fh, $host, $port); | |||||||||||
| 77 | 1 | }; | |||||||||||
| 78 | |||||||||||||
| 79 | 1 | ▶ | my $prepare_cb = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 80 | 1 | my ($fh, $host, $port) = @_; | |||||||||||
| 81 | 1 | $ctrl->log->info("Admin interface started on $host:$port"); | |||||||||||
| 82 | 1 | ▶ | $param{prepare_cb}->(@_) if $param{prepare_cb}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 1 | return 0; | |||||||||||
| 84 | 1 | }; | |||||||||||
| 85 | |||||||||||||
| 86 | 1 | $console->{started} = 1; | |||||||||||
| 87 | 1 | my $host = $console->host; | |||||||||||
| 88 | 1 | my $guard = AnyEvent::Socket::tcp_server | |||||||||||
| 89 | $host, $service, $accept_cb, $prepare_cb; | ||||||||||||
| 90 | 1 | $console->{guard} = $guard; | |||||||||||
| 91 | 1 | return 1; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | 1 | ▶ | sub accept_connection { | ||||||||||
|
Subroutine
accept_connection: called CC=1 SCAR=1.0
Pod
undocumented
| |||||||||||||
| 95 | 1 | my $console = shift; | |||||||||||
| 96 | 1 | my ($fh, $host, $service) = @_; | |||||||||||
| 97 | |||||||||||||
| 98 | 1 | my $hdl; $hdl = AnyEvent::Handle->new( | |||||||||||
| 99 | fh => $fh, | ||||||||||||
| 100 | 0 | ▶ | on_eof => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 101 | 0 | $console->{ctrl}->log->info("Console connection: eof"); | |||||||||||
| 102 | 0 | $hdl->destroy; | |||||||||||
| 103 | }, | ||||||||||||
| 104 | 0 | ▶ | on_error => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 105 | 0 | $console->{ctrl}->log->error("Console connection error: $!"); | |||||||||||
| 106 | 0 | $hdl->destroy; | |||||||||||
| 107 | }, | ||||||||||||
| 108 | 1 | ); | |||||||||||
| 109 | 1 | $console->{handles}{$hdl} = $hdl; | |||||||||||
| 110 | |||||||||||||
| 111 | 1 | ▶ | my $get_admin_cmd; $get_admin_cmd = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.4
| |||||||||||||
| 112 | 1 | my ($h, $line) = @_; | |||||||||||
| 113 | 1 | ▶ | if (lc $line eq 'exit') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | $console->{ctrl}->log->info( "Console exiting" ); | |||||||||||
| 115 | 0 | ▶ | $h->on_drain(sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 116 | 0 | delete $console->{handles}{$h}; | |||||||||||
| 117 | 0 | $h->destroy; | |||||||||||
| 118 | 0 | }); | |||||||||||
| 119 | 0 | return 1; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | ControlFreak::Command->process( | ||||||||||||
| 123 | cmd => $line, | ||||||||||||
| 124 | ctrl => $console->{ctrl}, | ||||||||||||
| 125 | 0 | ▶ | err_cb => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 126 | 0 | ▶ | my $error = shift || ""; | ||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 127 | 0 | $h->push_write("ERROR: $error$CRLF"); | |||||||||||
| 128 | }, | ||||||||||||
| 129 | 1 | ▶ | ok_cb => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||
| 130 | 1 | ▶ | my $out = shift || ""; | ||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 131 | 1 | ▶ | $out .= "\n" if $out; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | 1 | $h->push_write("${out}OK$CRLF"); | |||||||||||
| 133 | }, | ||||||||||||
| 134 | 1 | has_priv => $console->full, | |||||||||||
| 135 | ); | ||||||||||||
| 136 | |||||||||||||
| 137 | ## continue reading | ||||||||||||
| 138 | 1 | $h->push_read( line => $get_admin_cmd ); | |||||||||||
| 139 | 1 | }; | |||||||||||
| 140 | |||||||||||||
| 141 | 1 | $hdl->push_read( line => $get_admin_cmd ); | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | =head1 AUTHOR | ||||||||||||
| 145 | |||||||||||||
| 146 | Yann Kerherve <yannk@cpan.org> | ||||||||||||
| 147 | |||||||||||||
| 148 | =cut | ||||||||||||
| 149 | |||||||||||||
| 150 | 8 | "con=console"; | |||||||||||