Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "list.h"
#include "node.h"
#include "pubsub.h"

void create_node_tc(void)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
58 changes: 58 additions & 0 deletions pubsub.c
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions pubsub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdbool.h>

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);