viff

changeset 827:58c1a0dfbcfc

Fixed runtime shutdown. The runtime will now shutdown correctly by closing the listening port and the pair-wise connections before calling reactor.stop().
author Martin Geisler <mg@daimi.au.dk>
date Sun, 13 Jul 2008 16:28:15 +0200
parents 420569d71d4d
children fe3bcd629827
files viff/runtime.py
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Sun Jul 13 15:21:40 2008 +0200
     1.2 +++ b/viff/runtime.py	Sun Jul 13 16:28:15 2008 +0200
     1.3 @@ -47,6 +47,7 @@
     1.4  from twisted.internet import reactor
     1.5  from twisted.internet.error import ConnectionDone
     1.6  from twisted.internet.defer import Deferred, DeferredList, gatherResults, succeed
     1.7 +from twisted.internet.defer import maybeDeferred
     1.8  from twisted.internet.protocol import ClientFactory, ServerFactory
     1.9  from twisted.protocols.basic import Int16StringReceiver
    1.10  
    1.11 @@ -251,7 +252,7 @@
    1.12  
    1.13      def __init__(self):
    1.14          self.peer_id = None
    1.15 -
    1.16 +        self.lost_connection = Deferred()
    1.17          #: Data expected to be received in the future.
    1.18          self.incoming_data = {}
    1.19  
    1.20 @@ -260,6 +261,7 @@
    1.21  
    1.22      def connectionLost(self, reason):
    1.23          reason.trap(ConnectionDone)
    1.24 +        self.lost_connection.callback(self)
    1.25  
    1.26      def stringReceived(self, string):
    1.27          """Called when a share is received.
    1.28 @@ -497,15 +499,27 @@
    1.29          All connections are closed and the runtime cannot be used
    1.30          again after this has been called.
    1.31          """
    1.32 +        print "Synchronizing shutdown... ",
    1.33  
    1.34 -        def stop(_):
    1.35 -            print "Initiating shutdown sequence."
    1.36 +        def close_connections(_):
    1.37 +            print "done."
    1.38 +            print "Closing connections... ",
    1.39 +            results = [maybeDeferred(self.port.stopListening)]
    1.40              for protocol in self.protocols.itervalues():
    1.41 +                results.append(protocol.lost_connection)
    1.42                  protocol.loseConnection()
    1.43 +            return DeferredList(results)
    1.44 +
    1.45 +        def stop_reactor(_):
    1.46 +            print "done."
    1.47 +            print "Stopping reactor... ",
    1.48              reactor.stop()
    1.49 +            print "done."
    1.50  
    1.51          sync = self.synchronize()
    1.52 -        sync.addCallback(stop)
    1.53 +        sync.addCallback(close_connections) 
    1.54 +        sync.addCallback(stop_reactor)
    1.55 +        return sync
    1.56  
    1.57      def wait_for(self, *vars):
    1.58          """Make the runtime wait for the variables given.
    1.59 @@ -1522,10 +1536,10 @@
    1.60                  return self.ctx
    1.61  
    1.62          ctx_factory = SSLContextFactory(id)
    1.63 -        reactor.listenSSL(players[id].port, factory, ctx_factory)
    1.64 +        runtime.port = reactor.listenSSL(players[id].port, factory, ctx_factory)
    1.65      else:
    1.66          print "Not using SSL"
    1.67 -        reactor.listenTCP(players[id].port, factory)
    1.68 +        runtime.port = reactor.listenTCP(players[id].port, factory)
    1.69  
    1.70      for peer_id, player in players.iteritems():
    1.71          if peer_id > id: