From ae3763da9211cc0aa56c55ac947307d652b94d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Fri, 30 Nov 2018 10:11:47 +0200 Subject: [PATCH] MXS-2196: Replace listener_alloc with Listener::create --- include/maxscale/listener.hh | 76 ++++++++++++++++++------------------ server/core/listener.cc | 36 ++++++++--------- 2 files changed, 57 insertions(+), 55 deletions(-) diff --git a/include/maxscale/listener.hh b/include/maxscale/listener.hh index a509e845f..40aa9162e 100644 --- a/include/maxscale/listener.hh +++ b/include/maxscale/listener.hh @@ -27,6 +27,9 @@ struct dcb; class SERVICE; +class Listener; +using SListener = std::shared_ptr; + /** * The Listener class is used to link a network port to a service. It defines the name of the * protocol module that should be loaded as well as the authenticator that is used. @@ -35,23 +38,30 @@ class Listener { public: + ~Listener(); + /** - * Creates a new listener that points to a service + * Create a new listener * * @param service Service where the listener points to * @param name Name of the listener - * @param address The address where the listener listens - * @param port The port on which the listener listens - * @param protocol The protocol module to use - * @param authenticator The authenticator module to use - * @param auth_opts Options for the authenticator - * @param auth_instance The authenticator instance - * @param ssl The SSL configuration + * @param protocol Protocol module to use + * @param address The address to listen with + * @param port The port to listen on + * @param authenticator Name of the authenticator to be used + * @param auth_options Authenticator options + * @param ssl SSL configuration + * + * @return New listener or nullptr on error */ - Listener(SERVICE* service, const std::string& name, const std::string& address, uint16_t port, - const std::string& protocol, const std::string& authenticator, - const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl); - ~Listener(); + static SListener create(SERVICE* service, + const std::string& name, + const std::string& protocol, + const std::string& address, + unsigned short port, + const std::string& authenticator, + const std::string& auth_options, + SSL_LISTENER* ssl); /** * Start listening on the configured port @@ -180,9 +190,24 @@ private: struct users* m_users; /**< The user data for this listener */ SERVICE* m_service; /**< The service which used by this listener */ std::atomic m_active; /**< True if the port has not been deleted */ -}; -using SListener = std::shared_ptr; + /** + * Creates a new listener that points to a service + * + * @param service Service where the listener points to + * @param name Name of the listener + * @param address The address where the listener listens + * @param port The port on which the listener listens + * @param protocol The protocol module to use + * @param authenticator The authenticator module to use + * @param auth_opts Options for the authenticator + * @param auth_instance The authenticator instance + * @param ssl The SSL configuration + */ + Listener(SERVICE* service, const std::string& name, const std::string& address, uint16_t port, + const std::string& protocol, const std::string& authenticator, + const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl); +}; /** * @brief Serialize a listener to a file @@ -196,29 +221,6 @@ using SListener = std::shared_ptr; */ bool listener_serialize(const SListener& listener); -/** - * Create a new listener - * - * @param service Service where the listener points to - * @param name Name of the listener - * @param protocol Protocol module to use - * @param address The address to listen with - * @param port The port to listen on - * @param authenticator Name of the authenticator to be used - * @param auth_options Authenticator options - * @param ssl SSL configuration - * - * @return New listener or nullptr on error - */ -SListener listener_alloc(SERVICE* service, - const char* name, - const char* protocol, - const char* address, - unsigned short port, - const char* authenticator, - const char* auth_options, - SSL_LISTENER* ssl); - /** * @brief Free a listener * diff --git a/server/core/listener.cc b/server/core/listener.cc index 5c8a31940..440237718 100644 --- a/server/core/listener.cc +++ b/server/core/listener.cc @@ -76,35 +76,35 @@ Listener::~Listener() SSL_LISTENER_free(m_ssl); } -SListener listener_alloc(SERVICE* service, - const char* name, - const char* protocol, - const char* address, - unsigned short port, - const char* authenticator, - const char* auth_options, - SSL_LISTENER* ssl) +SListener Listener::create(SERVICE* service, + const std::string& name, + const std::string& protocol, + const std::string& address, + unsigned short port, + const std::string& authenticator, + const std::string& auth_options, + SSL_LISTENER* ssl) { - if (!authenticator) + const char* auth = !authenticator.empty() ? authenticator.c_str() : + get_default_authenticator(protocol.c_str()); + + if (!auth) { - if ((authenticator = get_default_authenticator(protocol)) == NULL) - { - MXS_ERROR("No authenticator defined for listener '%s' and could not get " - "default authenticator for protocol '%s'.", name, protocol); - return nullptr; - } + MXS_ERROR("No authenticator defined for listener '%s' and could not get " + "default authenticator for protocol '%s'.", name.c_str(), protocol.c_str()); + return nullptr; } void* auth_instance = NULL; - if (!authenticator_init(&auth_instance, authenticator, auth_options)) + if (!authenticator_init(&auth_instance, auth, auth_options.c_str())) { MXS_ERROR("Failed to initialize authenticator module '%s' for listener '%s'.", - authenticator, name); + auth, name.c_str()); return nullptr; } - SListener listener(new(std::nothrow) Listener(service, name, address, port, protocol, authenticator, + SListener listener(new(std::nothrow) Listener(service, name, address, port, protocol, auth, auth_options, auth_instance, ssl)); if (listener)