1 /*
2    Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
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 General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef CONNECTION_ACCEPTOR_INCLUDED
26 #define CONNECTION_ACCEPTOR_INCLUDED
27 
28 #include "channel_info.h"               // Channel_info
29 #include "connection_handler_manager.h" // Connection_handler_manager
30 
31 /**
32   This class presents a generic interface to initialize and run
33   a connection event loop for different types of listeners and
34   a callback functor to call on the connection event from the
35   listener that listens for connection. Listener type should
36   be a class providing methods setup_listener, listen_for_
37   connection_event and close_listener. The Connection event
38   callback functor object would on receiving connection event
39   from the client to process the connection.
40 */
41 template <typename Listener> class Connection_acceptor
42 {
43   Listener *m_listener;
44 
45 public:
Connection_acceptor(Listener * listener)46   Connection_acceptor(Listener *listener)
47   : m_listener(listener)
48   { }
49 
~Connection_acceptor()50   ~Connection_acceptor()
51   {
52     delete m_listener;
53   }
54 
55   /**
56     Initialize a connection acceptor.
57 
58     @retval   return true if initialization failed, else false.
59   */
init_connection_acceptor()60   bool init_connection_acceptor()
61   {
62     return m_listener->setup_listener();
63   }
64 
65   /**
66     Connection acceptor loop to accept connections from clients.
67   */
connection_event_loop()68   void connection_event_loop()
69   {
70     Connection_handler_manager *mgr= Connection_handler_manager::get_instance();
71     while (!abort_loop)
72     {
73       Channel_info *channel_info= m_listener->listen_for_connection_event();
74       if (channel_info != NULL)
75         mgr->process_new_connection(channel_info);
76     }
77   }
78 
79   /**
80     Close the listener.
81   */
close_listener()82   void close_listener()
83   {
84     m_listener->close_listener();
85   }
86 
87 };
88 #endif // CONNECTION_ACCEPTOR_INCLUDED
89