Fawkes API  Fawkes Development Version
fountain_thread.cpp
1 
2 /***************************************************************************
3  * fountain_thread.h - Fountain main thread
4  *
5  * Created: Fri Nov 16 11:22:30 2007
6  * Copyright 2005-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.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "fountain_thread.h"
24 
25 #include <core/exceptions/software.h>
26 #include <fvutils/net/fuse_server.h>
27 
28 #include <cstdio>
29 #include <string>
30 
31 using namespace fawkes;
32 using namespace firevision;
33 
34 /** @class FountainThread "fountain_thread.h"
35  * Fountain main thread.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor. */
40 FountainThread::FountainThread() : Thread("FountainThread", OPMODE_WAITFORWAKEUP)
41 {
42  fuse_server_ = NULL;
43  service_ = NULL;
44 }
45 
46 /** Destructor. */
48 {
49  if (fuse_server_) {
50  thread_collector->remove(fuse_server_);
51  delete fuse_server_;
52  fuse_server_ = NULL;
53  }
54  delete service_;
55  service_ = NULL;
56 }
57 
58 void
60 {
61  // Start FUSE server
62  unsigned int port = 0;
63  try {
64  port = config->get_uint("/firevision/fountain/tcp_port");
65  if (port > 0xFFFF) {
66  throw OutOfBoundsException("Network port out of bounds", port, 0, 0xFFFF);
67  }
68 
69  bool enable_ipv4 = true;
70  bool enable_ipv6 = true;
71  std::string listen_ipv4;
72  std::string listen_ipv6;
73 
74  try {
75  enable_ipv4 = config->get_bool("/network/ipv4/enable");
76  } catch (Exception &e) {
77  } // ignore, we stick with the default
78  try {
79  enable_ipv6 = config->get_bool("/network/ipv6/enable");
80  } catch (Exception &e) {
81  } // ignore, we stick with the default
82 
83  try {
84  listen_ipv4 = config->get_string("/network/ipv4/listen");
85  } catch (Exception &e) {
86  } // ignore, we stick with the default
87  try {
88  listen_ipv6 = config->get_string("/network/ipv6/listen");
89  } catch (Exception &e) {
90  } // ignore, we stick with the default
91 
92  fuse_server_ =
93  new FuseServer(enable_ipv4, enable_ipv6, listen_ipv4, listen_ipv6, port, thread_collector);
94  thread_collector->add(fuse_server_);
95  } catch (Exception &e) {
96  e.print_trace();
97  throw;
98  }
99 
100  // Announce service
101  std::string sname = "Fountain on ";
102  sname += nnresolver->short_hostname();
103  service_ = new NetworkService(sname.c_str(), "_fountain._tcp", port);
105 }
106 
107 void
109 {
111 
112  thread_collector->remove(fuse_server_);
113  delete fuse_server_;
114  fuse_server_ = NULL;
115  delete service_;
116  service_ = NULL;
117 }
118 
119 void
121 {
122  // do nothing, but implement to not exit
123  printf("Sucker Loop\n");
124 }
virtual void finalize()
Finalize the thread.
~FountainThread()
Destructor.
virtual void loop()
Code to execute in the thread.
virtual void init()
Initialize the thread.
FountainThread()
Constructor.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
NetworkNameResolver * nnresolver
Network name resolver to lookup IP addresses of hostnames and vice versa.
Definition: network.h:45
ServicePublisher * service_publisher
Service publisher to publish services on the network.
Definition: network.h:46
const char * short_hostname()
Get short hostname.
Definition: resolver.cpp:354
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:38
Index out of bounds.
Definition: software.h:86
virtual void unpublish_service(NetworkService *service)=0
Revoke service publication.
virtual void publish_service(NetworkService *service)=0
Publish service.
virtual void add(ThreadList &tl)=0
Add multiple threads.
virtual void remove(ThreadList &tl)=0
Remove multiple threads.
ThreadCollector * thread_collector
Thread collector.
Thread class encapsulation of pthreads.
Definition: thread.h:46
FireVision FUSE protocol server.
Definition: fuse_server.h:44
Fawkes library namespace.