class Sketch(object): __metaclass__ = ABCMeta def __init__(self, sleep_duration=c.host.default_sleep_duration): self.sleep_duration = sleep_duration self.tamp = TAMProxy() self.stopped = False self.start_time = None @abstractmethod def setup(self): raise NotImplementedError @abstractmethod def loop(self): raise NotImplementedError def start(self): self.start_time = time() self.iterations = 0 # float cuz this can get big if not self.tamp.started: self.tamp.start() self.stopped = False def stop(self): self.stopped = True @property def elapsed(self): return time() - self.start_time @property def throughput(self): return self.tamp.pf.packets_received / self.elapsed @property def frequency(self): return self.iterations / self.elapsed def run(self): self.start() try: self.setup() print "Entered loop" while not self.stopped: self.loop() self.iterations += 1 sleep(self.sleep_duration) except KeyboardInterrupt: self.stop() # as if the sketch had called it self.tamp.stop() print "Sketch finished running"
class Sketch(object): __metaclass__ = ABCMeta def __init__(self, sleep_duration=c.host.default_sleep_duration): self.sleep_duration = sleep_duration self.tamp = TAMProxy() self.stopped = False self.start_time = None @abstractmethod def setup(self): raise NotImplementedError @abstractmethod def loop(self): raise NotImplementedError def pre_setup(self): self.start_time = time() self.iterations = 0 self.tamp.start() self.stopped = False def post_setup(self): self.tamp.pf.pc.set_continuous_enabled(True) print "Entering Loop" def pre_loop(self): pass def post_loop(self): self.iterations += 1 sleep(self.sleep_duration) def on_exit(self): self.tamp.stop() print "Sketch finished running" def stop(self): self.stopped = True @property def elapsed(self): return time() - self.start_time @property def throughput(self): return self.tamp.pf.packets_received / self.elapsed @property def frequency(self): return self.iterations / self.elapsed def run(self): try: self.pre_setup() self.setup() self.post_setup() while not self.stopped: self.pre_loop() self.loop() self.post_loop() except KeyboardInterrupt: self.stop() # as if the sketch had called it self.on_exit()