Skip to content

Commit

Permalink
* module_get_{name/context} return a strdupped string.
Browse files Browse the repository at this point in the history
* module_{tell/publish/broadcast} require a "const void *message" parameter instead of "const unsigned char *".
  • Loading branch information
FedeDP committed Jul 13, 2019
1 parent 362b970 commit 84f4504
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 30 deletions.
10 changes: 6 additions & 4 deletions Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,19 @@ module_ret_code module_deregister_fd(const self_t *self, const int fd) {
return _deregister_fd(mod, fd);
}

module_ret_code module_get_name(const self_t *self, const char **name) {
module_ret_code module_get_name(const self_t *self, char **name) {
GET_MOD_PURE(self);

*name = mod->name;
*name = mem_strdup(mod->name);
MOD_ALLOC_ASSERT(*name);
return MOD_OK;
}

module_ret_code module_get_context(const self_t *self, const char **ctx) {
module_ret_code module_get_context(const self_t *self, char **ctx) {
GET_CTX_PURE(self);

*ctx = c->name;
*ctx = mem_strdup(c->name);
MOD_ALLOC_ASSERT(*ctx);
return MOD_OK;
}

Expand Down
12 changes: 6 additions & 6 deletions Lib/public/module/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ _public_ module_ret_code module_set_userdata(const self_t *self, const void *use
_public_ module_ret_code module_register_fd(const self_t *self, const int fd, const bool autoclose, const void *userptr);
_public_ module_ret_code module_deregister_fd(const self_t *self, const int fd);

/* Note that name and ctx must NOT be freed by user */
_public_ module_ret_code module_get_name(const self_t *mod_self, const char **name);
_public_ module_ret_code module_get_context(const self_t *mod_self, const char **ctx);
/* Note that name and ctx must be freed by user */
_public_ module_ret_code module_get_name(const self_t *mod_self, char **name);
_public_ module_ret_code module_get_context(const self_t *mod_self, char **ctx);

_public_ module_ret_code module_ref(const self_t *self, const char *name, const self_t **modref);

Expand All @@ -49,11 +49,11 @@ _public_ module_ret_code module_deregister_topic(const self_t *self, const char
_public_ module_ret_code module_subscribe(const self_t *self, const char *topic);
_public_ module_ret_code module_unsubscribe(const self_t *self, const char *topic);

_public_ module_ret_code module_tell(const self_t *self, const self_t *recipient, const unsigned char *message,
_public_ module_ret_code module_tell(const self_t *self, const self_t *recipient, const void *message,
const ssize_t size, const bool autofree);
_public_ module_ret_code module_publish(const self_t *self, const char *topic, const unsigned char *message,
_public_ module_ret_code module_publish(const self_t *self, const char *topic, const void *message,
const ssize_t size, const bool autofree);
_public_ module_ret_code module_broadcast(const self_t *self, const unsigned char *message,
_public_ module_ret_code module_broadcast(const self_t *self, const void *message,
const ssize_t size, const bool autofree);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion Lib/public/module/module_cmn.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum msg_type { USER, LOOP_STARTED, LOOP_STOPPED, TOPIC_REGISTERED, TOPIC_DEREGI

typedef struct {
const char *topic;
const unsigned char *message;
const void *message;
ssize_t size;
const self_t *sender;
enum msg_type type;
Expand Down
6 changes: 3 additions & 3 deletions Lib/public/module/module_easy.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ static void _ctor2_ module_pre_start(void)
#define m_tell(recipient, msg, size, free) module_tell(self(), recipient, msg, size, free)
#define m_publish(topic, msg, size, free) module_publish(self(), topic, msg, size, free)
#define m_broadcast(msg, size, free) module_broadcast(self(), msg, size, free)
#define m_tell_str(recipient, msg) module_tell(self(), recipient, (unsigned char *)msg, strlen(msg), false)
#define m_publish_str(topic, msg) module_publish(self(), topic, (unsigned char *)msg, strlen(msg), false)
#define m_broadcast_str(msg) module_broadcast(self(), (unsigned char *)msg, strlen(msg), false)
#define m_tell_str(recipient, msg) module_tell(self(), recipient, (const void *)msg, strlen(msg), false)
#define m_publish_str(topic, msg) module_publish(self(), topic, (const void *)msg, strlen(msg), false)
#define m_broadcast_str(msg) module_broadcast(self(), (const void *)msg, strlen(msg), false)
14 changes: 7 additions & 7 deletions Lib/pubsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
/** Actor-like PubSub interface **/

static map_ret_code tell_if(void *data, const char *key, void *value);
static pubsub_priv_t *create_pubsub_msg(const unsigned char *message, const self_t *sender, const char *topic,
static pubsub_priv_t *create_pubsub_msg(const void *message, const self_t *sender, const char *topic,
enum msg_type type, const size_t size, const bool autofree);
static void destroy_pubsub_msg(pubsub_priv_t *pubsub_msg);
static module_ret_code tell_pubsub_msg(pubsub_priv_t *m, module *mod, m_context *c);
static module_ret_code publish_msg(const module *mod, const char *topic, const unsigned char *message,
static module_ret_code publish_msg(const module *mod, const char *topic, const void *message,
const ssize_t size, const bool autofree);

static map_ret_code tell_if(void *data, const char *key, void *value) {
Expand All @@ -31,7 +31,7 @@ static map_ret_code tell_if(void *data, const char *key, void *value) {
return MAP_OK;
}

static pubsub_priv_t *create_pubsub_msg(const unsigned char *message, const self_t *sender, const char *topic,
static pubsub_priv_t *create_pubsub_msg(const void *message, const self_t *sender, const char *topic,
enum msg_type type, const size_t size, const bool autofree) {
pubsub_priv_t *m = memhook._malloc(sizeof(pubsub_priv_t));
if (m) {
Expand Down Expand Up @@ -71,7 +71,7 @@ static module_ret_code tell_pubsub_msg(pubsub_priv_t *m, module *mod, m_context
return MOD_OK;
}

static module_ret_code publish_msg(const module *mod, const char *topic, const unsigned char *message,
static module_ret_code publish_msg(const module *mod, const char *topic, const void *message,
const ssize_t size, const bool autofree) {
MOD_PARAM_ASSERT(message);
MOD_PARAM_ASSERT(size > 0);
Expand Down Expand Up @@ -203,7 +203,7 @@ module_ret_code module_unsubscribe(const self_t *self, const char *topic) {
return MOD_ERR;
}

module_ret_code module_tell(const self_t *self, const self_t *recipient, const unsigned char *message,
module_ret_code module_tell(const self_t *self, const self_t *recipient, const void *message,
const ssize_t size, const bool autofree) {
GET_MOD(self);
MOD_PARAM_ASSERT(message);
Expand All @@ -216,15 +216,15 @@ module_ret_code module_tell(const self_t *self, const self_t *recipient, const u
return tell_pubsub_msg(m, recipient->mod, recipient->ctx);
}

module_ret_code module_publish(const self_t *self, const char *topic, const unsigned char *message,
module_ret_code module_publish(const self_t *self, const char *topic, const void *message,
const ssize_t size, const bool autofree) {
MOD_PARAM_ASSERT(topic);
GET_MOD(self);

return publish_msg(mod, topic, message, size, autofree);
}

module_ret_code module_broadcast(const self_t *self, const unsigned char *message,
module_ret_code module_broadcast(const self_t *self, const void *message,
const ssize_t size, const bool autofree) {
GET_MOD(self);

Expand Down
4 changes: 3 additions & 1 deletion Samples/Easy/doggo.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <module/module_easy.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

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

Expand Down Expand Up @@ -104,9 +105,10 @@ static void receive_sleeping(const msg_t *msg, const void *userdata) {
}
} else if (msg->pubsub_msg->type == MODULE_STARTED) {
/* A new module has been started */
const char *name = NULL;
char *name = NULL;
module_get_name(msg->pubsub_msg->sender, &name);
m_log("Module '%s' has been started started.\n", name);
free(name);
}
}
}
4 changes: 3 additions & 1 deletion Samples/MultiCtx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
static void *loop(void *param);

static void logger(const self_t *self, const char *fmt, va_list args, const void *userdata) {
const char *mname = NULL, *cname = NULL;
char *mname = NULL, *cname = NULL;
if (module_get_name(self, &mname) == MOD_OK &&
module_get_context(self, &cname) == MOD_OK) {

printf("%s@%s:\t*", mname, cname);
vprintf(fmt, args);
}
free(mname);
free(cname);
}

void setup_signals(void) {
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [x] Test pubsub messagging for paused modules
- [x] Fix memleaks!
- [x] Unsubscribe shopuld not check if topic is registered in ctx as otherwise umsubscribing from a deregistered topic would not work.
- [x] pubsub interface should take "const void *" instead of "const unsigned char *" as data

### Generic
- [x] Add some diagnostic API, eg: module_dump() (to dump each module's state)
Expand All @@ -28,7 +29,7 @@

- [ ] Rename pubsub_msg to ps_msg inside msg_t?
- [ ] Rename pubsub_msg_t to ps_msg_t?
- [ ] module_get_name/ctx to return a strdup?
- [x] module_get_name/ctx to return a strdup string

### Doc
- [x] module_dump
Expand Down
12 changes: 6 additions & 6 deletions docs/src/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Where not specified, these functions return a :ref:`module_ret_code <module_ret_
:param size: size of data to be sent.
:param autofree: whether to autofree msg after last recipient's received it.
:type recipient: :c:type:`const self_t *`
:type msg: :c:type:`const unsigned char *`
:type msg: :c:type:`const void *`
:type size: :c:type:`const ssize_t`
:type autofree: :c:type:`const bool`

Expand All @@ -192,7 +192,7 @@ Where not specified, these functions return a :ref:`module_ret_code <module_ret_
:param size: size of data to be published.
:param autofree: whether to autofree msg after last recipient's received it.
:type topic: :c:type:`const char *`
:type msg: :c:type:`const unsigned char *`
:type msg: :c:type:`const void *`
:type size: :c:type:`const ssize_t`
:type autofree: :c:type:`const bool`

Expand All @@ -203,7 +203,7 @@ Where not specified, these functions return a :ref:`module_ret_code <module_ret_
:param msg: data to be delivered to all modules in a context.
:param size: size of data to be delivered.
:param autofree: whether to autofree msg after last recipient's received it.
:type msg: :c:type:`const unsigned char *`
:type msg: :c:type:`const void *`
:type size: :c:type:`const ssize_t`
:type autofree: :c:type:`const bool`

Expand Down Expand Up @@ -449,7 +449,7 @@ Again, where not specified, these functions return a :ref:`module_ret_code <modu
:param autofree: whether to autofree msg after last recipient's received it.
:type self: :c:type:`const self_t *`
:type recipient: :c:type:`const self_t *`
:type msg: :c:type:`const unsigned char *`
:type msg: :c:type:`const void *`
:type size: :c:type:`const ssize_t`
:type autofree: :c:type:`const bool`

Expand All @@ -464,7 +464,7 @@ Again, where not specified, these functions return a :ref:`module_ret_code <modu
:param autofree: whether to autofree msg after last recipient's received it.
:type self: :c:type:`const self_t *`
:type topic: :c:type:`const char *`
:type msg: :c:type:`const unsigned char *`
:type msg: :c:type:`const void *`
:type size: :c:type:`const ssize_t`
:type autofree: :c:type:`const bool`

Expand All @@ -477,6 +477,6 @@ Again, where not specified, these functions return a :ref:`module_ret_code <modu
:param size: size of data to be published.
:param autofree: whether to autofree msg after last recipient's received it.
:type self: :c:type:`const self_t *`
:type msg: :c:type:`const unsigned char *`
:type msg: :c:type:`const void *`
:type size: :c:type:`const ssize_t`
:type autofree: :c:type:`const bool`

0 comments on commit 84f4504

Please sign in to comment.