diff --git a/src/TopicTree.h b/src/TopicTree.h index 283d6a3..b1d6844 100644 --- a/src/TopicTree.h +++ b/src/TopicTree.h @@ -278,7 +278,8 @@ public: return emptyVector; } - void subscribe(std::string_view topic, Subscriber *subscriber, bool nonStrict = false) { + /* Returns number of subscribers after the call and whether or not we were successful in subscribing */ + std::pair subscribe(std::string_view topic, Subscriber *subscriber, bool nonStrict = false) { /* Start iterating from the root */ Topic *iterator = root; @@ -333,7 +334,9 @@ public: /* Add Topic to list of subscriptions only if we weren't already subscribed */ if (inserted) { subscriber->subscriptions.push_back(iterator); + return {(unsigned int) iterator->subs.size(), true}; } + return {(unsigned int) iterator->subs.size(), false}; } void publish(std::string_view topic, std::pair message, Subscriber *sender = nullptr) { @@ -347,8 +350,8 @@ public: messageId++; } - /* Returns whether we were subscribed prior */ - bool unsubscribe(std::string_view topic, Subscriber *subscriber, bool nonStrict = false) { + /* Returns a pair of numSubscribers after operation, and whether we were subscribed prior */ + std::pair unsubscribe(std::string_view topic, Subscriber *subscriber, bool nonStrict = false) { /* Subscribers are likely to have very few subscriptions (20 or fewer) */ if (subscriber) { /* Lookup exact Topic ptr from string */ @@ -360,7 +363,7 @@ public: std::map::iterator it = iterator->children.find(segment); if (it == iterator->children.end()) { /* This topic does not even exist */ - return false; + return {0, false}; } iterator = it->second; @@ -381,12 +384,13 @@ public: /* Remove us from Topic's subs */ iterator->subs.erase(subscriber); + unsigned int numSubscribers = (unsigned int) iterator->subs.size(); trimTree(iterator); - return true; + return {numSubscribers, true}; } } } - return false; + return {0, false}; } /* Can be called with nullptr, ignore it then */ diff --git a/src/WebSocket.h b/src/WebSocket.h index 4f0bb42..1415d23 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -197,8 +197,8 @@ public: } } - /* Subscribe to a topic according to MQTT rules and syntax */ - void subscribe(std::string_view topic, bool nonStrict = false) { + /* Subscribe to a topic according to MQTT rules and syntax. Returns [numSubscribers, success]. */ + std::pair subscribe(std::string_view topic, bool nonStrict = false) { WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, (us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this) ); @@ -209,11 +209,11 @@ public: webSocketData->subscriber = new Subscriber(this); } - webSocketContextData->topicTree.subscribe(topic, webSocketData->subscriber, nonStrict); + return webSocketContextData->topicTree.subscribe(topic, webSocketData->subscriber, nonStrict); } - /* Unsubscribe from a topic, returns true if we were subscribed */ - bool unsubscribe(std::string_view topic, bool nonStrict = false) { + /* Unsubscribe from a topic, returns true if we were subscribed. Returns [numSubscribers, success]. */ + std::pair unsubscribe(std::string_view topic, bool nonStrict = false) { WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, (us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this) );