avahi.hh
/* avahi.hh - publish and browse network ports with avahi
* Copyright 2009 Bas Wijnen <wijnen@debian.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_AVAHI_HH
#define SHEVEK_AVAHI_HH
#include <map>
#include <set>
#include <glibmm.h>
#include "refbase.hh"
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/error.h>
#include <avahi-common/simple-watch.h>
#include <avahi-glib/glib-watch.h>
namespace shevek
{
class avahi : public refbase
{
public:
void publish (Glib::ustring const &protocol, int port);
class browser;
inline Glib::RefPtr <browser> create_browser (Glib::ustring const &protocol);
static Glib::RefPtr <avahi> create (Glib::ustring const &name = Glib::ustring ()) { return Glib::RefPtr <avahi> (new avahi (name)); }
~avahi ();
private:
avahi (Glib::ustring const &name, bool allow_restart = true, bool blocking_poller = false);
std::map <Glib::ustring, int> m_ports;
char *m_name;
bool m_allow_restart;
AvahiPoll const *m_poll_api;
AvahiGLibPoll *m_glib_poll;
AvahiEntryGroup *m_group;
AvahiClient *m_client;
AvahiSimplePoll *m_poller;
void create_services (AvahiClient *client);
void create_client ();
void name_change (AvahiClient *client);
static void group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata);
static void callback (AvahiClient *client, AvahiClientState state, void *userdata);
};
class avahi::browser : public refbase
{
public:
struct details
{
AvahiIfIndex interface;
AvahiProtocol protocol;
Glib::ustring address;
AvahiLookupResultFlags flags;
details (AvahiIfIndex i, AvahiProtocol p) : interface (i), protocol (p) {}
details (AvahiIfIndex i, AvahiProtocol p, Glib::ustring a, AvahiLookupResultFlags f) : interface (i), protocol (p), address (a), flags (f) {}
details () {}
bool operator< (details const &that) const { return interface == that.interface ? protocol < that.protocol : interface < that.interface; }
};
typedef std::set <details> details_list;
struct owner
{
Glib::ustring host;
int port;
owner (Glib::ustring const &h, int p) : host (h), port (p) {}
owner () : port (-1) {}
bool operator< (owner const &that) const { return host == that.host ? port < that.port : host < that.host; }
details_list details;
};
typedef std::map <Glib::ustring, owner> list;
list const &get_list () { return m_list; }
sigc::signal1 <void, Glib::ustring const &> signal_changed () { return m_changed; }
~browser ();
static Glib::RefPtr <browser> create (Glib::ustring const &protocol) { return Glib::RefPtr <browser> (new browser (avahi::create (), protocol)); }
static list get_list_block (Glib::ustring const &protocol, Glib::ustring const &name = Glib::ustring ());
private:
Glib::RefPtr <avahi> m_parent;
list m_list;
AvahiServiceBrowser *m_sb;
sigc::signal1 <void, Glib::ustring const &> m_changed;
Glib::ustring m_filter;
friend class avahi;
browser (Glib::RefPtr <avahi> parent, Glib::ustring const &protocol);
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);
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);
};
Glib::RefPtr <avahi::browser> avahi::create_browser (Glib::ustring const &protocol)
{
return Glib::RefPtr <browser> (new browser (refptr_this <avahi> (), protocol));
}
}
#endif
shevek::refbase
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
shevek::avahi::browser::details::operator<
bool operator<(details const &that) const
Allow sorting so details can be put in a std::set.
Definition: avahi.hh:96
shevek::avahi::browser
Class for browsing other hosts.
Definition: avahi.hh:75
shevek::avahi
Serve and browse the local network using avahi.
Definition: avahi.hh:40
shevek::avahi::browser::details::flags
AvahiLookupResultFlags flags
Some flags about the result. Usually not useful; see the avahi documentation for possible values and ...
Definition: avahi.hh:88
shevek::avahi::browser::details::protocol
AvahiProtocol protocol
Protocol. This is always tcp, because other protocols are never browsed by this class.
Definition: avahi.hh:84
shevek::avahi::create
static Glib::RefPtr< avahi > create(Glib::ustring const &name=Glib::ustring())
Create an avahi object for serving and/or browsing.
Definition: avahi.hh:54
shevek::avahi::browser::details::details
details()
Create a details object. The application has no use for this.
Definition: avahi.hh:94
shevek
Definition: args.hh:52
shevek::avahi::browser::details::details
details(AvahiIfIndex i, AvahiProtocol p)
Create a details object. The application has no use for this.
Definition: avahi.hh:90
shevek::avahi::browser::details::details
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
shevek::avahi::~avahi
~avahi()
Unpublish all ports and free all structures associated with the object.
shevek::avahi::browser::details::interface
AvahiIfIndex interface
Network interface.
Definition: avahi.hh:82
shevek::avahi::create_browser
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
shevek::avahi::browser::details::address
Glib::ustring address
Hostname or ip address of the server.
Definition: avahi.hh:86
shevek::avahi::browser::details
Details about a discovered service. These are internally created and may be examined by the applicati...
Definition: avahi.hh:79
shevek::avahi::publish
void publish(Glib::ustring const &protocol, int port)
Publish a service on a port.