Automatic timeout with WAMP V2

#1

Hi All,

I was wondering if it was possible to start a WAMP (v2) server (Python) with a timeout information such as:

  • When nobody connect to the server, the server exit gracefully after that given timeout

  • When a client is connected, the server live forever.

  • When all the clients have left the server, the server will exit gracefully after that given timeout

And that should work in Twisted in either case when the WAMP server is the only thing that handle the requests and when it is embedded inside a Site.

Any thoughts and help on how to implement that will be great.

With WAMPv1 we’ve overridden the WampServerFactory to monitor “startFactory”, “on_connect” and “connection_lost” and called "reactor.stop()’ when the timeout was happening with no client connected.

Thanks,

Seb

0 Likes

#2

Hi All,

I was wondering if it was possible to start a WAMP (v2) server (Python)
with a timeout information such as:

- When nobody connect to the server, the server exit gracefully after
that given timeout
- When a client is connected, the server live forever.
- When all the clients have left the server, the server will exit
gracefully after that given timeout

Here is one idea:

Have a variable on the factory with the number of currently connected clients. In the protocol, hook into connectionMade/Lost and increment/decrement the variable. Make sure to call the base class methods connectionMade/Lost also. Whenever the variable is decremented, a check is done whether the variable has reached 0. If so, a reactor.callLater is scheduled with the timeout and code to stop the world. Whenever the variable is incremented, a check is done to see if there is a timer currently running. If so, it is cancelled and deleted.

/Tobias

···

Am 13.06.2014 17:43, schrieb Sebastien Jourdain:

And that should work in Twisted in either case when the WAMP server is
the only thing that handle the requests and when it is embedded inside a
Site.

Any thoughts and help on how to implement that will be great.

With WAMPv1 we've overridden the WampServerFactory to monitor
"startFactory", "on_connect" and "connection_lost" and called
"reactor.stop()' when the timeout was happening with no client connected.

Thanks,

Seb

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#3

Hum…

I’m getting lost on which class I should override to do what I want to do.

Here is what I just did that does not work as none of the expected methods get called.

class TimeoutWampWebSocketServerFactory(autobahn.twisted.websocket.WampWebSocketServerFactory):

def init(self, factory, *args, **kwargs):

OK, method called

manage timout option before…

del kwargs[‘timeout’]

WampWebSocketServerFactory.init(self, factory, *args, **kwargs)

def connectionMade(self):

Never called

WampWebSocketServerFactory.connectionMade(self)

def connectionLost(self, reason):

Never called

WampWebSocketServerFactory.connectionLost(self, reason)

def startFactory(self):

Never called

WampWebSocketServerFactory.startFactory(self)

I guess, I’m getting the wrong class…

Can someone list the full class name with the method names that I can override?

I’m looking for “startFactory”, “connectionMade”, “connectionLost” or anything similar.

And my setup is as follow.

Create WAMP router factory

from autobahn.wamp.router import RouterFactory

router_factory = RouterFactory()

create a WAMP router session factory

from autobahn.twisted.wamp import RouterSessionFactory

session_factory = RouterSessionFactory(router_factory)

Register protocol

session_factory.add(service())

create a WAMP-over-WebSocket transport server factory

transport_factory = vtk_wamp.TimeoutWampWebSocketServerFactory(session_factory, \

url = “%s://%s:%d” % (wsProtocol, options.host, options.port), \

debug = options.debug, \

debug_wamp = options.debug, \

timeout = options.timeout )

Thanks,

Seb

···

On Saturday, June 14, 2014 2:48:02 AM UTC-6, Tobias Oberstein wrote:

Am 13.06.2014 17:43, schrieb Sebastien Jourdain:

Hi All,

I was wondering if it was possible to start a WAMP (v2) server (Python)

with a timeout information such as:

  • When nobody connect to the server, the server exit gracefully after

that given timeout

  • When a client is connected, the server live forever.
  • When all the clients have left the server, the server will exit

gracefully after that given timeout

Here is one idea:

Have a variable on the factory with the number of currently connected
clients. In the protocol, hook into connectionMade/Lost and
increment/decrement the variable. Make sure to call the base class
methods connectionMade/Lost also. Whenever the variable is decremented,
a check is done whether the variable has reached 0. If so, a
reactor.callLater is scheduled with the timeout and code to stop the
world. Whenever the variable is incremented, a check is done to see if
there is a timer currently running. If so, it is cancelled and deleted.

/Tobias

And that should work in Twisted in either case when the WAMP server is

the only thing that handle the requests and when it is embedded inside a

Site.

Any thoughts and help on how to implement that will be great.

With WAMPv1 we’ve overridden the WampServerFactory to monitor

“startFactory”, “on_connect” and “connection_lost” and called

"reactor.stop()’ when the timeout was happening with no client connected.

Thanks,

Seb

You received this message because you are subscribed to the Google

Groups “Autobahn” group.

To unsubscribe from this group and stop receiving emails from it, send

an email to autobahnws+...@googlegroups.com

mailto:autobahnws+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

0 Likes

#4

Hi Seb,

try overriding methods on WebSocketServerProtocol (Protocol, not Factory):

__init__(addr) -> Protocol instance created
connectionMade() -> TCP connection established
onConnect(request) -> WebSocket opening handshake
onOpen() -> WebSocket connection open
..
onClose(reason, code) -> WebSocket connection closed
connectionLost(reason) -> TCP connection gone

