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
|
#include <dbus/dbus.h>
#include <stdio.h>
#include <sys/time.h>
#define DBUS_COMPILATION /* cheat and use DBusList */
#include <dbus/dbus-list.h>
#include <bus/connection.h>
#undef DBUS_COMPILATION
typedef struct
{
long time;
DBusTimeout *timeout;
} LoopTimeout;
static DBusList *timeouts;
static long
get_time (void)
{
struct timeval r;
long time;
/* Can't use dbus-sysdeps here since that isn't
* available outside of libdbus.
*/
gettimeofday (&r, NULL);
time = r.tv_sec * 1000;
time += r.tv_usec / 1000;
return time;
}
static void
add_timeout (DBusTimeout *timeout,
void *data)
{
LoopTimeout *lt;
lt = dbus_new (LoopTimeout, 1);
lt->time = get_time () + dbus_timeout_get_interval (timeout);
lt->timeout = timeout;
_dbus_list_append (&timeouts, lt);
printf ("add timeout!\n");
}
static void
remove_timeout (DBusTimeout *timeout,
void *data)
{
printf ("remove timeout!\n");
}
static DBusHandlerResult
message_handler (DBusMessageHandler *handler,
DBusConnection *connection,
DBusMessage *message,
void *user_data)
{
printf ("server got a message!: %s\n",
dbus_message_get_name (message));
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static void
new_connection_callback (DBusServer *server,
DBusConnection *new_connection,
void *data)
{
if (!bus_connection_setup (new_connection))
return;
dbus_connection_set_timeout_functions (new_connection,
add_timeout, remove_timeout,
NULL, NULL);
dbus_connection_ref (new_connection);
}
static dbus_bool_t running_loop;
static void
loop_quit (void)
{
running_loop = FALSE;
}
static void
loop_run (void)
{
running_loop = TRUE;
long start_time = get_time ();
/* Horribly inefficient main loop */
while (running_loop)
{
DBusList *link, *list_copy;
long time;
time = get_time ();
_dbus_list_copy (&timeouts, &list_copy);
link = _dbus_list_get_first_link (&list_copy);
while (link != NULL)
{
LoopTimeout *lt = link->data;
if (lt->time <= time)
{
dbus_timeout_handle (lt->timeout);
_dbus_list_remove (&timeouts, lt);
}
link = _dbus_list_get_next_link (&list_copy, link);
}
if (get_time () - start_time > 1000)
loop_quit ();
}
}
int
main (int argc,
char **argv)
{
DBusServer *server;
DBusConnection *connection;
DBusResultCode result;
DBusMessage *message;
DBusMessageHandler *handler;
server = dbus_server_listen ("debug:name=test-server", &result);
dbus_server_set_new_connection_function (server,
new_connection_callback,
NULL, NULL);
dbus_server_set_timeout_functions (server,
add_timeout, remove_timeout,
NULL, NULL);
if (server == NULL)
{
fprintf (stderr, "Failed to start server: %s\n",
dbus_result_to_string (result));
return 1;
}
connection = dbus_connection_open ("debug:name=test-server", &result);
dbus_connection_set_timeout_functions (connection,
add_timeout, remove_timeout,
NULL, NULL);
if (connection == NULL)
{
fprintf (stderr, "Failed to connect to server: %s\n",
dbus_result_to_string (result));
return 1;
}
message = dbus_message_new (DBUS_SERVICE_DBUS,
DBUS_MESSAGE_HELLO);
dbus_message_append_fields (message,
DBUS_TYPE_STRING, "test",
0);
handler = dbus_message_handler_new (message_handler, NULL, NULL);
dbus_connection_add_filter (connection, handler);
dbus_connection_send_message (connection, message, NULL, NULL);
dbus_message_unref (message);
bus_connection_init ();
loop_run ();
dbus_connection_disconnect (connection);
return 0;
}
|