hop-2012/experiments/cmsg/main.c

111 lines
2.6 KiB
C
Raw Normal View History

2011-01-05 18:08:13 +00:00
/* Copyright (C) 2010, 2011 Tony Garnock-Jones. All rights reserved. */
2010-12-27 21:56:42 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2011-01-02 18:40:36 +00:00
#include <signal.h>
2010-12-27 21:56:42 +00:00
#include <netinet/in.h>
#include <ucontext.h>
#include <assert.h>
2010-12-27 21:56:42 +00:00
typedef unsigned char u_char;
#include <event.h>
#include "cmsg_private.h"
#include "harness.h"
#include "net.h"
#include "ref.h"
#include "sexp.h"
#include "hashtable.h"
#include "node.h"
#include "queue.h"
2011-01-02 22:56:11 +00:00
#include "direct.h"
2011-01-02 23:14:51 +00:00
#include "fanout.h"
#include "relay.h"
2011-01-03 03:46:48 +00:00
#include "meta.h"
2011-01-10 00:42:36 +00:00
#include "messages.h"
#include "sexpio.h"
#define WANT_CONSOLE_LISTENER 1
static void factory_handle_message(node_t *n, sexp_t *m) {
2011-01-10 00:42:36 +00:00
parsed_message_t p;
2011-01-02 02:13:21 +00:00
2011-01-10 00:42:36 +00:00
if (parse_create(m, &p)) {
if (sexp_stringp(p.create.classname)
&& sexp_stringp(p.create.reply_sink)
&& sexp_stringp(p.create.reply_name)) {
cmsg_bytes_t classname_bytes = sexp_data(p.create.classname);
2011-01-02 02:13:21 +00:00
node_class_t *nc = lookup_node_class(classname_bytes);
if (nc == NULL) {
warn("Node class not found <<%.*s>>\n", classname_bytes.len, classname_bytes.bytes);
} else {
sexp_t *error = NULL;
sexp_t *reply;
2011-01-10 00:42:36 +00:00
if (new_node(nc, p.create.arg, &error) != NULL) {
2012-03-06 21:37:36 +00:00
reply = message_create_ok(NULL);
} else {
2011-01-10 00:42:36 +00:00
reply = message_create_failed(error);
2011-01-02 02:13:21 +00:00
}
2011-01-10 00:42:36 +00:00
post_node(sexp_data(p.create.reply_sink),
sexp_data(p.create.reply_name),
reply,
sexp_empty_bytes);
2011-01-02 02:13:21 +00:00
}
}
return;
2011-01-02 02:13:21 +00:00
}
2011-01-10 00:42:36 +00:00
warn("Message not understood in factory: ");
sexp_writeln(stderr_h, m);
}
static node_class_t factory_class = {
.name = "factory",
.extend = NULL,
.destroy = NULL,
.handle_message = factory_handle_message
};
static void init_factory(void) {
bind_node(cmsg_cstring_bytes("factory"), new_node(&factory_class, NULL, NULL));
}
2010-12-27 21:56:42 +00:00
#if WANT_CONSOLE_LISTENER
static void console_listener(void *arg) {
IOHandle *in_handle = new_iohandle(0);
while (1) {
cmsg_bytes_t buf = iohandle_readwait(in_handle, 1);
if (buf.len == 0) break;
iohandle_drain(in_handle, buf.len);
}
delete_iohandle(in_handle);
interrupt_harness();
}
#endif
2010-12-27 21:56:42 +00:00
int main(int argc, char *argv[]) {
2011-01-05 18:08:13 +00:00
info("cmsg ALPHA, Copyright (C) 2010, 2011 Tony Garnock-Jones. All rights reserved.\n");
2010-12-27 21:56:42 +00:00
event_init();
2011-01-02 18:40:36 +00:00
signal(SIGPIPE, SIG_IGN); /* avoid EPIPE when connections drop unexpectedly */
2010-12-27 21:56:42 +00:00
info("Using libevent version %s\n", event_get_version());
2011-01-02 18:58:18 +00:00
init_sexp();
2011-01-10 00:42:36 +00:00
init_messages();
init_node(cmsg_cstring_bytes("server"));
init_factory();
init_queue();
2011-01-02 22:56:11 +00:00
init_direct();
2011-01-02 23:14:51 +00:00
init_fanout();
init_relay();
2011-01-03 03:46:48 +00:00
init_meta();
#if WANT_CONSOLE_LISTENER
spawn(console_listener, NULL);
#endif
2010-12-27 21:56:42 +00:00
start_net(5671);
boot_harness();
2011-01-02 18:58:18 +00:00
done_sexp();
2010-12-27 21:56:42 +00:00
return 0;
}