def _on_customer_accepted(self, e):
     from logs import lg
     from userid import my_id
     from userid import global_id
     from p2p import p2p_queue
     customer_idurl = e.data.get('idurl')
     if not customer_idurl:
         lg.warn('unknown customer idurl in event data payload')
         return
     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):
         try:
             p2p_queue.open_queue(queue_id)
         except Exception as exc:
             lg.warn('failed to open queue %s : %s' % (queue_id, str(exc)))
     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))
Exemple #2
0
 def request(self, json_payload, newpacket, info):
     from logs import lg
     from lib import serialization
     from p2p import p2p_service
     from p2p import p2p_queue
     try:
         service_requests_list = json_payload['items']
     except:
         lg.warn("invlid json payload")
         return p2p_service.SendFail(newpacket, 'invlid 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)
 def request(self, json_payload, newpacket, info):
     import json
     from logs import lg
     from p2p import p2p_service
     from p2p import p2p_queue
     try:
         # service_info_json = json.loads(newpacket.Payload)
         service_requests_list = json_payload['items']
     except:
         lg.warn("invlid json payload")
         return p2p_service.SendFail(newpacket, 'invlid 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:
             lg.warn(str(exc))
             resp['result'] = 'denied'
             resp['reason'] = str(exc)
         service_responses_list.append(resp)
     return p2p_service.SendAck(newpacket, json.dumps({'items': service_responses_list}))