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 <core/exceptions/system.h>
25 #include <core/threading/thread_collector.h>
26 #include <netcomm/fawkes/handler.h>
27 #include <netcomm/fawkes/network_manager.h>
28 #include <netcomm/fawkes/server_thread.h>
29 #include <netcomm/utils/resolver.h>
30 #ifdef HAVE_AVAHI
31 # include <netcomm/dns-sd/avahi_thread.h>
32 # include <netcomm/service_discovery/service.h>
33 #else
34 # include <netcomm/service_discovery/dummy_service_browser.h>
35 # include <netcomm/service_discovery/dummy_service_publisher.h>
36 #endif
37 
38 #include <cstdlib>
39 
40 namespace fawkes {
41 
42 /** @class FawkesNetworkManager <netcomm/fawkes/network_manager.h>
43  * Fawkes Network Manager.
44  * This class provides a manager for network connections used in Fawkes.
45  *
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor.
50  * @param thread_collector thread collector that threads shall be registered to
51  * @param enable_ipv4 true to listen on the IPv4 TCP port
52  * @param enable_ipv6 true to listen on the IPv6 TCP port
53  * @param listen_ipv4 IPv4 address to listen on for incoming connections,
54  * empty string or 0.0.0.0 to listen on any local address
55  * @param listen_ipv6 IPv6 address to listen on for incoming connections,
56  * empty string or :: to listen on any local address
57  * @param fawkes_port port to listen on for Fawkes network connections
58  * @param service_name Avahi service name for Fawkes network service
59  */
61  bool enable_ipv4,
62  bool enable_ipv6,
63  const std::string &listen_ipv4,
64  const std::string &listen_ipv6,
65  unsigned short int fawkes_port,
66  const char * service_name)
67 {
68  fawkes_port_ = fawkes_port;
69  thread_collector_ = thread_collector;
70  fawkes_network_thread_ = new FawkesNetworkServerThread(
71  enable_ipv4, enable_ipv6, listen_ipv4, listen_ipv6, fawkes_port_, thread_collector_);
72  thread_collector_->add(fawkes_network_thread_);
73 #ifdef HAVE_AVAHI
74  avahi_thread_ = new AvahiThread(enable_ipv4, enable_ipv6);
75  service_publisher_ = avahi_thread_;
76  service_browser_ = avahi_thread_;
77  thread_collector_->add(avahi_thread_);
78  nnresolver_ = new NetworkNameResolver(avahi_thread_);
79  NetworkService *fawkes_service =
80  new NetworkService(nnresolver_, service_name, "_fawkes._tcp", fawkes_port_);
81  avahi_thread_->publish_service(fawkes_service);
82  delete fawkes_service;
83 #else
84  service_publisher_ = new DummyServicePublisher();
85  service_browser_ = new DummyServiceBrowser();
86  nnresolver_ = new NetworkNameResolver();
87 #endif
88 }
89 
90 /** Destructor. */
92 {
93  thread_collector_->remove(fawkes_network_thread_);
94  delete fawkes_network_thread_;
95 #ifdef HAVE_AVAHI
96  thread_collector_->remove(avahi_thread_);
97  delete avahi_thread_;
98 #else
99  delete service_publisher_;
100  delete service_browser_;
101 #endif
102  delete nnresolver_;
103 }
104 
105 /** Get Fawkes network hub.
106  * @return Fawkes network hub
107  */
110 {
111  return fawkes_network_thread_;
112 }
113 
114 /** Get network name resolver.
115  * @return network name resolver
116  */
119 {
120  return nnresolver_;
121 }
122 
123 /** Get service publisher
124  * @return service publisher
125  */
128 {
129  return service_publisher_;
130 }
131 
132 /** Get service browser.
133  * @return service browser
134  */
137 {
138  return service_browser_;
139 }
140 
141 /** Get Fawkes TCP port.
142  * @return TCP port on which Fawkes is listening
143  */
144 unsigned short int
146 {
147  return fawkes_port_;
148 }
149 
150 } // 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.
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:53
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:44
ServiceBrowser * service_browser()
Get service browser.
Fawkes Network Thread.
Definition: server_thread.h:46