Skip to content

Commit

Permalink
Constified msg_t and all its members in recv_cb. Updated todo.
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeDP committed Mar 18, 2018
1 parent fbb3ffd commit 4040353
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 52 deletions.
33 changes: 16 additions & 17 deletions Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ static int tell_if(void *data, void *m) {
* Only if mod is actually running and
* if topic is null or this module is subscribed to topic
*/
if (module_is(&mod->self, RUNNING) && (!msg->message->topic ||
hashmap_get(mod->subscriptions, (char *)msg->message->topic, (void **)&tmp) == MAP_OK)) {
if (module_is(&mod->self, RUNNING) && (!msg->msg->topic ||
hashmap_get(mod->subscriptions, (char *)msg->msg->topic, (void **)&tmp) == MAP_OK)) {

MODULE_DEBUG("Telling a message to %s.\n", mod->self.name);
mod->hook->recv(msg, mod->userdata);
Expand All @@ -359,34 +359,33 @@ module_ret_code module_tell(const self_t *self, const char *message, const char
GET_CTX(s->ctx);
CTX_GET_MOD(recipient, c);

msg_t msg = { 0 };

msg.fd = -1;
msg.message = malloc(sizeof(pubsub_msg_t));
msg.message->message = message;
msg.message->sender = s->name;
msg.message->topic = NULL;
msg_t msg = { .fd = -1 };
pubsub_msg_t *tmp = malloc(sizeof(pubsub_msg_t));
tmp->message = message;
tmp->sender = s->name;
tmp->topic = NULL;
msg.msg = tmp;

tell_if(&msg, mod);

free(msg.message);
free(tmp);
return MOD_OK;
}

module_ret_code module_publish(const self_t *self, const char *topic, const char *message) {
self_t *s = (self_t *)self;
GET_CTX(s->ctx);

msg_t msg = { 0 };
msg.fd = -1;
msg.message = malloc(sizeof(pubsub_msg_t));
msg.message->message = message;
msg.message->sender = s->name;
msg.message->topic = topic;
msg_t msg = { .fd = -1 };
pubsub_msg_t *tmp = malloc(sizeof(pubsub_msg_t));
tmp->message = message;
tmp->sender = s->name;
tmp->topic = topic;
msg.msg = tmp;

hashmap_iterate(c->modules, tell_if, &msg);

free(msg.message);
free(tmp);
return MOD_OK;
}

Expand Down
8 changes: 4 additions & 4 deletions Lib/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
static int init(void); \
static int check(void); \
static int evaluate(void); \
static void recv(msg_t *msg, const void *userdata); \
static void recv(const msg_t *msg, const void *userdata); \
static void destroy(void); \
static const self_t *self = NULL; \
static void _ctor2_ constructor(void) { \
Expand Down Expand Up @@ -89,14 +89,14 @@ typedef struct {
} pubsub_msg_t;

typedef struct {
int fd;
pubsub_msg_t *message;
const int fd;
const pubsub_msg_t *msg;
} msg_t;

/* Callbacks typedefs */
typedef int(*init_cb)(void);
typedef int(*evaluate_cb)(void);
typedef void(*recv_cb)(msg_t *msg, const void *userdata);
typedef void(*recv_cb)(const msg_t *msg, const void *userdata);
typedef void(*destroy_cb)(void);

/* Struct that holds user defined callbacks */
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ No, it is not.
It uses epoll, which is linux specific.
Moreover it heavily relies upon gcc attributes that may or may not be available for your compiler.
It is tested with both gcc and clang through [travis](https://travis-ci.org/FedeDP/libmodule).
I may consider and welcome any patch to support other platforms though.
Any patch to support other platforms is warmly welcomed though.

## Is there any documentation?

Expand Down
12 changes: 6 additions & 6 deletions Samples/Easy/a.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
MODULE("A");

static void recv_ready(msg_t *msg, const void *userdata);
static void recv_ready(const msg_t *msg, const void *userdata);

/*
* This macro will create a function that is automatically
Expand Down Expand Up @@ -70,8 +70,8 @@ static void destroy(void) {
* Our default poll callback.
* Note that message_t->msg/sender are unused for now.
*/
static void recv(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void recv(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
uint64_t t;
read(msg->fd, &t, sizeof(uint64_t));

Expand All @@ -95,8 +95,8 @@ static void recv(msg_t *msg, const void *userdata) {
* Secondary poll callback.
* Use m_become(ready) to start using this second poll callback.
*/
static void recv_ready(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void recv_ready(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
uint64_t t;
read(msg->fd, &t, sizeof(uint64_t));

Expand All @@ -110,6 +110,6 @@ static void recv_ready(msg_t *msg, const void *userdata) {
m_unbecome();
}
} else {
m_log("Received message %s from %s.\n", msg->message->message, msg->message->sender);
m_log("Received message %s from %s.\n", msg->msg->message, msg->msg->sender);
}
}
8 changes: 4 additions & 4 deletions Samples/Easy/b.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ static void destroy(void) {
* Our default poll callback.
* Note that message_t->msg/sender are unused for now.
*/
static void recv(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void recv(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
struct signalfd_siginfo fdsi;
ssize_t s = read(msg->fd, &fdsi, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo)) {
Expand All @@ -82,8 +82,8 @@ static void recv(msg_t *msg, const void *userdata) {
m_log("received signal %d. Leaving.\n", fdsi.ssi_signo);
modules_quit();
} else {
m_log("Received message '%s' from %s on topic '%s'.\n", msg->message->message, msg->message->sender, msg->message->topic);
m_log("Received message '%s' from %s on topic '%s'.\n", msg->msg->message, msg->msg->sender, msg->msg->topic);
/* Answer back to sender with a message */
m_tell("Nice!", msg->message->sender);
m_tell("Nice!", msg->msg->sender);
}
}
10 changes: 5 additions & 5 deletions Samples/MultiCtx/a.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static const char *myCtx = "FirstCtx";
*/
MODULE_CTX("A", myCtx);

static void recv_ready(msg_t *msg, const void *userdata);
static void recv_ready(const msg_t *msg, const void *userdata);

/*
* Initializes this module's state;
Expand Down Expand Up @@ -63,8 +63,8 @@ static void destroy(void) {
* Our default poll callback.
* Note that message_t->msg/sender are unused for now.
*/
static void recv(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void recv(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
uint64_t t;
read(msg->fd, &t, sizeof(uint64_t));

Expand All @@ -83,8 +83,8 @@ static void recv(msg_t *msg, const void *userdata) {
* Secondary poll callback.
* Use m_become(ready) to start using this second poll callback.
*/
static void recv_ready(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void recv_ready(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
uint64_t t;
read(msg->fd, &t, sizeof(uint64_t));

Expand Down
4 changes: 2 additions & 2 deletions Samples/MultiCtx/b.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ static void destroy(void) {
* Our default poll callback.
* Note that message_t->msg/sender are unused for now.
*/
static void recv(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void recv(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
struct signalfd_siginfo fdsi;
ssize_t s = read(msg->fd, &fdsi, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo)) {
Expand Down
12 changes: 6 additions & 6 deletions Samples/SharedSrc/mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ static int A_init(void);
static int B_init(void);
static int evaluate(void);
static void destroy(void);
static void A_recv(msg_t *msg, const void *userdata);
static void B_recv(msg_t *msg, const void *userdata);
static void A_recv(const msg_t *msg, const void *userdata);
static void B_recv(const msg_t *msg, const void *userdata);

static const self_t *selfA, *selfB;
static userhook hookA, hookB;
Expand Down Expand Up @@ -91,8 +91,8 @@ static void destroy(void) {
/*
* Our A module's poll callback.
*/
static void A_recv(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void A_recv(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
uint64_t t;
read(msg->fd, &t, sizeof(uint64_t));
module_log(selfA, "recv!\n");
Expand All @@ -107,8 +107,8 @@ static void A_recv(msg_t *msg, const void *userdata) {
/*
* Our B module's poll callback.
*/
static void B_recv(msg_t *msg, const void *userdata) {
if (!msg->message) {
static void B_recv(const msg_t *msg, const void *userdata) {
if (!msg->msg) {
uint64_t t;
read(msg->fd, &t, sizeof(uint64_t));
module_log(selfB, "recv!\n");
Expand Down
6 changes: 3 additions & 3 deletions Samples/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
all: Easy SharedSrc MultiCtx

Easy:
cd Easy; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build
cd Easy; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build -Wall

SharedSrc:
cd SharedSrc; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build
cd SharedSrc; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build -Wall

MultiCtx:
cd MultiCtx; $(CC) *.c -L../../build -lmodule -lpthread -I../../Lib -o sample.out -Wl,-rpath=../../build
cd MultiCtx; $(CC) *.c -L../../build -lmodule -lpthread -I../../Lib -o sample.out -Wl,-rpath=../../build -Wall
8 changes: 7 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
- [x] rename get_fd function (as same function should be used to eg: set module's initial subscriptions and initial state)
- [x] Let users implement "non-pollable" modules, ie: modules that are not bound to a FD (but only receives pubsub message) -> ie: set RUNNING state anyway if certain FD is passed
- [x] Update doc
- [x] msg_t all const
- [ ] split in module.h and modules.h (module/module{s}.h)

- [ ] Release 1.0

## 1.1
## Later

### Submodules

Expand All @@ -26,6 +28,10 @@
- [ ] modules_set_logger() function to set a logger?
- [ ] Logger will be called in module_log/m_log

### Dep system

- [ ] REQUIRE and AFTER macros

### Generic

- [ ] add an hashmap_exists() function
Expand Down
2 changes: 1 addition & 1 deletion docs/src/callbacks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ They are automatically declared by MODULE macro:
static int init(void);
static int check(void);
static int evaluate(void);
static void recv(message_t *msg, const void *userdata);
static void recv(const msg_t *msg, const void *userdata);
static void destroy(void);
.. c:function:: init(void)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/data_structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Types
} pubsub_msg_t;
typedef struct {
int fd;
pubsub_msg_t *message;
const int fd;
const pubsub_msg_t *msg;
} msg_t;
/* Callbacks typedefs */
Expand Down

0 comments on commit 4040353

Please sign in to comment.