00001 00002 /*************************************************************************** 00003 * acceptor_thread.cpp - Thread accepting Fawkes network connections 00004 * 00005 * Created: Fri Nov 17 14:09:38 2006 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <netcomm/utils/acceptor_thread.h> 00025 #include <netcomm/utils/incoming_connection_handler.h> 00026 #include <netcomm/socket/stream.h> 00027 00028 namespace fawkes { 00029 00030 /** @class NetworkAcceptorThread <netcomm/utils/acceptor_thread.h> 00031 * Network Acceptor Thread. 00032 * Opens and maintains a server socket and waits for incoming connections. If 00033 * that happens NetworkConnectionHandler::add_connection() is called. 00034 * 00035 * @ingroup NetComm 00036 * @author Tim Niemueller 00037 */ 00038 00039 /** Constructor. 00040 * @param handler Connection handler for newly accepted incoming connections. 00041 * @param port port to listen on for incoming connections 00042 * @param thread_name name of the thread 00043 * @exception SocketException as thrown by StreamSocket connstructor, bind and listen. 00044 */ 00045 NetworkAcceptorThread::NetworkAcceptorThread(NetworkIncomingConnectionHandler *handler, 00046 unsigned short int port, 00047 const char *thread_name) 00048 : Thread(thread_name) 00049 { 00050 __handler = handler; 00051 __port = port; 00052 00053 set_prepfin_conc_loop(true); 00054 00055 try { 00056 __socket = new StreamSocket(); 00057 __socket->bind(__port); 00058 __socket->listen(); 00059 } catch (SocketException &e) { 00060 throw; 00061 } 00062 } 00063 00064 00065 /** Constructor. 00066 * @param handler Connection handler for newly accepted incoming connections. 00067 * @param socket socket, must already be bound to the desired port. Socket::listen() 00068 * will be called by the acceptor thread. 00069 * @param thread_name name of the thread 00070 * @exception SocketException as thrown by StreamSocket connstructor, bind and listen. 00071 */ 00072 NetworkAcceptorThread::NetworkAcceptorThread(NetworkIncomingConnectionHandler *handler, 00073 StreamSocket *socket, 00074 const char *thread_name) 00075 : Thread(thread_name) 00076 { 00077 __handler = handler; 00078 __port = 0; 00079 __socket = socket; 00080 00081 set_prepfin_conc_loop(true); 00082 00083 try { 00084 __socket->listen(); 00085 } catch (SocketException &e) { 00086 throw; 00087 } 00088 } 00089 00090 00091 /** Destructor. */ 00092 NetworkAcceptorThread::~NetworkAcceptorThread() 00093 { 00094 delete __socket; 00095 } 00096 00097 00098 /** Thread loop. 00099 * Waits on a socket for an incoming connection (blocking accept). If a new 00100 * connection has been established it is reported to the handler. 00101 */ 00102 void 00103 NetworkAcceptorThread::loop() 00104 { 00105 StreamSocket *s = __socket->accept<StreamSocket>(); 00106 __handler->add_connection(s); 00107 } 00108 00109 } // end namespace fawkes