[API suggestion] "Keep alive" command/argument
RouterOS Scripting and API

3 posts   •   Page 1 of 1
User avatar
boen_robot
Forum Guru
Forum Guru
 
Posts: 1284
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

[API suggestion] "Keep alive" command/argument

by boen_robot » Sat Mar 03, 2012 7:31 pm

In languages like PHP, where the application must explicitly start to listen to the server and fail if it receives nothing in a certain timeout, using commands like "listen" can cause the connection to break unless listen produces something in the timeout interval.

To remedy this problem currently, clients need to send a continious ping command to keep the connection busy while they "listen". This however entails an unnecesary performance penalty, since the ping itself isn't really necessary - any data would do, as long as it is available within the timeout interval.

A more efficient solution would be to have a dedicated command that sends an empty !re reply at a certain specified interval OR if the listen command does that if an optional argument with an interval is specified (since the listen command is really where the problem is). Personally, I'd prefer the latter, but I'll be happy with the first too.

e.g.
Code: Select all
/ip/arp/listen
.tag=arp
=keep-alive-interval=2s


would return (after 4s, if nothing happens during that time):
Code: Select all
!re
.tag=arp

!re
.tag=arp


or if something (in this case a deleted item) has happened within that time:
Code: Select all
!re
.tag=arp

!re
.tag=arp
=.id=*68
=.dead=yes

!re
.tag=arp

PEAR2_Net_RouterOS(1.0.0b5) - My API client in PHP
(Karma? If you want... no pressure...)

User avatar
janisk
MikroTik Support
MikroTik Support
 
Posts: 5915
Joined: Tue Feb 14, 2006 10:46 am
Location: Riga, Latvia

Re: [API suggestion] "Keep alive" command/argument

by janisk » Mon Mar 05, 2012 10:20 am

you have to change that in PHP as there are no such issues with sockets. That should be configurable in PHP settings as that deviates from experience on all languages i know of. Technically TCP connection should be opened and never closed unless one of the ends imposes some artificial timeout value. Take for example SSH client connections - stays opened for weeks.

you can look here:
http://php.net/manual/en/function.strea ... imeout.php

also, if any other php setting fail you can send non-existing command to the router. It will kick the parser and reply that command does not exist. To be sure send md5 sum of some string as such command most probably will not be intreoduced into RouterOS

Code: Select all
/22083539850d5fc8428146d7f76a135d


Code: Select all
!trap
=category=0
=message=no such command or directory (22083539850d5fc8428146d7f76a135d)

!trap
=message=no such command

!done


an

User avatar
boen_robot
Forum Guru
Forum Guru
 
Posts: 1284
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: [API suggestion] "Keep alive" command/argument

by boen_robot » Mon Mar 05, 2012 3:40 pm

I'm aware of the stream_set_timeout() function, but that still terminates the connection if no data is received within the specific timeout. Sure, you can set the timeout to something long, but that's still a connection being terminated after that long time.

And during that receiving attempt, one can't send additional data until the receiving stops. To be more precise, one can't do anything, as the fread() function doesn't return until at least one packet is received (or there is something remaining in PHP's buffer). So sending non-existing commands isn't really possible.

I'll look into non blocking streams. Perhaps that will help. It will surely start to feel unnatural though.

Thanks.
PEAR2_Net_RouterOS(1.0.0b5) - My API client in PHP
(Karma? If you want... no pressure...)

3 posts   •   Page 1 of 1

Who is online

Users browsing this forum: No registered users and 9 guests

It is currently Thu Nov 27, 2014 4:30 am