/code/zato-server/src/zato/server/pubsub/core/topic.py

https://github.com/zatosource/zato · Python · 142 lines · 72 code · 39 blank · 31 comment · 10 complexity · 2eb82a2835522a1a27f454c07544354b MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Copyright (C) 2022, Zato Source s.r.o. https://zato.io
  4. Licensed under LGPLv3, see LICENSE.txt for terms and conditions.
  5. """
  6. # pylint: disable=unused-import, redefined-builtin, unused-variable
  7. # stdlib
  8. import logging
  9. # gevent
  10. from zato.common.typing_ import cast_
  11. from zato.server.pubsub.model import Topic
  12. # ################################################################################################################################
  13. # ################################################################################################################################
  14. if 0:
  15. from zato.common.typing_ import anylist, callable_, dict_, stranydict, strintdict
  16. from zato.server.pubsub.core.hook import HookAPI
  17. from zato.server.pubsub.model import inttopicdict, sublist, topiclist
  18. # ################################################################################################################################
  19. # ################################################################################################################################
  20. logger = logging.getLogger('zato_pubsub.ps')
  21. logger_zato = logging.getLogger('zato')
  22. # ################################################################################################################################
  23. # ################################################################################################################################
  24. class TopicAPI:
  25. def __init__(
  26. self,
  27. *,
  28. hook_api, # type: HookAPI
  29. server_name, # type: str
  30. server_pid, # type: int
  31. topic_meta_store_frequency, # type: int
  32. subscriptions_by_topic, # type: dict_[str, sublist]
  33. is_allowed_sub_topic_by_endpoint_id_func, # type: callable_
  34. ) -> 'None':
  35. self.hook_api = hook_api
  36. self.is_allowed_sub_topic_by_endpoint_id_func = is_allowed_sub_topic_by_endpoint_id_func
  37. self.server_name = server_name
  38. self.server_pid = server_pid
  39. self.topic_meta_store_frequency = topic_meta_store_frequency
  40. # Topic name -> List of Subscription objects
  41. self.subscriptions_by_topic = subscriptions_by_topic
  42. # Topic ID -> Topic object
  43. self.topics = cast_('inttopicdict', {})
  44. # Topic name -> Topic ID
  45. self.topic_name_to_id = {} # type: strintdict
  46. # ################################################################################################################################
  47. def has_topic_by_id(self, topic_id:'int') -> 'bool':
  48. try:
  49. self.topics[topic_id]
  50. except KeyError:
  51. return False
  52. else:
  53. return True
  54. # ################################################################################################################################
  55. def has_topic_by_name(self, topic_name:'str') -> 'bool':
  56. try:
  57. _ = self.get_topic_by_name(topic_name)
  58. except KeyError:
  59. return False
  60. else:
  61. return True
  62. # ################################################################################################################################
  63. def get_topics(self) -> 'inttopicdict':
  64. return self.topics
  65. # ################################################################################################################################
  66. def get_topic_by_name(self, topic_name:'str') -> 'Topic':
  67. topic_id = self.get_topic_id_by_name(topic_name)
  68. return self.topics[topic_id]
  69. # ################################################################################################################################
  70. def get_topic_by_id(self, topic_id:'int') -> 'Topic':
  71. return self.topics[topic_id]
  72. # ################################################################################################################################
  73. def get_topic_id_by_name(self, topic_name:'str') -> 'int':
  74. return self.topic_name_to_id[topic_name]
  75. # ################################################################################################################################
  76. def create_topic_object(self, config:'stranydict') -> 'None':
  77. self.hook_api.set_topic_config_hook_data(config)
  78. config['meta_store_frequency'] = self.topic_meta_store_frequency
  79. topic = Topic(config, self.server_name, self.server_pid)
  80. self.topics[config['id']] = topic
  81. self.topic_name_to_id[config['name']] = config['id']
  82. logger.info('Created topic object `%s` (id:%s) on server `%s` (pid:%s)', topic.name, topic.id,
  83. topic.server_name, topic.server_pid)
  84. # ################################################################################################################################
  85. def delete_topic(self, topic_id:'int', topic_name:'str') -> 'anylist':
  86. del self.topic_name_to_id[topic_name]
  87. subscriptions_by_topic = self.subscriptions_by_topic.pop(topic_name, [])
  88. del self.topics[topic_id]
  89. logger.info('Deleted topic object `%s` (%s), subs:`%s`',
  90. topic_name, topic_id, [elem.sub_key for elem in subscriptions_by_topic])
  91. return subscriptions_by_topic
  92. # ################################################################################################################################
  93. def get_sub_topics_for_endpoint(self, endpoint_id:'int') -> 'topiclist':
  94. """ Returns all topics to which endpoint_id can subscribe.
  95. """
  96. out = [] # type: topiclist
  97. for topic in self.topics.values():
  98. if self.is_allowed_sub_topic_by_endpoint_id_func(topic.name, endpoint_id):
  99. out.append(topic)
  100. return out
  101. # ################################################################################################################################
  102. # ################################################################################################################################