From: Gunnar Beutner Date: Thu, 23 Nov 2017 08:31:39 +0000 (+0100) Subject: Get rid of INITIALIZE_ONCE for the ExternalCommandProcessor X-Git-Tag: v2.9.0~310^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=63fd6e3905b40442c90666bec09bee593a3d8cb0;p=icinga2 Get rid of INITIALIZE_ONCE for the ExternalCommandProcessor --- diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 3ea5afe71..d1c9f73e0 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -43,39 +43,7 @@ using namespace icinga; -INITIALIZE_ONCE(&ExternalCommandProcessor::StaticInitialize); - -typedef std::function& arguments)> ExternalCommandCallback; - -struct ExternalCommandInfo -{ - ExternalCommandCallback Callback; - size_t MinArgs; - size_t MaxArgs; -}; - -static boost::mutex& GetMutex(void) -{ - static boost::mutex mtx; - return mtx; -} -static std::map& GetCommands(void) -{ - static std::map commands; - return commands; -} - -boost::signals2::signal&)> ExternalCommandProcessor::OnNewExternalCommand; - -static void RegisterCommand(const String& command, const ExternalCommandCallback& callback, size_t minArgs = 0, size_t maxArgs = UINT_MAX) -{ - boost::mutex::scoped_lock lock(GetMutex()); - ExternalCommandInfo eci; - eci.Callback = callback; - eci.MinArgs = minArgs; - eci.MaxArgs = (maxArgs == UINT_MAX) ? minArgs : maxArgs; - GetCommands()[command] = eci; -} +boost::signals2::signal&)> ExternalCommandProcessor::OnNewExternalCommand; void ExternalCommandProcessor::Execute(const String& line) { @@ -156,7 +124,17 @@ void ExternalCommandProcessor::Execute(double time, const String& command, const eci.Callback(time, realArguments); } -void ExternalCommandProcessor::StaticInitialize(void) +void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandCallback& callback, size_t minArgs, size_t maxArgs) +{ + boost::mutex::scoped_lock lock(GetMutex()); + ExternalCommandInfo eci; + eci.Callback = callback; + eci.MinArgs = minArgs; + eci.MaxArgs = (maxArgs == UINT_MAX) ? minArgs : maxArgs; + GetCommands()[command] = eci; +} + +void ExternalCommandProcessor::RegisterCommands(void) { RegisterCommand("PROCESS_HOST_CHECK_RESULT", &ExternalCommandProcessor::ProcessHostCheckResult, 3); RegisterCommand("PROCESS_SERVICE_CHECK_RESULT", &ExternalCommandProcessor::ProcessServiceCheckResult, 4); @@ -2254,3 +2232,16 @@ void ExternalCommandProcessor::DisableServicegroupSvcNotifications(double, const service->ModifyAttribute("enable_notifications", false); } } + +boost::mutex& ExternalCommandProcessor::GetMutex(void) +{ + static boost::mutex mtx; + return mtx; +} + +std::map& ExternalCommandProcessor::GetCommands(void) +{ + static std::map commands; + return commands; +} + diff --git a/lib/icinga/externalcommandprocessor.hpp b/lib/icinga/externalcommandprocessor.hpp index 07fbfa512..51cf2d291 100644 --- a/lib/icinga/externalcommandprocessor.hpp +++ b/lib/icinga/externalcommandprocessor.hpp @@ -29,13 +29,20 @@ namespace icinga { +typedef std::function& arguments)> ExternalCommandCallback; + +struct ExternalCommandInfo +{ + ExternalCommandCallback Callback; + size_t MinArgs; + size_t MaxArgs; +}; + class I2_ICINGA_API ExternalCommandProcessor { public: static void Execute(const String& line); static void Execute(double time, const String& command, const std::vector& arguments); - static void StaticInitialize(void); - static boost::signals2::signal&)> OnNewExternalCommand; private: @@ -165,6 +172,13 @@ private: private: static void ChangeCustomCommandVarInternal(const Command::Ptr& command, const String& name, const Value& value); + + static void RegisterCommand(const String& command, const ExternalCommandCallback& callback, size_t minArgs = 0, size_t maxArgs = UINT_MAX); + static void RegisterCommands(void); + + static boost::mutex& GetMutex(void); + static std::map& GetCommands(void); + }; }