Ticket #185: avahi-bug2.c

File avahi-bug2.c, 2.8 kB (added by MathiasHasselmann, 1 year ago)
Line 
1 #include <avahi-client/lookup.h>
2 #include <avahi-client/publish.h>
3 #include <avahi-glib/glib-watch.h>
4 #include <glib.h>
5
6 static void
7 service_browser_cb (AvahiServiceBrowser *b,
8                      AvahiIfIndex interface,
9                      AvahiProtocol protocol,
10                      AvahiBrowserEvent event,
11                      const char *name,
12                      const char *type,
13                      const char *domain,
14                      AvahiLookupResultFlags flags,
15                      void *userdata)
16 {
17   static const gchar *names[] = {
18     [AVAHI_BROWSER_NEW] "new",
19     [AVAHI_BROWSER_REMOVE] "remove",
20     [AVAHI_BROWSER_CACHE_EXHAUSTED] "cache-exhausted",
21     [AVAHI_BROWSER_ALL_FOR_NOW] "all-for-now",
22     [AVAHI_BROWSER_FAILURE] "failure",
23   };
24
25   g_print ("%s: b=%p, e=%d(%s), n=%s, t=%s\n", G_STRFUNC, b, event, names[event], name, type);
26 }
27
28 static AvahiEntryGroup*
29 announce (AvahiClient *client,
30           guint        port)
31 {
32   AvahiEntryGroup *g = avahi_entry_group_new (client, NULL, NULL);
33
34   avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
35                                  "YaddaTest", "_test._tcp", NULL, NULL, port, NULL);
36   avahi_entry_group_add_service_subtype (g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
37                                          "YaddaTest", "_test._tcp", NULL, "_yadda._sub._test._tcp");
38
39   avahi_entry_group_commit (g);
40
41   return g;
42 }
43
44 static gboolean
45 restart_cb (gpointer data)
46 {
47   announce (data, 9001);
48   return FALSE;
49 }
50
51 static gboolean
52 kill_cb (gpointer data)
53 {
54   AvahiClient *client = avahi_entry_group_get_client (data);
55
56   avahi_entry_group_free (data);
57
58   if (g_getenv ("DELAY"))
59     {
60       g_print ("%s: using delay strategy\n", G_STRFUNC);
61       g_timeout_add (1000, restart_cb, client);
62     }
63   else if (g_getenv ("IDLE"))
64     {
65       g_print ("%s: using idle strategy\n", G_STRFUNC);
66       g_idle_add (restart_cb, client);
67     }
68   else
69     {
70       g_print ("%s: using immediate strategy\n", G_STRFUNC);
71       restart_cb (client);
72     }
73
74   return FALSE;
75 }
76
77 int
78 main (int   argc,
79       char *argv[])
80 {
81   AvahiServiceBrowser *b;
82   AvahiEntryGroup *g;
83   AvahiGLibPoll *poll;
84   AvahiClient *client;
85   GMainLoop *loop;
86
87   poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
88   client = avahi_client_new (avahi_glib_poll_get (poll), 0, NULL, NULL, NULL);
89
90   b = avahi_service_browser_new (client,
91                                  AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
92                                  "_yadda._sub._test._tcp", NULL, 0,
93                                  service_browser_cb, NULL);
94
95   g = announce (client, 9000);
96   g_timeout_add (1500, kill_cb, g);
97
98   loop = g_main_loop_new (NULL, FALSE);
99   g_main_loop_run (loop);
100   g_main_loop_unref (loop);
101
102   avahi_client_free (client);
103   avahi_glib_poll_free (poll);
104
105   return 0;
106 }