|
| 1 | +import logging |
| 2 | +import threading |
| 3 | +from typing import Callable, Dict, Iterable |
| 4 | + |
| 5 | + |
| 6 | +class PubSubHub: |
| 7 | + def __init__(self): |
| 8 | + self.__lock = threading.Lock() |
| 9 | + self.__subscribers: Dict[str, Callable] = {} # key: session_id, value: handler |
| 10 | + self.__topic_subscribers: Dict[ |
| 11 | + str, Dict[str, Callable] |
| 12 | + ] = {} # key: topic, value: dict[session_id, handler] |
| 13 | + self.__subscriber_topics: Dict[ |
| 14 | + str, Dict[str, Callable] |
| 15 | + ] = {} # key: session_id, value: dict[topic, handler] |
| 16 | + |
| 17 | + def send_all(self, message: any): |
| 18 | + logging.debug(f"pubsub.send_all({message})") |
| 19 | + with self.__lock: |
| 20 | + for handler in self.__subscribers.values(): |
| 21 | + self.__send(handler, [message]) |
| 22 | + |
| 23 | + def send_all_on_topic(self, topic: str, message: any): |
| 24 | + logging.debug(f"pubsub.send_all_on_topic({topic}, {message})") |
| 25 | + with self.__lock: |
| 26 | + if topic in self.__topic_subscribers: |
| 27 | + for handler in self.__topic_subscribers[topic].values(): |
| 28 | + self.__send(handler, [topic, message]) |
| 29 | + |
| 30 | + def send_others(self, except_session_id: str, message: any): |
| 31 | + logging.debug(f"pubsub.send_others({except_session_id}, {message})") |
| 32 | + with self.__lock: |
| 33 | + for session_id, handler in self.__subscribers.items(): |
| 34 | + if except_session_id != session_id: |
| 35 | + self.__send(handler, [message]) |
| 36 | + |
| 37 | + def send_others_on_topic(self, except_session_id: str, topic: str, message: any): |
| 38 | + logging.debug( |
| 39 | + f"pubsub.send_others_on_topic({except_session_id}, {topic}, {message})" |
| 40 | + ) |
| 41 | + with self.__lock: |
| 42 | + if topic in self.__topic_subscribers: |
| 43 | + for session_id, handler in self.__topic_subscribers[topic].values(): |
| 44 | + if except_session_id != session_id: |
| 45 | + self.__send(handler, [topic, message]) |
| 46 | + |
| 47 | + def subscribe(self, session_id: str, handler: Callable): |
| 48 | + logging.debug(f"pubsub.subscribe({session_id})") |
| 49 | + with self.__lock: |
| 50 | + self.__subscribers[session_id] = handler |
| 51 | + |
| 52 | + def subscribe_topic(self, session_id: str, topic: str, handler: Callable): |
| 53 | + logging.debug(f"pubsub.subscribe_topic({session_id}, {topic})") |
| 54 | + with self.__lock: |
| 55 | + topic_subscribers = self.__topic_subscribers.get(topic) |
| 56 | + if topic_subscribers == None: |
| 57 | + topic_subscribers = {} |
| 58 | + self.__topic_subscribers[topic] = topic_subscribers |
| 59 | + topic_subscribers[session_id] = handler |
| 60 | + subscriber_topics = self.__subscriber_topics.get(session_id) |
| 61 | + if subscriber_topics == None: |
| 62 | + subscriber_topics = {} |
| 63 | + self.__subscriber_topics[session_id] = subscriber_topics |
| 64 | + subscriber_topics[topic] = handler |
| 65 | + |
| 66 | + def unsubscribe(self, session_id: str): |
| 67 | + logging.debug(f"pubsub.unsubscribe({session_id})") |
| 68 | + with self.__lock: |
| 69 | + self.__unsubscribe(session_id) |
| 70 | + |
| 71 | + def unsubscribe_topic(self, session_id: str, topic: str): |
| 72 | + logging.debug(f"pubsub.unsubscribe({session_id}, {topic})") |
| 73 | + with self.__lock: |
| 74 | + self.__unsubscribe_topic(session_id, topic) |
| 75 | + |
| 76 | + def unsubscribe_all(self, session_id: str): |
| 77 | + logging.debug(f"pubsub.unsubscribe_all({session_id})") |
| 78 | + with self.__lock: |
| 79 | + self.__unsubscribe(session_id) |
| 80 | + if session_id in self.__subscriber_topics: |
| 81 | + for topic in self.__subscriber_topics[session_id].keys(): |
| 82 | + self.__unsubscribe_topic(session_id, topic) |
| 83 | + |
| 84 | + def __unsubscribe(self, session_id: str): |
| 85 | + logging.debug(f"pubsub.__unsubscribe({session_id})") |
| 86 | + self.__subscribers.pop(session_id) |
| 87 | + |
| 88 | + def __unsubscribe_topic(self, session_id: str, topic: str): |
| 89 | + logging.debug(f"pubsub.__unsubscribe_topic({session_id}, {topic})") |
| 90 | + topic_subscribers = self.__topic_subscribers.get(topic) |
| 91 | + if topic_subscribers != None: |
| 92 | + topic_subscribers.pop(session_id) |
| 93 | + if len(topic_subscribers) == 0: |
| 94 | + self.__topic_subscribers.pop(topic) |
| 95 | + subscriber_topics = self.__subscriber_topics.get(session_id) |
| 96 | + if subscriber_topics != None: |
| 97 | + subscriber_topics.pop(topic) |
| 98 | + if len(subscriber_topics) == 0: |
| 99 | + self.__subscriber_topics.pop(session_id) |
| 100 | + |
| 101 | + def __send(self, handler: Callable, args: Iterable): |
| 102 | + th = threading.Thread( |
| 103 | + target=handler, |
| 104 | + args=args, |
| 105 | + daemon=True, |
| 106 | + ) |
| 107 | + th.start() |
| 108 | + |
| 109 | + |
| 110 | +class PubSub: |
| 111 | + def __init__(self, pubsub: PubSubHub, session_id: str): |
| 112 | + self.__pubsub = pubsub |
| 113 | + self.__session_id = session_id |
| 114 | + |
| 115 | + def send_all(self, message: any): |
| 116 | + self.__pubsub.send_all(message) |
| 117 | + |
| 118 | + def send_all_on_topic(self, topic: str, message: any): |
| 119 | + self.__pubsub.send_all_on_topic(topic, message) |
| 120 | + |
| 121 | + def send_others(self, message: any): |
| 122 | + self.__pubsub.send_others(self.__session_id, message) |
| 123 | + |
| 124 | + def send_others_on_topic(self, topic: str, message: any): |
| 125 | + self.__pubsub.send_others_on_topic(self.__session_id, topic, message) |
| 126 | + |
| 127 | + def subscribe(self, handler: Callable): |
| 128 | + self.__pubsub.subscribe(self.__session_id, handler) |
| 129 | + |
| 130 | + def subscribe_topic(self, topic: str, handler: Callable): |
| 131 | + self.__pubsub.subscribe_topic(self.__session_id, topic, handler) |
| 132 | + |
| 133 | + def unsubscribe(self): |
| 134 | + self.__pubsub.unsubscribe(self.__session_id) |
| 135 | + |
| 136 | + def unsubscribe_topic(self, topic: str): |
| 137 | + self.__pubsub.unsubscribe_topic(self.__session_id, topic) |
| 138 | + |
| 139 | + def unsubscribe_all(self): |
| 140 | + self.__pubsub.unsubscribe_all(self.__session_id) |
0 commit comments