Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
65 /** Constructor.
66  * @param handler Connection handler for newly accepted incoming connections.
67  * @param socket socket, must already be bound to the desired port. Socket::listen()
68  * will be called by the acceptor thread.
69  * @param thread_name name of the thread
70  * @exception SocketException as thrown by StreamSocket connstructor, bind and listen.
71  */
73  StreamSocket *socket,
74  const char *thread_name)
75  : Thread(thread_name)
76 {
77  __handler = handler;
78  __port = 0;
79  __socket = socket;
80 
82 
83  try {
84  __socket->listen();
85  } catch (SocketException &e) {
86  throw;
87  }
88 }
89 
90 
91 /** Destructor. */
93 {
94  delete __socket;
95 }
96 
97 
98 /** Thread loop.
99  * Waits on a socket for an incoming connection (blocking accept). If a new
100  * connection has been established it is reported to the handler.
101  */
102 void
104 {
105  StreamSocket *s = __socket->accept<StreamSocket>();
106  __handler->add_connection(s);
107 }
108 
109 } // end namespace fawkes
virtual void add_connection(StreamSocket *s)=0
Add an incoming connection.
virtual void loop()
Thread loop.
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:715
TCP stream socket over IP.
Definition: stream.h:31
virtual Socket * accept()
Accept connection.
Definition: socket.cpp:371
virtual void bind(const unsigned short int port)
Bind socket.
Definition: socket.cpp:298
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:357
Socket exception.
Definition: socket.h:58