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

0
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #include <string.h>
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 #include <stdlib.h>
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 #include <time.h>
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 #include <strophe.h>
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 xmpp_conn_t *conn1, *conn2;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 unsigned long mincount, maxcount, count;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 long samples; /* Current number of samples taken */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 long max_samples; /* The number of samples to do, if specified by user */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 time_t lasttime; /* Time of the last sample we took */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 char* jid1 = "test1@getjabber.ath.cx";
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 char* jid2 = "test2@getjabber.ath.cx";
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 char* pass1 = "test1";
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 char* pass2 = "test2";
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 xmpp_stanza_t *bstanza;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if(conn == conn2)
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 count++;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if(time(NULL) != lasttime)
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 printf("%d stanzas/sec\n", count);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if(count > maxcount)
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 printf("%d beats old maximum of %d\n", count, maxcount);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 maxcount = count;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 count = 0;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 lasttime = time(NULL);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 xmpp_send(conn1, bstanza);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return 1;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 /* define a handler for connection events */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 const int error, xmpp_stream_error_t * const stream_error,
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 void * const userdata)
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if (status == XMPP_CONN_CONNECT) {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 xmpp_stanza_t* pres;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 fprintf(stderr, "DEBUG: connected\n");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 xmpp_handler_add(conn,message_handler, NULL, "message", NULL, ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 /* Send initial <presence/> so that we appear online to contacts */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 pres = xmpp_stanza_new(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 xmpp_stanza_set_name(pres, "presence");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 xmpp_send(conn, pres);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 xmpp_stanza_release(pres);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 if(conn == conn2)
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 count = 0;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 lasttime = time(NULL);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 xmpp_send(conn1, bstanza);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 else {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 fprintf(stderr, "DEBUG: disconnected\n");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 xmpp_stop(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 }
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 int main(int argc, char *argv[])
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 {
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 xmpp_ctx_t *ctx;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 xmpp_log_t *log = NULL;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 xmpp_stanza_t *body, *text;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 printf("Loading...\n");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 /* init library */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 xmpp_initialize();
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 /* create a context */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 #ifdef LOGGING
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 #endif
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 ctx = xmpp_ctx_new(NULL, log);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 /* create a connection */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 conn1 = xmpp_conn_new(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 conn2 = xmpp_conn_new(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 /* setup authentication information */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 xmpp_conn_set_jid(conn1, jid1);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 xmpp_conn_set_pass(conn1, pass1);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 xmpp_conn_set_jid(conn2, jid2);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 xmpp_conn_set_pass(conn2, pass2);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 bstanza = xmpp_stanza_new(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 xmpp_stanza_set_name(bstanza, "message");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 xmpp_stanza_set_type(bstanza, "chat");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 xmpp_stanza_set_attribute(bstanza, "to", jid2);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 body = xmpp_stanza_new(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 xmpp_stanza_set_name(body, "body");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 text = xmpp_stanza_new(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 xmpp_stanza_set_text(text, "Hello");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 xmpp_stanza_add_child(body, text);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 xmpp_stanza_add_child(bstanza, body);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 xmpp_stanza_release(text);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 xmpp_stanza_release(body);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 /* initiate connection */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 xmpp_connect_client(conn1, "localhost", 5222, conn_handler, ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 xmpp_connect_client(conn2, "localhost", 5222, conn_handler, ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 printf("Connecting...\n");
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 /* enter the event loop -
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 our connect handler will trigger an exit */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 xmpp_run(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 /* release our connection and context */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 xmpp_conn_release(conn1);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 xmpp_conn_release(conn2);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 xmpp_ctx_free(ctx);
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 /* final shutdown of the library */
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 xmpp_shutdown();
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 return 0;
2f2165f8c8ea Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 }

mercurial