MXS-2196: Replace listener_alloc with Listener::create

This commit is contained in:
Markus Mäkelä
2018-11-30 10:11:47 +02:00
parent 39f668ff3c
commit ae3763da92
2 changed files with 57 additions and 55 deletions

View File

@ -27,6 +27,9 @@
struct dcb; struct dcb;
class SERVICE; class SERVICE;
class Listener;
using SListener = std::shared_ptr<Listener>;
/** /**
* The Listener class is used to link a network port to a service. It defines the name of the * 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. * protocol module that should be loaded as well as the authenticator that is used.
@ -35,23 +38,30 @@ class Listener
{ {
public: public:
~Listener();
/** /**
* Creates a new listener that points to a service * Create a new listener
* *
* @param service Service where the listener points to * @param service Service where the listener points to
* @param name Name of the listener * @param name Name of the listener
* @param address The address where the listener listens * @param protocol Protocol module to use
* @param port The port on which the listener listens * @param address The address to listen with
* @param protocol The protocol module to use * @param port The port to listen on
* @param authenticator The authenticator module to use * @param authenticator Name of the authenticator to be used
* @param auth_opts Options for the authenticator * @param auth_options Authenticator options
* @param auth_instance The authenticator instance * @param ssl SSL configuration
* @param ssl The SSL configuration *
* @return New listener or nullptr on error
*/ */
Listener(SERVICE* service, const std::string& name, const std::string& address, uint16_t port, static SListener create(SERVICE* service,
const std::string& protocol, const std::string& authenticator, const std::string& name,
const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl); const std::string& protocol,
~Listener(); 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 * Start listening on the configured port
@ -180,9 +190,24 @@ private:
struct users* m_users; /**< The user data for this listener */ struct users* m_users; /**< The user data for this listener */
SERVICE* m_service; /**< The service which used by this listener */ SERVICE* m_service; /**< The service which used by this listener */
std::atomic<bool> m_active; /**< True if the port has not been deleted */ std::atomic<bool> m_active; /**< True if the port has not been deleted */
};
using SListener = std::shared_ptr<Listener>; /**
* 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 * @brief Serialize a listener to a file
@ -196,29 +221,6 @@ using SListener = std::shared_ptr<Listener>;
*/ */
bool listener_serialize(const SListener& listener); 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 * @brief Free a listener
* *

View File

@ -76,35 +76,35 @@ Listener::~Listener()
SSL_LISTENER_free(m_ssl); SSL_LISTENER_free(m_ssl);
} }
SListener listener_alloc(SERVICE* service, SListener Listener::create(SERVICE* service,
const char* name, const std::string& name,
const char* protocol, const std::string& protocol,
const char* address, const std::string& address,
unsigned short port, unsigned short port,
const char* authenticator, const std::string& authenticator,
const char* auth_options, const std::string& auth_options,
SSL_LISTENER* ssl) SSL_LISTENER* ssl)
{ {
if (!authenticator) const char* auth = !authenticator.empty() ? authenticator.c_str() :
{ get_default_authenticator(protocol.c_str());
if ((authenticator = get_default_authenticator(protocol)) == NULL)
if (!auth)
{ {
MXS_ERROR("No authenticator defined for listener '%s' and could not get " MXS_ERROR("No authenticator defined for listener '%s' and could not get "
"default authenticator for protocol '%s'.", name, protocol); "default authenticator for protocol '%s'.", name.c_str(), protocol.c_str());
return nullptr; return nullptr;
} }
}
void* auth_instance = NULL; 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'.", MXS_ERROR("Failed to initialize authenticator module '%s' for listener '%s'.",
authenticator, name); auth, name.c_str());
return nullptr; 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)); auth_options, auth_instance, ssl));
if (listener) if (listener)