/* Copyright (C) 2010 Tony Garnock-Jones. All rights reserved. */ #include #include #include #include #include #include #include #include #include "cmsg_private.h" #include "harness.h" #include "ref.h" #include "sexp.h" #include "hashtable.h" #include "node.h" #include "subscription.h" typedef struct fanout_extension_t_ { sexp_t *name; hashtable_t subscriptions; } fanout_extension_t; static sexp_t *fanout_extend(node_t *n, sexp_t *args) { if ((sexp_length(args) == 1) && sexp_stringp(sexp_head(args))) { cmsg_bytes_t name = sexp_data(sexp_head(args)); fanout_extension_t *f = calloc(1, sizeof(*f)); f->name = INCREF(sexp_head(args)); init_hashtable(&f->subscriptions, 5, NULL, (void (*)(void *)) free_subscription); n->extension = f; return bind_node(name, n) ? NULL : sexp_cstring("bind failed"); } else { return sexp_cstring("invalid args"); } } static void fanout_destructor(node_t *n) { fanout_extension_t *f = n->extension; if (f != NULL) { /* can be NULL if fanout_extend was given invalid args */ DECREF(f->name, sexp_destructor); destroy_hashtable(&f->subscriptions); free(f); } } struct delivery_context { fanout_extension_t *f; sexp_t *body; }; static void send_to_sub(void *contextv, cmsg_bytes_t key, void *subv) { struct delivery_context *context = contextv; subscription_t *sub = subv; send_to_subscription(&context->f->subscriptions, sub, context->body); } static void fanout_handle_message(node_t *n, sexp_t *m) { fanout_extension_t *f = n->extension; size_t msglen = sexp_length(m); sexp_t *args; cmsg_bytes_t selector; if (msglen == 0 || !sexp_stringp(sexp_head(m))) { warn("Invalid message in fanout\n"); return; } selector = sexp_data(sexp_head(m)); args = sexp_tail(m); if ((msglen == 4) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("post"))) { struct delivery_context context; context.f = f; context.body = sexp_listref(args, 1); hashtable_foreach(&f->subscriptions, send_to_sub, &context); return; } if ((msglen == 6) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("subscribe"))) { handle_subscribe_message(&f->subscriptions, args); return; } if ((msglen == 2) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("unsubscribe"))) { handle_unsubscribe_message(&f->subscriptions, args); return; } warn("Message not understood in fanout; selector <<%.*s>>, length %u\n", selector.len, selector.bytes, msglen); } static node_class_t fanout_class = { .name = "fanout", .extend = fanout_extend, .destroy = fanout_destructor, .handle_message = fanout_handle_message }; void init_fanout(void) { register_node_class(&fanout_class); }