def publish(self, method, message=None, key=None): key = key if key else self.key message = message if message else {} message['method'] = method self.logger.info("Publishing: %s" % message) message = json.dumps(message, cls=ComplexEncoder, encoding='utf-8') message = aes.encrypt(message, key) st = self.name+message self.pubsub.publish(st) return True
def initialize_rpc(self, obj, **kwargs): rpc = zmq.Context() rpc_socket = rpc.socket(zmq.REP) rpc_socket.bind("tcp://*:%s" % obj.get('port')) self.logger.info("RPC listening on: %s" % obj.get('port')) settings.KEY = base64.urlsafe_b64decode(str(obj.get('key'))) self.logger.info("%s Initialized" % self.name) while True: if self.initializing: self.initializing = False self.publish(dict(method='initialized')) message = aes.decrypt(rpc_socket.recv(), settings.KEY) ob = json.loads(message) try: res = getattr(self, ob.get("method"))(ob) st = json.dumps(res, cls=ComplexEncoder) rpc_socket.send(aes.encrypt(st, settings.KEY)) except Exception as e: self.logger.exception(e) gevent.sleep(.1)
def run(self): rpc = zmq.Context() rpc_socket = rpc.socket(zmq.REP) rpc_socket.bind("tcp://*:%s" % settings.CLIENT_RPC) self.logger.info("RPC listening on: %s" % settings.CLIENT_RPC) while True: try: self.logger.info("Waiting for RPC") message = rpc_socket.recv() self.logger.info("RPC Got: %s" % message) message = aes.decrypt(message, settings.KEY) ob = json.loads(message) res = getattr(self, ob.get("method"))(ob) self.logger.info("Result: %s" % res) st = json.dumps(res, cls=ComplexEncoder) st = aes.encrypt(st, settings.KEY) self.logger.info("Result: %s" % st) rpc_socket.send(st) except Exception as e: rpc_socket.send("{'error':true}") self.logger.exception(e)
def publish(self, message): message['name'] = self.name message['method'] = message.get('method', 'node_change') self.manager.publish(aes.encrypt(json.dumps(message, cls=ComplexEncoder), settings.KEY)) self.test_triggers(message)
def node_change(self, obj): mes = json.dumps(obj, cls=ComplexEncoder) mes = aes.encrypt(mes, settings.KEY) self.clients_pubsub.publish(mes) return True