main.c

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

mercurial