From c945258a9f4c89bf8f19108ed0ee2f477d5a9493 Mon Sep 17 00:00:00 2001 From: Bernard Blackham Date: Mon, 2 Aug 2004 09:36:27 +0000 Subject: [PATCH] Added random idler choice. --- sql-edition/servers/Idler.py | 11 ++++++++-- sql-edition/servers/VendServer.py | 36 +++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/sql-edition/servers/Idler.py b/sql-edition/servers/Idler.py index ecf3e36..4efb56f 100755 --- a/sql-edition/servers/Idler.py +++ b/sql-edition/servers/Idler.py @@ -9,6 +9,9 @@ class Idler: def next(self): pass + def reset(self): + pass + class TrainIdler(Idler): def __init__(self, v): self.idle_state = 0 @@ -68,6 +71,9 @@ class TrainIdler(Idler): self.idle_state += 1 self.idle_state %= 18*36*54 + def reset(self): + self.idle_state = 0 + class GrayIdler(Idler): def __init__(self, v, one=None, zero=None): self.bits = 8 @@ -129,5 +135,6 @@ class GrayIdler(Idler): return bin - - + def reset(self): + self.i = 0 + self.grayCode = 0 diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index f7bd1cd..bed5366 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -209,6 +209,28 @@ class MessageKeeper: def done(self): return len(self.scrolling_message) == 0 +idlers = [] +idler = None +def setup_idlers(v): + global idlers, idler + idlers = [ + TrainIdler(v), + GrayIdler(v), + GrayIdler(v,one="*",zero="-"), + GrayIdler(v,one="/",zero="\\"), + GrayIdler(v,one="X",zero="O"), + ] + idler = choose_idler() + +def choose_idler(): + global idler + idler = idlers[int(random()*len(idlers))] + idler.reset() + +def idle_step(): + global idler + idler.next() + def run_forever(rfh, wfh, options, cf): v = VendingMachine(rfh, wfh) logging.debug('PING is ' + str(v.ping())) @@ -221,9 +243,7 @@ def run_forever(rfh, wfh, options, cf): mk = MessageKeeper(v) mk.set_message(GREETING) time_to_autologout = None - #idler = TrainIdler(v) - #idler = GrayIdler(v) - idler = GrayIdler(v,one="*",zero="-") + setup_idlers(v) time_to_idle = None last_timeout_refresh = None @@ -254,15 +274,17 @@ def run_forever(rfh, wfh, options, cf): # start autologout time_to_autologout = time() + 15 - if time_to_idle == None and cur_user == '': time_to_idle = time() + 60 + if time_to_idle == None and cur_user == '': + time_to_idle = time() + 2 + choose_idler() if time_to_idle != None and cur_user != '': time_to_idle = None - if time_to_idle is not None and time() > time_to_idle: idler.next() + if time_to_idle is not None and time() > time_to_idle: idle_step() mk.update_display() e = v.next_event(0) if e == None: - e = v.next_event(0.1) + e = v.next_event(0.05) if e == None: continue time_to_idle = None @@ -556,6 +578,8 @@ if __name__ == '__main__': clean_up_nicely(options, config_opts) logging.warning("Vend Server stopped") break + except SystemExit: + break except: (exc_type, exc_value, exc_traceback) = sys.exc_info() tb = format_tb(exc_traceback, 20) -- 2.20.1