From 85b0b01756d8cb32cf0f9dd407a38d8460841154 Mon Sep 17 00:00:00 2001 From: Prabal Dev Date: Mon, 31 Mar 2025 17:41:44 +0530 Subject: [PATCH 1/2] [wip] pubsub --- main.c | 36 ++++++++++++++++++++++++++++++++++- pubsub.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pubsub.h | 18 ++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 pubsub.c create mode 100644 pubsub.h diff --git a/main.c b/main.c index 8329bda..3143214 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #include "list.h" #include "node.h" +#include "pubsub.h" void create_node_tc(void) { @@ -192,7 +193,7 @@ void out_of_mem_tc(void) destroy_list(head, NULL, NULL); } -int main(void) +void sll_test_case(void) { create_node_tc(); create_list_tc(); @@ -202,5 +203,38 @@ int main(void) //// Node->Node->Node->Node but all Node->data will be NULL //// Test out this case and discuss if it is the correct behaviour, or should be stop making Nodes if Node->data /// filling fails? / In that case, we will also need to delete the created Node for which data filling fails +} + +static void* subscriber_event_cb1(void* data) +{ + printf("\ncalled sub event cb 1\n"); + return NULL; +} + +static void* subscriber_event_cb2(void* data) +{ + printf("\ncalled sub event cb 2\n"); + return NULL; +} +PubsubSubscriber sub1 = {.subscriber_cb_fptr = subscriber_event_cb1, .sub_data = NULL}; +PubsubSubscriber sub2 = {.subscriber_cb_fptr = subscriber_event_cb2, .sub_data = NULL}; +void pubsub_test_case(void) +{ + pubsub_create(); + + snprintf(sub1.topic, strlen("/topic1") + 1, "%s", "/topic1"); + pubsub_subscribe(sub1); + + snprintf(sub1.topic, strlen("/topic1") + 1, "%s", "/topic1"); + pubsub_subscribe(sub2); + PubsubPublisher event = {.data = NULL}; + snprintf(event.topic, strlen("/topic1") + 1, "%s", "/topic1"); + pubsub_publish(event); +} + +int main(void) +{ + // sll_test_case(); + pubsub_test_case(); return 0; } diff --git a/pubsub.c b/pubsub.c new file mode 100644 index 0000000..eb0c81c --- /dev/null +++ b/pubsub.c @@ -0,0 +1,57 @@ +#include "pubsub.h" + +#include "list.h" +#include "node.h" +#include "stdbool.h" + +#define DBG printf("\n%s():L%d\n", __func__, __LINE__) + +static Head* subscriber_list = NULL; + +bool pubsub_create(void) +{ + subscriber_list = create_list(NULL); + if (subscriber_list) { + return true; + } + return false; +} + +static bool compare_topic(void* t1, void* t2) +{ + char* topic1 = (char*)t1; + char* topic2 = (char*)t2; + if (topic1 && topic2) { + if (strlen(topic1) != strlen(topic2)) { + return false; + } + if (0 == strncmp(topic1, topic2, strlen(topic1))) { + return true; + } + } + return false; +} + +bool pubsub_publish(PubsubPublisher event) { + if(subscriber_list) { + if(true == find_in_list(subscriber_list, event.topic, compare_topic)){ + printf("\nfound\n"); + } else { + DBG; + } + } else { + DBG; + } + return true; +} + +bool pubsub_subscribe(PubsubSubscriber sub) +{ + if (SLL_SUCCESS == append_to_list(subscriber_list, sizeof(sub), &sub)) { + DBG; + return true; + } else { + DBG; + } + return false; +} diff --git a/pubsub.h b/pubsub.h new file mode 100644 index 0000000..ae43716 --- /dev/null +++ b/pubsub.h @@ -0,0 +1,18 @@ +#include + +typedef struct PubsubPublisher +{ + char topic[20]; + void* data; +} PubsubPublisher; + +typedef void* (*subscriber_cb_t)(void*); +typedef struct PubsubSubscriber { + char topic[20]; + subscriber_cb_t subscriber_cb_fptr; + void* sub_data; +}PubsubSubscriber; + +bool pubsub_create(void); +bool pubsub_publish(PubsubPublisher event); +bool pubsub_subscribe(PubsubSubscriber sub); From 66f24cbdcdc8e432507856d4f0a5de5242fd88e1 Mon Sep 17 00:00:00 2001 From: Clang Robot <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 31 Mar 2025 12:13:57 +0000 Subject: [PATCH 2/2] [clang-format-bot] Formatting changes --- main.c | 10 +++++----- pubsub.c | 7 ++++--- pubsub.h | 5 +++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 3143214..479554a 100644 --- a/main.c +++ b/main.c @@ -216,18 +216,18 @@ static void* subscriber_event_cb2(void* data) printf("\ncalled sub event cb 2\n"); return NULL; } -PubsubSubscriber sub1 = {.subscriber_cb_fptr = subscriber_event_cb1, .sub_data = NULL}; -PubsubSubscriber sub2 = {.subscriber_cb_fptr = subscriber_event_cb2, .sub_data = NULL}; +PubsubSubscriber sub1 = { .subscriber_cb_fptr = subscriber_event_cb1, .sub_data = NULL }; +PubsubSubscriber sub2 = { .subscriber_cb_fptr = subscriber_event_cb2, .sub_data = NULL }; void pubsub_test_case(void) { pubsub_create(); - + snprintf(sub1.topic, strlen("/topic1") + 1, "%s", "/topic1"); pubsub_subscribe(sub1); - + snprintf(sub1.topic, strlen("/topic1") + 1, "%s", "/topic1"); pubsub_subscribe(sub2); - PubsubPublisher event = {.data = NULL}; + PubsubPublisher event = { .data = NULL }; snprintf(event.topic, strlen("/topic1") + 1, "%s", "/topic1"); pubsub_publish(event); } diff --git a/pubsub.c b/pubsub.c index eb0c81c..e62c0fc 100644 --- a/pubsub.c +++ b/pubsub.c @@ -32,9 +32,10 @@ static bool compare_topic(void* t1, void* t2) return false; } -bool pubsub_publish(PubsubPublisher event) { - if(subscriber_list) { - if(true == find_in_list(subscriber_list, event.topic, compare_topic)){ +bool pubsub_publish(PubsubPublisher event) +{ + if (subscriber_list) { + if (true == find_in_list(subscriber_list, event.topic, compare_topic)) { printf("\nfound\n"); } else { DBG; diff --git a/pubsub.h b/pubsub.h index ae43716..36f8567 100644 --- a/pubsub.h +++ b/pubsub.h @@ -7,11 +7,12 @@ typedef struct PubsubPublisher } PubsubPublisher; typedef void* (*subscriber_cb_t)(void*); -typedef struct PubsubSubscriber { +typedef struct PubsubSubscriber +{ char topic[20]; subscriber_cb_t subscriber_cb_fptr; void* sub_data; -}PubsubSubscriber; +} PubsubSubscriber; bool pubsub_create(void); bool pubsub_publish(PubsubPublisher event);