def on_customer_accepted(evt): customer_idurl = id_url.field(evt.data.get('idurl')) if not customer_idurl: lg.warn('unknown customer idurl in event data payload') return False customer_glob_id = global_id.idurl2glob(customer_idurl) queue_id = global_id.MakeGlobalQueueID( queue_alias='supplier-file-modified', owner_id=customer_glob_id, supplier_id=my_id.getGlobalID(), ) if not p2p_queue.is_queue_exist(queue_id): customer_key_id = global_id.MakeGlobalID(customer=customer_glob_id, key_alias='customer') if my_keys.is_key_registered(customer_key_id): try: p2p_queue.open_queue(queue_id) except Exception as exc: lg.warn('failed to open queue %s : %s' % (queue_id, str(exc))) else: lg.warn('customer key %r for supplier queue not registered' % customer_key_id) if p2p_queue.is_queue_exist(queue_id): if not p2p_queue.is_producer_exist(my_id.getGlobalID()): try: p2p_queue.add_producer(my_id.getGlobalID()) except Exception as exc: lg.warn('failed to add producer: %s' % str(exc)) if p2p_queue.is_producer_exist(my_id.getGlobalID()): if not p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id): try: p2p_queue.connect_producer(my_id.getGlobalID(), queue_id) except Exception as exc: lg.warn('failed to connect producer: %s' % str(exc)) if p2p_queue.is_producer_connected(my_id.getGlobalID(), queue_id): if not p2p_queue.is_event_publishing(my_id.getGlobalID(), 'supplier-file-modified'): try: p2p_queue.start_event_publisher( my_id.getGlobalID(), 'supplier-file-modified') except Exception as exc: lg.warn('failed to start event publisher: %s' % str(exc)) return True
def request(self, json_payload, newpacket, info): from logs import lg from lib import serialization from p2p import p2p_service from stream import p2p_queue try: service_requests_list = json_payload['items'] except: lg.warn("invalid json payload") return p2p_service.SendFail(newpacket, 'invalid json payload') service_responses_list = [] for r_json in service_requests_list: resp = r_json.copy() r_scope = r_json.get('scope', '') r_action = r_json.get('action', '') try: if r_scope == 'queue': if r_action == 'open': resp['result'] = 'denied' if not p2p_queue.open_queue( queue_id=r_json.get('queue_id'), ) else 'OK' elif r_action == 'close': resp['result'] = 'denied' if not p2p_queue.close_queue( queue_id=r_json.get('queue_id'), ) else 'OK' elif r_scope == 'consumer': if r_action == 'start': resp['result'] = 'denied' if not p2p_queue.add_consumer( consumer_id=r_json.get('consumer_id'), ) else 'OK' elif r_action == 'stop': resp['result'] = 'denied' if not p2p_queue.remove_consumer( consumer_id=r_json.get('consumer_id'), ) else 'OK' elif r_action == 'add_callback': resp['result'] = 'denied' if not p2p_queue.add_callback_method( consumer_id=r_json.get('consumer_id'), callback_method=r_json.get('method'), ) else 'OK' elif r_action == 'remove_callback': resp['result'] = 'denied' if not p2p_queue.remove_callback_method( consumer_id=r_json.get('consumer_id'), callback_method=r_json.get('method'), ) else 'OK' elif r_action == 'subscribe': resp['result'] = 'denied' if not p2p_queue.subscribe_consumer( consumer_id=r_json.get('consumer_id'), queue_id=r_json.get('queue_id'), ) else 'OK' elif r_action == 'unsubscribe': resp['result'] = 'denied' if not p2p_queue.unsubscribe_consumer( consumer_id=r_json.get('consumer_id'), queue_id=r_json.get('queue_id'), ) else 'OK' elif r_scope == 'producer': resp['result'] = 'denied' resp['reason'] = 'remote requests for producing messages is not allowed' if False: # TODO: do we need that ? if r_action == 'start': resp['result'] = 'denied' if not p2p_queue.add_producer( producer_id=r_json.get('producer_id'), ) else 'OK' elif r_action == 'stop': resp['result'] = 'denied' if not p2p_queue.remove_producer( producer_id=r_json.get('producer_id'), ) else 'OK' elif r_action == 'connect': resp['result'] = 'denied' if not p2p_queue.connect_producer( producer_id=r_json.get('producer_id'), queue_id=r_json.get('queue_id'), ) else 'OK' elif r_action == 'disconnect': resp['result'] = 'denied' if not p2p_queue.disconnect_producer( producer_id=r_json.get('producer_id'), queue_id=r_json.get('queue_id'), ) else 'OK' except Exception as exc: resp['result'] = 'denied' resp['reason'] = str(exc) service_responses_list.append(resp) lg.out(self.debug_level, 'service_p2p_notifications.request %s:%s is [%s] : %s' % ( r_scope, r_action, resp['result'], resp.get('reason', 'OK'), )) payload = serialization.DictToBytes({'items': service_responses_list, }, values_to_text=True) return p2p_service.SendAck(newpacket, payload)