From bb9b49a8eac352deb0da806f53fe3b1f3fb60b01 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 27 May 2015 22:41:51 +0100 Subject: [PATCH] Made verifications blocking calls with timeout --- src/client/stabber.c | 6 ++++++ src/server/server.c | 4 +++- src/server/stanza.c | 16 ++++++++++++++- src/server/verify.c | 48 ++++++++++++++++++++++++++++++++++++++++++-- src/server/verify.h | 1 + stabber.h | 2 ++ 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/client/stabber.c b/src/client/stabber.c index 5505901..a1b34df 100644 --- a/src/client/stabber.c +++ b/src/client/stabber.c @@ -16,6 +16,12 @@ stbbr_start(int port) return server_run(port); } +void +stbbr_set_timeout(int seconds) +{ + verify_set_timeout(seconds); +} + int stbbr_auth_passwd(char *password) { diff --git a/src/server/server.c b/src/server/server.c index 1680703..5db028b 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -10,6 +10,7 @@ #include "server/parser.h" #include "server/prime.h" #include "server/stanza.h" +#include "server/verify.h" #include "server/server.h" #include "server/log.h" @@ -242,8 +243,9 @@ server_run(int port) pthread_mutex_unlock(&send_queue_lock); kill_recv = FALSE; - client = NULL; + verify_set_timeout(10); + log_init(); log_println("Starting on port: %d...", port); diff --git a/src/server/stanza.c b/src/server/stanza.c index e1a3c96..a2e7705 100644 --- a/src/server/stanza.c +++ b/src/server/stanza.c @@ -6,6 +6,7 @@ #include "server/stanza.h" #include "server/log.h" +pthread_mutex_t stanzas_lock; static GList *stanzas; XMPPStanza* @@ -62,6 +63,7 @@ stanza_show(XMPPStanza *stanza) void stanza_show_all(void) { + pthread_mutex_lock(&stanzas_lock); GList *curr = stanzas; while (curr) { XMPPStanza *stanza = curr->data; @@ -69,12 +71,15 @@ stanza_show_all(void) log_println(""); curr = g_list_next(curr); } + pthread_mutex_unlock(&stanzas_lock); } void stanza_add(XMPPStanza *stanza) { + pthread_mutex_lock(&stanzas_lock); stanzas = g_list_append(stanzas, stanza); + pthread_mutex_unlock(&stanzas_lock); } void @@ -253,7 +258,9 @@ _stanzas_equal(XMPPStanza *first, XMPPStanza *second) int stanza_verify_any(XMPPStanza *stanza) { + pthread_mutex_lock(&stanzas_lock); if (!stanzas) { + pthread_mutex_unlock(&stanzas_lock); return 0; } @@ -261,30 +268,35 @@ stanza_verify_any(XMPPStanza *stanza) while (curr) { XMPPStanza *curr_stanza = curr->data; if (_stanzas_equal(stanza, curr_stanza) == 0) { + pthread_mutex_unlock(&stanzas_lock); return 1; } curr = g_list_previous(curr); } + pthread_mutex_unlock(&stanzas_lock); return 0; } int stanza_verify_last(XMPPStanza *stanza) { + pthread_mutex_lock(&stanzas_lock); if (!stanzas) { + pthread_mutex_unlock(&stanzas_lock); return 0; } GList *last = g_list_last(stanzas); if (!last) { + pthread_mutex_unlock(&stanzas_lock); return 0; } XMPPStanza *last_stanza = (XMPPStanza *)last->data; - int res = _stanzas_equal(stanza, last_stanza); + pthread_mutex_unlock(&stanzas_lock); if (res == 0) { return 1; } else { @@ -319,6 +331,8 @@ stanza_free(XMPPStanza *stanza) void stanza_free_all(void) { + pthread_mutex_lock(&stanzas_lock); g_list_free_full(stanzas, (GDestroyNotify)stanza_free); stanzas = NULL; + pthread_mutex_unlock(&stanzas_lock); } diff --git a/src/server/verify.c b/src/server/verify.c index 5f51bcc..ebb717b 100644 --- a/src/server/verify.c +++ b/src/server/verify.c @@ -1,4 +1,5 @@ #include +#include #include @@ -7,6 +8,17 @@ static int depth = 0; static XMPPStanza *curr_stanza = NULL; +static int timeoutsecs = 0; + +void +verify_set_timeout(int seconds) +{ + if (seconds <= 0) { + timeoutsecs = 0; + } else { + timeoutsecs = seconds; + } +} static void start_element(void *data, const char *element, const char **attributes) @@ -59,7 +71,23 @@ verify_any(char *stanza_text) XML_Parse(parser, stanza_text, strlen(stanza_text), 0); XML_ParserFree(parser); - int result = stanza_verify_any(curr_stanza); + int result = 0; + if (timeoutsecs <= 0) { + result = stanza_verify_any(curr_stanza); + } else { + double elapsed = 0.0; + GTimer *timer = g_timer_new(); + while (elapsed < timeoutsecs * 1.0) { + result = stanza_verify_any(curr_stanza); + if (result) { + break; + } + + usleep(1000 * 50); + elapsed = g_timer_elapsed(timer, NULL); + } + } + if (result) { log_println("VERIFY SUCCESS: %s", stanza_text); } else { @@ -83,7 +111,23 @@ verify_last(char *stanza_text) XML_Parse(parser, stanza_text, strlen(stanza_text), 0); XML_ParserFree(parser); - int result = stanza_verify_last(curr_stanza); + int result = 0; + if (timeoutsecs <= 0) { + result = stanza_verify_last(curr_stanza); + } else { + double elapsed = 0.0; + GTimer *timer = g_timer_new(); + while (elapsed < timeoutsecs * 1.0) { + result = stanza_verify_last(curr_stanza); + if (result) { + break; + } + + usleep(1000 * 50); + elapsed = g_timer_elapsed(timer, NULL); + } + } + if (result) { log_println("VERIFY LAST SUCCESS: %s", stanza_text); } else { diff --git a/src/server/verify.h b/src/server/verify.h index 00093f3..c403b1a 100644 --- a/src/server/verify.h +++ b/src/server/verify.h @@ -1,6 +1,7 @@ #ifndef __H_VERIFY #define __H_VERIFY +void verify_set_timeout(int seconds); int verify_last(char *stanza); int verify_any(char *stanza); diff --git a/stabber.h b/stabber.h index 0d9cf40..ea259bb 100644 --- a/stabber.h +++ b/stabber.h @@ -4,6 +4,8 @@ int stbbr_start(int port); void stbbr_stop(void); +void stbbr_set_timeout(int seconds); + int stbbr_auth_passwd(char *password); void stbbr_for(char *id, char *stream);