/* 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 direct_extension_t_ { sexp_t *name; hashtable_t routing_table; hashtable_t subscriptions; } direct_extension_t; static sexp_t *direct_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)); direct_extension_t *d = calloc(1, sizeof(*d)); d->name = INCREF(sexp_head(args)); init_hashtable(&d->routing_table, 5, NULL, NULL); init_hashtable(&d->subscriptions, 5, NULL, NULL); n->extension = d; return bind_node(name, n) ? NULL : sexp_cstring("bind failed"); } else { return sexp_cstring("invalid args"); } } static void free_direct_chain(void *context, cmsg_bytes_t key, void *value) { free_subscription_chain(value); } static void direct_destructor(node_t *n) { direct_extension_t *d = n->extension; if (d != NULL) { /* can be NULL if direct_extend was given invalid args */ DECREF(d->name, sexp_destructor); hashtable_foreach(&d->routing_table, free_direct_chain, NULL); destroy_hashtable(&d->routing_table); destroy_hashtable(&d->subscriptions); free(d); } } static void route_message(direct_extension_t *d, sexp_t *rk, sexp_t *body) { subscription_t *chain = NULL; subscription_t *newchain; hashtable_get(&d->routing_table, sexp_data(rk), (void **) &chain); newchain = send_to_subscription_chain(&d->subscriptions, chain, body); if (newchain != chain) { hashtable_put(&d->routing_table, sexp_data(rk), newchain); } } static void direct_handle_message(node_t *n, sexp_t *m) { direct_extension_t *d = 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 direct\n"); return; } selector = sexp_data(sexp_head(m)); args = sexp_tail(m); if ((msglen == 4) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("post"))) { sexp_t *rk = sexp_listref(args, 0); if (sexp_stringp(rk)) { route_message(d, rk, sexp_listref(args, 1)); } else { warn("Non-string routing key in direct\n"); } return; } if ((msglen == 6) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("subscribe"))) { subscription_t *sub = handle_subscribe_message(&d->subscriptions, args); if (sub != NULL) { sexp_t *filter = sexp_listref(args, 0); hashtable_get(&d->routing_table, sexp_data(filter), (void **) &sub->link); hashtable_put(&d->routing_table, sexp_data(filter), sub); } return; } if ((msglen == 2) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("unsubscribe"))) { handle_unsubscribe_message(&d->subscriptions, args); return; } warn("Message not understood in direct; selector <<%.*s>>, length %u\n", selector.len, selector.bytes, msglen); } static node_class_t direct_class = { .name = "direct", .extend = direct_extend, .destroy = direct_destructor, .handle_message = direct_handle_message }; void init_direct(void) { register_node_class(&direct_class); }