API-MikroTik

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
);
Mojo::IOLoop->start();
 
# Subscribe
$tag = $api->subscribe(
    '/interface/listen' => sub {
        my ($api, $err, $res) = @_;
        ...;
    }
);
Mojo::IOLoop->timer(3 => sub { $api->cancel($tag) });
Mojo::IOLoop->start();
 
# Errors handling
$api->command(
    '/random/command' => sub {
        my ($api, $err, $list) = @_;
 
        if ($err) {
            warn "Error: $err, category: " . $list->[0]{category};
            return;

lib/API/MikroTik.pm  view on Meta::CPAN

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
}
 
sub _fail_all {
    $_[0]->_fail($_, $_[2])
        for grep { $_->{loop} eq $_[1] } values %{$_[0]->{requests}};
}
 
sub _finish {
    my ($self, $r, $err) = @_;
    delete $self->{requests}{$r->{tag}};
    if (my $timer = $r->{timeout}) { $r->{loop}->remove($timer) }
    $r->{cb}->($self, ($self->{error} = $err // ''), $r->{data});
}
 
sub _login {
    my ($self, $loop, $cb) = @_;
    warn "-- trying to log in\n" if DEBUG;
 
    $loop->delay(
        sub {
            $self->_command($loop, '/login', {}, undef, $_[0]->begin());

lib/API/MikroTik.pm  view on Meta::CPAN

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
sub _write_sentence {
    my ($self, $stream, $r) = @_;
    warn "-- writing sentence for tag: $r->{tag}\n" if DEBUG;
 
    $stream->write($r->{sentence});
 
    return $r->{tag} if $r->{subscription};
 
    weaken $self;
    $r->{timeout} = $r->{loop}
        ->timer($self->timeout => sub { $self->_fail($r, 'response timeout') });
 
    return $r->{tag};
}
 
1;
 
 
=encoding utf8
 
=head1 NAME

lib/API/MikroTik.pm  view on Meta::CPAN

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
);
Mojo::IOLoop->start();
 
# Subscribe
$tag = $api->subscribe(
    '/interface/listen' => sub {
        my ($api, $err, $el) = @_;
        ...;
    }
);
Mojo::IOLoop->timer(3 => sub { $api->cancel($tag) });
Mojo::IOLoop->start();
 
# Errors handling
$api->command(
    '/random/command' => sub {
        my ($api, $err, $list) = @_;
 
        if ($err) {
            warn "Error: $err, category: " . $list->[0]{category};
            return;

lib/API/MikroTik.pm  view on Meta::CPAN

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
User name for authentication purposes. Defaults to C<admin>.
 
=head1 METHODS
 
=head2 cancel
 
  # subscribe to a command output
  my $tag = $api->subscribe('/ping', {address => '127.0.0.1'} => sub {...});
 
  # cancel command after 10 seconds
  Mojo::IOLoop->timer(10 => sub { $api->cancel($tag) });
 
  # or with callback
  $api->cancel($tag => sub {...});
 
Cancels background commands. Can accept a callback as last argument.
 
=head2 cmd
 
  my $list = $api->cmd('/interface/print');

lib/API/MikroTik.pm  view on Meta::CPAN

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
L<Mojo::Promise> object instead of accepting a callback. L<Mojolicious> v7.54+ is
required for promises functionality.
 
=head2 subscribe
 
  my $tag = $api->subscribe('/ping',
      {address => '127.0.0.1'} => sub {
        my ($api, $err, $res) = @_;
      });
 
  Mojo::IOLoop->timer(
      3 => sub { $api->cancel($tag) }
  );
 
Subscribe to an output of commands with continuous responses such as C<listen> or
C<ping>. Should be terminated with L</cancel>.
 
=head1 DEBUGGING
 
You can set the API_MIKROTIK_DEBUG environment variable to get some debug output
printed to stderr.

t/lib/API/MikroTik/Mockup.pm  view on Meta::CPAN

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
                        eval {
                            my $resp = '';
                            $resp .= encode_sentence(@$_) for ($self->$cmd($_));
                            $stream->write($resp);
                        } or warn "unhandled command \"$cmd\": $@";
                    }
                }
            );
            $stream->on(
                close => sub { $loop->remove($_) for values %{$self->{timers}} }
            );
        }
    );
 
    return $serv_id;
};
 
sub cmd_cancel {
    my ($self, $attr) = @_;
    my $tag     = $attr->{'.tag'};
    my $cmd_tag = $attr->{'tag'};
 
    return ['!trap', {message => 'unknown command'}, undef, $tag]
        unless my $id = delete $self->{timers}{$cmd_tag};
    $self->ioloop->remove($id);
 
    return (
        ['!trap', {category => 2, message => 'interrupted'}, undef, $cmd_tag],
        _done($tag), _done($cmd_tag));
}
 
sub cmd_close_premature {
    my ($self, $attr) = @_;
 
    my $sent = encode_sentence('!re', {message => 'response'}, undef,
        $attr->{'.tag'});
    substr $sent, (length($sent) / 2), -1, '';
 
    $self->{h}->write($sent);
    $self->ioloop->timer(0.5 => sub { $self->{h}->close() });
 
    return ();
}
 
sub cmd_err {
    my (undef, $attr) = @_;
    my $tag = $attr->{'.tag'};
    return ['!trap', {message => 'random error', category => 0}, undef, $tag];
}

t/lib/API/MikroTik/Mockup.pm  view on Meta::CPAN

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    my $resp = ['!re', _gen_attr(@{$attr}{'.proplist', 'count'}), undef, $tag];
    return ($resp, $resp, _done($tag));
}
 
sub cmd_subs {
    my ($self, $attr) = @_;
    my $tag = $attr->{'.tag'} // 0;
    my $key = $attr->{'key'};
 
    $self->{timers}{$tag} = $self->ioloop->recurring(
        0.5 => sub {
            $self->{h}
                ->write(encode_sentence('!re', {key => $key}, undef, $tag));
        }
    );
 
    return ();
}
 
sub _done {

t/mikrotik.t  view on Meta::CPAN

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    {key => 'nnn'} => sub {
        $res = $_[2] unless $err = $_[1];
        $api->cancel($tag);
    }
);
 
my ($err1, $err2);
$api->cmd('/err' => sub { $err1 = $_[1] . '1' });
$api->cmd('/err' => sub { $err2 = $_[1] . '2' });
 
Mojo::IOLoop->timer(1.3 => sub { Mojo::IOLoop->stop() });
Mojo::IOLoop->start();
 
is_deeply $res, {key => 'nnn'}, 'right result';
is $err'interrupted',   'right error';
is $err1, 'random error1', 'right error';
is $err2, 'random error2', 'right error';
 
done_testing();
 
sub _gen_result {



( run in 1.258 second using v1.01-cache-2.11-cpan-87723dcf8b7 )