Made verifications blocking calls with timeout

This commit is contained in:
James Booth
2015-05-27 22:41:51 +01:00
parent 8907bd0ac9
commit bb9b49a8ea
6 changed files with 73 additions and 4 deletions

View File

@@ -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)
{

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -1,4 +1,5 @@
#include <string.h>
#include <unistd.h>
#include <expat.h>
@@ -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 {

View File

@@ -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);

View File

@@ -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);