ControlFreak

 view release on metacpan or  search on metacpan

lib/ControlFreak/Console.pm  view on Meta::CPAN

package ControlFreak::Console;
use strict;
use warnings;

use Carp;
use AnyEvent::Socket();
use AnyEvent::Handle();
use Scalar::Util();

our $CRLF = "\015\12";

use Object::Tiny qw{
    host
    service
    full
    started
};

sub new {
    my $console = shift->SUPER::new(@_);
    my %param = @_;
    $console->{ctrl} = $param{ctrl}
        or croak "Console requires a controller";
    Scalar::Util::weaken($console->{ctrl});
    $console->{started} = 0;
    $param{ctrl}->set_console($console);
    $console->{full} = 1;
    return $console;
}

=head1 NAME

ControlFreak::Console - Handles all communications with ControlFreak

=cut

=head1 SYNOPSIS

    $con = ControlFreak::Console->new(
        host    => $host,
        service => $service,
        full    => 1,
        ctrl    => $ctrl,
    );
    $con->start;

    ## return all the current connection handles
    @hdls = $con->conns;

    $con->add_conn($hdl);

    $ok = $con->process_command($string);

    $con->stop;

=head1 METHODS

=head2 start

Starts the console

=cut

sub start {
    my $console = shift;
    my %param   = @_;
    my $ctrl = $console->{ctrl};

    my $service = $console->service;
    my $accept_cb = sub {
        my ($fh, $host, $port) = @_;
        my $addr = $host eq 'unix/'
                 ? "$host:$service"
                 :  AnyEvent::Socket::format_hostport($host, $port);
        $ctrl->log->info("new connection to admin from $addr");
        $console->accept_connection($fh, $host, $port);
    };

    my $prepare_cb = sub {
        my ($fh, $host, $port) = @_;
        $ctrl->log->info("Admin interface started on $host:$port");
        $param{prepare_cb}->(@_) if $param{prepare_cb};
        return 0;
    };



( run in 0.870 second using v1.01-cache-2.11-cpan-39bf76dae61 )