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/utils/acceptor_thread.h>
25 #include <netcomm/utils/incoming_connection_handler.h>
26 #include <netcomm/socket/stream.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 
99 /** Constructor.
100  * @param handler Connection handler for newly accepted incoming connections.
101  * @param socket socket, must already be bound to the desired port. Socket::listen()
102  * will be called by the acceptor thread.
103  * @param thread_name name of the thread
104  * @exception SocketException as thrown by StreamSocket connstructor, bind and listen.
105  */
107  StreamSocket *socket,
108  const char *thread_name)
109  : Thread(thread_name)
110 {
111  __handler = handler;
112  __port = 0;
113  __socket = socket;
114 
115  set_prepfin_conc_loop(true);
116 
117  try {
118  __socket->listen();
119  } catch (SocketException &e) {
120  throw;
121  }
122 }
123 
124 
125 /** Destructor. */
127 {
128  delete __socket;
129 }
130 
131 
132 /** Thread loop.
133  * Waits on a socket for an incoming connection (blocking accept). If a new
134  * connection has been established it is reported to the handler.
135  */
136 void
138 {
139  StreamSocket *s = __socket->accept<StreamSocket>();
140  __handler->add_connection(s);
141 }
142 
143 } // end namespace fawkes
virtual void add_connection(StreamSocket *s)=0
Add an incoming connection.
Fawkes library namespace.
virtual void loop()
Thread loop.
AddrType
Address type specification.
Definition: socket.h:78
Thread class encapsulation of pthreads.
Definition: thread.h:42
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:727
TCP stream socket over IP.
Definition: stream.h:31
virtual Socket * accept()
Accept connection.
Definition: socket.cpp:565
virtual void bind(const unsigned short int port)
Bind socket.
Definition: socket.cpp:419
Interface for handling incoming connections.
NetworkAcceptorThread(NetworkIncomingConnectionHandler *handler, unsigned short int port, const char *thread_name="NetworkAcceptorThread")
Constructor.
virtual void listen(int backlog=1)
Listen on socket.
Definition: socket.cpp:547
Socket exception.
Definition: socket.h:58