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 <string>
29 #include <cstdio>
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. */
41  : Thread("FountainThread", OPMODE_WAITFORWAKEUP)
42 {
43  __fuse_server = NULL;
44  __service = NULL;
45 }
46 
47 
48 /** Destructor. */
50 {
51  if ( __fuse_server ) {
52  thread_collector->remove(__fuse_server);
53  delete __fuse_server;
54  __fuse_server = NULL;
55  }
56  delete __service;
57  __service = NULL;
58 }
59 
60 
61 void
63 {
64  // Start FUSE server
65  unsigned int port = 0;
66  try {
67  port = config->get_uint("/firevision/fountain/tcp_port");
68  if ( port > 0xFFFF ) {
69  throw OutOfBoundsException("Network port out of bounds", port, 0, 0xFFFF);
70  }
71 
72  bool enable_ipv4 = true;
73  bool enable_ipv6 = true;
74  std::string listen_ipv4;
75  std::string listen_ipv6;
76 
77  try {
78  enable_ipv4 = config->get_bool("/network/ipv4/enable");
79  } catch (Exception &e) {} // ignore, we stick with the default
80  try {
81  enable_ipv6 = config->get_bool("/network/ipv6/enable");
82  } catch (Exception &e) {} // ignore, we stick with the default
83 
84  try {
85  listen_ipv4 = config->get_string("/network/ipv4/listen");
86  } catch (Exception &e) {} // ignore, we stick with the default
87  try {
88  listen_ipv6 = config->get_string("/network/ipv6/listen");
89  } catch (Exception &e) {} // ignore, we stick with the default
90 
91  __fuse_server = new FuseServer(enable_ipv4, enable_ipv6,
92  listen_ipv4, listen_ipv6,
93  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 
108 void
110 {
112 
113  thread_collector->remove(__fuse_server);
114  delete __fuse_server;
115  __fuse_server = NULL;
116  delete __service;
117  __service = NULL;
118 }
119 
120 
121 void
123 {
124  // do nothing, but implement to not exit
125  printf("Sucker Loop\n");
126 }
FireVision FUSE protocol server.
Definition: fuse_server.h:46
ServicePublisher * service_publisher
Service publisher to publish services on the network.
Definition: network.h:49
virtual void unpublish_service(NetworkService *service)=0
Revoke service publication.
virtual void remove(ThreadList &tl)=0
Remove multiple threads.
virtual void publish_service(NetworkService *service)=0
Publish service.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
ThreadCollector * thread_collector
Thread collector.
FountainThread()
Constructor.
const char * short_hostname()
Get short hostname.
Definition: resolver.cpp:371
Base class for exceptions in Fawkes.
Definition: exception.h:36
NetworkNameResolver * nnresolver
Network name resolver to lookup IP addresses of hostnames and vice versa.
Definition: network.h:48
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
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
virtual void finalize()
Finalize the thread.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
Index out of bounds.
Definition: software.h:88
~FountainThread()
Destructor.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.