Add C++ filter example
The filter example uses the C++ template which helps new filter creation by showing the minimal implementation required for a filter.
This commit is contained in:
@ -15,6 +15,10 @@ target_link_libraries(testfilter maxscale-common)
|
|||||||
set_target_properties(testfilter PROPERTIES VERSION "1.0.0")
|
set_target_properties(testfilter PROPERTIES VERSION "1.0.0")
|
||||||
install_module(testfilter core)
|
install_module(testfilter core)
|
||||||
|
|
||||||
|
add_library(examplecppfilter SHARED examplefilter.cc examplefiltersession.cc)
|
||||||
|
set_target_properties(examplecppfilter PROPERTIES VERSION "1.0.0")
|
||||||
|
install_module(examplecppfilter core)
|
||||||
|
|
||||||
add_library(testprotocol SHARED testprotocol.c)
|
add_library(testprotocol SHARED testprotocol.c)
|
||||||
set_target_properties(testprotocol PROPERTIES VERSION "1.0.0")
|
set_target_properties(testprotocol PROPERTIES VERSION "1.0.0")
|
||||||
install_module(testprotocol core)
|
install_module(testprotocol core)
|
||||||
|
78
examples/examplefilter.cc
Normal file
78
examples/examplefilter.cc
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// All log messages from this module are prefixed with this
|
||||||
|
#define MXS_MODULE_NAME "examplefilter"
|
||||||
|
|
||||||
|
#include "examplefilter.hh"
|
||||||
|
|
||||||
|
// This declares a module in MaxScale
|
||||||
|
extern "C" MXS_MODULE* MXS_CREATE_MODULE()
|
||||||
|
{
|
||||||
|
static MXS_MODULE info =
|
||||||
|
{
|
||||||
|
MXS_MODULE_API_FILTER,
|
||||||
|
MXS_MODULE_IN_DEVELOPMENT,
|
||||||
|
MXS_FILTER_VERSION,
|
||||||
|
"An example filter that does nothing",
|
||||||
|
"V1.0.0",
|
||||||
|
RCAP_TYPE_NONE,
|
||||||
|
&ExampleFilter::s_object, // This is defined in the MaxScale filter template
|
||||||
|
NULL, /* Process init. */
|
||||||
|
NULL, /* Process finish. */
|
||||||
|
NULL, /* Thread init. */
|
||||||
|
NULL, /* Thread finish. */
|
||||||
|
{
|
||||||
|
{ "an_example_parameter", MXS_MODULE_PARAM_STRING, "a-default-value" },
|
||||||
|
{ MXS_END_MODULE_PARAMS }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return &info;
|
||||||
|
}
|
||||||
|
|
||||||
|
ExampleFilter::ExampleFilter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ExampleFilter::~ExampleFilter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
ExampleFilter* ExampleFilter::create(const char* zName, char** pzOptions, MXS_CONFIG_PARAMETER* pParams)
|
||||||
|
{
|
||||||
|
return new ExampleFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
ExampleFilterSession* ExampleFilter::newSession(MXS_SESSION* pSession)
|
||||||
|
{
|
||||||
|
return ExampleFilterSession::create(pSession, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void ExampleFilter::diagnostics(DCB* pDcb) const
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
json_t* ExampleFilter::diagnostics_json() const
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
uint64_t ExampleFilter::getCapabilities()
|
||||||
|
{
|
||||||
|
return RCAP_TYPE_NONE;
|
||||||
|
}
|
46
examples/examplefilter.hh
Normal file
46
examples/examplefilter.hh
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#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 <maxscale/cppdefs.hh>
|
||||||
|
#include <maxscale/filter.hh>
|
||||||
|
#include "examplefiltersession.hh"
|
||||||
|
|
||||||
|
class ExampleFilter : public maxscale::Filter<ExampleFilter, ExampleFilterSession>
|
||||||
|
{
|
||||||
|
// Prevent copy-constructor and assignment operator usage
|
||||||
|
ExampleFilter(const ExampleFilter&);
|
||||||
|
ExampleFilter& operator = (const ExampleFilter&);
|
||||||
|
|
||||||
|
public:
|
||||||
|
~ExampleFilter();
|
||||||
|
|
||||||
|
// Creates a new filter instance
|
||||||
|
static ExampleFilter* create(const char* zName, char** pzOptions, MXS_CONFIG_PARAMETER* ppParams);
|
||||||
|
|
||||||
|
// Creates a new session for this filter
|
||||||
|
ExampleFilterSession* newSession(MXS_SESSION* pSession);
|
||||||
|
|
||||||
|
// Print diagnostics to a DCB
|
||||||
|
void diagnostics(DCB* pDcb) const;
|
||||||
|
|
||||||
|
// Returns JSON form diagnostic data
|
||||||
|
json_t* diagnostics_json() const;
|
||||||
|
|
||||||
|
// Get filter capabilities
|
||||||
|
uint64_t getCapabilities();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Used in the create function
|
||||||
|
ExampleFilter();
|
||||||
|
};
|
43
examples/examplefiltersession.cc
Normal file
43
examples/examplefiltersession.cc
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// All log messages from this module are prefixed with this
|
||||||
|
#define MXS_MODULE_NAME "examplefilter"
|
||||||
|
|
||||||
|
#include "examplefiltersession.hh"
|
||||||
|
#include "examplefilter.hh"
|
||||||
|
|
||||||
|
ExampleFilterSession::ExampleFilterSession(MXS_SESSION* pSession)
|
||||||
|
: mxs::FilterSession(pSession)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ExampleFilterSession::~ExampleFilterSession()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//static
|
||||||
|
ExampleFilterSession* ExampleFilterSession::create(MXS_SESSION* pSession, const ExampleFilter* pFilter)
|
||||||
|
{
|
||||||
|
return new ExampleFilterSession(pSession);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ExampleFilterSession::routeQuery(GWBUF* pPacket)
|
||||||
|
{
|
||||||
|
return mxs::FilterSession::routeQuery(pPacket);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ExampleFilterSession::clientReply(GWBUF* pPacket)
|
||||||
|
{
|
||||||
|
return mxs::FilterSession::clientReply(pPacket);
|
||||||
|
}
|
44
examples/examplefiltersession.hh
Normal file
44
examples/examplefiltersession.hh
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#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 <maxscale/cppdefs.hh>
|
||||||
|
#include <maxscale/filter.hh>
|
||||||
|
|
||||||
|
class ExampleFilter;
|
||||||
|
|
||||||
|
class ExampleFilterSession : public maxscale::FilterSession
|
||||||
|
{
|
||||||
|
// Prevent copy-constructor and assignment operator usage
|
||||||
|
ExampleFilterSession(const ExampleFilterSession&);
|
||||||
|
ExampleFilterSession& operator = (const ExampleFilterSession&);
|
||||||
|
|
||||||
|
public:
|
||||||
|
~ExampleFilterSession();
|
||||||
|
|
||||||
|
// Called when a client session has been closed
|
||||||
|
void close();
|
||||||
|
|
||||||
|
// Create a new filter session
|
||||||
|
static ExampleFilterSession* create(MXS_SESSION* pSession, const ExampleFilter* pFilter);
|
||||||
|
|
||||||
|
// Handle a query from the client
|
||||||
|
int routeQuery(GWBUF* pPacket);
|
||||||
|
|
||||||
|
// Handle a reply from server
|
||||||
|
int clientReply(GWBUF* pPacket);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Used in the create function
|
||||||
|
ExampleFilterSession(MXS_SESSION* pSession);
|
||||||
|
};
|
Reference in New Issue
Block a user