in scripts/mcast.py [0:0]
def loop(self, interval=1):
packets = dict()
try:
while True:
(segment, (ip4_addr, port)) = self.sock.recvfrom(1024)
if not segment:
logging.info("Exiting as received an empty packet...")
sys.exit(0)
tup = segment.split()
if len(tup) < 3:
logging.info("Exiting as received an empty data packet...")
sys.exit(0)
# @see DATA_FMT
(id, time_sent, data) = tup
try:
id = int(id)
time_sent = float(time_sent)
except ValueError:
logging.warn("Unexpected formatted data was received. Skip it.")
continue
last_ids = packets.get((ip4_addr, port), [])
if last_ids:
last_id = last_ids[-1]
else:
last_id = 0
packets[(ip4_addr, port)] = []
delta = time.time() - time_sent
from_s = "from %s:%d" % (ip4_addr, port)
logging.info("Received '%s' (#%d) %s, time=%f" % (data, id, from_s, delta))
if id < last_id:
logging.debug("Inversion! #%d %s is younger than last one (#%d)." % (id, from_s, last_id))
elif id == last_id:
logging.debug("DUP segment! #%d %s" % (id, from_s))
else:
if id > (last_id + 1):
last_received = packets[(ip4_addr, port)]
losts = ["#%d" % i for i in range(last_id + 1, id) if i not in last_received]
losts_s = ", ".join(losts)
logging.debug("LOST segments! %s %s" % (losts_s, from_s))
packets[(ip4_addr, port)].append(id)
# TODO: Send back to client.
#ssize = self.sock.sendto(segment, (ip4_addr, port))
#if ssize < len(segment):
# logging.warn("Failed to send back to the client: %s (port: %d)" % (ip4_addr, port))
time.sleep(interval)
except (KeyboardInterrupt, SystemExit):
logging.info("Exiting...")
self.dump_stat(packets)
except:
traceback.print_exc()