def ack_req(self, dest, data): f = DDT2EncodedFrame() f.type = T_ACK f.seq = 0 f.d_station = dest f.data = data self._sm.outgoing(self, f)
def new_session(self, session): f = DDT2EncodedFrame() f.type = T_NEW + session.type f.seq = 0 f.d_station = session._st f.data = struct.pack("B", int(session._id)) + session.name wait_time = 5 for i in range(0, 10): self._sm.outgoing(self, f) f.sent_event.wait(10) f.sent_event.clear() print "Sent request, blocking..." session.wait_for_state_change(wait_time) state = session.get_state() if state == base.ST_CLSD: print "Session is closed" break elif state == base.ST_SYNC: print "Waiting for synchronization" wait_time = 15 else: print "Established session %i:%i" % (session._id, session._rs) session.set_state(base.ST_OPEN) return True session.set_state(base.ST_CLSD) print "Failed to establish session" return False
def advertise_status(self, stat, msg): if stat > station_status.STATUS_MAX or stat < station_status.STATUS_MIN: raise Exception("Status integer %i out of range" % stat) f = DDT2EncodedFrame() f.d_station = "CQCQCQ" f.type = self.T_STATUS f.data = "%i%s" % (stat, msg) self._sm.outgoing(self, f)
def ping_station(self, station): f = DDT2EncodedFrame() f.d_station = station f.type = self.T_PNG_REQ f.data = "Ping Request" f.set_compress(False) self._sm.outgoing(self, f) self._emit("ping-request", f.s_station, f.d_station, "Request")
def send_ack(self, blocks): f = DDT2EncodedFrame() f.seq = 0 f.type = T_ACK f.data = "".join([chr(x) for x in blocks]) print(("Stateful : Acking blocks %s (%s)" % (blocks, {"": f.data}))) self._sm.outgoing(self, f)
def send_reqack(self, blocks): f = DDT2EncodedFrame() f.seq = 0 f.type = T_REQACK # FIXME: This needs to support 16-bit block numbers! f.data = "".join([chr(x) for x in blocks]) print(("Stateful : Requesting ack of blocks %s" % blocks)) self._sm.outgoing(self, f)
def write(self, data, dest="CQCQCQ"): f = DDT2EncodedFrame() f.seq = 0 f.type = self.T_DEF f.d_station = dest f.data = data f.set_compress(self.compress) self._sm.outgoing(self, f)
def ping_echo_station(self, station, data, cb=None, *cbdata): if cb: self.__ping_handlers[station] = (cb, cbdata) f = DDT2EncodedFrame() f.d_station = station f.type = self.T_PNG_ERQ f.data = data f.set_compress(False) self._sm.outgoing(self, f) self._emit("ping-request", f.s_station, f.d_station, "%s %i %s" % (_("Echo of"), len(data), _("bytes")))
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))
def end_session(self, session): if session.stateless: return while session.get_state() == base.ST_SYNC: print "Waiting for session in SYNC" session.wait_for_state_change(2) f = DDT2EncodedFrame() f.type = T_END f.seq = 0 f.d_station = session._st if session._rs: f.data = str(session._rs) else: f.data = str(session._id) session.set_state(base.ST_CLSW) for i in range(0, 3): print "Sending End-of-Session" self._sm.outgoing(self, f) f.sent_event.wait(10) f.sent_event.clear() print "Sent, waiting for response" session.wait_for_state_change(15) if session.get_state() == base.ST_CLSD: print "Session closed" return True session.set_state(base.ST_CLSD) print "Session closed because no response" return False