main.c

Fri, 15 May 2009 21:03:40 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 15 May 2009 21:03:40 +0100
changeset 0
2f2165f8c8ea
child 1
cfb60868c745
permissions
-rw-r--r--

Initial commit

#include <string.h>
#include <stdlib.h>
#include <time.h>

#include <strophe.h>


xmpp_conn_t *conn1, *conn2;

unsigned long mincount, maxcount, count;

long samples; /* Current number of samples taken */
long max_samples; /* The number of samples to do, if specified by user */

time_t lasttime; /* Time of the last sample we took */

char* jid1 = "test1@getjabber.ath.cx";
char* jid2 = "test2@getjabber.ath.cx";
char* pass1 = "test1";
char* pass2 = "test2";

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)
		{
			printf("%d stanzas/sec\n", count);
			if(count > maxcount)
			{
				printf("%d beats old maximum of %d\n", count, maxcount);
				maxcount = count;
			}
			count = 0;
			lasttime = time(NULL);
		}
		xmpp_send(conn1, bstanza);
	}

	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 = time(NULL);
		
		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;
}

mercurial