Add factory message handler
This commit is contained in:
parent
9e0191b274
commit
54c13c9694
41
main.c
41
main.c
|
@ -27,7 +27,46 @@ static node_t *factory_construct(node_class_t *nc, sexp_t *args) {
|
|||
}
|
||||
|
||||
static void factory_handle_message(node_t *n, sexp_t *m) {
|
||||
info("factory_handle_message\n");
|
||||
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 factory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
selector = sexp_data(sexp_head(m));
|
||||
args = sexp_tail(m);
|
||||
|
||||
if ((msglen == 5)
|
||||
&& !cmsg_bytes_cmp(selector, cmsg_cstring_bytes("create"))) {
|
||||
sexp_t *classname = sexp_listref(args, 0);
|
||||
sexp_t *ctor_arg = sexp_listref(args, 1);
|
||||
sexp_t *reply_sink = sexp_listref(args, 2);
|
||||
sexp_t *reply_name = sexp_listref(args, 3);
|
||||
if (sexp_stringp(classname) && sexp_stringp(reply_sink) && sexp_stringp(reply_name)) {
|
||||
cmsg_bytes_t classname_bytes = sexp_data(classname);
|
||||
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 {
|
||||
new_node(nc, ctor_arg);
|
||||
{
|
||||
sexp_t *createok = sexp_cons(sexp_bytes(cmsg_cstring_bytes("create-ok")), NULL);
|
||||
INCREF(createok);
|
||||
post_node(sexp_data(reply_sink),
|
||||
sexp_data(reply_name),
|
||||
createok);
|
||||
DECREF(createok, sexp_destructor);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warn("Message not understood in factory; selector <<%.*s>>, length %u\n",
|
||||
selector.len, selector.bytes,
|
||||
msglen);
|
||||
}
|
||||
}
|
||||
|
||||
static node_class_t factory_class = {
|
||||
|
|
Loading…
Reference in New Issue