Discussion:
[Twisted-Python] Sending disconnect messages to clients
Chris Norman
2016-03-22 19:42:06 UTC
Permalink
Hi all,
I'm sure I asked this question before, but I can't find any answers on
google, and I've changed my work flow a bit now, so thought it was worth
re-asking.

So on my server's protocol, I have a send method. This allows me to pass
arguments which get sent off to the client in the right format. The send
method looks something like this:

def send(self, command, **kwargs):
"""Cause the client to issue getattr(connection, command)(**kwargs).
If disconnect evaluates to True, disconnect the client after the message
is sent."""
disconnect = kwargs.get('disconnect', False)
try:
del kwargs['disconnect']
except KeyError:
pass # Argument not found.
d = defer.Deferred()
d.addCallback(self.prepare_command) Convert the command and kwargs to
json.
d.addCallback(self.deferred_write) # Write the json to the transport.
if disconnect:
d.addCallback(self.deferred_disconnect) # Issue
self.transport.loseConnection().
reactor.callFromThread(d.callback, [command, kwargs]) # Call the
deferred's callback chain.
return d # Return the deferred.

If I try something like:
protocol.send('alert', message = '*** Disconnected. ***', disconnect = True)
the client gets disconnected, but never sees the "*** Disconnected. ***"
message.

I guess I could do a reactor.callLater and just wait for the transport
to get the message, but that seems sloppy, and I can't help thinking
there must be something I'm missing.

Any ideas welcome!

Cheers all,
L. Daniel Burr
2016-03-22 19:49:46 UTC
Permalink
Hi Chris,

On March 22, 2016 at 2:42:14 PM, Chris Norman (***@googlemail.com) wrote:

Hi all, 
I'm sure I asked this question before, but I can't find any answers on 
google, and I've changed my work flow a bit now, so thought it was worth 
re-asking. 

So on my server's protocol, I have a send method. This allows me to pass 
arguments which get sent off to the client in the right format. The send 
method looks something like this: 

def send(self, command, **kwargs): 
"""Cause the client to issue getattr(connection, command)(**kwargs). 
If disconnect evaluates to True, disconnect the client after the message 
is sent.""" 
disconnect = kwargs.get('disconnect', False) 
try: 
del kwargs['disconnect'] 
except KeyError: 
pass # Argument not found. 
d = defer.Deferred() 
d.addCallback(self.prepare_command) Convert the command and kwargs to 
json. 
d.addCallback(self.deferred_write) # Write the json to the transport. 
if disconnect: 
d.addCallback(self.deferred_disconnect) # Issue 
self.transport.loseConnection(). 
You are disconnecting right here, without waiting for your Deferred to fire.  It might make better sense to add the call the self.transport.loseConnection() to your Deferred’s callback chain.

Hope this helps,

Daniel
--
L. Daniel Burr
***@me.com
(312) 656-8387
Chris Norman
2016-03-22 20:08:40 UTC
Permalink
Hi Daniel,
Post by L. Daniel Burr
Hi Chris,
On March 22, 2016 at 2:42:14 PM, Chris Norman
Post by Chris Norman
Hi all,
I'm sure I asked this question before, but I can't find any answers on
google, and I've changed my work flow a bit now, so thought it was worth
re-asking.
So on my server's protocol, I have a send method. This allows me to pass
arguments which get sent off to the client in the right format. The send
"""Cause the client to issue getattr(connection, command)(**kwargs).
If disconnect evaluates to True, disconnect the client after the message
is sent."""
disconnect = kwargs.get('disconnect', False)
del kwargs['disconnect']
pass # Argument not found.
d = defer.Deferred()
d.addCallback(self.prepare_command) Convert the command and kwargs to
json.
d.addCallback(self.deferred_write) # Write the json to the transport.
d.addCallback(self.deferred_disconnect) # Issue
self.transport.loseConnection().
You are disconnecting right here, without waiting for your Deferred to
fire. It might make better sense to add the call the
self.transport.loseConnection() to your Deferred’s callback chain.
Hope this helps,
Actually it's been added to the callback. I think my mail client wrapped
the lines, but issue self.transport.LoseConnection() is the comment, so
self.deferred_disconnect is added to the callback chain.

Sorry for the confusion.

Anything else which can cause this?

Cheers,
Post by L. Daniel Burr
Daniel
--
L. Daniel Burr
(312) 656-8387
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
L. Daniel Burr
2016-03-22 20:37:20 UTC
Permalink
Hi Chris,
On March 22, 2016 at 3:09:08 PM, Chris Norman (***@googlemail.com) wrote:

Hi Daniel,


[SNIP]

Actually it's been added to the callback. I think my mail client wrapped the lines, but issue self.transport.LoseConnection() is the comment, so self.deferred_disconnect is added to the callback chain.

Sorry for the confusion.

Anything else which can cause this?


Without seeing the rest of your code, I can’t suggest anything concrete.  You say the client never “sees” the message, but your call to loseConnection() still occurs, so you’re not encountering an exception during the callback chain.  Can you come up with an minimal example the demonstrates the problem?

Daniel
—
L. Daniel Burr
***@me.com
(312) 656-8387
Chris Norman
2016-03-22 21:14:37 UTC
Permalink
OK... ignore me.

I made your test program and it worked fine. So I tried connecting to my
server with telnet and saw the expected message... So I checked the
error function which should have been called on the client with the
particular message to see it was as good as pass...

Sorry for all the messages, everything works fine, panic over!

Thank you for your help with this!
Post by L. Daniel Burr
Hi Chris,
On March 22, 2016 at 3:09:08 PM, Chris Norman
Post by Chris Norman
Hi Daniel,
[SNIP]
Post by Chris Norman
Actually it's been added to the callback. I think my mail client
wrapped the lines, but issue self.transport.LoseConnection() is the
comment, so self.deferred_disconnect is added to the callback chain.
Sorry for the confusion.
Anything else which can cause this?
Without seeing the rest of your code, I can’t suggest anything
concrete. You say the client never “sees” the message, but your call
to loseConnection() still occurs, so you’re not encountering an
exception during the callback chain. Can you come up with an minimal
example the demonstrates the problem?
Daniel
—
L. Daniel Burr
(312) 656-8387
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Continue reading on narkive:
Loading...