From 8ab247b23702cbe2a501b22981b043196de85106 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 7 May 2018 14:41:43 +0300 Subject: [PATCH] MXS-1848 Introduce maxscape::MonitorApi The purpose of this template class is to provide the implementation of the monitor C-api. It's to be instantiated with the class that actually provides the monitor behaviour. A separate class maxscale::MonitorInstance will be provided that then in turn implements the behaviour common to most monitors. So, the structure will be like: class SomeMonitor : public maxscale::MonitorInstance {...} extern "C" MXS_MODULE* MXS_CREATE_MODULE() { static MXS_MODULE info = { ... &maxscale::MonitorApi::s_api, ... }; return &info; } --- include/maxscale/monitor.hh | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 include/maxscale/monitor.hh diff --git a/include/maxscale/monitor.hh b/include/maxscale/monitor.hh new file mode 100644 index 000000000..6c6d316f0 --- /dev/null +++ b/include/maxscale/monitor.hh @@ -0,0 +1,85 @@ +#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 + +namespace maxscale +{ + +/** + * The purpose of the template MonitorApi is to provide an implementation + * of the monitor C-API. The template is instantiated with a class that + * provides the actual behaviour of a monitor. + */ +template +class MonitorApi +{ +public: + MonitorApi() = delete; + MonitorApi(const MonitorApi&) = delete; + MonitorApi& operator = (const MonitorApi&) = delete; + + static MXS_MONITOR_INSTANCE* createInstance(MXS_MONITOR* pMonitor) + { + MonitorInstance* pInstance = NULL; + MXS_EXCEPTION_GUARD(pInstance = MonitorInstance::create(pMonitor)); + return pInstance; + } + + static void destroyInstance(MXS_MONITOR_INSTANCE* pInstance) + { + MXS_EXCEPTION_GUARD(MonitorInstance::destroy(static_cast(pInstance))); + } + + static bool startMonitor(MXS_MONITOR_INSTANCE* pInstance, + const MXS_CONFIG_PARAMETER* pParams) + { + bool started = false; + MXS_EXCEPTION_GUARD(started = static_cast(pInstance)->start(pParams)); + return started; + } + + static void stopMonitor(MXS_MONITOR_INSTANCE* pInstance) + { + MXS_EXCEPTION_GUARD(static_cast(pInstance)->stop()); + } + + static void diagnostics(const MXS_MONITOR_INSTANCE* pInstance, DCB* pDcb) + { + MXS_EXCEPTION_GUARD(static_cast(pInstance)->diagnostics(pDcb)); + } + + static json_t* diagnostics_json(const MXS_MONITOR_INSTANCE* pInstance) + { + json_t* pJson = NULL; + MXS_EXCEPTION_GUARD(pJson = static_cast(pInstance)->diagnostics_json()); + return pJson; + } + + static MXS_MONITOR_API s_api; +}; + +template +MXS_MONITOR_API MonitorApi::s_api = +{ + &MonitorApi::createInstance, + &MonitorApi::destroyInstance, + &MonitorApi::startMonitor, + &MonitorApi::stopMonitor, + &MonitorApi::diagnostics, + &MonitorApi::diagnostics_json, +}; + +}