class Receiver(Link): def __init__(self, connection, name, source): Link.__init__(self, connection) self.proto = ProtoReceiver(name, source, None) @synchronized def flow(self, limit, drain=False): self.proto.flow(limit, drain) @synchronized def pending(self, block=False, timeout=None): if block: self.wait(self._pending_unblocked, timeout) return self.proto.pending() def _pending_unblocked(self): return self.credit() == 0 or self.proto.pending() > 0 @synchronized def draining(self, block=False, timeout=None): if block: self.wait(self._draining_unblocked, timeout) return self.proto.draining() def _draining_unblocked(self): return self.proto.draining() @synchronized def get(self): return decode(self.proto.get())
def session(): from connection import Connection from session import Session from link import link, Sender, Receiver from protocol import Fragment, Linkage a = Connection(lambda n: Session(n, link)) a.tracing = set(["ops", "err"]) a.id = "A" b = Connection(lambda n: Session(n, link)) b.tracing = set(["err"]) b.id = "B" ssn = Session("test", link) a.add(ssn) ssn.begin() # nss = Session("test", link) # b.add(nss) def pump(): a.tick() b.tick() b.write(a.read()) a.write(b.read()) pump() nss = [s for s in b.incoming.values() if s.name == "test"][0] nss.begin() snd = Sender("L", "S", "T") ssn.add(snd) rcv = Receiver("L", "S", "T") nss.add(rcv) snd.attach() rcv.attach() rcv.flow(10) pump() snd.send(fragments=Fragment(True, True, 0, 0, "m1")) snd.send(fragments=Fragment(True, True, 0, 0, "m2")) dt3 = snd.send(fragments=Fragment(True, True, 0, 0, "m3")) pump() print "PENDING", rcv.pending() pump() snd.send(fragments=Fragment(True, True, 0, 0, "m4")) pump() xfrs = [] while rcv.pending(): x = rcv.get() xfrs.append(x) print "XFR", x rcv.disposition(xfrs[-1].delivery_tag, "ACCEPTED") pump() snd.send(fragments=Fragment(True, True, 0, 0, "m5")) pump() rcv.disposition(xfrs[0].delivery_tag, "ACCEPTED") print "----------" pump() print "----------" print "ssn.outgoing:", ssn.outgoing print "snd.unsettled:", snd.unsettled for xfr in xfrs[1:-1]: rcv.disposition(xfr.delivery_tag, "ACCEPTED") print "rcv.unsettled", rcv.unsettled print "rcv.pending()", rcv.pending() rcv.disposition(rcv.get().delivery_tag) pump() print "----------" print "ssn.outgoing:", ssn.outgoing print "snd.unsettled:", snd.unsettled print "settling:", dt3 snd.settle(dt3) print "ssn.outgoing:", ssn.outgoing print "snd.unsettled:", snd.unsettled for dt in list(snd.unsettled): snd.settle(dt) snd.detach() rcv.detach() pump() print "ssn.outgoing:", ssn.outgoing print "snd.unsettled:", snd.unsettled