git » nmdb » commit 7d1ee50

Make requests protocol-independant.

author Alberto Bertogli
2007-05-31 01:15:11 UTC
committer Alberto Bertogli
2007-05-31 01:15:11 UTC
parent 2ac34c99b959f9344ed4a2ceae2c747b4a467999

Make requests protocol-independant.

This is the first step towards implementing TCP support.

This patch makes the struct req_info a more general structure, allowing
its users not to depend on the networking protocol used.

Before this patch, db.c uses directly the tipc_* functions.

Now, we make req_info contain the function pointers needed to operate on
it (send the various replies), so the TIPC code is much more isolated and
the database code doesn't care about TIPC at all.


Signed-off-by: Alberto Bertogli <albertito@gmail.com>

nmdb/db.c +13 -12
nmdb/queue.h +1 -1
nmdb/req.h +38 -0
nmdb/tipc.c +18 -5
nmdb/tipc.h +0 -26

diff --git a/nmdb/db.c b/nmdb/db.c
index f289df0..defc881 100644
--- a/nmdb/db.c
+++ b/nmdb/db.c
@@ -10,6 +10,7 @@
 #include "be.h"
 #include "queue.h"
 #include "net-const.h"
+#include "req.h"
 
 
 static void *db_loop(void *arg);
@@ -100,10 +101,10 @@ static void process_op(db_t *db, struct queue_entry *e)
 	if (e->operation == REQ_SET_SYNC) {
 		rv = db_set(db, e->key, e->ksize, e->val, e->vsize);
 		if (!rv) {
-			tipc_reply_err(e->req, ERR_DB);
+			e->req->reply_err(e->req, ERR_DB);
 			return;
 		}
-		tipc_reply_set(e->req, REP_OK);
+		e->req->reply_set(e->req, REP_OK);
 
 	} else if (e->operation == REQ_SET_ASYNC) {
 		db_set(db, e->key, e->ksize, e->val, e->vsize);
@@ -114,25 +115,25 @@ static void process_op(db_t *db, struct queue_entry *e)
 
 		val = malloc(vsize);
 		if (val == NULL) {
-			tipc_reply_err(e->req, ERR_MEM);
+			e->req->reply_err(e->req, ERR_MEM);
 			return;
 		}
 		rv = db_get(db, e->key, e->ksize, val, &vsize);
 		if (rv == 0) {
-			tipc_reply_get(e->req, REP_NOTIN, NULL, 0);
+			e->req->reply_get(e->req, REP_NOTIN, NULL, 0);
 			free(val);
 			return;
 		}
-		tipc_reply_get(e->req, REP_OK, val, vsize);
+		e->req->reply_get(e->req, REP_OK, val, vsize);
 		free(val);
 
 	} else if (e->operation == REQ_DEL_SYNC) {
 		rv = db_del(db, e->key, e->ksize);
 		if (rv == 0) {
-			tipc_reply_del(e->req, REP_NOTIN);
+			e->req->reply_del(e->req, REP_NOTIN);
 			return;
 		}
-		tipc_reply_del(e->req, REP_OK);
+		e->req->reply_del(e->req, REP_OK);
 
 	} else if (e->operation == REQ_DEL_ASYNC) {
 		db_del(db, e->key, e->ksize);
@@ -144,12 +145,12 @@ static void process_op(db_t *db, struct queue_entry *e)
 		/* Compare */
 		dbval = malloc(dbvsize);
 		if (dbval == NULL) {
-			tipc_reply_err(e->req, ERR_MEM);
+			e->req->reply_err(e->req, ERR_MEM);
 			return;
 		}
 		rv = db_get(db, e->key, e->ksize, dbval, &dbvsize);
 		if (rv == 0) {
-			tipc_reply_get(e->req, REP_NOTIN, NULL, 0);
+			e->req->reply_get(e->req, REP_NOTIN, NULL, 0);
 			free(dbval);
 			return;
 		}
@@ -159,16 +160,16 @@ static void process_op(db_t *db, struct queue_entry *e)
 			/* Swap */
 			rv = db_set(db, e->key, e->ksize, e->newval, e->nvsize);
 			if (!rv) {
-				tipc_reply_err(e->req, ERR_DB);
+				e->req->reply_err(e->req, ERR_DB);
 				return;
 			}
 
-			tipc_reply_cas(e->req, REP_OK);
+			e->req->reply_cas(e->req, REP_OK);
 			free(dbval);
 			return;
 		}
 
