""" shark.py """ import pyshark from constants import * def _run_shark(q_cont, q_mle, ife, df, mode, ns, time): num_pkts = 0 ts = time[0] tf = time[1] cap_live = pyshark.LiveCapture(interface=ife, display_filter=df) while True: if mode.value == MODE_IDLE: continue # mode is a global set during initialization by the browser elif mode.value == MODE_LIVE: cap_live.reset() for pkt in cap_live.sniff_continuously(): num_pkts = num_pkts + 1 try: msg_type = int(pkt.data.data[0:2],16) if msg_type < MSG_TYPE_NOTIFY_MLE: q_cont.put(pkt) print ('put in Cont Q: ', num_pkts) elif msg_type == MSG_TYPE_NOTIFY_MLE: q_mle.put(pkt) print ('put in MLE Q: ', num_pkts) except Exception as e: print(str(e)) pass if mode.value == MODE_IDLE: break elif mode.value == MODE_FILE: cap_file = pyshark.FileCapture(input_file=LOG_PATH + ns.filename, display_filter=df) for pkt in cap_file: num_pkts = num_pkts + 1 time = float(pkt.frame_info.get_field("frame.time_relative").__str__()[0:9]) if time >= ts and time < tf: q_cont.put(pkt) if DEBUG: print ('put in Q: ', num_pkts) if DEBUG: print("file replay finished") cap_file.close() mode.value = MODE_IDLE