git » nmdb » commit b4a2c2c

Make multiple TCP server support reliable.

author Alberto Bertogli
2007-06-01 14:39:32 UTC
committer Alberto Bertogli
2007-06-01 14:39:32 UTC
parent 7067979e4ef1938481084a3715d60b9b464696b0

Make multiple TCP server support reliable.

Handle correctly having more than one TCP server, mixed with TIPC servers,
by getting the compare right so that it sorts according to IP:port for
TCP and port for TIPC.

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

libnmdb/libnmdb.c +41 -14
libnmdb/nmdb.h +0 -1

diff --git a/libnmdb/libnmdb.c b/libnmdb/libnmdb.c
index f346492..89adca0 100644
--- a/libnmdb/libnmdb.c
+++ b/libnmdb/libnmdb.c
@@ -88,19 +88,52 @@ nmdb_t *nmdb_init()
 	return db;
 }
 
-/* Compare two servers, using their ports. It is used internally to keep the
- * server array sorted with qsort(). */
+/* Compare two servers by their connection identifiers. It is used internally
+ * to keep the server array sorted with qsort(). */
 static int compare_servers(const void *s1, const void *s2)
 {
 	struct nmdb_srv *srv1 = (struct nmdb_srv *) s1;
 	struct nmdb_srv *srv2 = (struct nmdb_srv *) s2;
 
-	if (srv1->id < srv2->id)
-		return -1;
-	else if (srv1->id == srv2->id)
-		return 0;
-	else
-		return 1;
+	if (srv1->type != srv2->type) {
+		if (srv1->type < srv2->type)
+			return -1;
+		else
+			return 1;
+	}
+
+	if (srv1->type == TIPC_CONN) {
+		if (srv1->info.tipc.port < srv2->info.tipc.port)
+			return -1;
+		else if (srv1->info.tipc.port == srv2->info.tipc.port)
+			return 0;
+		else
+			return 1;
+	} else if (srv1->type == TCP_CONN) {
+		in_addr_t a1, a2;
+		a1 = srv1->info.tcp.srvsa.sin_addr.s_addr;
+		a2 = srv2->info.tcp.srvsa.sin_addr.s_addr;
+
+		if (a1 < a2) {
+			return -1;
+		} else if (a1 == a2) {
+			in_port_t p1, p2;
+			p1 = srv1->info.tcp.srvsa.sin_port;
+			p2 = srv2->info.tcp.srvsa.sin_port;
+
+			if (p1 < p2)
+				return -1;
+			else if (p1 == p2)
+				return 0;
+			else
+				return 1;
+		} else {
+			return 1;
+		}
+	}
+
+	/* We should never get here */
+	return 0;
 }
 
 /* Add a TIPC server to the db connection. Requests will select which server
@@ -138,9 +171,6 @@ int nmdb_add_tipc_server(nmdb_t *db, int port)
 	newsrv->info.tipc.srvsa.scope = TIPC_CLUSTER_SCOPE;
 	newsrv->info.tipc.srvlen = (socklen_t) sizeof(newsrv->info.tipc.srvsa);
 
-	/* we use the port as an id for sorting */
-	newsrv->id = port;
-
 	newsrv->type = TIPC_CONN;
 
 	/* keep the list sorted by port, so we can do a reliable selection */
@@ -191,9 +221,6 @@ int nmdb_add_tcp_server(nmdb_t *db, const char *addr, int port)
 		return -1;
 	}
 
-	/* FIXME: find a decent ID to use */
-	newsrv->id = port;
-
 	newsrv->type = TCP_CONN;
 
 	/* keep the list sorted by port, so we can do a reliable selection */
diff --git a/libnmdb/nmdb.h b/libnmdb/nmdb.h
index 9906033..e1e3b27 100644
--- a/libnmdb/nmdb.h
+++ b/libnmdb/nmdb.h
@@ -21,7 +21,6 @@ struct nmdb_srv {
 			socklen_t srvlen;
 		} tcp;
 	} info;
-	unsigned long id;
 };
 
 typedef struct nmdb_t {