libshevek
avahi.hh
1 /* avahi.hh - publish and browse network ports with avahi
2  * Copyright 2009 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_AVAHI_HH
19 #define SHEVEK_AVAHI_HH
20 
21 #include <map>
22 #include <set>
23 #include <glibmm.h>
24 #include "refbase.hh"
25 
26 #include <avahi-client/client.h>
27 #include <avahi-client/lookup.h>
28 #include <avahi-client/publish.h>
29 #include <avahi-common/alternative.h>
30 #include <avahi-common/error.h>
31 #include <avahi-common/simple-watch.h>
32 #include <avahi-glib/glib-watch.h>
33 
34 namespace shevek
35 {
37 
40  class avahi : public refbase
41  {
42  public:
44 
46  void publish (Glib::ustring const &protocol, int port);
48  class browser;
50 
52  inline Glib::RefPtr <browser> create_browser (Glib::ustring const &protocol);
54  static Glib::RefPtr <avahi> create (Glib::ustring const &name = Glib::ustring ()) { return Glib::RefPtr <avahi> (new avahi (name)); }
56  ~avahi ();
57  private:
58  avahi (Glib::ustring const &name, bool allow_restart = true, bool blocking_poller = false);
59  std::map <Glib::ustring, int> m_ports;
60  char *m_name;
61  bool m_allow_restart;
62  AvahiPoll const *m_poll_api;
63  AvahiGLibPoll *m_glib_poll;
64  AvahiEntryGroup *m_group;
65  AvahiClient *m_client;
66  AvahiSimplePoll *m_poller;
67  void create_services (AvahiClient *client);
68  void create_client ();
69  void name_change (AvahiClient *client);
70  static void group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata);
71  static void callback (AvahiClient *client, AvahiClientState state, void *userdata);
72  };
73 
75  class avahi::browser : public refbase
76  {
77  public:
79  struct details
80  {
82  AvahiIfIndex interface;
84  AvahiProtocol protocol;
86  Glib::ustring address;
88  AvahiLookupResultFlags flags;
90  details (AvahiIfIndex i, AvahiProtocol p) : interface (i), protocol (p) {}
92  details (AvahiIfIndex i, AvahiProtocol p, Glib::ustring a, AvahiLookupResultFlags f) : interface (i), protocol (p), address (a), flags (f) {}
94  details () {}
96  bool operator< (details const &that) const { return interface == that.interface ? protocol < that.protocol : interface < that.interface; }
97  };
99  typedef std::set <details> details_list;
101  struct owner
102  {
104  Glib::ustring host;
106  int port;
108  owner (Glib::ustring const &h, int p) : host (h), port (p) {}
110  owner () : port (-1) {}
112  bool operator< (owner const &that) const { return host == that.host ? port < that.port : host < that.host; }
114  details_list details;
115  };
117  typedef std::map <Glib::ustring, owner> list;
119  list const &get_list () { return m_list; }
121  sigc::signal1 <void, Glib::ustring const &> signal_changed () { return m_changed; }
123  ~browser ();
125  static Glib::RefPtr <browser> create (Glib::ustring const &protocol) { return Glib::RefPtr <browser> (new browser (avahi::create (), protocol)); }
127  static list get_list_block (Glib::ustring const &protocol, Glib::ustring const &name = Glib::ustring ());
128  private:
129  Glib::RefPtr <avahi> m_parent;
130  list m_list;
131  AvahiServiceBrowser *m_sb;
132  sigc::signal1 <void, Glib::ustring const &> m_changed;
133  Glib::ustring m_filter;
134  friend class avahi;
135  browser (Glib::RefPtr <avahi> parent, Glib::ustring const &protocol);
136  static void resolve_callback (AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, char const *name, char const *type, char const *domain, char const *host_name, AvahiAddress const *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void * userdata);
137  static void browse_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, char const *name, char const *type, char const *domain, AvahiLookupResultFlags flags, void *userdata);
138  };
139 
140  Glib::RefPtr <avahi::browser> avahi::create_browser (Glib::ustring const &protocol)
141  {
142  return Glib::RefPtr <browser> (new browser (refptr_this <avahi> (), protocol));
143  }
144 }
145 
146 #endif
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
bool operator<(details const &that) const
Allow sorting so details can be put in a std::set.
Definition: avahi.hh:96
Class for browsing other hosts.
Definition: avahi.hh:75
Serve and browse the local network using avahi.
Definition: avahi.hh:40
AvahiLookupResultFlags flags
Some flags about the result. Usually not useful; see the avahi documentation for possible values and ...
Definition: avahi.hh:88
AvahiProtocol protocol
Protocol. This is always tcp, because other protocols are never browsed by this class.
Definition: avahi.hh:84
static Glib::RefPtr< avahi > create(Glib::ustring const &name=Glib::ustring())
Create an avahi object for serving and/or browsing.
Definition: avahi.hh:54
details()
Create a details object. The application has no use for this.
Definition: avahi.hh:94
Definition: args.hh:52
details(AvahiIfIndex i, AvahiProtocol p)
Create a details object. The application has no use for this.
Definition: avahi.hh:90
details(AvahiIfIndex i, AvahiProtocol p, Glib::ustring a, AvahiLookupResultFlags f)
Create a details object. The application has no use for this.
Definition: avahi.hh:92
~avahi()
Unpublish all ports and free all structures associated with the object.
AvahiIfIndex interface
Network interface.
Definition: avahi.hh:82
Glib::RefPtr< browser > create_browser(Glib::ustring const &protocol)
Create a browser and populate it with a list of available hosts for the requested protocol...
Definition: avahi.hh:140
Glib::ustring address
Hostname or ip address of the server.
Definition: avahi.hh:86
Details about a discovered service. These are internally created and may be examined by the applicati...
Definition: avahi.hh:79
void publish(Glib::ustring const &protocol, int port)
Publish a service on a port.