From 40547b6702940c68cec48be580d80a6d95614c29 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 2 Mar 2008 12:09:42 -0500 Subject: Move msg handlers to modules, and make it ETERM based. --- src/Makefile | 2 +- src/msg.c | 70 +++++++++++++++++++++++++++++++++++++++++ src/msg.h | 13 ++++++++ src/mysqlerl.c | 99 +++++----------------------------------------------------- 4 files changed, 92 insertions(+), 92 deletions(-) create mode 100644 src/msg.c create mode 100644 src/msg.h diff --git a/src/Makefile b/src/Makefile index 5a9287d..c29b76f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,7 +10,7 @@ PRIVDIR = ../priv BEAMDIR = ../ebin BINS = $(PRIVDIR)/mysqlerl $(BEAMDIR)/mysqlerl.app -MYSQLERLOBJS = io.o log.o mysqlerl.o +MYSQLERLOBJS = io.o log.o msg.o mysqlerl.o BEAMS = mysqlerl.beam mysqlerl_app.beam mysqlerl_connection.beam \ mysqlerl_connection_sup.beam LIBS = -lmysqlclient diff --git a/src/msg.c b/src/msg.c new file mode 100644 index 0000000..aeac147 --- /dev/null +++ b/src/msg.c @@ -0,0 +1,70 @@ +#include "msg.h" + +#include "io.h" +#include "log.h" + +#include + +ETERM * +read_msg() +{ + ETERM *msg; + unsigned char *buf; + msglen_t len; + + logmsg("DEBUG: reading message length."); + if (restartable_read((unsigned char *)&len, sizeof(len)) == -1) { + logmsg("ERROR: couldn't read %d byte message prefix: %s.", + sizeof(len), strerror(errno)); + + exit(2); + } + + len = ntohl(len); + buf = (unsigned char *)malloc(len); + if (buf == NULL) { + logmsg("ERROR: Couldn't malloc %d bytes: %s.", len, + strerror(errno)); + + exit(2); + } + + logmsg("DEBUG: reading message body (len: %d).", len); + if (restartable_read(buf, len) == -1) { + logmsg("ERROR: couldn't read %d byte message: %s.", + len, strerror(errno)); + + free(buf); + exit(2); + } + + msg = erl_decode(buf); + free(buf); + + return msg; +} + +int +write_msg(ETERM *msg) +{ + unsigned char *buf; + msglen_t nlen, buflen; + + buflen = erl_term_len(msg); + buf = (unsigned char *)malloc(buflen); + erl_encode(msg, buf); + erl_free_term(msg); + + nlen = htonl(buflen); + if (restartable_write((unsigned char *)&nlen, sizeof(nlen)) == -1) { + free(buf); + return -1; + } + if (restartable_write(buf, buflen) == -1) { + free(buf); + return -1; + } + free(buf); + + return 0; +} diff --git a/src/msg.h b/src/msg.h new file mode 100644 index 0000000..2cbf31b --- /dev/null +++ b/src/msg.h @@ -0,0 +1,13 @@ +#ifndef _MSG_H +#define _MSG_H + +#include +#include +#include + +typedef u_int32_t msglen_t; + +ETERM *read_msg(); +int write_msg(ETERM *msg); + +#endif diff --git a/src/mysqlerl.c b/src/mysqlerl.c index b0bbc87..08ade42 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -6,93 +6,18 @@ #include "io.h" #include "log.h" +#include "msg.h" -#include -#include #include - -#include -#include #include const char *QUERY_MSG = "sql_query"; -typedef u_int32_t msglen_t; - -struct msg { - ETERM *cmd; - unsigned char *buf; -}; -typedef struct msg msg_t; - -msg_t * -read_msg() -{ - msg_t *msg; - msglen_t len; - - msg = (msg_t *)malloc(sizeof(msg_t)); - if (msg == NULL) { - logmsg("ERROR: Couldn't allocate message for reading: %s.\n", - strerror(errno)); - - exit(2); - } - - logmsg("DEBUG: reading message length."); - if (restartable_read((unsigned char *)&len, sizeof(len)) == -1) { - logmsg("ERROR: couldn't read %d byte message prefix: %s.", - sizeof(len), strerror(errno)); - - free(msg); - exit(2); - } - - len = ntohl(len); - msg->buf = malloc(len); - if (msg->buf == NULL) { - logmsg("ERROR: Couldn't malloc %d bytes: %s.", len, - strerror(errno)); - - free(msg); - exit(2); - } - - logmsg("DEBUG: reading message body (len: %d).", len); - if (restartable_read(msg->buf, len) == -1) { - logmsg("ERROR: couldn't read %d byte message: %s.", - len, strerror(errno)); - - free(msg->buf); - free(msg); - exit(2); - } - - msg->cmd = erl_decode(msg->buf); - - return msg; -} - -int -write_cmd(const char *cmd, msglen_t len) -{ - msglen_t nlen; - - nlen = htonl(len); - if (restartable_write((unsigned char *)&nlen, sizeof(nlen)) == -1) - return -1; - if (restartable_write((unsigned char *)cmd, len) == -1) - return -1; - - return 0; -} - void handle_sql_query(MYSQL *dbh, ETERM *cmd) { ETERM *query, *resp; - char *q, *buf; - int buflen; + char *q; query = erl_element(2, cmd); q = erl_iolist_to_string(query); @@ -182,23 +107,19 @@ handle_sql_query(MYSQL *dbh, ETERM *cmd) erl_free(q); logmsg("DEBUG: prepping buffers and sending."); - buflen = erl_term_len(resp); - buf = (char *)malloc(buflen); - erl_encode(resp, (unsigned char *)buf); + write_msg(resp); erl_free_term(resp); - write_cmd(buf, buflen); - free(buf); } void -dispatch_db_cmd(MYSQL *dbh, msg_t *msg) +dispatch_db_cmd(MYSQL *dbh, ETERM *msg) { ETERM *tag; - tag = erl_element(1, msg->cmd); + tag = erl_element(1, msg); if (strncmp((char *)ERL_ATOM_PTR(tag), QUERY_MSG, strlen(QUERY_MSG)) == 0) { - handle_sql_query(dbh, msg->cmd); + handle_sql_query(dbh, msg); } else { logmsg("WARNING: message type %s unknown.", (char *)ERL_ATOM_PTR(tag)); erl_free_term(tag); @@ -220,7 +141,7 @@ main(int argc, char *argv[]) { MYSQL dbh; char *host, *port, *db_name, *user, *passwd; - msg_t *msg; + ETERM *msg; openlog(); logmsg("INFO: starting up."); @@ -246,11 +167,7 @@ main(int argc, char *argv[]) while ((msg = read_msg()) != NULL) { dispatch_db_cmd(&dbh, msg); - - /* XXX: Move this to function */ - erl_free_compound(msg->cmd); - free(msg->buf); - free(msg); + erl_free_term(msg); } mysql_close(&dbh); -- cgit v1.2.3