3030
3131static void subscriber_destroy (const struct ref * );
3232
33- struct subscriber * subscriber_new (struct topic * t ,
34- struct client_session * s ,
35- unsigned char qos ) {
33+ /*
34+ * Allocate memory on the heap to create and return a pointer to a struct
35+ * subscriber, assigining the passed in QoS, session pointer, and
36+ * instantiating a reference counter to 0.
37+ * It may fail as it needs to allocate some bytes on the heap.
38+ */
39+ struct subscriber * subscriber_new (struct client_session * s , unsigned char qos ) {
3640 struct subscriber * sub = try_alloc (sizeof (* sub ));
3741 sub -> session = s ;
3842 sub -> granted_qos = qos ;
@@ -41,6 +45,13 @@ struct subscriber *subscriber_new(struct topic *t,
4145 return sub ;
4246}
4347
48+ /*
49+ * Allocate memory on the heap to clone a subscriber pointer, deep copies all
50+ * fields into the newly allocated pointer except for the reference counter,
51+ * the new pointer will have its own refcount set to 0. Finally the newly
52+ * allocated pointer is returned.
53+ * It may fail as it needs to allocate some bytes on the heap.
54+ */
4455struct subscriber * subscriber_clone (const struct subscriber * s ) {
4556 struct subscriber * sub = try_alloc (sizeof (* sub ));
4657 sub -> session = s -> session ;
@@ -50,13 +61,21 @@ struct subscriber *subscriber_clone(const struct subscriber *s) {
5061 return sub ;
5162}
5263
53- static void subscriber_destroy (const struct ref * r ) {
54- struct subscriber * sub = container_of (r , struct subscriber , refcount );
55- free_memory (sub );
56- }
57-
64+ /*
65+ * Checks if a client is subscribed to a topic by trying to fetch the
66+ * client_session by its ID on the subscribers inner hashmap of the topic.
67+ */
5868bool is_subscribed (const struct topic * t , const struct client_session * s ) {
5969 struct subscriber * dummy = NULL ;
6070 HASH_FIND_STR (t -> subscribers , s -> session_id , dummy );
6171 return dummy != NULL ;
6272}
73+
74+ /*
75+ * Auxiliary function, defines the destructor behavior for subscriber, just
76+ * decreasing the reference counter till 0, then free the memory.
77+ */
78+ static void subscriber_destroy (const struct ref * r ) {
79+ struct subscriber * sub = container_of (r , struct subscriber , refcount );
80+ free_memory (sub );
81+ }
0 commit comments