@@ -88,7 +88,7 @@ struct topic {
8888 */
8989struct topic_store {
9090 // The main topics Trie structure
91- Trie topics ;
91+ Trie * topics ;
9292 // A list of wildcards subscriptions, as it's not possible to know in
9393 // advance what topics will match some wildcard subscriptions
9494 List * wildcards ;
@@ -238,35 +238,121 @@ extern pthread_mutex_t mutex;
238238struct server ;
239239
240240bool is_subscribed (const struct topic * , const struct client_session * );
241+
241242struct subscriber * subscriber_new (struct topic * ,
242243 struct client_session * , unsigned char );
244+
243245struct subscriber * subscriber_clone (const struct subscriber * );
244- struct subscriber * topic_add_subscriber (struct topic * ,
245- struct client_session * , unsigned char );
246246
247+ /*
248+ * Initialize a struct topic pointer by setting its name, subscribers and
249+ * retained_msg are set to NULL.
250+ * The function expects a non-null pointer and can't fail, if a null topic
251+ * is passed, the function return prematurely.
252+ */
247253void topic_init (struct topic * , const char * );
254+
255+ /*
256+ * Allocate a new topic struct on the heap, initialize it then return a pointer
257+ * to it. The function can fail as a memory allocation is requested, if it
258+ * fails the program execution graceful crash.
259+ */
248260struct topic * topic_new (const char * );
261+
262+ /*
263+ * Deallocate the topic name, retained_msg and all its subscribers
264+ */
265+ void topic_destroy (struct topic * );
266+
267+ /*
268+ * Allocate a new subscriber struct on the heap referring to the passed in
269+ * topic, client_session and QoS, then add it to the topic map.
270+ * The function can fail as a memory allocation is requested, if it fails the
271+ * program execution graceful crash.
272+ */
273+ struct subscriber * topic_add_subscriber (struct topic * ,
274+ struct client_session * , unsigned char );
275+
276+ /*
277+ * Remove a subscriber from the topic, the subscriber to be removed refers to
278+ * the client_id belonging to the client pointer passed in.
279+ * The subscriber deletion is really a reference count subtraction, DECREF
280+ * macro takes care of the counter, if it reaches 0 it de-allocates the memory
281+ * reserved to the struct subscriber.
282+ * The function can't fail.
283+ */
249284void topic_del_subscriber (struct topic * , struct client * );
285+
286+ /*
287+ * Allocate a new store structure on the heap and return it after its
288+ * initialization, also allocating a new list on the heap to keep track of
289+ * wildcard topics.
290+ * The function may gracefully crash as the memory allocation may fail.
291+ */
250292struct topic_store * topic_store_new (void );
251- /* Find a topic by name and return it */
293+
294+ /*
295+ * Deallocate heap memory for the list and every wildcard item stored into,
296+ * also the store is deallocated
297+ */
298+ void topic_store_destroy (struct topic_store * );
299+
300+ /*
301+ * Return a topic associated to a topic name from the store, returns NULL if no
302+ * topic is found.
303+ */
252304struct topic * topic_store_get (const struct topic_store * , const char * );
253- /* Get or create a new topic if it doesn't exists */
305+
306+ /*
307+ * Return a topic associated to a topic name from the store, if no topic is
308+ * insert it into the store before returning it. Like topic_store_get but
309+ * cannot return NULL.
310+ * The function may fail as in case of no topic found it tries to allocate
311+ * space on the heap for the new inserted topic.
312+ */
254313struct topic * topic_store_get_or_put (struct topic_store * , const char * );
314+
315+ /*
316+ * Check if the store contains a topic by name key
317+ */
255318bool topic_store_contains (const struct topic_store * , const char * );
319+
320+ /*
321+ * Insert a topic into the store or update it if already present
322+ */
256323void topic_store_put (struct topic_store * , struct topic * );
324+
325+ /*
326+ * Remove a topic into the store
327+ */
257328void topic_store_del (struct topic_store * , const char * );
329+
330+ /*
331+ * Add a wildcard topic to the topic_store struct, does not check if it already
332+ * exists
333+ */
258334void topic_store_add_wildcard (struct topic_store * , struct subscription * );
335+
336+ /*
337+ * Remove a wildcard by id key from the topic_store struct
338+ */
259339void topic_store_remove_wildcard (struct topic_store * , char * );
340+
341+ /*
342+ * Run a function to each node of the topic_store trie holding the topic
343+ * entries
344+ */
260345void topic_store_map (struct topic_store * , const char * ,
261346 void (* fn )(struct trie_node * , void * ), void * );
347+
348+ /*
349+ * Check if the wildcards list of the topic_store is empty
350+ */
262351bool topic_store_wildcards_empty (const struct topic_store * );
263352
264353#define topic_store_wildcards_foreach (item , store ) \
265354 list_foreach(item, store->wildcards)
266355
267- /* unsigned next_free_mid(struct client_session *); */
268- /* void session_init(struct client_session *, const char *); */
269- /* struct client_session *client_session_alloc(const char *); */
270-
271356#define has_inflight (session ) ((session)->inflights > 0)
357+
272358#define inflight_msg_clear (msg ) DECREF((msg)->packet, struct mqtt_packet)
0 commit comments