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  __fuse_server = new FuseServer(port, thread_collector);
72  thread_collector->add(__fuse_server);
73  } catch (Exception &e) {
74  e.print_trace();
75  throw;
76  }
77 
78  // Announce service
79  std::string sname = "Fountain on ";
80  sname += nnresolver->short_hostname();
81  __service = new NetworkService(sname.c_str(), "_fountain._tcp", port);
83 }
84 
85 
86 void
88 {
90 
91  thread_collector->remove(__fuse_server);
92  delete __fuse_server;
93  __fuse_server = NULL;
94  delete __service;
95  __service = NULL;
96 }
97 
98 
99 void
101 {
102  // do nothing, but implement to not exit
103  printf("Sucker Loop\n");
104 }
FireVision FUSE protocol server.
Definition: fuse_server.h:43
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 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