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