#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <strophe.h>
xmpp_conn_t *conn1, *conn2;
unsigned long mincount, maxcount;
unsigned long count = 0;
unsigned long sum_squares = 0;
unsigned long samples = 0; /* Current number of samples taken */
unsigned long discard_samples = 5;
unsigned long max_samples = 20; /* The number of samples to do, if specified by user */
time_t lasttime; /* Time of the last sample we took */
char* jid1 = "test1@localhost";
char* jid2 = "test2@localhost";
char* pass1 = "test";
char* pass2 = "test";
xmpp_stanza_t *bstanza;
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1;
if(conn == conn2)
{
count++;
if(time(NULL) != lasttime)
{
if(lasttime > 0)
{
printf("%ld stanzas/sec\n", count);
if(count > maxcount)
{
printf("%ld beats old maximum of %ld\n", count, maxcount);
maxcount = count;
}
if(samples >= discard_samples)
sum_squares += count*count;
samples++;
if(samples >= max_samples)
{
printf("----\nMax: %ld\nRMS: %.2Lf\n----\n",
maxcount,
sqrtl(sum_squares/(samples-discard_samples))
);
xmpp_stop(ctx);
return 0;
}
}
count = 0;
lasttime = time(NULL);
}
xmpp_send(conn1, bstanza);
//fprintf(stderr, "C: %d\n", count);
}
return 1;
}
/* define a handler for connection events */
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
const int error, xmpp_stream_error_t * const stream_error,
void * const userdata)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
if (status == XMPP_CONN_CONNECT) {
xmpp_stanza_t* pres;
fprintf(stderr, "DEBUG: connected\n");
xmpp_handler_add(conn,message_handler, NULL, "message", NULL, ctx);
/* Send initial <presence/> so that we appear online to contacts */
pres = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(pres, "presence");
xmpp_send(conn, pres);
xmpp_stanza_release(pres);
if(conn == conn2)
{
count = 0;
lasttime = 0;
xmpp_send(conn1, bstanza);
}
}
else {
fprintf(stderr, "DEBUG: disconnected\n");
xmpp_stop(ctx);
}
}
int main(int argc, char *argv[])
{
xmpp_ctx_t *ctx;
xmpp_log_t *log = NULL;
xmpp_stanza_t *body, *text;
printf("Loading...\n");
/* init library */
xmpp_initialize();
/* create a context */
#ifdef LOGGING
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
#endif
ctx = xmpp_ctx_new(NULL, log);
/* create a connection */
conn1 = xmpp_conn_new(ctx);
conn2 = xmpp_conn_new(ctx);
/* setup authentication information */
xmpp_conn_set_jid(conn1, jid1);
xmpp_conn_set_pass(conn1, pass1);
xmpp_conn_set_jid(conn2, jid2);
xmpp_conn_set_pass(conn2, pass2);
bstanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(bstanza, "message");
xmpp_stanza_set_type(bstanza, "chat");
xmpp_stanza_set_attribute(bstanza, "to", jid2);
body = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(body, "body");
text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text, "Hello");
xmpp_stanza_add_child(body, text);
xmpp_stanza_add_child(bstanza, body);
xmpp_stanza_release(text);
xmpp_stanza_release(body);
/* initiate connection */
xmpp_connect_client(conn1, "localhost", 5222, conn_handler, ctx);
xmpp_connect_client(conn2, "localhost", 5222, conn_handler, ctx);
printf("Connecting...\n");
/* enter the event loop -
our connect handler will trigger an exit */
xmpp_run(ctx);
/* release our connection and context */
xmpp_conn_release(conn1);
xmpp_conn_release(conn2);
xmpp_ctx_free(ctx);
/* final shutdown of the library */
xmpp_shutdown();
return 0;
}