Made verifications blocking calls with timeout
This commit is contained in:
@@ -16,6 +16,12 @@ stbbr_start(int port)
|
|||||||
return server_run(port);
|
return server_run(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
stbbr_set_timeout(int seconds)
|
||||||
|
{
|
||||||
|
verify_set_timeout(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
stbbr_auth_passwd(char *password)
|
stbbr_auth_passwd(char *password)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "server/parser.h"
|
#include "server/parser.h"
|
||||||
#include "server/prime.h"
|
#include "server/prime.h"
|
||||||
#include "server/stanza.h"
|
#include "server/stanza.h"
|
||||||
|
#include "server/verify.h"
|
||||||
#include "server/server.h"
|
#include "server/server.h"
|
||||||
#include "server/log.h"
|
#include "server/log.h"
|
||||||
|
|
||||||
@@ -242,8 +243,9 @@ server_run(int port)
|
|||||||
pthread_mutex_unlock(&send_queue_lock);
|
pthread_mutex_unlock(&send_queue_lock);
|
||||||
|
|
||||||
kill_recv = FALSE;
|
kill_recv = FALSE;
|
||||||
|
|
||||||
client = NULL;
|
client = NULL;
|
||||||
|
verify_set_timeout(10);
|
||||||
|
|
||||||
log_init();
|
log_init();
|
||||||
log_println("Starting on port: %d...", port);
|
log_println("Starting on port: %d...", port);
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "server/stanza.h"
|
#include "server/stanza.h"
|
||||||
#include "server/log.h"
|
#include "server/log.h"
|
||||||
|
|
||||||
|
pthread_mutex_t stanzas_lock;
|
||||||
static GList *stanzas;
|
static GList *stanzas;
|
||||||
|
|
||||||
XMPPStanza*
|
XMPPStanza*
|
||||||
@@ -62,6 +63,7 @@ stanza_show(XMPPStanza *stanza)
|
|||||||
void
|
void
|
||||||
stanza_show_all(void)
|
stanza_show_all(void)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&stanzas_lock);
|
||||||
GList *curr = stanzas;
|
GList *curr = stanzas;
|
||||||
while (curr) {
|
while (curr) {
|
||||||
XMPPStanza *stanza = curr->data;
|
XMPPStanza *stanza = curr->data;
|
||||||
@@ -69,12 +71,15 @@ stanza_show_all(void)
|
|||||||
log_println("");
|
log_println("");
|
||||||
curr = g_list_next(curr);
|
curr = g_list_next(curr);
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
stanza_add(XMPPStanza *stanza)
|
stanza_add(XMPPStanza *stanza)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&stanzas_lock);
|
||||||
stanzas = g_list_append(stanzas, stanza);
|
stanzas = g_list_append(stanzas, stanza);
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -253,7 +258,9 @@ _stanzas_equal(XMPPStanza *first, XMPPStanza *second)
|
|||||||
int
|
int
|
||||||
stanza_verify_any(XMPPStanza *stanza)
|
stanza_verify_any(XMPPStanza *stanza)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&stanzas_lock);
|
||||||
if (!stanzas) {
|
if (!stanzas) {
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,30 +268,35 @@ stanza_verify_any(XMPPStanza *stanza)
|
|||||||
while (curr) {
|
while (curr) {
|
||||||
XMPPStanza *curr_stanza = curr->data;
|
XMPPStanza *curr_stanza = curr->data;
|
||||||
if (_stanzas_equal(stanza, curr_stanza) == 0) {
|
if (_stanzas_equal(stanza, curr_stanza) == 0) {
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
curr = g_list_previous(curr);
|
curr = g_list_previous(curr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
stanza_verify_last(XMPPStanza *stanza)
|
stanza_verify_last(XMPPStanza *stanza)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&stanzas_lock);
|
||||||
if (!stanzas) {
|
if (!stanzas) {
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
GList *last = g_list_last(stanzas);
|
GList *last = g_list_last(stanzas);
|
||||||
if (!last) {
|
if (!last) {
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
XMPPStanza *last_stanza = (XMPPStanza *)last->data;
|
XMPPStanza *last_stanza = (XMPPStanza *)last->data;
|
||||||
|
|
||||||
int res = _stanzas_equal(stanza, last_stanza);
|
int res = _stanzas_equal(stanza, last_stanza);
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
@@ -319,6 +331,8 @@ stanza_free(XMPPStanza *stanza)
|
|||||||
void
|
void
|
||||||
stanza_free_all(void)
|
stanza_free_all(void)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&stanzas_lock);
|
||||||
g_list_free_full(stanzas, (GDestroyNotify)stanza_free);
|
g_list_free_full(stanzas, (GDestroyNotify)stanza_free);
|
||||||
stanzas = NULL;
|
stanzas = NULL;
|
||||||
|
pthread_mutex_unlock(&stanzas_lock);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <expat.h>
|
#include <expat.h>
|
||||||
|
|
||||||
@@ -7,6 +8,17 @@
|
|||||||
|
|
||||||
static int depth = 0;
|
static int depth = 0;
|
||||||
static XMPPStanza *curr_stanza = NULL;
|
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
|
static void
|
||||||
start_element(void *data, const char *element, const char **attributes)
|
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_Parse(parser, stanza_text, strlen(stanza_text), 0);
|
||||||
XML_ParserFree(parser);
|
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) {
|
if (result) {
|
||||||
log_println("VERIFY SUCCESS: %s", stanza_text);
|
log_println("VERIFY SUCCESS: %s", stanza_text);
|
||||||
} else {
|
} else {
|
||||||
@@ -83,7 +111,23 @@ verify_last(char *stanza_text)
|
|||||||
XML_Parse(parser, stanza_text, strlen(stanza_text), 0);
|
XML_Parse(parser, stanza_text, strlen(stanza_text), 0);
|
||||||
XML_ParserFree(parser);
|
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) {
|
if (result) {
|
||||||
log_println("VERIFY LAST SUCCESS: %s", stanza_text);
|
log_println("VERIFY LAST SUCCESS: %s", stanza_text);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef __H_VERIFY
|
#ifndef __H_VERIFY
|
||||||
#define __H_VERIFY
|
#define __H_VERIFY
|
||||||
|
|
||||||
|
void verify_set_timeout(int seconds);
|
||||||
int verify_last(char *stanza);
|
int verify_last(char *stanza);
|
||||||
int verify_any(char *stanza);
|
int verify_any(char *stanza);
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
int stbbr_start(int port);
|
int stbbr_start(int port);
|
||||||
void stbbr_stop(void);
|
void stbbr_stop(void);
|
||||||
|
|
||||||
|
void stbbr_set_timeout(int seconds);
|
||||||
|
|
||||||
int stbbr_auth_passwd(char *password);
|
int stbbr_auth_passwd(char *password);
|
||||||
void stbbr_for(char *id, char *stream);
|
void stbbr_for(char *id, char *stream);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user