def abort(self, protocol, exc): """Abort the execution due to an exception. The *protocol* received bad data which resulted in *exc* being raised when unpacking. """ print "*** bad data from Player %d: %s" % (protocol.peer_id, exc) print "*** aborting!" for p in self.protocols.itervalues(): p.loseConnection() reactor.stop() print "*** all protocols disconnected"
def auction(rt): def debug(low, mid, high): string = [" " for _ in range(high+1)] string[low] = " |" string[mid] = " ^" string[high] = " |" print "B: " + " ".join(["%2d" % b for b in B]) print "S: " + " ".join(["%2d" % s for s in S]) print " " + " ".join(["%2d" % x for x in range(len(B)+1)]) print " " + " ".join(string) def branch(result, low, mid, high): print "low: %d, high: %d, last result: %s" % (low, high, result) timestamp() if result == 1: low = mid else: high = mid if low+1 < high: mid = (low + high)//2 if options.verbose: debug(low, mid, high) result = rt.open(buyer_bids[mid] >= seller_bids[mid]) result.addCallback(output, "%s >= %s: %%s" % (B[mid], S[mid])) result.addCallback(branch, low, mid, high) return result else: if options.verbose: debug(low, mid, high) return low def check_result(result): expected = max([i for i, (b, s) in enumerate(zip(B, S)) if b > s]) if result == expected: print "Result: %d (correct)" % result else: print "Result: %d (incorrect, expected %d)" % (result, expected) result = branch(0, 0, len(seller_bids), 0) result.addCallback(check_result) result.addCallback(lambda _: reactor.stop())
def auction(rt): def debug(low, mid, high): string = [" " for _ in range(high + 1)] string[low] = " |" string[mid] = " ^" string[high] = " |" print "B: " + " ".join(["%2d" % b for b in B]) print "S: " + " ".join(["%2d" % s for s in S]) print " " + " ".join(["%2d" % x for x in range(len(B) + 1)]) print " " + " ".join(string) def branch(result, low, mid, high): print "low: %d, high: %d, last result: %s" % (low, high, result) timestamp() if result == 1: low = mid else: high = mid if low + 1 < high: mid = (low + high) // 2 if options.verbose: debug(low, mid, high) result = rt.open(buyer_bids[mid] >= seller_bids[mid]) result.addCallback(output, "%s >= %s: %%s" % (B[mid], S[mid])) result.addCallback(branch, low, mid, high) return result else: if options.verbose: debug(low, mid, high) return low def check_result(result): expected = max([i for i, (b, s) in enumerate(zip(B, S)) if b > s]) if result == expected: print "Result: %d (correct)" % result else: print "Result: %d (incorrect, expected %d)" % (result, expected) result = branch(0, 0, len(seller_bids), 0) result.addCallback(check_result) result.addCallback(lambda _: reactor.stop())
def stop_reactor(_): print "done." print "Stopping reactor...", reactor.stop() print "done."
def finish(*x): reactor.stop() print "Stopped reactor"