Fawkes API  Fawkes Development Version
acceptor_thread.cpp
1 
2 /***************************************************************************
3  * acceptor_thread.cpp - Thread accepting Fawkes network connections
4  *
5  * Created: Fri Nov 17 14:09:38 2006
6  * Copyright 2006-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. 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/socket/stream.h>
25 #include <netcomm/utils/acceptor_thread.h>
26 #include <netcomm/utils/incoming_connection_handler.h>
27 
28 namespace fawkes {
29 
30 /** @class NetworkAcceptorThread <netcomm/utils/acceptor_thread.h>
31  * Network Acceptor Thread.
32  * Opens and maintains a server socket and waits for incoming connections. If
33  * that happens NetworkConnectionHandler::add_connection() is called.
34  *
35  * @ingroup NetComm
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param handler Connection handler for newly accepted incoming connections.
41  * @param port port to listen on for incoming connections
42  * @param thread_name name of the thread
43  * @exception SocketException as thrown by StreamSocket connstructor, bind and listen.
44  */
46  unsigned short int port,
47  const char * thread_name)
48 : Thread(thread_name)
49 {
50  handler_ = handler;
51  port_ = port;
52 
54 
55  try {
56  socket_ = new StreamSocket();
57  socket_->bind(port_);
58  socket_->listen();
59  } catch (SocketException &e) {
60  throw;
61  }
62 }
63 
64 /** Constructor.
65  * @param handler Connection handler for newly accepted incoming connections.
66  * @param addr_type Specify IPv4 or IPv6
67  * @param listen_addr IP address to listen on (format depends on addr_type), nullptr to listen
68  * on any local (address type specific) address, e.g., :: for IPv6.
69  * @param port port to listen on for incoming connections
70  * @param thread_name name of the thread
71  * @exception SocketException as thrown by StreamSocket connstructor, bind and listen.
72  */
74  Socket::AddrType addr_type,
75  const std::string & listen_addr,
76  unsigned short int port,
77  const char * thread_name)
78 : Thread(thread_name)
79 {
80  handler_ = handler;
81  port_ = port;
82 
84 
85  try {
86  socket_ = new StreamSocket(addr_type);
87  if (listen_addr.empty()) {
88  socket_->bind(port_);
89  } else {
90  socket_->bind(port_, listen_addr.c_str());
91  }
92  socket_->listen();
93  } catch (SocketException &e) {
94  throw;
95  }
96 }
97 
98 /** Constructor.
99  * @param handler Connection handler for newly accepted incoming connections.
100  * @param socket socket, must already be bound to the desired port. Socket::listen()
101  * will be called by the acceptor thread.
102  * @param thread_name name of the thread
103  * @exception SocketException as thrown by StreamSocket connstructor, bind and listen.
104  */
106  StreamSocket * socket,
107  const char * thread_name)
108 : Thread(thread_name)
109 {
110  handler_ = handler;
111  port_ = 0;
112  socket_ = socket;
113 
114  set_prepfin_conc_loop(true);
115 
116  try {
117  socket_->listen();
118  } catch (SocketException &e) {
119  throw;
120  }
121 }
122 
123 /** Destructor. */
125 {
126  delete socket_;
127 }
128 
129 /** Thread loop.
130  * Waits on a socket for an incoming connection (blocking accept). If a new
131  * connection has been established it is reported to the handler.
132  */
133 void
135 {
136  StreamSocket *s = socket_->accept<StreamSocket>();
137  handler_->add_connection(s);
138 }
139 
140 } // end namespace fawkes
virtual void loop()
Thread loop.
NetworkAcceptorThread(NetworkIncomingConnectionHandler *handler, unsigned short int port, const char *thread_name="NetworkAcceptorThread")
Constructor.
Interface for handling incoming connections.
virtual void add_connection(StreamSocket *s)=0
Add an incoming connection.
Socket exception.
Definition: socket.h:57
virtual void bind(const unsigned short int port)
Bind socket.
Definition: socket.cpp:465
AddrType
Address type specification.
Definition: socket.h:75
virtual Socket * accept()
Accept connection.
Definition: socket.cpp:599
virtual void listen(int backlog=1)
Listen on socket.
Definition: socket.cpp:582
TCP stream socket over IP.
Definition: stream.h:32
Thread class encapsulation of pthreads.
Definition: thread.h:46
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:716
Fawkes library namespace.