···

==

I'd go with connectionMade/Lost for the counting of active connections.

Cheers,
/Tobias

Am 17.06.2014 01:44, schrieb Sebastien Jourdain:

Hum...

I'm getting lost on which class I should override to do what I want to do.

Here is what I just did that does not work as none of the expected
methods get called.

class
TimeoutWampWebSocketServerFactory(autobahn.twisted.websocket.WampWebSocketServerFactory):

     def __init__(self, factory, *args, **kwargs):
         # OK, method called
         # manage timout option before...
         del kwargs['timeout']
         WampWebSocketServerFactory.__init__(self, factory, *args, **kwargs)

     def connectionMade(self):
         # Never called
         WampWebSocketServerFactory.connectionMade(self)

     def connectionLost(self, reason):
         # Never called
         WampWebSocketServerFactory.connectionLost(self, reason)

     def startFactory(self):
         # Never called
         WampWebSocketServerFactory.startFactory(self)

I guess, I'm getting the wrong class...

Can someone list the full class name with the method names that I can
override?
I'm looking for "startFactory", "connectionMade", "connectionLost" or
anything similar.

And my setup is as follow.

     # Create WAMP router factory
     from autobahn.wamp.router import RouterFactory
     router_factory = RouterFactory()

     # create a WAMP router session factory
     from autobahn.twisted.wamp import RouterSessionFactory
     session_factory = RouterSessionFactory(router_factory)

     # Register protocol
     session_factory.add(service())

     # create a WAMP-over-WebSocket transport server factory
     transport_factory =
vtk_wamp.TimeoutWampWebSocketServerFactory(session_factory, \
            url = "%s://%s:%d" % (wsProtocol, options.host,
options.port), \
            debug = options.debug,
              \
            debug_wamp = options.debug,
              \
            timeout = options.timeout )

Thanks,

Seb

On Saturday, June 14, 2014 2:48:02 AM UTC-6, Tobias Oberstein wrote:

    Am 13.06.2014 17:43, schrieb Sebastien Jourdain:
     > Hi All,
     >
     > I was wondering if it was possible to start a WAMP (v2) server
    (Python)
     > with a timeout information such as:
     >
     > - When nobody connect to the server, the server exit gracefully
    after
     > that given timeout
     > - When a client is connected, the server live forever.
     > - When all the clients have left the server, the server will exit
     > gracefully after that given timeout

    Here is one idea:

    Have a variable on the factory with the number of currently connected
    clients. In the protocol, hook into connectionMade/Lost and
    increment/decrement the variable. Make sure to call the base class
    methods connectionMade/Lost also. Whenever the variable is decremented,
    a check is done whether the variable has reached 0. If so, a
    reactor.callLater is scheduled with the timeout and code to stop the
    world. Whenever the variable is incremented, a check is done to see if
    there is a timer currently running. If so, it is cancelled and deleted.

    /Tobias

     >
     > And that should work in Twisted in either case when the WAMP
    server is
     > the only thing that handle the requests and when it is embedded
    inside a
     > Site.
     >
     > Any thoughts and help on how to implement that will be great.
     >
     > With WAMPv1 we've overridden the WampServerFactory to monitor
     > "startFactory", "on_connect" and "connection_lost" and called
     > "reactor.stop()' when the timeout was happening with no client
    connected.
     >
     > Thanks,
     >
     > Seb
     >
     > --
     > You received this message because you are subscribed to the Google
     > Groups "Autobahn" group.
     > To unsubscribe from this group and stop receiving emails from it,
    send
     > an email to autobah...@googlegroups.com <javascript:>
     > <mailto:autobahnws+...@googlegroups.com <javascript:>>.
     > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#5

Here is the full solution

from autobahn.twisted.websocket import WampWebSocketServerFactory

from autobahn.twisted.websocket import WampWebSocketServerProtocol

class TimeoutWampWebSocketServerFactory(WampWebSocketServerFactory):

“”"

TimeoutWampWebSocketServerFactory is WampWebSocketServerFactory subclass

that adds support to close the web-server after a timeout when the last

connected client drops.

Currently, the protocol must call connectionMade() and connectionLost() methods

to notify the factory that the connection was started/closed.

If the connection count drops to zero, then the reap timer

is started which will end the process if no other connections are made in

the timeout interval.

“”"

def init(self, factory, *args, **kwargs):

self._connection_count = 0

self._timeout = kwargs[‘timeout’]

self._reaper = reactor.callLater(self._timeout, lambda: reactor.stop())

del kwargs[‘timeout’]

WampWebSocketServerFactory.init(self, factory, *args, **kwargs)

WampWebSocketServerFactory.protocol = TimeoutWampWebSocketServerProtocol

def connectionMade(self):

if self._reaper:

self._reaper.cancel()

self._reaper = None

self._connection_count += 1

def connectionLost(self, reason):

if self._connection_count > 0:

self._connection_count -= 1

if self._connection_count == 0 and not self._reaper:

self._reaper = reactor.callLater(self._timeout, lambda: reactor.stop())

=============================================================================

class TimeoutWampWebSocketServerProtocol(WampWebSocketServerProtocol):

def connectionMade(self):

WampWebSocketServerProtocol.connectionMade(self)

self.factory.connectionMade()

def connectionLost(self, reason):

WampWebSocketServerProtocol.connectionLost(self, reason)

self.factory.connectionLost(reason)

0 Likes