示例#1
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}))