summaryrefslogtreecommitdiffstats
path: root/test/watch.c
blob: bdb18306fe9818ec9b23448465208376b212e07f (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "watch.h"
#include <stdio.h>

#define DBUS_COMPILATION /* cheat and use DBusList */
#include <dbus/dbus-list.h>
#undef DBUS_COMPILATION

#include <sys/types.h>
#include <unistd.h>

/* Cheesy main loop used in test programs.  Any real app would use the
 * GLib or Qt or other non-sucky main loops.
 */ 

#undef	MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))

static int watch_list_serial = 0;
static DBusList *watches = NULL;
static dbus_bool_t exited = FALSE;
static DBusList *connections = NULL;

typedef enum
{
  WATCH_CONNECTION,
  WATCH_SERVER
} WatchType;

typedef struct
{
  WatchType type;
  void *data;
} WatchData;

static void
free_watch_data (void *data)
{
  WatchData *wd = data;

  if (wd->type == WATCH_CONNECTION)
    dbus_connection_unref (wd->data);
  else if (wd->type == WATCH_SERVER)
    dbus_server_unref (wd->data);

  dbus_free (wd);
}

static dbus_bool_t
add_connection_watch (DBusWatch      *watch,
                      DBusConnection *connection)
{
  WatchData *wd;

  if (!_dbus_list_append (&watches, watch))
    return FALSE;
  
  wd = dbus_new0 (WatchData, 1);
  if (wd == NULL)
    {
      _dbus_list_remove_last (&watches, watch);
      return FALSE;
    }  
  wd->type = WATCH_CONNECTION;
  wd->data = connection;

  dbus_connection_ref (connection);  

  dbus_watch_set_data (watch, wd, free_watch_data);

  watch_list_serial += 1;

#if 0
  printf ("Added connection %swatch for fd %d\n",
          dbus_watch_get_flags (watch) & DBUS_WATCH_WRITABLE ? "write " : "",
          dbus_watch_get_fd (watch));
#endif

  return TRUE;
 }

static void
remove_connection_watch (DBusWatch      *watch,
                         DBusConnection *connection)
{
  if (!_dbus_list_remove (&watches, watch))
    _dbus_assert_not_reached ("removed nonexistent watch");

  dbus_watch_set_data (watch, NULL, NULL);

  watch_list_serial += 1;

#if 0
  printf ("Removed connection watch for fd %d\n",
          dbus_watch_get_fd (watch));
#endif
}

static dbus_bool_t
add_server_watch (DBusWatch      *watch,
                  DBusServer     *server)
{
  WatchData *wd;
  
  if (!_dbus_list_append (&watches, watch))
    return FALSE;
  
  wd = dbus_new0 (WatchData, 1);
  if (wd == NULL)
    {
      _dbus_list_remove_last (&watches, watch);
      return FALSE;
    }
  
  wd->type = WATCH_SERVER;
  wd->data = server;

  dbus_server_ref (server);

  dbus_watch_set_data (watch, wd, free_watch_data);

  watch_list_serial += 1;

#if 0
  printf ("Added server %swatch for fd %d\n",
          dbus_watch_get_flags (watch) & DBUS_WATCH_WRITABLE ? "write " : "",
          dbus_watch_get_fd (watch));
#endif

  return TRUE;
}

static void
remove_server_watch (DBusWatch      *watch,
                     DBusServer     *server)
{
  if (!_dbus_list_remove (&watches, watch))
    _dbus_assert_not_reached ("removed nonexistent server watch");
  
  dbus_watch_set_data (watch, NULL, NULL);

  watch_list_serial += 1;

#if 0
  printf ("Removed server watch for fd %d\n",
          dbus_watch_get_fd (watch));
#endif
}

static int count = 0;

static void
disconnect (DBusConnection *connection)
{
  fprintf (stderr, "Disconnected\n");
  
  _dbus_list_remove (&connections, connection);
  dbus_connection_unref (connection);
  quit_mainloop ();
}


static void
check_messages (void)
{
  DBusList *link;
  
  link = _dbus_list_get_first_link (&connections);
  while (link != NULL)
    {
      DBusList *next = _dbus_list_get_next_link (&connections, link);
      DBusConnection *connection = link->data;
      DBusMessage *message;
      const char *name;
      
      while ((message = dbus_connection_pop_message (connection)))
        {
          DBusMessage *reply;

	  name = dbus_message_get_name (message);
	  if (name && strcmp (name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
	    {
	      disconnect (connection);
	    }
	  else
	    {
	      fprintf (stderr, "Received message %d, sending reply\n", count);
	      
	      reply = dbus_message_new ("org.freedesktop.DBus.Test", "org.freedesktop.DBus.Test");
	      if (!dbus_connection_send (connection,
                                         reply,
                                         NULL))
                fprintf (stderr, "No memory to send reply\n");
	      dbus_message_unref (reply);
	      
	      dbus_message_unref (message);
	      
	      count += 1;
	      if (count > 100)
		{
		  printf ("Saw %d messages, exiting\n", count);
		  quit_mainloop ();
		}
	    }
        }
      
      link = next;
    }
}

void
do_mainloop (void)
{
  /* Of course with any real app you'd use GMainLoop or
   * QSocketNotifier and not have to see all this crap.
   */
  while (!exited && watches != NULL)
    {
      fd_set read_set;
      fd_set write_set;
      fd_set err_set;
      int max_fd;
      DBusList *link;
      int initial_watch_serial;
      
      check_messages ();

      if (exited)
	break;
      
      FD_ZERO (&read_set);
      FD_ZERO (&write_set);
      FD_ZERO (&err_set);

      max_fd = -1;

      link = _dbus_list_get_first_link (&watches);
      while (link != NULL)
        {
          DBusList *next = _dbus_list_get_next_link (&watches, link);
          DBusWatch *watch;
          
          watch = link->data;

          if (dbus_watch_get_enabled (watch))
            {
              int fd;
              unsigned int flags;

              fd = dbus_watch_get_fd (watch);
              flags = dbus_watch_get_flags (watch);
              
              max_fd = MAX (max_fd, fd);
              
              if (flags & DBUS_WATCH_READABLE)
                FD_SET (fd, &read_set);
              
              if (flags & DBUS_WATCH_WRITABLE)
                FD_SET (fd, &write_set);
              
              FD_SET (fd, &err_set);
            }
          
          link = next;
        }

      select (max_fd + 1, &read_set, &write_set, &err_set, NULL);

      initial_watch_serial = watch_list_serial;
      link = _dbus_list_get_first_link (&watches);
      while (link != NULL)
        {
          DBusList *next = _dbus_list_get_next_link (&watches, link);
          DBusWatch *watch;

          if (initial_watch_serial != watch_list_serial)
            {
              /* Watches were added/removed,
               * hosing our list; break out of here
               */
              /* A more elegant solution might be to ref
               * all watches, then check which have fd >= 0
               * as we iterate over them, since removed
               * watches have their fd invalidated.
               */
              printf ("Aborting watch iteration due to serial increment\n");
              break;
            }
          
          watch = link->data;

          if (dbus_watch_get_enabled (watch))
            {
              int fd;
              unsigned int flags;
              unsigned int condition;

          
              fd = dbus_watch_get_fd (watch);
              flags = dbus_watch_get_flags (watch);

              condition = 0;
          
              if ((flags & DBUS_WATCH_READABLE) &&
                  FD_ISSET (fd, &read_set))
                condition |= DBUS_WATCH_READABLE;

              if ((flags & DBUS_WATCH_WRITABLE) &&
                  FD_ISSET (fd, &write_set))
                condition |= DBUS_WATCH_WRITABLE;

              if (FD_ISSET (fd, &err_set))
                condition |= DBUS_WATCH_ERROR;

              if (condition != 0)
                {
                  WatchData *wd;

                  wd = dbus_watch_get_data (watch);

                  if (wd->type == WATCH_CONNECTION)
                    {
                      DBusConnection *connection = wd->data;

                      dbus_connection_handle_watch (connection,
                                                    watch,
                                                    condition);
                    }
                  else if (wd->type == WATCH_SERVER)
                    {
                      DBusServer *server = wd->data;
                  
                      dbus_server_handle_watch (server,
                                                watch,
                                                condition);
                    }
                }
            }
          
          link = next;
        }
    }
}

void
quit_mainloop (void)
{
  exited = TRUE;
}


void
setup_connection (DBusConnection *connection)
{
  if (!dbus_connection_set_watch_functions (connection,
                                            (DBusAddWatchFunction) add_connection_watch,
                                            (DBusRemoveWatchFunction) remove_connection_watch,
                                            NULL,
                                            connection,
                                            NULL))
    _dbus_assert_not_reached ("not enough memory");

  dbus_connection_ref (connection);
  _dbus_list_append (&connections, connection);
}

void
setup_server (DBusServer *server)
{
  if (!dbus_server_set_watch_functions (server,
                                        (DBusAddWatchFunction) add_server_watch,
                                        (DBusRemoveWatchFunction) remove_server_watch,
                                        NULL,
                                        server,
                                        NULL))
    _dbus_assert_not_reached ("not enough memory");
}