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 HeartbeatSubscriber(object): def __init__(self): self.topic = "sub"+uuid.uuid4().hex def start(self): self.dashi = DashiConnection(self.topic, "amqp://*****:*****@127.0.0.1//", "heartbeater") self.dashi.handle(self.heartbeat) consumer = gevent.spawn(self.dashi.consume) # send subscribe request ret = self.dashi.call("heartbeater", "subscribe", subscriber=self.topic) assert ret is True consumer.join() def heartbeat(self, beat): print("Got heartbeat: %s" % beat)
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 HeartbeatSubscriber(object): def __init__(self): self.topic = "sub" + uuid.uuid4().hex def start(self): self.dashi = DashiConnection(self.topic, "amqp://*****:*****@127.0.0.1//", "heartbeater") self.dashi.handle(self.heartbeat) consumer = gevent.spawn(self.dashi.consume) # send subscribe request ret = self.dashi.call("heartbeater", "subscribe", subscriber=self.topic) assert ret is True consumer.join() def heartbeat(self, beat): print "Got heartbeat: %s" % beat
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()
class StreamBossClient(object): def __init__(self): self.topic = "streamboss" self.dashi = DashiConnection("streambossweb", 'amqp://%s:%s@%s:%s//' % ( RABBITMQ_USER, RABBITMQ_PASSWORD, RMQHOST, RMQPORT), RABBITMQ_EXCHANGE, ssl=False, sysname=None) def get_streams(self): return self.dashi.call(self.topic, "get_all_streams") def create_stream(self, stream_name): self.dashi.call(self.topic, "create_stream", stream_name=stream_name) def create_operation(self, operation_name, process_definition_id, input_stream, output_stream): self.dashi.call(self.topic, "create_operation", operation_name=operation_name, process_definition_id=process_definition_id, input_stream=input_stream, output_stream=output_stream) def get_operations(self): return self.dashi.call(self.topic, "get_all_operations")