def setup_zmq_server(): global proxy global foo global url fail = True while fail: try: url = "tcp://127.0.0.1:" + str(get_ephemeral_port()) proxy = ZMQJSONProxyServer(ctx, url) fail = False except ZMQError: pass foo = Foo() proxy.expose("foo", foo) def threadfunc(): proxy.run_loop() # Run the proxy in a separate thread. TBF: Caution! exceptions here # leave this running, because the 'proxy.quit_loop()' call is # skipped. Thus the unit test will hang. This is why asserts come # after 'proxy.quit_loop()'. threading.Thread(target=threadfunc).start()
def __init__(self, bank_name=None, url=None, sim=False): Thread.__init__(self, name=bank_name) self.bank = Bank(bank_name=bank_name.upper(), simulate=sim) ctx = zmq.Context() self.proxy = ZMQJSONProxyServer(ctx, url) self.proxy.expose("bank", self.bank) self.proxy.expose("bank.roach", self.bank.roach) self.proxy.expose("bank.valon", self.bank.valon)
def main_loop(bank_name=None, URL=None, sim=False): # The proxy server, can proxy many classes. global proxy ctx = zmq.Context() if not URL: dibas_dir = os.getenv('DIBAS_DIR') if dibas_dir == None: raise Exception("'DIBAS_DIR' is not set!") config_file = dibas_dir + '/etc/config/dibas.conf' config = ConfigParser.ConfigParser() config.readfp(open(config_file)) if bank_name: playerport = config.getint(bank_name.upper(), 'player_port') else: playerport = config.getint('DEFAULTS', 'player_port') URL = "tcp://0.0.0.0:%i" % playerport proxy = ZMQJSONProxyServer(ctx, URL) # A class to expose bank = Bank(bank_name, sim) # Expose some interfaces. The classes can be any class, including # contained within another exposed class. The name can be anything # at all that uniquely identifies the interface. proxy.expose("bank", bank) proxy.expose("bank.roach", bank.roach) proxy.expose("bank.valon", bank.valon) # Run the proxy: proxy.run_loop(bank._watchdog)
def main_loop(bank_name = None, URL = None, sim = False): # The proxy server, can proxy many classes. global proxy ctx = zmq.Context() if not URL: dibas_dir = os.getenv('DIBAS_DIR') if dibas_dir == None: raise Exception("'DIBAS_DIR' is not set!") config_file = dibas_dir + '/etc/config/dibas.conf' config = ConfigParser.ConfigParser() config.readfp(open(config_file)) if bank_name: playerport = config.getint(bank_name.upper(), 'player_port') else: playerport = config.getint('DEFAULTS', 'player_port') URL = "tcp://0.0.0.0:%i" % playerport proxy = ZMQJSONProxyServer(ctx, URL) # A class to expose bank = Bank(bank_name, sim) # Expose some interfaces. The classes can be any class, including # contained within another exposed class. The name can be anything # at all that uniquely identifies the interface. proxy.expose("bank", bank) proxy.expose("bank.roach", bank.roach) proxy.expose("bank.valon", bank.valon) # Run the proxy: proxy.run_loop(bank._watchdog)
class BankThread(Thread): def __init__(self, bank_name=None, url=None, sim=False): Thread.__init__(self, name=bank_name) self.bank = Bank(bank_name=bank_name.upper(), simulate=sim) ctx = zmq.Context() self.proxy = ZMQJSONProxyServer(ctx, url) self.proxy.expose("bank", self.bank) self.proxy.expose("bank.roach", self.bank.roach) self.proxy.expose("bank.valon", self.bank.valon) def run(self): self.proxy.run_loop(watchdogfn=self.bank._watchdog) def stop_thread(self): print "running quit thread" self.proxy.quit_loop()