-		tipc_reply_cas(e->req, REP_NOMATCH);
+		e->req->reply_cas(e->req, REP_NOMATCH);
 		free(dbval);
 
 	} else {
diff --git a/nmdb/queue.h b/nmdb/queue.h
index 93e7227..f50abe5 100644
--- a/nmdb/queue.h
+++ b/nmdb/queue.h
@@ -4,7 +4,7 @@
 
 #include <pthread.h>		/* for mutexes */
 #include <stdint.h>		/* for uint32_t */
-#include "tipc.h"		/* for req_info */
+#include "req.h"		/* for req_info */
 
 struct queue {
 	pthread_mutex_t lock;
diff --git a/nmdb/req.h b/nmdb/req.h
new file mode 100644
index 0000000..ec9d695
--- /dev/null
+++ b/nmdb/req.h
@@ -0,0 +1,38 @@
+
+#ifndef _REQ_H
+#define _REQ_H
+
+#include <stdint.h>		/* uint32_t */
+#include <sys/types.h>		/* size_t */
+#include <sys/socket.h>		/* socklen_t */
+
+
+/* req_info types, according to the protocol */
+#define REQTYPE_TIPC 1
+
+
+struct req_info {
+	/* network information */
+	int fd;
+	int type;
+
+	struct sockaddr *clisa;
+	socklen_t clilen;
+
+	/* operation information */
+	uint32_t id;
+	uint32_t cmd;
+	unsigned char *payload;
+	size_t psize;
+
+	/* operations */
+	void (*reply_err)(struct req_info *req, uint32_t reply);
+	void (*reply_get)(struct req_info *req, uint32_t reply,
+			unsigned char *val, size_t vsize);
+	void (*reply_set)(struct req_info *req, uint32_t reply);
+	void (*reply_del)(struct req_info *req, uint32_t reply);
+	void (*reply_cas)(struct req_info *req, uint32_t reply);
+};
+
+#endif
+
diff --git a/nmdb/tipc.c b/nmdb/tipc.c
index b4735ba..576a707 100644
--- a/nmdb/tipc.c
+++ b/nmdb/tipc.c
@@ -12,6 +12,7 @@
 #include "common.h"
 #include "queue.h"
 #include "net-const.h"
+#include "req.h"
 
 
 static void parse_msg(struct req_info *req, unsigned char *buf,
@@ -21,6 +22,14 @@ static void parse_set(struct req_info *req, int impact_db, int async);
 static void parse_del(struct req_info *req, int impact_db, int async);
 static void parse_cas(struct req_info *req, int impact_db);
 
+void tipc_reply_err(struct req_info *req, uint32_t reply);
+void tipc_reply_get(struct req_info *req, uint32_t reply,
+		unsigned char *val, size_t vsize);
+void tipc_reply_set(struct req_info *req, uint32_t reply);
+void tipc_reply_del(struct req_info *req, uint32_t reply);
+void tipc_reply_cas(struct req_info *req, uint32_t reply);
+
+
 /*
  * Miscelaneous helper functions
  */
@@ -41,8 +50,7 @@ static void rep_send_error(const struct req_info *req, const unsigned int code)
 	memcpy(minibuf + 8, &c, 4);
 
 	/* If this send fails, there's nothing to be done */
-	r = sendto(req->fd, minibuf, 3 * 4, 0, (struct sockaddr *) req->clisa,
-			req->clilen);
+	r = sendto(req->fd, minibuf, 3 * 4, 0, req->clisa, req->clilen);
 
 	if (r < 0) {
 		perror("rep_send_error() failed");
@@ -58,8 +66,7 @@ static int rep_send(const struct req_info *req, const unsigned char *buf,
 	if (settings.passive)
 		return 1;
 
-	rv = sendto(req->fd, buf, size, 0,
-			(struct sockaddr *) req->clisa, req->clilen);
+	rv = sendto(req->fd, buf, size, 0, req->clisa, req->clilen);
 	if (rv < 0) {
 		rep_send_error(req, ERR_SEND);
 		return 0;
@@ -307,8 +314,14 @@ void tipc_recv(int fd, short event, void *arg)
 	}
 
 	req.fd = fd;
-	req.clisa = &clisa;
+	req.type = REQTYPE_TIPC;
+	req.clisa = (struct sockaddr *) &clisa;
 	req.clilen = clilen;
+	req.reply_err = tipc_reply_err;
+	req.reply_get = tipc_reply_get;
+	req.reply_set = tipc_reply_set;
+	req.reply_del = tipc_reply_del;
+	req.reply_cas = tipc_reply_cas;
 
 	/* parse the message */
 	parse_msg(&req, buf, rv);
diff --git a/nmdb/tipc.h b/nmdb/tipc.h
index a743000..cb5c4e5 100644
--- a/nmdb/tipc.h
+++ b/nmdb/tipc.h
@@ -2,34 +2,8 @@
 #ifndef _MYTIPC_H
 #define _MYTIPC_H
 
-#include <stdint.h>		/* uint32_t */
-#include <sys/types.h>		/* size_t */
-#include <sys/socket.h>		/* socklen_t */
-#include <linux/tipc.h>		/* sockaddr_tipc */
-
-struct req_info {
-	/* network information */
-	int fd;
-	struct sockaddr_tipc *clisa;
-	socklen_t clilen;
-
-	/* operation information */
-	uint32_t id;
-	uint32_t cmd;
-	unsigned char *payload;
-	size_t psize;
-};
-
-
 int tipc_init(void);
 void tipc_recv(int fd, short event, void *arg);
 
-void tipc_reply_err(struct req_info *req, uint32_t reply);
-void tipc_reply_get(struct req_info *req, uint32_t reply,
-		unsigned char *val, size_t vsize);
-void tipc_reply_set(struct req_info *req, uint32_t reply);
-void tipc_reply_del(struct req_info *req, uint32_t reply);
-void tipc_reply_cas(struct req_info *req, uint32_t reply);
-
 #endif