Skip to content

Commit 6a848c1

Browse files
committed
Add some docs in subscriber source
1 parent 9ceadad commit 6a848c1

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/handlers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ static int subscribe_handler(struct io_event *e) {
604604
* the topic to the wildcards list as we can't know at this point
605605
* which topic it will match
606606
*/
607-
struct subscriber *sub =
608-
subscriber_new(t, e->client->session, s->tuples[i].qos);
607+
struct subscriber *sub = subscriber_new(e->client->session,
608+
s->tuples[i].qos);
609609
add_wildcard(topic, sub, wildcard);
610610
}
611611
#if THREADSNR > 0

src/sol_internal.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,27 @@ extern pthread_mutex_t mutex;
237237

238238
struct server;
239239

240+
/*
241+
* Checks if a client is subscribed to a topic by trying to fetch the
242+
* client_session by its ID on the subscribers inner hashmap of the topic.
243+
*/
240244
bool is_subscribed(const struct topic *, const struct client_session *);
241245

242-
struct subscriber *subscriber_new(struct topic *,
243-
struct client_session *, unsigned char);
246+
/*
247+
* Allocate memory on the heap to create and return a pointer to a struct
248+
* subscriber, assigining the passed in QoS, session pointer, and
249+
* instantiating a reference counter to 0.
250+
* It may fail as it needs to allocate some bytes on the heap.
251+
*/
252+
struct subscriber *subscriber_new(struct client_session *, unsigned char);
244253

254+
/*
255+
* Allocate memory on the heap to clone a subscriber pointer, deep copies all
256+
* fields into the newly allocated pointer except for the reference counter,
257+
* the new pointer will have its own refcount set to 0. Finally the newly
258+
* allocated pointer is returned.
259+
* It may fail as it needs to allocate some bytes on the heap.
260+
*/
245261
struct subscriber *subscriber_clone(const struct subscriber *);
246262

247263
/*

src/subscriber.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030

3131
static 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+
*/
4455
struct 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+
*/
5868
bool 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+
}

src/topic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void topic_destroy(struct topic *t) {
8484
struct subscriber *topic_add_subscriber(struct topic *t,
8585
struct client_session *s,
8686
unsigned char qos) {
87-
struct subscriber *sub = subscriber_new(t, s, qos), *tmp;
87+
struct subscriber *sub = subscriber_new(s, qos), *tmp;
8888
HASH_FIND_STR(t->subscribers, sub->id, tmp);
8989
if (!tmp)
9090
HASH_ADD_STR(t->subscribers, id, sub);

0 commit comments

Comments
 (0)