Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
MDNS.hxx
Go to the documentation of this file.
1
35#ifndef _OS_MDNS_HXX_
36#define _OS_MDNS_HXX_
37
38#if defined (__linux__)
39#include <netdb.h>
40#include <stdio.h>
41#include <semaphore.h>
42#include <avahi-client/client.h>
43#include <avahi-client/lookup.h>
44#include <avahi-client/publish.h>
45#include <avahi-common/error.h>
46#include <avahi-common/malloc.h>
47#include <avahi-common/simple-watch.h>
48
49#include "os/OS.hxx"
50
51#elif defined(__FreeRTOS__)
52#include <netdb.h>
53#endif
54
55struct addrinfo;
56
57#include "utils/macros.h"
58
59
62class MDNS
63#if defined (__linux__)
64 : public OSThread
65#endif
66{
67public:
70 MDNS()
71#if defined (__linux__)
72 :
73 OSThread()
74 , sem_()
75 , group_(nullptr)
76 , simplePoll_(nullptr)
77 , client_(nullptr)
78#endif
79 {
80#if defined (__linux__)
81 start("mDNS Server", 0, 2048);
82 printf("mDNS client start\n");
83 sem_.wait();
84#endif
85 }
86
89 ~MDNS()
90 {
91#if defined(__linux__)
92 shutdown();
93#endif
94 }
95
96#if defined(__linux__)
99 void shutdown();
100#endif
101
107 void publish(const char *name, const char *service, uint16_t port);
108
113 void unpublish(const char *name, const char *service);
114
117 void commit()
118 {
119#if defined (__linux__)
120 avahi_entry_group_commit(group_);
121#endif
122 }
123
134 static int lookup(const char *service, struct addrinfo *hints,
135 struct addrinfo **addr);
136
140 static void scan(const char *service);
141
142private:
143#if defined (__linux__)
144 struct LookupUserdata
145 {
146 LookupUserdata(const struct addrinfo *h)
147 : count(0)
148 , done(0)
149 , c(nullptr)
150 , sp(nullptr)
151 , hints(h)
152 , addr(nullptr)
153 {
154 }
155
156 unsigned count : 31;
157 unsigned done : 1;
158 AvahiClient *c;
159 AvahiSimplePoll *sp;
160 const struct addrinfo *hints;
161 struct addrinfo *addr;
162 };
163
178 static void resolve_callback(AvahiServiceResolver *r,
179 AvahiIfIndex interface, AvahiProtocol protocol,
180 AvahiResolverEvent event, const char *name,
181 const char *type, const char *domain,
182 const char *host_name,
183 const AvahiAddress *address, uint16_t port,
184 AvahiStringList *txt,
185 AvahiLookupResultFlags flags, void* userdata);
186
198 static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface,
199 AvahiProtocol protocol, AvahiBrowserEvent event,
200 const char *name, const char *type,
201 const char *domain,
202 AvahiLookupResultFlags flags, void* userdata);
203
209 static void entry_group_callback(AvahiEntryGroup *g,
210 AvahiEntryGroupState state, void *userdata);
211
217 static void client_callback(AvahiClient *c, AvahiClientState state,
218 void * userdata);
219
223 void *entry() override;
224
226 OSSem sem_;
227
229 AvahiEntryGroup *group_;
230
232 AvahiSimplePoll *simplePoll_;
233
235 AvahiClient *client_;
236#endif
237
239};
240
241#endif /* _OS_MDNS_HXX_ */
MDNS abstraction object.
Definition MDNS.hxx:66
static void scan(const char *service)
Start continuous scan for mDNS service name.
Definition MDNS.cxx:243
static int lookup(const char *service, struct addrinfo *hints, struct addrinfo **addr)
Lookup an mDNS name.
Definition MDNS.cxx:149
void commit()
Commit the mDNS publisher.
Definition MDNS.hxx:117
void unpublish(const char *name, const char *service)
Constructor.
Definition MDNS.cxx:137
This class provides a counting semaphore API.
Definition OS.hxx:243
This class provides a threading API.
Definition OS.hxx:46
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
Structure to contain information about address of a service provider.
Definition netdb.h:48