Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AvahiDiscoveryAgent.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * AvahiDiscoveryAgent.h
17  * The Avahi implementation of DiscoveryAgentInterface.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef OLAD_AVAHIDISCOVERYAGENT_H_
22 #define OLAD_AVAHIDISCOVERYAGENT_H_
23 
24 #include <avahi-client/client.h>
25 #include <avahi-common/thread-watch.h>
26 #include <avahi-client/publish.h>
27 
28 #include <ola/base/Macro.h>
29 #include <ola/util/Backoff.h>
30 #include <map>
31 #include <string>
32 #include <vector>
33 
34 #include "olad/DiscoveryAgent.h"
35 
36 namespace ola {
37 
43  public:
46 
47  bool Init();
48 
49  void RegisterService(const std::string &service_name,
50  const std::string &type,
51  uint16_t port,
52  const RegisterOptions &options);
53 
57  void ClientStateChanged(AvahiClientState state, AvahiClient *client);
58 
62  void GroupStateChanged(const std::string &service_key,
63  AvahiEntryGroup *group,
64  AvahiEntryGroupState state);
65 
69  void ReconnectTimeout();
70 
71  private:
72  // The structure used to track services.
73  struct ServiceEntry : public RegisterOptions {
74  public:
75  const std::string service_name;
76  // This may differ from the service name if there was a collision.
77  std::string actual_service_name;
78  const uint16_t port;
79  AvahiEntryGroup *group;
80  AvahiEntryGroupState state;
81  struct EntryGroupParams *params;
82 
83  ServiceEntry(const std::string &service_name,
84  const std::string &type_spec,
85  uint16_t port,
86  const RegisterOptions &options);
87 
88  std::string key() const;
89  const std::string &type() const { return m_type; }
90  const std::vector<std::string> sub_types() const { return m_sub_types; }
91 
92  private:
93  std::string m_type_spec; // type[,subtype]
94  std::string m_type;
95  std::vector<std::string> m_sub_types;
96  };
97 
98  typedef std::map<std::string, ServiceEntry*> Services;
99 
100  AvahiThreadedPoll *m_threaded_poll;
101  AvahiClient *m_client;
102  AvahiTimeout *m_reconnect_timeout;
103  Services m_services;
104  BackoffGenerator m_backoff;
105 
106  bool InternalRegisterService(ServiceEntry *service);
107  void CreateNewClient();
108  void UpdateServices();
109  void DeregisterAllServices();
110  void SetUpReconnectTimeout();
111  bool RenameAndRegister(ServiceEntry *service);
112 
113  static std::string ClientStateToString(AvahiClientState state);
114  static std::string GroupStateToString(AvahiEntryGroupState state);
115 
117 };
118 } // namespace ola
119 #endif // OLAD_AVAHIDISCOVERYAGENT_H_