From 97b610452f4b617e23a672444f809b71761a3e16 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 2 Jan 2011 17:56:11 -0500 Subject: [PATCH] Direct exchange --- Makefile | 3 +- direct.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ direct.h | 6 ++ main.c | 2 + queue.c | 3 +- sexp.c | 8 +++ sexp.h | 3 + t4 | 4 ++ t5 | 7 +++ t6 | 6 ++ 10 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 direct.c create mode 100644 direct.h create mode 100644 t4 create mode 100644 t5 create mode 100644 t6 diff --git a/Makefile b/Makefile index ada6574..02d14bd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ TARGET = cmsg -OBJECTS = main.o harness.o net.o util.o relay.o hashtable.o dataq.o sexp.o sexpio.o node.o queue.o +OBJECTS = main.o harness.o net.o util.o relay.o hashtable.o dataq.o sexp.o sexpio.o node.o \ + queue.o direct.o UUID_CFLAGS:=$(shell uuid-config --cflags) UUID_LDFLAGS:=$(shell uuid-config --ldflags) diff --git a/direct.c b/direct.c new file mode 100644 index 0000000..8428b31 --- /dev/null +++ b/direct.c @@ -0,0 +1,184 @@ +/* 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" + +typedef struct direct_extension_t_ { + sexp_t *name; + hashtable_t routing_table; + hashtable_t subscriptions; +} direct_extension_t; + +typedef struct subscription_t_ { + sexp_t *uuid; + sexp_t *sink; + sexp_t *name; + struct subscription_t_ *link; +} subscription_t; + +static void free_subscription(subscription_t *sub) { + DECREF(sub->uuid, sexp_destructor); + DECREF(sub->sink, sexp_destructor); + DECREF(sub->name, sexp_destructor); + free(sub); +} + +static void free_subscription_chain(void *context, cmsg_bytes_t key, void *value) { + subscription_t *chain = value; + while (chain != NULL) { + subscription_t *next = chain->link; + free_subscription(chain); + chain = next; + } +} + +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 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_subscription_chain, NULL); + destroy_hashtable(&d->routing_table); + destroy_hashtable(&d->subscriptions); + free(d); + } +} + +static int send_to_sub(subscription_t *sub, sexp_t *body) { + return post_node(sexp_data(sub->sink), sexp_data(sub->name), body, sub->uuid); +} + +static void route_message(direct_extension_t *d, sexp_t *rk, sexp_t *body) { + subscription_t *chain = NULL; + subscription_t *prev = NULL; + hashtable_get(&d->routing_table, sexp_data(rk), (void **) &chain); + while (chain != NULL) { + subscription_t *next = chain->link; + if (!send_to_sub(chain, body)) { /* Destination no longer exists. */ + info("Destination not found\n"); + if (prev == NULL) { + hashtable_put(&d->routing_table, sexp_data(rk), chain->link); + } else { + prev->link = chain->link; + } + chain->link = NULL; + free_subscription(chain); + } + chain = next; + } +} + +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"); + } + } else if ((msglen == 6) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("subscribe"))) { + unsigned char uuid[CMSG_UUID_BUF_SIZE]; + if (gen_uuid(uuid) != 0) { + warn("Could not generate UUID\n"); + } else { + sexp_t *filter = sexp_listref(args, 0); + sexp_t *reply_sink = sexp_listref(args, 3); + sexp_t *reply_name = sexp_listref(args, 4); + subscription_t *sub = malloc(sizeof(*sub)); + sub->uuid = INCREF(sexp_bytes(CMSG_BYTES(sizeof(uuid), uuid))); + sub->sink = sexp_listref(args, 1); + sub->name = sexp_listref(args, 2); + sub->link = NULL; + if (!sexp_stringp(filter) + || !sexp_stringp(sub->sink) || !sexp_stringp(sub->name) + || !sexp_stringp(reply_sink) || !sexp_stringp(reply_name)) { + DECREF(sub->uuid, sexp_destructor); + free(sub); + warn("Bad sink/name/reply_sink/reply_name in subscribe"); + } else { + INCREF(sub->sink); + INCREF(sub->name); + hashtable_put(&d->subscriptions, sexp_data(sub->uuid), sub); + hashtable_get(&d->routing_table, sexp_data(filter), (void **) &sub->link); + hashtable_put(&d->routing_table, sexp_data(filter), sub); + { + sexp_t *subok = sexp_cons(sexp_cstring("subscribe-ok"), sexp_cons(sub->uuid, NULL)); + INCREF(subok); + post_node(sexp_data(reply_sink), sexp_data(reply_name), subok, sexp_empty_bytes); + DECREF(subok, sexp_destructor); + } + } + } + } else if ((msglen == 2) && !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("unsubscribe"))) { + if (sexp_stringp(sexp_head(args))) { + cmsg_bytes_t uuid = sexp_data(sexp_head(args)); + subscription_t *sub; + if (hashtable_get(&d->subscriptions, uuid, (void **) &sub)) { + /* TODO: clean up more eagerly perhaps? */ + hashtable_erase(&d->subscriptions, uuid); + DECREF(sub->uuid, sexp_destructor); + sub->uuid = NULL; + } + } else { + warn("Invalid unsubscription\n"); + } + } else { + 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); +} diff --git a/direct.h b/direct.h new file mode 100644 index 0000000..38b7528 --- /dev/null +++ b/direct.h @@ -0,0 +1,6 @@ +#ifndef cmsg_direct_h +#define cmsg_direct_h + +extern void init_direct(void); + +#endif diff --git a/main.c b/main.c index d61d715..4a5a55f 100644 --- a/main.c +++ b/main.c @@ -21,6 +21,7 @@ typedef unsigned char u_char; #include "hashtable.h" #include "node.h" #include "queue.h" +#include "direct.h" #define WANT_CONSOLE_LISTENER 1 @@ -101,6 +102,7 @@ int main(int argc, char *argv[]) { init_node(); init_factory(); init_queue(); + init_direct(); #if WANT_CONSOLE_LISTENER spawn(console_listener, NULL); #endif diff --git a/queue.c b/queue.c index e0ec2be..18d1a37 100644 --- a/queue.c +++ b/queue.c @@ -179,7 +179,7 @@ static void queue_handle_message(node_t *n, sexp_t *m) { cmsg_bytes_t selector; if (msglen == 0 || !sexp_stringp(sexp_head(m))) { - warn("Invalid message in factory\n"); + warn("Invalid message in queue\n"); return; } @@ -204,6 +204,7 @@ static void queue_handle_message(node_t *n, sexp_t *m) { if (!sexp_stringp(sub->sink) || !sexp_stringp(sub->name) || !sexp_stringp(reply_sink) || !sexp_stringp(reply_name)) { DECREF(sub->uuid, sexp_destructor); + free(sub); warn("Bad sink/name/reply_sink/reply_name in subscribe"); } else { INCREF(sub->sink); diff --git a/sexp.c b/sexp.c index 4e9b20c..ab7926d 100644 --- a/sexp.c +++ b/sexp.c @@ -48,6 +48,14 @@ static inline void release_shell(sexp_t *x) { freelist = x; } +sexp_t *sexp_incref(sexp_t *x) { + return INCREF(x); +} + +sexp_t *sexp_decref(sexp_t *x) { + return DECREF(x, sexp_destructor); +} + void sexp_data_destructor(sexp_data_t *data) { cmsg_bytes_free(data->data); free(data); diff --git a/sexp.h b/sexp.h index 664c9fb..8632079 100644 --- a/sexp.h +++ b/sexp.h @@ -35,6 +35,9 @@ extern sexp_t *sexp_empty_bytes; extern void init_sexp(void); extern void done_sexp(void); +extern sexp_t *sexp_incref(sexp_t *x); +extern sexp_t *sexp_decref(sexp_t *x); + extern void sexp_data_destructor(sexp_data_t *data); extern void sexp_destructor(sexp_t *x); diff --git a/t4 b/t4 new file mode 100644 index 0000000..a304da2 --- /dev/null +++ b/t4 @@ -0,0 +1,4 @@ +(9:subscribe5:test40:0:5:test45:login) +(4:post7:factory(6:create6:direct(2:dx)5:test41:k)0:) +(4:post2:dx(9:subscribe1:a5:test48:consumer5:test41:k)0:) +(4:post2:dx(9:subscribe1:c5:test48:consumer5:test41:k)0:) diff --git a/t5 b/t5 new file mode 100644 index 0000000..12bedce --- /dev/null +++ b/t5 @@ -0,0 +1,7 @@ +(9:subscribe5:test50:0:5:test55:login) +(4:post7:factory(6:create6:direct(2:dx)5:test51:k)0:) +(4:post7:factory(6:create5:queue(2:q5)5:test51:k)0:) +(4:post2:q5(9:subscribe0:5:test59:consumer15:test51:k)0:) +(4:post2:q5(9:subscribe0:5:test59:consumer25:test51:k)0:) +(4:post2:dx(9:subscribe1:a2:q50:5:test51:k)0:) +(4:post2:dx(9:subscribe1:b2:q50:5:test51:k)0:) diff --git a/t6 b/t6 new file mode 100644 index 0000000..bda111c --- /dev/null +++ b/t6 @@ -0,0 +1,6 @@ +(9:subscribe5:test60:0:5:test65:login) +(4:post2:dx(4:post1:a9:messageA10:)0:) +(4:post2:dx(4:post1:a9:messageA20:)0:) +(4:post2:dx(4:post1:b8:messageB0:)0:) +(4:post2:dx(4:post1:c8:messageC0:)0:) +(11:unsubscribe5:test6)