Fawkes API  Fawkes Development Version
resolver_thread.h
1 
2 /***************************************************************************
3  * resolver_thread.h - Fawkes network name resolver thread
4  *
5  * Created: Fri May 11 22:10:03 2007
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __NETCOMM_UTILS_RESOLVER_THREAD_H_
25 #define __NETCOMM_UTILS_RESOLVER_THREAD_H_
26 
27 #include <core/threading/thread.h>
28 #include <core/utils/lock_hashset.h>
29 #include <core/utils/lock_hashmap.h>
30 #include <utils/misc/string_compare.h>
31 #ifdef HAVE_AVAHI
32 #include <netcomm/dns-sd/avahi_resolver_handler.h>
33 #endif
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <stdint.h>
37 #include <cstddef>
38 #include <string>
39 #include <utility>
40 #include <map>
41 #include <list>
42 
43 namespace fawkes {
44 
45 class AvahiThread;
46 class NetworkNameResolver;
47 class Mutex;
48 
49 #ifdef HAVE_AVAHI
50 class NetworkNameResolverThread : public Thread, public AvahiResolverHandler
51 #else
53 #endif
54 {
55  public:
57  AvahiThread *avahi_thread = NULL);
59 
60  void resolve_name(const std::string &name);
61  void resolve_address(struct sockaddr *addr, socklen_t addrlen);
62 
63  bool resolve_name_immediately(const std::string &name,
64  struct sockaddr **addr, socklen_t *addr_len);
65  bool resolve_address_immediately(struct sockaddr *addr, std::string &name, bool &namefound);
66 
67  virtual void resolved_name(char *name, struct sockaddr *addr, socklen_t addrlen);
68  virtual void resolved_address(struct sockaddr *addr, socklen_t addrlen, char *name);
69  virtual void name_resolution_failed(char *name);
70  virtual void address_resolution_failed(struct sockaddr *addr, socklen_t addrlen);
71 
72  virtual void loop();
73 
74  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
75  protected: virtual void run() { Thread::run(); }
76 
77  private:
78  NetworkNameResolver *__resolver;
79 #ifdef HAVE_AVAHI
80  AvahiThread *__avahi_thread;
81 #endif
82 
83  Mutex *__namesq_mutex;
84  unsigned int __namesq_active;
86  NamesQMap __namesqs[2];
87  NamesQMap *__namesq;
88  NamesQMap *__namesq_proc;
89 
90 
91  Mutex *__addrq_mutex;
92  unsigned int __addrq_active;
93  typedef std::list<struct sockaddr *> AddrQList;
94  AddrQList __addrqs[2];
95  AddrQList *__addrq;
96  AddrQList *__addrq_proc;
97 };
98 
99 } // end namespace fawkes
100 
101 #endif
NetworkNameResolverThread(NetworkNameResolver *resolver, AvahiThread *avahi_thread=NULL)
Constructor.
void resolve_address(struct sockaddr *addr, socklen_t addrlen)
Enqueue address for resolution.
virtual void name_resolution_failed(char *name)
Name resolution failed.
Fawkes library namespace.
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
Thread class encapsulation of pthreads.
Definition: thread.h:42
Worker thread for NetworkNameResolver.
virtual void resolved_name(char *name, struct sockaddr *addr, socklen_t addrlen)
Name has been successfully resolved.
void resolve_name(const std::string &name)
Enqueue name for resolution.
const char * name() const
Get name of thread.
Definition: thread.h:95
Avahi main thread.
Definition: avahi_thread.h:54
Network name and address resolver.
Definition: resolver.h:48
virtual void resolved_address(struct sockaddr *addr, socklen_t addrlen, char *name)
Address has been successfully resolved.
virtual void address_resolution_failed(struct sockaddr *addr, socklen_t addrlen)
Address resolution failed.
Mutex mutual exclusion lock.
Definition: mutex.h:32
virtual void run()
Stub to see name in backtrace for easier debugging.
bool resolve_address_immediately(struct sockaddr *addr, std::string &name, bool &namefound)
Immediately resolve address.
bool resolve_name_immediately(const std::string &name, struct sockaddr **addr, socklen_t *addr_len)
Immediately resolve a name.
virtual void loop()
Thread loop.