*/
static const char *config_keys[] =
{
+ "CacheFlush",
"Listen",
"Server",
"TimeToLive",
static avl_tree_t *cache_tree = NULL;
static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
+static time_t cache_flush_last;
+static int cache_flush_interval = 1800;
/*
* Private functions
*/
+static int cache_flush (void)
+{
+ char **keys = NULL;
+ int keys_num = 0;
+
+ char **tmp;
+ int i;
+
+ char *key;
+ time_t *value;
+ avl_iterator_t *iter;
+
+ time_t curtime = time (NULL);
+
+ iter = avl_get_iterator (cache_tree);
+ while (avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
+ {
+ if ((curtime - *value) <= cache_flush_interval)
+ continue;
+ tmp = (char **) realloc (keys,
+ (keys_num + 1) * sizeof (char *));
+ if (tmp == NULL)
+ {
+ sfree (keys);
+ avl_iterator_destroy (iter);
+ ERROR ("network plugin: cache_flush: realloc"
+ " failed.");
+ return (-1);
+ }
+ keys = tmp;
+ keys[keys_num] = key;
+ keys_num++;
+ } /* while (avl_iterator_next) */
+ avl_iterator_destroy (iter);
+
+ for (i = 0; i < keys_num; i++)
+ {
+ if (avl_remove (cache_tree, keys[i], (void *) &key,
+ (void *) &value) != 0)
+ {
+ WARNING ("network plugin: cache_flush: avl_remove"
+ " (%s) failed.", keys[i]);
+ continue;
+ }
+
+ sfree (key);
+ sfree (value);
+ }
+
+ sfree (keys);
+
+ DEBUG ("network plugin: cache_flush: Removed %i %s",
+ keys_num, (keys_num == 1) ? "entry" : "entries");
+ cache_flush_last = curtime;
+ return (0);
+} /* int cache_flush */
+
static int cache_check (const char *type, const value_list_t *vl)
{
char key[1024];
}
}
- /* TODO: Flush cache */
+ if ((time (NULL) - cache_flush_last) > cache_flush_interval)
+ cache_flush ();
pthread_mutex_unlock (&cache_lock);
if (buffer_len < (15))
{
- DEBUG ("packet is too short: buffer_len = %i", buffer_len);
+ DEBUG ("network plugin: packet is too short: buffer_len = %i",
+ buffer_len);
return (-1);
}
value_list_t vl = VALUE_LIST_INIT;
char type[DATA_MAX_NAME_LEN];
- DEBUG ("buffer = %p; buffer_len = %i;", buffer, buffer_len);
+ DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
+ buffer, buffer_len);
memset (&vl, '\0', sizeof (vl));
memset (&type, '\0', sizeof (type));
&& (strlen (type) > 0)
&& (cache_check (type, &vl) == 0))
{
- DEBUG ("dispatching values");
+ DEBUG ("network plugin: parse_packet:"
+ " dispatching values");
plugin_dispatch_values (type, &vl);
}
else
{
- DEBUG ("NOT dispatching values");
+ DEBUG ("network plugin: parse_packet:"
+ " NOT dispatching values");
}
}
else if (ntohs (header->type) == TYPE_TIME)
}
else
{
- DEBUG ("Unknown part type: 0x%0hx", ntohs (header->type));
+ DEBUG ("network plugin: parse_packet: Unknown part"
+ " type: 0x%0hx", ntohs (header->type));
buffer = ((char *) buffer) + ntohs (header->length);
}
} /* while (buffer_len > sizeof (part_header_t)) */
static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
{
- int loop = 1;
+ int loop = 0;
DEBUG ("fd = %i; calling `bind'", se->fd);
sockent_t *se;
int status;
- DEBUG ("buffer_len = %i", buffer_len);
+ DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
for (se = sending_sockets; se != NULL; se = se->next)
{
static void flush_buffer (void)
{
+ DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
+ send_buffer_fill);
+
network_send_buffer (send_buffer, send_buffer_fill);
send_buffer_ptr = send_buffer;
send_buffer_fill = 0;
}
else if (strcasecmp ("Forward", key) == 0)
{
- if ((strcasecmp ("true", value) == 0)
- || (strcasecmp ("yes", value) == 0)
- || (strcasecmp ("on", value) == 0))
+ if ((strcasecmp ("true", val) == 0)
+ || (strcasecmp ("yes", val) == 0)
+ || (strcasecmp ("on", val) == 0))
network_config_forward = 1;
else
network_config_forward = 0;
}
+ else if (strcasecmp ("CacheFlush", key) == 0)
+ {
+ int tmp = atoi (val);
+ if (tmp > 0)
+ cache_flush_interval = tmp;
+ else return (1);
+ }
else
{
return (-1);
}
return (0);
-}
+} /* int network_config */
static int network_shutdown (void)
{
memset (send_buffer_type, '\0', sizeof (send_buffer_type));
cache_tree = avl_create ((int (*) (const void *, const void *)) strcmp);
+ cache_flush_last = time (NULL);
/* setup socket(s) and so on */
if (sending_sockets != NULL)
plugin_register_config ("network", network_config,
config_keys, config_keys_num);
plugin_register_init ("network", network_init);
-}
+} /* void module_register */