src/rrdd.c et al.: Use the tree implementation from GLib 2.0.
[rrdd.git] / src / rrdd.c
index 7cd2431..e82f0d9 100644 (file)
@@ -22,6 +22,8 @@
 #define RRDD_DEBUG 1
 
 #include "rrdd.h"
+#include <glib-2.0/glib.h>
+#include <rrd.h>
 
 #if RRDD_DEBUG
 # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
@@ -29,8 +31,6 @@
 # define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
 #endif
 
-#define DEFAULT_PORT "42217"
-
 /*
  * Types
  */
@@ -71,7 +71,7 @@ static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
 static int connetion_threads_num = 0;
 
 /* Cache stuff */
-static avl_tree_t     *cache_tree = NULL;
+static GTree          *cache_tree = NULL;
 static cache_item_t   *cache_queue_head = NULL;
 static cache_item_t   *cache_queue_tail = NULL;
 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -200,10 +200,7 @@ static int handle_request_update (int fd, /* {{{ */
 
   time_t now;
 
-  avl_node_t *node;
-  cache_item_t ci_temp;
   cache_item_t *ci;
-
   char answer[4096];
 
   now = time (NULL);
@@ -216,12 +213,10 @@ static int handle_request_update (int fd, /* {{{ */
   file = buffer_ptr;
   buffer_ptr += strlen (file) + 1;
 
-  ci_temp.file = file;
-
   pthread_mutex_lock (&cache_lock);
 
-  node = avl_search (cache_tree, (void *) &ci_temp);
-  if (node == NULL)
+  ci = g_tree_lookup (cache_tree, file);
+  if (ci == NULL)
   {
     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
     if (ci == NULL)
@@ -246,22 +241,11 @@ static int handle_request_update (int fd, /* {{{ */
     ci->last_flush_time = now;
     ci->flags = CI_FLAGS_IN_TREE;
 
-    if (avl_insert (cache_tree, (void *) ci) == NULL)
-    {
-      pthread_mutex_unlock (&cache_lock);
-      RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
-      free (ci->file);
-      free (ci);
-      return (-1);
-    }
+    g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
 
-    RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
+    RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
         ci->file);
   }
-  else /* if (ci != NULL) */
-  {
-    ci = (cache_item_t *) node->item;
-  }
   assert (ci != NULL);
 
   while (*buffer_ptr != 0)
@@ -548,7 +532,6 @@ static int open_listen_socket (const char *addr) /* {{{ */
   for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
   {
     int fd;
-    struct sockaddr_storage sa;
     listen_socket_t *temp;
 
     temp = (listen_socket_t *) realloc (listen_fds,
@@ -626,6 +609,9 @@ static void *listen_thread_main (void *args) /* {{{ */
     open_listen_socket (config_listen_address_list[i]);
   }
 
+  if (config_listen_address_list_len < 1)
+    open_listen_socket (RRDD_SOCK_PATH);
+
   if (listen_fds_num < 1)
   {
     RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
@@ -787,10 +773,10 @@ static int daemonize (void) /* {{{ */
 
   openlog ("rrdd", LOG_PID, LOG_DAEMON);
 
-  cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
+  cache_tree = g_tree_new ((GCompareFunc) strcmp);
   if (cache_tree == NULL)
   {
-    RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
+    RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
     return (-1);
   }