def read(self, count=None): while self.get_state() == base.ST_SYNC: print("Stateful : Waiting for session to open") self.wait_for_state_change(5) if self.get_state() != base.ST_OPEN: raise base.SessionClosedError("State is %i" % self.get_state()) buf = self._read(count) if not buf and self.get_state() != base.ST_OPEN: raise base.SessionClosedError() return buf
def write(self, buf, timeout=0): while self.get_state() == base.ST_SYNC: print("Stateful : Waiting for session to open") self.wait_for_state_change(5) if self.get_state() != base.ST_OPEN: raise base.SessionClosedError("State is %s" % self.get_state()) blocks = [] while buf: chunk = buf[:self.bsize] buf = buf[self.bsize:] f = DDT2EncodedFrame() f.seq = self.oseq f.type = T_DAT f.data = chunk f.sent_event.clear() self.outq.enqueue(f) blocks.append(f) self.oseq = (self.oseq + 1) % 256 self.queue_next() self.event.set() while timeout is not None and \ blocks and \ self.get_state() != base.ST_CLSD: block = blocks[0] del blocks[0] print(("Stateful : Waiting for block %i to be ack'd" % block.seq)) block.sent_event.wait() if block.sent_event.isSet(): print(("Stateful : Block %i is sent, waiting for ack" % block.seq)) block.ackd_event.wait(timeout) if block.ackd_event.isSet() and block.sent_event.isSet(): print(("Stateful : %i ACKED" % block.seq)) else: print(("Stateful : %i Not ACKED (probably canceled)" % block.seq)) break else: print(("Stateful : Block %i not sent?" % block.seq))