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