[Twisted-Python] Keeping a list of connected PB clients
Hi, I am trying to figure out how to keep an continuously updated list of connected clients to a PB server. I have had success in figuring out how to add clients to my list when they connect, but I am not sure what methods get called when a client disconnects. This is what I have so far: from twisted.spread import pb class DSagePBServerFactory(pb.PBServerFactory): def __init__(self, root): pb.PBServerFactory.__init__(self, root) self.clients = [] def clientConnectionMade(self, broker): """Keeps a 3-tuple of connected clients. tuple[0] - the broker transport tuple[1] - the clients ip tuple[2] - the clients port """ self.clients.append((broker.transport, broker.transport.getPeer().host, broker.transport.getPeer().port)) print self.clients There does not seem to be a corresponding clientConnectionLost method for the Factory class. Any suggestions would be appreciated. Yi
On Nov 19, 2006, at 7:00 PM, Yi Qiang wrote:
There does not seem to be a corresponding clientConnectionLost method for the Factory class. Any suggestions would be appreciated.
My approach to this was to put any post-connection cleanup in the Avatar's "logout" method. -phil
On 11/19/06, Phil Christensen <phil@bubblehouse.org> wrote:
On Nov 19, 2006, at 7:00 PM, Yi Qiang wrote:
There does not seem to be a corresponding clientConnectionLost method for the Factory class. Any suggestions would be appreciated.
My approach to this was to put any post-connection cleanup in the Avatar's "logout" method.
Will that method be called regardless of how the client disconnects from the server?
On Nov 19, 2006, at 9:13 PM, Yi Qiang wrote:
On 11/19/06, Phil Christensen <phil@bubblehouse.org> wrote: On Nov 19, 2006, at 7:00 PM, Yi Qiang wrote:
There does not seem to be a corresponding clientConnectionLost method for the Factory class. Any suggestions would be appreciated.
My approach to this was to put any post-connection cleanup in the Avatar's "logout" method.
Will that method be called regardless of how the client disconnects from the server?
Yeah, in my experience the server *always* detects the connection loss and calls logout. Incidentally, this was with a standard reactor on a UNIXish system; I'm relatively sure this works this way on all platforms. -phil
On Mon, 20 Nov 2006 09:19:43 -0500, Phil Christensen <phil@bubblehouse.org> wrote:
On Nov 19, 2006, at 9:13 PM, Yi Qiang wrote:
On 11/19/06, Phil Christensen <phil@bubblehouse.org> wrote: On Nov 19, 2006, at 7:00 PM, Yi Qiang wrote:
There does not seem to be a corresponding clientConnectionLost method for the Factory class. Any suggestions would be appreciated.
My approach to this was to put any post-connection cleanup in the Avatar's "logout" method.
Will that method be called regardless of how the client disconnects from the server?
Yeah, in my experience the server *always* detects the connection loss and calls logout.
Incidentally, this was with a standard reactor on a UNIXish system; I'm relatively sure this works this way on all platforms.
If there is traffic over a connection, a disconnect will always be noticed eventually (it might take a few minutes, though). If a connection is idle, a disconnect can go unnoticed for a long time. This is just as much a problem with the cred "logout" method as using clientConnectionLost (since one relies on the other, of course). You can address this by using TCP keepalive or an application-level ping/pong message every few minutes, to make sure there is traffic on the connection. Of course your application may always keep the connection busy, so you may not need to worry about this at all. Jean-Paul
On Nov 20, 2006, at 9:41 AM, Jean-Paul Calderone wrote:
On Mon, 20 Nov 2006 09:19:43 -0500, Phil Christensen <phil@bubblehouse.org> wrote:
Yeah, in my experience the server *always* detects the connection loss and calls logout.
Incidentally, this was with a standard reactor on a UNIXish system; I'm relatively sure this works this way on all platforms.
If there is traffic over a connection, a disconnect will always be noticed eventually (it might take a few minutes, though).
If a connection is idle, a disconnect can go unnoticed for a long time. This is just as much a problem with the cred "logout" method as using clientConnectionLost (since one relies on the other, of course).
You can address this by using TCP keepalive or an application-level ping/pong message every few minutes, to make sure there is traffic on the connection.
Of course your application may always keep the connection busy, so you may not need to worry about this at all.
Yeah, just another anecdote w.r.t. JP's point here... While I found that the server was quick to notice disconnects, my client -- which is more of a passive participant, primarily receiving messages from the server -- would not notice the server disappearing until another outgoing message was sent (or attempted to be sent). I found implementing a noop keepalive took care of that. JP, could you elaborate on your comment about idle connections? Is the PB server supposed to disconnect idle connections after some period of time? If so, I'd definitely like to make sure this doesn't happen just because someone isn't paying attention to a client in the background. Thanks, -phil
participants (3)
-
Jean-Paul Calderone
-
Phil Christensen
-
Yi Qiang