-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdpong.c
318 lines (276 loc) · 9.67 KB
/
dpong.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* ========================================================================= *
* File: dpong.c
* vim: et:ts=3:sw=3:
*
* Description:
* DBUS pong utility file (= dping server).
*
* Copyright (C) 2005-2011 Nokia Corporation.
*
* Author: Leonid Moiseichuk <leonid.moiseichuk@nokia.com>
* Contact: Eero Tamminen <eero.tamminen@nokia.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ========================================================================= */
/* ========================================================================= *
* Includes
* ========================================================================= */
#include "utils.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <glib/gmain.h>
#include <dbus/dbus.h>
/* ========================================================================= *
* Definitions.
* ========================================================================= */
typedef struct
{
unsigned report;
unsigned counter;
unsigned recv;
unsigned lost;
unsigned min_time;
unsigned max_time;
unsigned tot_time;
unsigned initial_ts;
GMainContext* context;
GMainLoop* loop;
} SERVER;
/* ========================================================================= *
* Local data.
* ========================================================================= */
static unsigned s_report = 1000;
static SERVER* s_server;
static DBusError s_error;
/* ========================================================================= *
* Local methods.
* ========================================================================= */
static SERVER *
server_new(void)
{
SERVER* server = (SERVER*)malloc(sizeof(SERVER));
if (server)
{
memset(server, 0, sizeof(*server));
server->report = s_report;
server->initial_ts = get_time_us();
}
return server;
} /* server_new */
static void
server_delete(SERVER* server)
{
free(server);
}
static DBusHandlerResult
ping_object_message_handler_cb(DBusConnection* a_conn, DBusMessage *a_message, void* a_user_data)
{
/* Make compiler happy */
a_conn = a_conn;
a_user_data = a_user_data;
/* Is it our call?*/
if (dbus_message_is_method_call(a_message, INTERFACE, METHOD))
{
unsigned counter;
unsigned orig_timestamp;
if ( dbus_message_get_args(a_message, &s_error,
DBUS_TYPE_UINT32, &counter,
DBUS_TYPE_UINT32, &orig_timestamp,
DBUS_TYPE_INVALID) )
{
const unsigned now = get_time_us();
/* We shall be careful: message can be lost */
const unsigned diff = (now > orig_timestamp ? now - orig_timestamp : 0);
SERVER* server = s_server; /* Probably will be identified for each client */
/* If we're measuring the roundtrip performance, then just reply
and be done with it */
if (!dbus_message_get_no_reply(a_message))
{
DBusMessage *reply;
unsigned timestamp = get_time_us();
reply = dbus_message_new_method_return(a_message);
if (NULL == reply)
{
fatal("DPONG: ERROR: dbus_message_new_method_return() failure.");
}
dbus_message_append_args(reply,
DBUS_TYPE_UINT32, &counter,
DBUS_TYPE_UINT32, &orig_timestamp,
DBUS_TYPE_UINT32, ×tamp,
DBUS_TYPE_INVALID);
if (!dbus_connection_send(a_conn, reply, NULL))
{
fprintf(stderr, "DPONG: WARNING: dbus_connection_send() failure.\n");
}
dbus_message_unref(reply);
dbus_connection_flush(a_conn);
}
else
{
/* Check message losing */
if (counter == server->counter)
{
server->recv++;
if (diff > 0)
{
if (0 == server->min_time || server->min_time > diff)
server->min_time = diff;
if (0 == server->max_time || server->max_time < diff)
server->max_time = diff;
server->tot_time += diff;
}
}
else
{
/* Number of messages lost */
server->lost += (server->counter < counter ? counter - server->counter : server->report - server->counter);
}
/* Increase the index of message */
server->counter = counter + 1;
/* Reporting if it necessary */
if (0 == (server->counter % server->report))
{
/* fprintf (stdout, "dpong timestamp: %u microseconds\n", get_time_us()); */
fprintf (stdout, "MESSAGES recv %u lost %u LATENCY min %5u avg %5u max %5u THROUGHPUT %.1f m/s\n",
server->recv, server->lost,
server->min_time, (server->tot_time / server->recv), server->max_time,
server->recv/((double)(get_time_us()-server->initial_ts)/1000000)
);
/* Setup values for new test cycle */
if (server->counter < server->report)
{
/* We lose some messages and receive the first one from
the new cycle */
server->recv = 1;
server->lost = counter - 1;
server->min_time = diff;
server->tot_time = diff;
server->max_time = diff;
}
else
{
/* Normal flow, all messages received */
server->recv = 0;
server->lost = 0;
server->min_time = 0;
server->tot_time = 0;
server->max_time = 0;
server->initial_ts = get_time_us();
}
}
}
return 0;
}
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
} /* ping_object_message_handler_cb */
static void
ping_object_unregistered_cb(DBusConnection* a_conn, void* a_user_data)
{
/* Make compiler happy */
a_conn = a_conn;
a_user_data = a_user_data;
fprintf (stderr, "DPONG: PingObject unregistered.\n");
} /* ping_object_unregistered_cb */
static void usage(void)
{
fprintf(stderr,
"Usage: dpong [<report period>]\n"
"\n"
"Example:\n"
" dpong &\n"
" dping\n");
}
/* ========================================================================= *
* Public methods: main
* ========================================================================= */
int main(int argc, const char* argv[])
{
DBusConnection* session;
#if 0
dbus_uint32_t result;
#endif
static const DBusObjectPathVTable vtable = {
.unregister_function = ping_object_unregistered_cb,
.message_function = ping_object_message_handler_cb
};
if (argc > 2)
{
usage();
return 1;
}
if (argc == 2)
{
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
{
usage();
return 1;
}
else
{
s_report = (unsigned)atoi(argv[1]);
if (s_report <= 0) {
fprintf(stderr,
"DPONG: ERROR: invalid reporting period. Quitting!\n");
return 1;
}
}
}
g_type_init();
s_server = server_new();
set_base_time();
printf ("DPONG: server starting with PID %u ...\n", (unsigned) getpid());
printf ("DPONG: reporting period is set to %u messages.\n", s_report);
printf ("DPONG: base test time set to %ld seconds.\n", base_time);
dbus_error_init (&s_error) ;
/* Setup main loop */
s_server->context = g_main_context_new();
s_server->loop = g_main_loop_new(s_server->context, FALSE) ;
session = dbus_bus_get(DBUS_SERVER_TYPE, &s_error);
if (check_dbus_error(&s_error) || NULL == session )
{
fatal("DPONG: ERROR: dbus_bus_get() failure.\n");
}
dbus_connection_setup_with_g_main (session, s_server->context) ;
dbus_bus_request_name (session, SERVICE, DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &s_error);
if ( check_dbus_error(&s_error) )
{
fatal("DPONG: ERROR: Unable to use service name " SERVICE ".");
}
#if 0
/* Create registry */
registry = bus_registry_new(BusContext*);
if ( !registry )
{
fatal("unable to create registry");
}
if ( bus_registry_acquire_service(registry, session, SERVICE, 0, &result, transaction, &s_error) )
{
fprintf (stderr, "bus_registry_acquire_service --> %u + %u\n", result, s_error);
fatal("unable to acquire service");
}
#endif
if ( !dbus_connection_register_object_path(session, OBJECT, &vtable, NULL) )
{
fatal("DPONG: ERROR: dbus_connection_register_object_path() failure.");
}
fprintf (stderr, "DPONG: server base service is \"%s\".\n",
dbus_bus_get_unique_name(session));
fprintf (stderr, "DPONG: server is ready to accept calls!\n");
g_main_loop_run (s_server->loop) ;
dbus_connection_close (session);
server_delete(s_server);
return 0;
} /* main */
/* ========================================================================= *
* No more code in file dpong.c *
* ========================================================================= */