viff

changeset 1190:b1ee0d5e116f

Made it possible to run with reactors other than ViffReactor as before.
author Marcel Keller <mkeller@cs.au.dk>
date Wed, 27 May 2009 16:12:42 +0200
parents d8337973e050
children 8ec45943c12a c15bc2925969
files viff/runtime.py
diffstat 1 files changed, 14 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Fri May 22 11:23:20 2009 +0200
     1.2 +++ b/viff/runtime.py	Wed May 27 16:12:42 2009 +0200
     1.3 @@ -40,6 +40,7 @@
     1.4  
     1.5  from viff.field import GF256, FieldElement
     1.6  from viff.util import wrapper, rand, deep_wait, track_memory_usage
     1.7 +import viff.reactor
     1.8  
     1.9  from twisted.internet import reactor
    1.10  from twisted.internet.task import LoopingCall
    1.11 @@ -318,10 +319,7 @@
    1.12                      deferred = deq.popleft()
    1.13                      if not deq:
    1.14                          del self.waiting_deferreds[key]
    1.15 -
    1.16 -                    # Just queue, callbacks will be executed
    1.17 -                    # in process_deferred_queue().
    1.18 -                    self.factory.runtime.queue_deferred(deferred, data)
    1.19 +                    self.factory.runtime.handle_deferred_data(deferred, data)
    1.20                  else:
    1.21                      deq = self.incoming_data.setdefault(key, deque())
    1.22                      deq.append(data)
    1.23 @@ -544,6 +542,8 @@
    1.24          #: Record the recursion depth.
    1.25          self.depth_counter = 0
    1.26          self.max_depth = 0
    1.27 +        #: Use deferred queues only if the ViffReactor is running.
    1.28 +        self.using_viff_reactor = isinstance(reactor, viff.reactor.ViffReactor)
    1.29  
    1.30      def add_player(self, player, protocol):
    1.31          self.players[player.id] = player
    1.32 @@ -792,10 +792,14 @@
    1.33          Python integer."""
    1.34          raise NotImplemented("Override this abstract method in a subclass.")
    1.35  
    1.36 -    def queue_deferred(self, deferred, data):
    1.37 -        """Put deferred and data into the queue."""
    1.38 +    def handle_deferred_data(self, deferred, data):
    1.39 +        """Put deferred and data into the queue if the ViffReactor is running. 
    1.40 +        Otherwise, just execute the callback."""
    1.41  
    1.42 -        self.deferred_queue.append((deferred, data))
    1.43 +        if (self.using_viff_reactor):
    1.44 +            self.deferred_queue.append((deferred, data))
    1.45 +        else:
    1.46 +            deferred.callback(data)
    1.47  
    1.48      def process_deferred_queue(self):
    1.49          """Execute the callbacks of the deferreds in the queue.
    1.50 @@ -987,8 +991,9 @@
    1.51              print "Will connect to %s" % player
    1.52              connect(player.host, player.port)
    1.53  
    1.54 -    # Process the deferred queue after every reactor iteration.
    1.55 -    reactor.setLoopCall(runtime.process_deferred_queue)
    1.56 +    if runtime.using_viff_reactor:
    1.57 +        # Process the deferred queue after every reactor iteration.
    1.58 +        reactor.setLoopCall(runtime.process_deferred_queue)
    1.59  
    1.60      return result
    1.61