File: Base.pm

package info (click to toggle)
libnet-cli-interact-perl 2.142720-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 420 kB
  • ctags: 127
  • sloc: perl: 1,907; makefile: 2
file content (140 lines) | stat: -rw-r--r-- 3,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package Net::CLI::Interact::Transport::Wrapper::Base;
{
  $Net::CLI::Interact::Transport::Wrapper::Base::VERSION = '2.142720';
}

use Moo;
use Sub::Quote;
use MooX::Types::MooseLike::Base qw(Int RegexpRef Str Object);

with 'Net::CLI::Interact::Role::FindMatch';

{
    package # hide from pause
        Net::CLI::Interact::Transport::Wrapper::Base::Options;
    use Moo;
}

has 'use_net_telnet_connection' => (
    is => 'rw',
    isa => Int,
    default => quote_sub('0'),
);

has 'irs_re' => (
    is => 'ro',
    isa => RegexpRef,
    default => quote_sub(q{ qr/(?:\015\012|\015|\012)/ }), # first wins
);

has 'ors' => (
    is => 'rw',
    isa => Str,
    default => quote_sub(q{"\n"}),
);

has 'timeout' => (
    is => 'rw',
    isa => quote_sub(q{ die "$_[0] is not a posint!" unless $_[0] > 0 }),
    default => quote_sub('10'),
);

has 'app' => (
    is => 'lazy',
    isa => Str,
    predicate => 1,
    clearer => 1,
);

has 'stash' => (
    is => 'rw',
    isa => Str,
    default => quote_sub(q{''}),
);

has 'wrapper' => (
    is => 'lazy',
    isa => Object,
    predicate => 'connect_ready',
    clearer => 1,
);

sub _build_wrapper {
    my $self = shift;
    $self->logger->log('transport', 'notice', 'connecting with: ',
        $self->app, (join ' ', map {($_ =~ m/\s/) ? ("'". $_ ."'") : $_}
                                   $self->runtime_options));
    # this better be wrapped otherwise it'll blow up
};

sub init { (shift)->wrapper(@_) }

sub flush {
    my $self = shift;
    my $content = $self->stash . $self->buffer;
    $self->stash('');
    $self->buffer('');
    return $content;
}

sub disconnect {
    my $self = shift;
    $self->clear_wrapper;
    $self->flush;
}

sub _abc { die "not implemented." }

sub put { _abc() }
sub pump { _abc() }
sub buffer { _abc() }

sub DEMOLISH { (shift)->disconnect }

sub do_action {
    my ($self, $action) = @_;
    $self->logger->log('transport', 'info', 'callback received for', $action->type);

    if ($action->type eq 'match') {
        my $irs_re = $self->irs_re;
        my $cont = $action->continuation;

        while ($self->pump) {
            # remove control characters
            (my $buffer = $self->buffer) =~ s/[\000-\010\013\014\016-\032\034-\037]//g;
            $self->logger->log('dump', 'debug', "SEEN:\n". $buffer);

            if ($buffer =~ m/^(.*$irs_re)(.*)/s) {
                $self->stash($self->stash . $1);
                $self->buffer($2 || '');
            }

            if ($cont and $self->find_match($self->buffer, $cont->first->value)) {
                $self->logger->log('transport', 'debug', 'continuation matched');
                $self->buffer('');
                $self->put($cont->last->value);
            }
            elsif (my $hit = $self->find_match($self->buffer, $action->value)) {
                $self->logger->log('transport', 'info',
                    sprintf 'output matched %s, storing and returning', $hit);
                $action->prompt_hit($hit);
                $action->response_stash($self->stash);
                $action->response($self->buffer);
                $self->flush;
                last;
            }
            else {
                $self->logger->log('transport', 'debug', "nope, doesn't (yet) match",
                    (ref $action->value eq ref [] ? (join '|', @{$action->value})
                                                : $action->value));
            }
        }
    }
    if ($action->type eq 'send') {
        my $command = sprintf $action->value, @{ $action->params };
        $self->logger->log('dialogue', 'notice', 'queueing data for send: "'. $command .'"');
        $self->put( $command, ($action->no_ors ? () : $self->ors) );
    }
}

1;