Browse Source

Use lub_list_t in konf.c

Serj Kalichev 6 years ago
parent
commit
39625360d3
6 changed files with 44 additions and 134 deletions
  1. 10 20
      bin/konfd.c
  2. 5 28
      konf/buf.h
  3. 20 80
      konf/buf/buf.c
  4. 0 6
      konf/buf/private.h
  5. 2 0
      lub/list.h
  6. 7 0
      lub/list/list.c

+ 10 - 20
bin/konfd.c

@@ -99,7 +99,7 @@ int main(int argc, char **argv)
 	int i;
 	char *str;
 	konf_tree_t *conf;
-	lub_bintree_t bufs;
+	lub_list_t *bufs;
 	konf_buf_t *tbuf;
 	struct options *opts = NULL;
 	int pidfd = -1;
@@ -193,9 +193,7 @@ int main(int argc, char **argv)
 	conf = konf_tree_new("", 0);
 
 	/* Initialize the tree of buffers */
-	lub_bintree_init(&bufs,
-		konf_buf_bt_offset(),
-		konf_buf_bt_compare, konf_buf_bt_getkey);
+	bufs = lub_list_new(konf_buf_compare, konf_buf_delete);
 
 	/* Set signal handler */
 	sigemptyset(&sig_set);
@@ -247,19 +245,16 @@ int main(int argc, char **argv)
 			if ((i == sock) || (i == ro_sock)) {
 				int new;
 				socklen_t size = sizeof(raddr);
-				new = accept(i,
-					(struct sockaddr *)&raddr, &size);
-				if (new < 0) {
+				new = accept(i, (struct sockaddr *)&raddr, &size);
+				if (new < 0)
 					continue;
-				}
 #ifdef DEBUG
 				fprintf(stderr, "------------------------------\n");
 				fprintf(stderr, "Connection established %u\n", new);
 #endif
-				konf_buftree_remove(&bufs, new);
+				konf_buftree_remove(bufs, new);
 				tbuf = konf_buf_new(new);
-				/* Insert it into the binary tree */
-				lub_bintree_insert(&bufs, tbuf);
+				lub_list_add(bufs, tbuf);
 				/* In a case of RW socket we use buf's data pointer
 				  to indicate RW or RO socket. NULL=RO, not-NULL=RW */
 				if (i == sock)
@@ -268,12 +263,12 @@ int main(int argc, char **argv)
 			} else {
 				int nbytes;
 
-				tbuf = konf_buftree_find(&bufs, i);
+				tbuf = konf_buftree_find(bufs, i);
 				/* Data arriving on an already-connected socket. */
 				if ((nbytes = konf_buf_read(tbuf)) <= 0) {
 					close(i);
 					FD_CLR(i, &active_fd_set);
-					konf_buftree_remove(&bufs, i);
+					konf_buftree_remove(bufs, i);
 #ifdef DEBUG
 					fprintf(stderr, "Connection closed %u\n", i);
 #endif
@@ -294,13 +289,8 @@ int main(int argc, char **argv)
 	/* Free resources */
 	konf_tree_delete(conf);
 
-	/* delete each buf */
-	while ((tbuf = lub_bintree_findfirst(&bufs))) {
-		/* remove the buf from the tree */
-		lub_bintree_remove(&bufs, tbuf);
-		/* release the instance */
-		konf_buf_delete(tbuf);
-	}
+	/* Delete bufs */
+	lub_list_free_all(bufs);
 
 	retval = 0;
 err:

+ 5 - 28
konf/buf.h

@@ -1,39 +1,19 @@
 /*
  * buf.h
  */
- /**
-\ingroup clish
-\defgroup clish_conf config
-@{
 
-\brief This class is a config in memory container.
-
-Use it to implement config in memory.
-
-*/
 #ifndef _konf_buf_h
 #define _konf_buf_h
 
 #include <stdio.h>
 
-#include <lub/bintree.h>
+#include <lub/list.h>
 
 typedef struct konf_buf_s konf_buf_t;
 
-/*=====================================
- * CONF INTERFACE
- *===================================== */
-/*-----------------
- * meta functions
- *----------------- */
 konf_buf_t *konf_buf_new(int fd);
-int konf_buf_bt_compare(const void *clientnode, const void *clientkey);
-void konf_buf_bt_getkey(const void *clientnode, lub_bintree_key_t * key);
-size_t konf_buf_bt_offset(void);
-/*-----------------
- * methods
- *----------------- */
-void konf_buf_delete(konf_buf_t *instance);
+int konf_buf_compare(const void *clientnode, const void *clientkey);
+void konf_buf_delete(void *instance);
 int konf_buf_read(konf_buf_t *instance);
 int konf_buf_add(konf_buf_t *instance, void *str, size_t len);
 char * konf_buf_string(char *instance, int len);
@@ -47,10 +27,7 @@ char * konf_buf__get_buf(const konf_buf_t *instance);
 void * konf_buf__get_data(const konf_buf_t *instance);
 void konf_buf__set_data(konf_buf_t *instance, void *data);
 
-int konf_buftree_read(lub_bintree_t *instance, int fd);
-char * konf_buftree_parse(lub_bintree_t *instance, int fd);
-void konf_buftree_remove(lub_bintree_t *instance, int fd);
-konf_buf_t *konf_buftree_find(lub_bintree_t *instance, int fd);
+void konf_buftree_remove(lub_list_t *instance, int fd);
+konf_buf_t *konf_buftree_find(lub_list_t *instance, int fd);
 
 #endif				/* _konf_buf_h */
-/** @} clish_conf */

+ 20 - 80
konf/buf/buf.c

@@ -16,39 +16,16 @@
 
 #define KONF_BUF_CHUNK 1024
 
-/*---------------------------------------------------------
- * PRIVATE META FUNCTIONS
- *--------------------------------------------------------- */
-int konf_buf_bt_compare(const void *clientnode, const void *clientkey)
-{
-	const konf_buf_t *this = clientnode;
-	int keyfd;
-
-	memcpy(&keyfd, clientkey, sizeof(keyfd));
-
-	return (this->fd - keyfd);
-}
-
 /*-------------------------------------------------------- */
-static void konf_buf_key(lub_bintree_key_t * key,
-	int fd)
+int konf_buf_compare(const void *first, const void *second)
 {
-	memcpy(key, &fd, sizeof(fd));
+	const konf_buf_t *f = (const konf_buf_t *)first;
+	const konf_buf_t *s = (const konf_buf_t *)second;
+	return (f->fd - s->fd);
 }
 
 /*-------------------------------------------------------- */
-void konf_buf_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
-{
-	const konf_buf_t *this = clientnode;
-
-	konf_buf_key(key, this->fd);
-}
-
-/*---------------------------------------------------------
- * PRIVATE METHODS
- *--------------------------------------------------------- */
-static void
-konf_buf_init(konf_buf_t * this, int fd)
+static void konf_buf_init(konf_buf_t * this, int fd)
 {
 	this->fd = fd;
 	this->buf = malloc(KONF_BUF_CHUNK);
@@ -56,9 +33,6 @@ konf_buf_init(konf_buf_t * this, int fd)
 	this->pos = 0;
 	this->rpos = 0;
 	this->data = NULL;
-
-	/* Be a good binary tree citizen */
-	lub_bintree_node_init(&this->bt_node);
 }
 
 /*--------------------------------------------------------- */
@@ -67,14 +41,6 @@ static void konf_buf_fini(konf_buf_t * this)
 	free(this->buf);
 }
 
-/*---------------------------------------------------------
- * PUBLIC META FUNCTIONS
- *--------------------------------------------------------- */
-size_t konf_buf_bt_offset(void)
-{
-	return offsetof(konf_buf_t, bt_node);
-}
-
 /*--------------------------------------------------------- */
 konf_buf_t *konf_buf_new(int fd)
 {
@@ -86,11 +52,10 @@ konf_buf_t *konf_buf_new(int fd)
 	return this;
 }
 
-/*---------------------------------------------------------
- * PUBLIC METHODS
- *--------------------------------------------------------- */
-void konf_buf_delete(konf_buf_t * this)
+/*-------------------------------------------------------- */
+void konf_buf_delete(void *data)
 {
+	konf_buf_t *this = (konf_buf_t *)data;
 	konf_buf_fini(this);
 	free(this);
 }
@@ -262,52 +227,27 @@ void konf_buf__set_data(konf_buf_t *this, void *data)
  *--------------------------------------------------------- */
 
 /*--------------------------------------------------------- */
-konf_buf_t *konf_buftree_find(lub_bintree_t * this,
-	int fd)
+static int find_fd(const void *key, const void *data)
 {
-	lub_bintree_key_t key;
-
-	konf_buf_key(&key, fd);
-
-	return lub_bintree_find(this, &key);
+	const int *fd = (const int *)key;
+	const konf_buf_t *d = (const konf_buf_t *)data;
+	return (*fd - d->fd);
 }
 
 /*--------------------------------------------------------- */
-void konf_buftree_remove(lub_bintree_t * this,
-	int fd)
+konf_buf_t *konf_buftree_find(lub_list_t *this, int fd)
 {
-	konf_buf_t *tbuf;
-
-	if ((tbuf = konf_buftree_find(this, fd)) == NULL)
-		return;
-
-	lub_bintree_remove(this, tbuf);
-	konf_buf_delete(tbuf);
+	return lub_list_find(this, find_fd, &fd);
 }
 
 /*--------------------------------------------------------- */
-int konf_buftree_read(lub_bintree_t * this, int fd)
+void konf_buftree_remove(lub_list_t *this, int fd)
 {
-	konf_buf_t *buf;
-
-	buf = konf_buftree_find(this, fd);
-	if (!buf)
-		return -1;
+	lub_list_node_t *t;
 
-	return konf_buf_read(buf);
-}
-
-
-/*--------------------------------------------------------- */
-char * konf_buftree_parse(lub_bintree_t * this,
-	int fd)
-{
-	konf_buf_t *buf;
-
-	buf = konf_buftree_find(this, fd);
-	if (!buf)
-		return NULL;
+	if ((t = lub_list_find_node(this, find_fd, &fd)) == NULL)
+		return;
 
-	return konf_buf_parse(buf);
+	konf_buf_delete((konf_buf_t *)lub_list_node__get_data(t));
+	lub_list_del(this, t);
 }
-

+ 0 - 6
konf/buf/private.h

@@ -1,15 +1,9 @@
-/*
- * conf.h
- */
 #ifndef _konf_buf_private_h
 #define _konf_buf_private_h
 
 #include "konf/buf.h"
 #include "lub/bintree.h"
 
-/*---------------------------------------------------------
- * PRIVATE TYPES
- *--------------------------------------------------------- */
 struct konf_buf_s {
 	lub_bintree_node_t bt_node;
 	int fd;

+ 2 - 0
lub/list.h

@@ -40,6 +40,8 @@ unsigned int lub_list_len(lub_list_t *list);
 lub_list_node_t *lub_list_match_node(lub_list_t *list,
 	lub_list_match_fn matchFn, const void *userkey,
 	lub_list_node_t **saveptr);
+void *lub_list_find_node(lub_list_t *list,
+	lub_list_match_fn matchFn, const void *userkey);
 void *lub_list_match(lub_list_t *list,
 	lub_list_match_fn matchFn, const void *userkey,
 	lub_list_node_t **saveptr);

+ 7 - 0
lub/list/list.c

@@ -256,6 +256,13 @@ lub_list_node_t *lub_list_match_node(lub_list_t *this,
 	return NULL;
 }
 
+/*--------------------------------------------------------- */
+void *lub_list_find_node(lub_list_t *this,
+	lub_list_match_fn matchFn, const void *userkey)
+{
+	return lub_list_match_node(this, matchFn, userkey, NULL);
+}
+
 /*--------------------------------------------------------- */
 void *lub_list_match(lub_list_t *this,
 	lub_list_match_fn matchFn, const void *userkey,