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<SomeMonitor>::s_api,
            ...
        };
        return &info;
    }
			
			
This commit is contained in:
		
							
								
								
									
										85
									
								
								include/maxscale/monitor.hh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								include/maxscale/monitor.hh
									
									
									
									
									
										Normal file
									
								
							@ -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 <maxscale/cppdefs.hh>
 | 
			
		||||
#include <maxscale/monitor.h>
 | 
			
		||||
 | 
			
		||||
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 MonitorInstance>
 | 
			
		||||
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<MonitorInstance*>(pInstance)));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static bool startMonitor(MXS_MONITOR_INSTANCE* pInstance,
 | 
			
		||||
                             const MXS_CONFIG_PARAMETER* pParams)
 | 
			
		||||
    {
 | 
			
		||||
        bool started = false;
 | 
			
		||||
        MXS_EXCEPTION_GUARD(started = static_cast<MonitorInstance*>(pInstance)->start(pParams));
 | 
			
		||||
        return started;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static void stopMonitor(MXS_MONITOR_INSTANCE* pInstance)
 | 
			
		||||
    {
 | 
			
		||||
        MXS_EXCEPTION_GUARD(static_cast<MonitorInstance*>(pInstance)->stop());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static void diagnostics(const MXS_MONITOR_INSTANCE* pInstance, DCB* pDcb)
 | 
			
		||||
    {
 | 
			
		||||
        MXS_EXCEPTION_GUARD(static_cast<const MonitorInstance*>(pInstance)->diagnostics(pDcb));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static json_t* diagnostics_json(const MXS_MONITOR_INSTANCE* pInstance)
 | 
			
		||||
    {
 | 
			
		||||
        json_t* pJson = NULL;
 | 
			
		||||
        MXS_EXCEPTION_GUARD(pJson = static_cast<const MonitorInstance*>(pInstance)->diagnostics_json());
 | 
			
		||||
        return pJson;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static MXS_MONITOR_API s_api;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<class MonitorInstance>
 | 
			
		||||
MXS_MONITOR_API MonitorApi<MonitorInstance>::s_api =
 | 
			
		||||
{
 | 
			
		||||
    &MonitorApi<MonitorInstance>::createInstance,
 | 
			
		||||
    &MonitorApi<MonitorInstance>::destroyInstance,
 | 
			
		||||
    &MonitorApi<MonitorInstance>::startMonitor,
 | 
			
		||||
    &MonitorApi<MonitorInstance>::stopMonitor,
 | 
			
		||||
    &MonitorApi<MonitorInstance>::diagnostics,
 | 
			
		||||
    &MonitorApi<MonitorInstance>::diagnostics_json,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user