From 927f8b06fe3550f45c8d7b119acaa77d9b22888c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 4 Jan 2005 23:52:38 +0000 Subject: [PATCH] some work git-svn-id: file:///home/lennart/svn/public/avahi/trunk@6 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- Makefile | 8 ++-- main.c | 9 ++++- server.c | 114 ++++++++++++++++++++++++++++++++++++++----------------- server.h | 26 ++++++------- 4 files changed, 105 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index dc3a033..1be90ce 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,10 @@ -CFLAGS=-g -O0 -Wall -W -pipe $(shell pkg-config --cflags glib-2.0) +CFLAGS=-g -O0 -Wall -W -pipe $(shell pkg-config --cflags glib-2.0) -Wno-unused LIBS=$(shell pkg-config --libs glib-2.0) -#flexmdns: main.o iface.o netlink.o server.o address.o util.o local.o -# $(CC) -o $@ $^ $(LIBS) +all: flexmdns prioq-test + +flexmdns: main.o iface.o netlink.o server.o address.o util.o local.o prioq.o + $(CC) -o $@ $^ $(LIBS) #test-llist: test-llist.o # $(CC) -o $@ $^ $(LIBS) diff --git a/main.c b/main.c index 0dcfa70..1765540 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include "flx.h" +#include "server.h" static GMainLoop *loop = NULL; @@ -16,6 +17,7 @@ int main(int argc, char *argv[]) { flxLocalAddrSource *l; flxAddress a; gchar *r; + flxQuery q; flx = flx_server_new(NULL); @@ -27,7 +29,12 @@ int main(int argc, char *argv[]) { flx_address_parse("::1", AF_INET6, &a); flx_server_add_address(flx, 0, 0, AF_UNSPEC, "ip6-localhost", &a); - g_timeout_add(1000, timeout, NULL); + q.name = "campari.local."; + q.class = FLX_DNS_CLASS_IN; + q.type = FLX_DNS_TYPE_A; + flx_server_post_query_job(flx, 0, AF_UNSPEC, NULL, &q); + + g_timeout_add(5000, timeout, NULL); loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(loop); diff --git a/server.c b/server.c index 35dbe3a..1f8bb6f 100644 --- a/server.c +++ b/server.c @@ -4,6 +4,42 @@ #include "server.h" #include "util.h" +#include "iface.h" + +static gint timeval_cmp(const GTimeVal *a, const GTimeVal *b) { + g_assert(a); + g_assert(b); + + if (a->tv_sec < b->tv_sec) + return -1; + + if (a->tv_sec > b->tv_sec) + return 1; + + if (a->tv_usec < b->tv_usec) + return -1; + + if (a->tv_usec > b->tv_usec) + return 1; + + return 0; +} + +static gint query_job_instance_compare(gpointer a, gpointer b) { + flxQueryJobInstance *j = a, *k = b; + g_assert(j); + g_assert(k); + + return timeval_cmp(&j->job->time, &k->job->time); +} + +static gint response_job_instance_compare(gpointer a, gpointer b) { + flxResponseJobInstance *j = a, *k = b; + g_assert(j); + g_assert(k); + + return timeval_cmp(&j->job->time, &k->job->time); +} flxServer *flx_server_new(GMainContext *c) { flxServer *s = g_new(flxServer, 1); @@ -19,8 +55,8 @@ flxServer *flx_server_new(GMainContext *c) { s->rrset_by_name = g_hash_table_new(g_str_hash, g_str_equal); s->entries = NULL; - s->first_response_job = s->last_response_job = NULL; - s->first_query_jobs = s->last_query_job = NULL; + s->query_job_queue = flx_prio_queue_new(query_job_instance_compare); + s->response_job_queue = flx_prio_queue_new(response_job_instance_compare); s->monitor = flx_interface_monitor_new(s->context); @@ -30,6 +66,12 @@ flxServer *flx_server_new(GMainContext *c) { void flx_server_free(flxServer* s) { g_assert(s); + while (s->query_job_queue->last) + flx_server_remove_query_job_instance(s, s->query_job_queue->last->data); + + flx_prio_queue_free(s->query_job_queue); + flx_prio_queue_free(s->response_job_queue); + flx_interface_monitor_free(s->monitor); flx_server_remove(s, 0); @@ -271,11 +313,13 @@ void flx_server_add_address(flxServer *s, gint id, gint interface, guchar protoc } flxQueryJob* flx_query_job_new(void) { - flxQueryJob *job = g_new(flxQueryJob); + flxQueryJob *job = g_new(flxQueryJob, 1); job->query.name = NULL; job->query.class = 0; job->query.type = 0; job->ref = 1; + job->time.tv_sec = 0; + job->time.tv_usec = 0; return job; } @@ -293,20 +337,32 @@ void flx_query_job_unref(flxQueryJob *job) { g_free(job); } +static gboolean query_job_exists(flxServer *s, gint interface, guchar protocol, flxQuery *q) { + flxPrioQueueNode *n; + g_assert(s); + g_assert(q); + + for (n = s->query_job_queue->root; n; n = n->next) + if (flx_query_equal(&((flxQueryJobInstance*) n->data)->job->query, q)) + return TRUE; + + return FALSE; +} + static void post_query_job(flxServer *s, gint interface, guchar protocol, flxQueryJob *job) { - flxQueryJobInstance *i; g_assert(s); g_assert(job); if (interface <= 0) { - flxInterface *i; + const flxInterface *i; - for (i = s->monitor->interfaces; i; i = i->next) + for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->next) post_query_job(s, i->index, protocol, job); } else if (protocol == AF_UNSPEC) { - post_query_job(s, index, AF_INET, job); - post_query_job(s, index, AF_INET6, job); + post_query_job(s, interface, AF_INET, job); + post_query_job(s, interface, AF_INET6, job); } else { + flxQueryJobInstance *i; if (query_job_exists(s, interface, protocol, &job->query)) return; @@ -315,16 +371,11 @@ static void post_query_job(flxServer *s, gint interface, guchar protocol, flxQue i->job = flx_query_job_ref(job); i->interface = interface; i->protocol = protocol; - if (i->prev = s->last_query_job) - i->prev->next = i; - else - s->first_query_job = i; - i->next = NULL; - s->last_query_job = i; + i->node = flx_prio_queue_put(s->query_job_queue, i); } } -void flx_server_post_query_job(flxServer *s, gint interface, guchar protocol, const flxQuery *q) { +void flx_server_post_query_job(flxServer *s, gint interface, guchar protocol, const GTimeVal *tv, const flxQuery *q) { flxQueryJob *job; g_assert(s); g_assert(q); @@ -333,42 +384,37 @@ void flx_server_post_query_job(flxServer *s, gint interface, guchar protocol, co job->query.name = g_strdup(q->name); job->query.class = q->class; job->query.type = q->type; + if (tv) + job->time = *tv; post_query_job(s, interface, protocol, job); } void flx_server_drop_query_job(flxServer *s, gint interface, guchar protocol, const flxQuery *q) { - flxQueryJobInstance *i, *next; + flxPrioQueueNode *n, *next; g_assert(s); g_assert(interface > 0); g_assert(protocol != AF_UNSPEC); g_assert(q); - for (i = s->first_query_job; i; i = next) { - next = i->next; + for (n = s->query_job_queue->root; n; n = next) { + next = n->next; - if (flx_query_equal(i->query, q)) - flx_server_remove_query_job_instance(s, i); + if (flx_query_equal(&((flxQueryJobInstance*) n->data)->job->query, q)) + flx_server_remove_query_job_instance(s, n->data); } } -gboolean flx_query_equal(const flxQuery *a, const flxQuery *b) { - return strcmp(a->name, b->name) == 0 && a->type == b->type && a->class == b->class; -} - void flx_server_remove_query_job_instance(flxServer *s, flxQueryJobInstance *i) { g_assert(s); g_assert(i); + g_assert(i->node); - if (i->prev) - i->prev = i->next; - else - s->first_query_job = i->next; - - if (i->next) - i->next = i->prev; - else - s->last_query_job = i->prev; - + flx_prio_queue_remove(s->query_job_queue, i->node); flx_query_job_unref(i->job); g_free(i); } + +gboolean flx_query_equal(const flxQuery *a, const flxQuery *b) { + return strcmp(a->name, b->name) == 0 && a->type == b->type && a->class == b->class; +} + diff --git a/server.h b/server.h index f1a15fc..4134638 100644 --- a/server.h +++ b/server.h @@ -3,6 +3,7 @@ #include "flx.h" #include "iface.h" +#include "prioq.h" struct _flxEntry; typedef struct _flxEntry flxEntry; @@ -21,31 +22,29 @@ struct _flxEntry { typedef struct _flxQueryJob { gint ref; + GTimeVal time; flxQuery query; } flxQueryJob; -struct _flxQueryJobInstance; -typedef struct _flxQueryJobInstance flxQueryJobInstance; -struct _flxQueryJobInstance { +typedef struct _flxQueryJobInstance { + flxPrioQueueNode *node; flxQueryJob *job; gint interface; guchar protocol; - flxQueryJobInstance *next, *prev; -}; +} flxQueryJobInstance; typedef struct _flxResponseJob { gint ref; + GTimeVal time; flxRecord response; } flxResponseJob; -struct _flxResponseJobInstance; -typedef struct _flxResponseJobInstance flxResponseJobInstance; -struct _flxResponseJobInstance { +typedef struct _flxResponseJobInstance { + flxPrioQueueNode *node; flxResponseJob *job; gint interface; guchar protocol; - flxResponseJob *next, *prev; -}; +} flxResponseJobInstance; struct _flxServer { GMainContext *context; @@ -58,20 +57,19 @@ struct _flxServer { flxEntry *entries; - flxResponseJobInstance *first_response_job, *last_response_job; - flxQueryJobInstance *first_query_job, *last_query_job; + flxPrioQueue *query_job_queue; + flxPrioQueue *response_job_queue; }; flxQueryJob* flx_query_job_new(void); flxQueryJob* flx_query_job_ref(flxQueryJob *job); void flx_query_job_unref(flxQueryJob *job); -void flx_server_post_query_job(flxServer *s, gint interface, guchar protocol, const flxQuery *q); +void flx_server_post_query_job(flxServer *s, gint interface, guchar protocol, const GTimeVal *tv, const flxQuery *q); void flx_server_drop_query_job(flxServer *s, gint interface, guchar protocol, const flxQuery *q); void flx_server_remove_query_job_instance(flxServer *s, flxQueryJobInstance *i); gboolean flx_query_equal(const flxQuery *a, const flxQuery *b); - #endif -- 2.39.5