API-MikroTik
view release on metacpan or search on metacpan
262728293031323334353637383940414243444546);
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
171172173174175176177178179180181182183184185186187188189190191}
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
243244245246247248249250251252253254255256257258259260261262263sub
_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
288289290291292293294295296297298299300301302303304305306307308);
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
390391392393394395396397398399400401402403404405406407408409User 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
472473474475476477478479480481482483484485486487488489490491492L<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
4041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
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
112113114115116117118119120121122123124125126127128129130131132
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
979899100101102103104105106107108109110111112113114115116117
{
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 )