Hi,
It's a MUD server, so players type in commands and receive textual responses.

One of the admin commands is the ability to shutdown the server (or CTRL-C might be pressed on the console). I'd like this action to notify all connected transports that the server is going down for shutdown, so they're not rudely disconnected, then once the notifications have all gone through, then the server is free to shutdown.

I hope all this makes sense.

Cheers,

On 12/17/2015 11:49 AM, Glyph Lefkowitz wrote:

On Dec 16, 2015, at 9:25 AM, Chris Norman <chris.norman2@googlemail.com> wrote:

Hi all,
I'm writing a MUD server, and I want a way for transports to be notified ofa shutdown before being disconnected, and the reactor being stopped.

I've tried:

for t in transports:
t.write('Shutting down.\r\n')
t.loseConnection()
reactor.stop()

This doesn't seem to notify the transports.

I also tried:
for t in transports:
t.write('Shutting down.\r\n')
t.loseConnection()
while t.connected:
 pass
reactor.stop()

That just blocked and did nothing, presumably something do with my while loop.

Is there a stopWhenEmpty function on the somewhere? I did look over the methods, and I couldn't find anything promising.

I'm just using the standard from twisted.internet import reactor reactor, so no special cases here. In case it matters the transports I'm using are twisted.protocols.basic.LineReceiver, and everything else works with them.

Cheers in advance for the help.

This is definitely doable, but before I explain it would help to know why you want to do this.

The reason I ask is: servers crash; hardware fails.  The falcon cannot hear the falconer; things fall apart; the centre cannot hold.

When those servers do crash (and they will), you don't get a clean notification of disconnects.  So if you're writing your application to rely very heavily on the ability to do a clean shutdown and get notifications of every disconnect at the time you expect reactor.stop() to be running, you are probably designing your system in a way that will be very fragile and prone to data loss.  I know, I have made this mistake more than once myself :).

So, before you continue: do you actually need to do this?  Could you just ignore the notification of the connection drop and exit gracefully, perhaps properly cleaning up whatever state is left over at next startup?  If you really need this, understanding why you need it would also help in determining which implementation technique to suggest (there are a few).

-glyph



_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python