diff --git a/main.c b/main.c index 8329bda..479554a 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..e62c0fc --- /dev/null +++ b/pubsub.c @@ -0,0 +1,58 @@ +#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..36f8567 --- /dev/null +++ b/pubsub.h @@ -0,0 +1,19 @@ +#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);