mirror of
https://github.com/Icinga/icinga2.git
synced 2025-05-16 20:40:16 +02:00
Cleaned up AsyncTask class.
This commit is contained in:
parent
656825701d
commit
6cbccdc91c
@ -7,6 +7,7 @@ pkglib_LTLIBRARIES = \
|
|||||||
libbase_la_SOURCES = \
|
libbase_la_SOURCES = \
|
||||||
application.cpp \
|
application.cpp \
|
||||||
application.h \
|
application.h \
|
||||||
|
asynctask.cpp \
|
||||||
asynctask.h \
|
asynctask.h \
|
||||||
component.cpp \
|
component.cpp \
|
||||||
component.h \
|
component.h \
|
||||||
|
50
base/asynctask.cpp
Normal file
50
base/asynctask.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Icinga 2 *
|
||||||
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or *
|
||||||
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
* as published by the Free Software Foundation; either version 2 *
|
||||||
|
* of the License, or (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the Free Software Foundation *
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "i2-base.h"
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
AsyncTask::AsyncTask(const AsyncTask::CompletionCallback& completionCallback)
|
||||||
|
: m_Finished(false), m_CompletionCallback(completionCallback)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
AsyncTask::~AsyncTask(void)
|
||||||
|
{
|
||||||
|
assert(m_Finished);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsyncTask::Start(void)
|
||||||
|
{
|
||||||
|
assert(Application::IsMainThread());
|
||||||
|
|
||||||
|
Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsyncTask::Finish(void)
|
||||||
|
{
|
||||||
|
Event::Post(boost::bind(&AsyncTask::ForwardCallback, static_cast<AsyncTask::Ptr>(GetSelf())));
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsyncTask::ForwardCallback(void)
|
||||||
|
{
|
||||||
|
m_CompletionCallback(GetSelf());
|
||||||
|
m_CompletionCallback = CompletionCallback();
|
||||||
|
m_Finished = true;
|
||||||
|
}
|
@ -23,46 +23,26 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T>
|
class I2_BASE_API AsyncTask : public Object
|
||||||
class AsyncTask : public Object
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef shared_ptr<AsyncTask<T> > Ptr;
|
typedef shared_ptr<AsyncTask> Ptr;
|
||||||
typedef weak_ptr<AsyncTask<T> > WeakPtr;
|
typedef weak_ptr<AsyncTask> WeakPtr;
|
||||||
|
|
||||||
typedef function<void (const shared_ptr<T>&)> CompletionCallback;
|
typedef function<void (const AsyncTask::Ptr&)> CompletionCallback;
|
||||||
|
|
||||||
AsyncTask(const CompletionCallback& completionCallback)
|
AsyncTask(const CompletionCallback& completionCallback);
|
||||||
: m_Finished(false), m_CompletionCallback(completionCallback)
|
~AsyncTask(void);
|
||||||
{ }
|
|
||||||
|
|
||||||
~AsyncTask(void)
|
void Start(void);
|
||||||
{
|
|
||||||
assert(m_Finished);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Start(void)
|
|
||||||
{
|
|
||||||
assert(Application::IsMainThread());
|
|
||||||
|
|
||||||
Run();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void Run(void) = 0;
|
virtual void Run(void) = 0;
|
||||||
|
|
||||||
void Finish(void)
|
void Finish(void);
|
||||||
{
|
|
||||||
Event::Post(boost::bind(&T::ForwardCallback, static_cast<shared_ptr<T> >(GetSelf())));
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ForwardCallback(void)
|
void ForwardCallback(void);
|
||||||
{
|
|
||||||
m_CompletionCallback(GetSelf());
|
|
||||||
m_CompletionCallback = CompletionCallback();
|
|
||||||
m_Finished = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool m_Finished;
|
bool m_Finished;
|
||||||
CompletionCallback m_CompletionCallback;
|
CompletionCallback m_CompletionCallback;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="application.cpp" />
|
<ClCompile Include="application.cpp" />
|
||||||
|
<ClCompile Include="asynctask.cpp" />
|
||||||
<ClCompile Include="component.cpp" />
|
<ClCompile Include="component.cpp" />
|
||||||
<ClCompile Include="configobject.cpp" />
|
<ClCompile Include="configobject.cpp" />
|
||||||
<ClCompile Include="dictionary.cpp" />
|
<ClCompile Include="dictionary.cpp" />
|
||||||
|
@ -79,6 +79,9 @@
|
|||||||
<ClCompile Include="process.cpp">
|
<ClCompile Include="process.cpp">
|
||||||
<Filter>Quelldateien</Filter>
|
<Filter>Quelldateien</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="asynctask.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="application.h">
|
<ClInclude Include="application.h">
|
||||||
|
@ -55,6 +55,7 @@
|
|||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
# pragma warning(disable:4251)
|
# pragma warning(disable:4251)
|
||||||
# pragma warning(disable:4275)
|
# pragma warning(disable:4275)
|
||||||
|
# pragma warning(disable:4345)
|
||||||
# define _CRT_SECURE_NO_DEPRECATE
|
# define _CRT_SECURE_NO_DEPRECATE
|
||||||
# define _CRT_SECURE_NO_WARNINGS
|
# define _CRT_SECURE_NO_WARNINGS
|
||||||
#else /* _MSC_VER */
|
#else /* _MSC_VER */
|
||||||
|
@ -31,7 +31,7 @@ deque<Process::Ptr> Process::m_Tasks;
|
|||||||
condition_variable Process::m_TasksCV;
|
condition_variable Process::m_TasksCV;
|
||||||
|
|
||||||
Process::Process(const string& command, const CompletionCallback& completionCallback)
|
Process::Process(const string& command, const CompletionCallback& completionCallback)
|
||||||
: AsyncTask<Process>(completionCallback), m_Command(command), m_UsePopen(false)
|
: AsyncTask(completionCallback), m_Command(command), m_UsePopen(false)
|
||||||
{
|
{
|
||||||
if (!m_ThreadCreated) {
|
if (!m_ThreadCreated) {
|
||||||
thread t(&Process::WorkerThreadProc);
|
thread t(&Process::WorkerThreadProc);
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
class I2_BASE_API Process : public AsyncTask<Process>
|
class I2_BASE_API Process : public AsyncTask
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef shared_ptr<Process> Ptr;
|
typedef shared_ptr<Process> Ptr;
|
||||||
|
@ -24,7 +24,7 @@ using namespace icinga;
|
|||||||
map<string, CheckTaskType> CheckTask::m_Types;
|
map<string, CheckTaskType> CheckTask::m_Types;
|
||||||
|
|
||||||
CheckTask::CheckTask(const Service& service, const CompletionCallback& completionCallback)
|
CheckTask::CheckTask(const Service& service, const CompletionCallback& completionCallback)
|
||||||
: AsyncTask<CheckTask>(completionCallback), m_Service(service)
|
: AsyncTask(completionCallback), m_Service(service)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
Service& CheckTask::GetService(void)
|
Service& CheckTask::GetService(void)
|
||||||
|
@ -25,7 +25,7 @@ namespace icinga
|
|||||||
|
|
||||||
struct CheckTaskType;
|
struct CheckTaskType;
|
||||||
|
|
||||||
class I2_CIB_API CheckTask : public AsyncTask<CheckTask>
|
class I2_CIB_API CheckTask : public AsyncTask
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef shared_ptr<CheckTask> Ptr;
|
typedef shared_ptr<CheckTask> Ptr;
|
||||||
|
@ -93,8 +93,9 @@ void CheckerComponent::CheckTimerHandler(void)
|
|||||||
Logger::Write(LogInformation, "checker", msgbuf.str());
|
Logger::Write(LogInformation, "checker", msgbuf.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckerComponent::CheckCompletedHandler(const CheckTask::Ptr& task)
|
void CheckerComponent::CheckCompletedHandler(const AsyncTask::Ptr& atask)
|
||||||
{
|
{
|
||||||
|
CheckTask::Ptr task = static_pointer_cast<CheckTask>(atask);
|
||||||
Service service = task->GetService();
|
Service service = task->GetService();
|
||||||
|
|
||||||
service.RemoveTag("current_task");
|
service.RemoveTag("current_task");
|
||||||
|
@ -60,7 +60,7 @@ private:
|
|||||||
void CheckTimerHandler(void);
|
void CheckTimerHandler(void);
|
||||||
void ResultTimerHandler(void);
|
void ResultTimerHandler(void);
|
||||||
|
|
||||||
void CheckCompletedHandler(const CheckTask::Ptr& task);
|
void CheckCompletedHandler(const AsyncTask::Ptr& task);
|
||||||
|
|
||||||
void AdjustCheckTimer(void);
|
void AdjustCheckTimer(void);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user