class HeartbeatService(object): def __init__(self): self.topic = "heartbeater" self.subscribers = [] self.counter = count(0) def start(self): self.dashi = DashiConnection(self.topic, "amqp://*****:*****@127.0.0.1//", "heartbeater") # support the "subscribe" operation with this method self.dashi.handle(self.subscribe) gevent.spawn(self.dashi.consume) gevent.spawn(self._beater).join() def subscribe(self, subscriber): print "Got subscription: subscriber=%s" % subscriber self.subscribers.append(subscriber) return True def _beater(self): while True: n = self.counter.next() logging.debug("Beat %s", n) for subscriber in self.subscribers: self.dashi.fire(subscriber, "heartbeat", beat=n) gevent.sleep(1)
class HeartbeatService(object): def __init__(self): self.topic = "heartbeater" self.subscribers = [] self.counter = count(0) def start(self): self.dashi = DashiConnection(self.topic, "amqp://*****:*****@127.0.0.1//", "heartbeater") # support the "subscribe" operation with this method self.dashi.handle(self.subscribe) gevent.spawn(self.dashi.consume) gevent.spawn(self._beater).join() def subscribe(self, subscriber): print "Got subscription: subscriber=%s" % subscriber self.subscribers.append(subscriber) return True def _beater(self): while True: n = self.counter.next() logging.debug("Beat %s", n) for subscriber in self.subscribers: self.dashi.fire(subscriber, "heartbeat", beat=n) gevent.sleep(1)
class rpc: def __init__(self, name, loghandle=None): self.name = name if loghandle: self.loghandle = loghandle else: self.loghandle = name dashiconfig = domosSettings.getExchangeConfig() self.dashi = DashiConnection(self.name, dashiconfig["amqp_uri"], dashiconfig['exchange'], sysname=dashiconfig['prefix']) def handle(self, func, handle): self.dashi.handle(func, handle) def fire(self, target, func, **kwargs): self.dashi.fire(target, func, **kwargs) def call(self, target, func, **kwargs): return self.dashi.call(target, func, **kwargs) def logmsg(self, lvl, msg): call = 'log_{}'.format(lvl) self.fire('log', call, msg=msg, handle=self.loghandle) def log_debug(self, msg): self.fire('log', 'log_debug', msg=msg, handle=self.loghandle) def log_info(self, msg): self.fire('log', 'log_info', msg=msg, handle=self.loghandle) def log_warn(self, msg): self.fire('log', 'log_warn', msg=msg, handle=self.loghandle) def log_error(self, msg): self.fire('log', 'log_error', msg=msg, handle=self.loghandle) def log_crit(self, msg): self.fire('log', 'log_crit', msg=msg, handle=self.loghandle) def listen(self, timeout=2): try: self.dashi.consume(timeout=timeout) except socket.timeout as ex: pass
class DashiTalker(Thread): def __init__(self, console, name): Thread.__init__(self) self.name = name self.done = False self.exchange = "default_dashi_exchange" global g_rabbit_url self.dashi = DashiConnection(self.name, g_rabbit_url, self.exchange, ssl=False) self.subscribers = [] self.console = console self.dashi.handle(self.new_joined_chat, "new_joined_chat") self.dashi.handle(self.incoming_message, "incoming_message") def new_joined_chat(self, subscriber): self.subscribers.append(subscriber) self.console.write("%s has entered the room" % (subscriber)) return True def input_message(self, msg, sender_name=None): if sender_name: msg = "%s said: %s" % (sender_name, msg) for subscriber in self.subscribers: self.dashi.fire(subscriber, "incoming_message", message=msg) def request_conversation(self, with_who): rc = self.dashi.call(with_who, "new_joined_chat", subscriber=self.name) if rc: self.subscribers.append(with_who) self.console.write("You have contact with %s" % (with_who)) def incoming_message(self, message): self.console.write(message) def run(self): while not self.done: try: self.dashi.consume(timeout=2) except socket.timeout as ex: pass def end(self): self.done = True self.input_message("%s has left the room" % (self.name))
class DashiTalker(Thread): def __init__(self, console, name): Thread.__init__(self) self.name = name self.done = False self.exchange = "default_dashi_exchange" global g_rabbit_url self.dashi = DashiConnection(self.name, g_rabbit_url, self.exchange, ssl=True) self.subscribers = [] self.console = console self.dashi.handle(self.new_joined_chat, "new_joined_chat") self.dashi.handle(self.incoming_message, "incoming_message") def new_joined_chat(self, subscriber): self.subscribers.append(subscriber) self.console.write("%s has entered the room" % (subscriber)) return True def input_message(self, msg, sender_name=None): if sender_name: msg = "%s said: %s" % (sender_name, msg) for subscriber in self.subscribers: self.dashi.fire(subscriber, "incoming_message", message=msg) def request_conversation(self, with_who): rc = self.dashi.call(with_who, "new_joined_chat", subscriber=self.name) if rc: self.subscribers.append(with_who) self.console.write("You have contact with %s" % (with_who)) def incoming_message(self, message): self.console.write(message) def run(self): while not self.done: try: self.dashi.consume(timeout=2) except socket.timeout, ex: pass
class DashiCeiConnection(CeiConnection): _name = 'ceiclient' def __init__(self, broker, username, password, exchange=None, timeout=None, port=5672, ssl=False, sysname=None): self.amqp_broker = broker self.amqp_username = username self.amqp_password = password self.amqp_port = port self.sysname = sysname self.amqp_exchange = exchange or DEFAULT_EXCHANGE self.timeout = timeout self.dashi_connection = DashiConnection(self._name, 'amqp://%s:%s@%s:%s//' % ( self.amqp_username, self.amqp_password, self.amqp_broker, self.amqp_port), self.amqp_exchange, ssl=ssl, sysname=self.sysname) def call(self, service, operation, **kwargs): try: return self.dashi_connection.call(service, operation, self.timeout, **kwargs) except socket.timeout as e: raise CeiClientError("timed out") except socket.error as e: raise CeiClientError(e) def fire(self, service, operation, **kwargs): try: return self.dashi_connection.fire(service, operation, **kwargs) except socket.timeout as e: raise CeiClientError("timed out") except socket.error as e: raise CeiClientError(e) def disconnect(self): self.dashi_connection.disconnect()