main.c

Fri, 15 May 2009 22:12:22 +0100

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

Print RMS of 15 samples

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

mercurial