Fawkes API  Fawkes Development Version
network_manager.cpp
1 
2 /***************************************************************************
3  * network_manager.cpp - Fawkes network manager
4  *
5  * Created: Wed Nov 16 00:05:18 2006
6  * Copyright 2006-2011 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 #include <netcomm/fawkes/network_manager.h>
25 #include <core/threading/thread_collector.h>
26 
27 #include <core/exceptions/system.h>
28 #include <netcomm/fawkes/server_thread.h>
29 #include <netcomm/fawkes/handler.h>
30 #include <netcomm/utils/resolver.h>
31 #ifdef HAVE_AVAHI
32 #include <netcomm/dns-sd/avahi_thread.h>
33 #include <netcomm/service_discovery/service.h>
34 #else
35 #include <netcomm/service_discovery/dummy_service_publisher.h>
36 #include <netcomm/service_discovery/dummy_service_browser.h>
37 #endif
38 
39 #include <cstdlib>
40 
41 namespace fawkes {
42 #if 0 /* just to make Emacs auto-indent happy */
43 }
44 #endif
45 
46 /** @class FawkesNetworkManager <netcomm/fawkes/network_manager.h>
47  * Fawkes Network Manager.
48  * This class provides a manager for network connections used in Fawkes.
49  *
50  * @author Tim Niemueller
51  */
52 
53 /** Constructor.
54  * @param thread_collector thread collector that threads shall be registered to
55  * @param enable_ipv4 true to listen on the IPv4 TCP port
56  * @param enable_ipv6 true to listen on the IPv6 TCP port
57  * @param listen_ipv4 IPv4 address to listen on for incoming connections,
58  * empty string or 0.0.0.0 to listen on any local address
59  * @param listen_ipv6 IPv6 address to listen on for incoming connections,
60  * empty string or :: to listen on any local address
61  * @param fawkes_port port to listen on for Fawkes network connections
62  * @param service_name Avahi service name for Fawkes network service
63  */
65  bool enable_ipv4, bool enable_ipv6,
66  const std::string &listen_ipv4, const std::string &listen_ipv6,
67  unsigned short int fawkes_port,
68  const char *service_name)
69 {
70  __fawkes_port = fawkes_port;
71  __thread_collector = thread_collector;
72  __fawkes_network_thread = new FawkesNetworkServerThread(enable_ipv4, enable_ipv6,
73  listen_ipv4, listen_ipv6,
74  __fawkes_port,
75  __thread_collector);
76  __thread_collector->add(__fawkes_network_thread);
77 #ifdef HAVE_AVAHI
78  __avahi_thread = new AvahiThread(enable_ipv4, enable_ipv6);
79  __service_publisher = __avahi_thread;
80  __service_browser = __avahi_thread;
81  __thread_collector->add(__avahi_thread);
82  __nnresolver = new NetworkNameResolver(__avahi_thread);
83  NetworkService *fawkes_service = new NetworkService(__nnresolver, service_name,
84  "_fawkes._tcp",
85  __fawkes_port);
86  __avahi_thread->publish_service(fawkes_service);
87  delete fawkes_service;
88 #else
89  __service_publisher = new DummyServicePublisher();
90  __service_browser = new DummyServiceBrowser();
91  __nnresolver = new NetworkNameResolver();
92 #endif
93 }
94 
95 
96 /** Destructor. */
98 {
99  __thread_collector->remove(__fawkes_network_thread);
100  delete __fawkes_network_thread;
101 #ifdef HAVE_AVAHI
102  __thread_collector->remove(__avahi_thread);
103  delete __avahi_thread;
104 #else
105  delete __service_publisher;
106  delete __service_browser;
107 #endif
108  delete __nnresolver;
109 }
110 
111 
112 /** Get Fawkes network hub.
113  * @return Fawkes network hub
114  */
117 {
118  return __fawkes_network_thread;
119 }
120 
121 
122 /** Get network name resolver.
123  * @return network name resolver
124  */
127 {
128  return __nnresolver;
129 }
130 
131 
132 /** Get service publisher
133  * @return service publisher
134  */
137 {
138  return __service_publisher;
139 }
140 
141 
142 /** Get service browser.
143  * @return service browser
144  */
147 {
148  return __service_browser;
149 }
150 
151 /** Get Fawkes TCP port.
152  * @return TCP port on which Fawkes is listening
153  */
154 unsigned short int
156 {
157  return __fawkes_port;
158 }
159 
160 
161 } // end namespace fawkes
Service browser.
Service publisher interface.
NetworkNameResolver * nnresolver()
Get network name resolver.
unsigned short int fawkes_port() const
Get Fawkes TCP port.
virtual void remove(ThreadList &tl)=0
Remove multiple threads.
Fawkes library namespace.
Thread collector.
FawkesNetworkHub * hub()
Get Fawkes network hub.
Dummy service publisher interface.
void publish_service(NetworkService *service)
Publish service.
Fawkes Network Hub.
Definition: hub.h:33
ServicePublisher * service_publisher()
Get service publisher.
FawkesNetworkManager(ThreadCollector *thread_collector, bool enable_ipv4, bool enable_ipv6, const std::string &listen_ipv4, const std::string &listen_ipv6, unsigned short int fawkes_port, const char *service_name)
Constructor.
Avahi main thread.
Definition: avahi_thread.h:54
virtual void add(ThreadList &tl)=0
Add multiple threads.
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:37
Network name and address resolver.
Definition: resolver.h:48
ServiceBrowser * service_browser()
Get service browser.
Fawkes Network Thread.
Definition: server_thread.h:46