From 8aeb49cffa4a804741c036a14b4a44881bcc410e Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 14 Nov 2017 14:54:09 +0200 Subject: [PATCH] MXS-1461 Add FilterModule class --- .../filter/dbfwfilter/test/filtermodule.cc | 89 ++++++++ .../dbfwfilter/test/maxscale/filtermodule.hh | 198 ++++++++++++++++++ 2 files changed, 287 insertions(+) create mode 100644 server/modules/filter/dbfwfilter/test/filtermodule.cc create mode 100644 server/modules/filter/dbfwfilter/test/maxscale/filtermodule.hh diff --git a/server/modules/filter/dbfwfilter/test/filtermodule.cc b/server/modules/filter/dbfwfilter/test/filtermodule.cc new file mode 100644 index 000000000..5edf85794 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/filtermodule.cc @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include +#include "maxscale/filtermodule.hh" +#include "../../../../core/maxscale/modules.h" + +using std::auto_ptr; + +namespace maxscale +{ + +// +// FilterModule +// + +const char* FilterModule::zName = MODULE_FILTER; + +auto_ptr FilterModule::createInstance(const char* zName, + char** pzOptions, + MXS_CONFIG_PARAMETER* pParameters) +{ + auto_ptr sInstance; + + MXS_FILTER* pFilter = m_pApi->createInstance(zName, pzOptions, pParameters); + + if (pFilter) + { + sInstance.reset(new Instance(this, pFilter)); + } + + return sInstance; +} + +// +// FilterModule::Instance +// + +FilterModule::Instance::Instance(FilterModule* pModule, MXS_FILTER* pInstance) + : m_module(*pModule) + , m_pInstance(pInstance) +{ +} + +FilterModule::Instance::~Instance() +{ + m_module.destroyInstance(m_pInstance); +} + +auto_ptr FilterModule::Instance::newSession(MXS_SESSION* pSession) +{ + auto_ptr sFilter_session; + + MXS_FILTER_SESSION* pFilter_session = m_module.newSession(m_pInstance, pSession); + + if (pFilter_session) + { + sFilter_session.reset(new Session(this, pFilter_session)); + } + + return sFilter_session; +} + +// +// FilterModule::Session +// + +FilterModule::Session::Session(Instance* pInstance, MXS_FILTER_SESSION* pFilter_session) + : m_instance(*pInstance) + , m_pFilter_session(pFilter_session) +{ +} + +FilterModule::Session::~Session() +{ + m_instance.freeSession(m_pFilter_session); +} + +} diff --git a/server/modules/filter/dbfwfilter/test/maxscale/filtermodule.hh b/server/modules/filter/dbfwfilter/test/maxscale/filtermodule.hh new file mode 100644 index 000000000..4175ed84a --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/maxscale/filtermodule.hh @@ -0,0 +1,198 @@ +#pragma once +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include +#include +#include +#include "module.hh" + +namespace maxscale +{ + +/** + * An instance of FilterModule represents a filter module. + */ +class FilterModule : public Module +{ + FilterModule(const FilterModule&); + FilterModule& operator = (const FilterModule&); + +public: + static const char* zName; /*< The name describing the module type. */ + typedef MXS_FILTER_OBJECT type_t; /*< The type of the module object. */ + + class Session; + class Instance + { + Instance(const Instance&); + Instance& operator = (const Instance&); + public: + ~Instance(); + + /** + * Create a new filter session. + * + * @param pSession The session to which the filter session belongs. + * + * @return A new filter session or NULL if the creation failed. + */ + std::auto_ptr newSession(MXS_SESSION* pSession); + + private: + friend class FilterModule; + + Instance(FilterModule* pModule, MXS_FILTER* pInstance); + + private: + friend class Session; + + void freeSession(MXS_FILTER_SESSION* pFilter_session) + { + m_module.freeSession(m_pInstance, pFilter_session); + } + + int routeQuery(MXS_FILTER_SESSION* pFilter_session, GWBUF* pStatement) + { + return m_module.routeQuery(m_pInstance, pFilter_session, pStatement); + } + + int clientReply(MXS_FILTER_SESSION* pFilter_session, GWBUF* pStatement) + { + return m_module.clientReply(m_pInstance, pFilter_session, pStatement); + } + + void setDownstream(MXS_FILTER_SESSION* pFilter_session, MXS_DOWNSTREAM* pDownstream) + { + m_module.setDownstream(m_pInstance, pFilter_session, pDownstream); + } + + void setUpstream(MXS_FILTER_SESSION* pFilter_session, MXS_UPSTREAM* pUpstream) + { + m_module.setUpstream(m_pInstance, pFilter_session, pUpstream); + } + + private: + FilterModule& m_module; + MXS_FILTER* m_pInstance; + }; + + class Session + { + Session(const Session&); + Session& operator = (const Session&); + + public: + ~Session(); + + /** + * The following member functions correspond to the MaxScale filter API. + */ + int routeQuery(GWBUF* pStatement) + { + return m_instance.routeQuery(m_pFilter_session, pStatement); + } + + int clientReply(GWBUF* pBuffer) + { + return m_instance.clientReply(m_pFilter_session, pBuffer); + } + + void setDownstream(MXS_DOWNSTREAM* pDownstream) + { + m_instance.setDownstream(m_pFilter_session, pDownstream); + } + + void setUpstream(MXS_UPSTREAM* pUpstream) + { + m_instance.setUpstream(m_pFilter_session, pUpstream); + } + + private: + friend class Instance; + + Session(Instance* pInstance, MXS_FILTER_SESSION* pFilter_session); + + private: + Instance& m_instance; + MXS_FILTER_SESSION* m_pFilter_session; + }; + + /** + * Create a new instance. + * + * @param zName The name of the instance (config file section name), + * @param pzOptions Optional options. + * @param pParameters Configuration parameters. + * + * @return A new instance or NULL if creation failed. + */ + std::auto_ptr createInstance(const char* zName, + char** pzOptions, + MXS_CONFIG_PARAMETER* pParameters); + +private: + friend class Instance; + + void destroyInstance(MXS_FILTER* pInstance) + { + m_pApi->destroyInstance(pInstance); + } + + MXS_FILTER_SESSION* newSession(MXS_FILTER* pInstance, MXS_SESSION* pSession) + { + return m_pApi->newSession(pInstance, pSession); + } + + void freeSession(MXS_FILTER* pInstance, MXS_FILTER_SESSION* pFilter_session) + { + m_pApi->freeSession(pInstance, pFilter_session); + } + + int routeQuery(MXS_FILTER* pInstance, MXS_FILTER_SESSION* pFilter_session, GWBUF* pStatement) + { + return m_pApi->routeQuery(pInstance, pFilter_session, pStatement); + } + + int clientReply(MXS_FILTER* pInstance, MXS_FILTER_SESSION* pFilter_session, GWBUF* pStatement) + { + return m_pApi->clientReply(pInstance, pFilter_session, pStatement); + } + + void setDownstream(MXS_FILTER* pInstance, + MXS_FILTER_SESSION* pFilter_session, + MXS_DOWNSTREAM* pDownstream) + { + m_pApi->setDownstream(pInstance, pFilter_session, pDownstream); + } + + void setUpstream(MXS_FILTER* pInstance, + MXS_FILTER_SESSION* pFilter_session, + MXS_UPSTREAM* pUpstream) + { + m_pApi->setUpstream(pInstance, pFilter_session, pUpstream); + } + +private: + friend class Module; + + FilterModule(MXS_FILTER_OBJECT* pApi) + : m_pApi(pApi) + { + } + +private: + MXS_FILTER_OBJECT* m_pApi; +}; + +}