diff --git a/base/application.cpp b/base/application.cpp index 2d5d1b53f..253d9d3e4 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -99,14 +99,7 @@ void Application::RunEventLoop(void) if (m_ShuttingDown) break; - vector events; - - Event::Wait(&events, boost::get_system_time() + boost::posix_time::seconds(sleep)); - - for (vector::iterator it = events.begin(); it != events.end(); it++) { - Event::Ptr ev = *it; - ev->OnEventDelivered(); - } + Event::ProcessEvents(boost::get_system_time() + boost::posix_time::seconds(sleep)); } } diff --git a/base/asynctask.h b/base/asynctask.h index 08237355c..75fe0283b 100644 --- a/base/asynctask.h +++ b/base/asynctask.h @@ -53,18 +53,10 @@ protected: void Finish(void) { - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(&T::FinishForwarder, static_cast >(GetSelf()))); - Event::Post(ev); + Event::Post(boost::bind(boost::cref(OnTaskCompleted), static_cast >(GetSelf()))); } bool m_Finished; - -private: - static void FinishForwarder(const shared_ptr& task) - { - task->OnTaskCompleted(task); - } }; } diff --git a/base/event.cpp b/base/event.cpp index b83d70f60..d3692f60e 100644 --- a/base/event.cpp +++ b/base/event.cpp @@ -21,33 +21,43 @@ using namespace icinga; -deque Event::m_Events; +vector Event::m_Events; condition_variable Event::m_EventAvailable; mutex Event::m_Mutex; -bool Event::Wait(vector *events, const system_time& wait_until) +Event::Event(const function& callback) + : m_Callback(callback) +{ } + +void Event::ProcessEvents(const system_time& wait_until) { - mutex::scoped_lock lock(m_Mutex); + vector events; - while (m_Events.empty()) { - if (!m_EventAvailable.timed_wait(lock, wait_until)) - return false; + { + mutex::scoped_lock lock(m_Mutex); + + while (m_Events.empty()) { + if (!m_EventAvailable.timed_wait(lock, wait_until)) + return; + } + + events.swap(m_Events); } - - vector result; - std::copy(m_Events.begin(), m_Events.end(), back_inserter(*events)); - m_Events.clear(); - return true; + vector::iterator it; + for (it = events.begin(); it != events.end(); it++) + it->m_Callback(); } -void Event::Post(const Event::Ptr& ev) +void Event::Post(const function& callback) { if (Application::IsMainThread()) { - ev->OnEventDelivered(); + callback(); return; } + Event ev(callback); + { mutex::scoped_lock lock(m_Mutex); m_Events.push_back(ev); diff --git a/base/event.h b/base/event.h index 5919f4e65..4f1b16e7b 100644 --- a/base/event.h +++ b/base/event.h @@ -23,19 +23,18 @@ namespace icinga { -class I2_BASE_API Event : public Object +class I2_BASE_API Event { public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; - - static bool Wait(vector *events, const system_time& wait_until); - static void Post(const Event::Ptr& ev); - - boost::signal OnEventDelivered; + static void ProcessEvents(const system_time& wait_until); + static void Post(const function& callback); private: - static deque m_Events; + Event(const function& callback); + + function m_Callback; + + static vector m_Events; static condition_variable m_EventAvailable; static mutex m_Mutex; }; diff --git a/base/logger.cpp b/base/logger.cpp index 5699b12d3..591fe7952 100644 --- a/base/logger.cpp +++ b/base/logger.cpp @@ -49,9 +49,7 @@ void Logger::Write(LogSeverity severity, const string& facility, entry.Facility = facility; entry.Message = message; - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(&Logger::ForwardLogEntry, entry)); - Event::Post(ev); + Event::Post(boost::bind(&Logger::ForwardLogEntry, entry)); } /** diff --git a/base/socket.cpp b/base/socket.cpp index 32a2c45ef..fb2549935 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -111,11 +111,8 @@ void Socket::CloseInternal(bool from_dtor) /* nobody can possibly have a valid event subscription when the destructor has been called */ - if (!from_dtor) { - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(boost::ref(OnClosed), GetSelf())); - Event::Post(ev); - } + if (!from_dtor) + Event::Post(boost::bind(boost::ref(OnClosed), GetSelf())); } /** @@ -159,9 +156,7 @@ int Socket::GetLastSocketError(void) void Socket::HandleSocketError(const exception& ex) { if (!OnError.empty()) { - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(boost::ref(OnError), GetSelf(), runtime_error(ex.what()))); - Event::Post(ev); + Event::Post(boost::bind(boost::ref(OnError), GetSelf(), runtime_error(ex.what()))); CloseInternal(false); } else { diff --git a/base/tcpclient.cpp b/base/tcpclient.cpp index 2b0d18f52..3f95783e3 100644 --- a/base/tcpclient.cpp +++ b/base/tcpclient.cpp @@ -159,9 +159,7 @@ void TcpClient::HandleReadable(void) m_RecvQueue->Write(NULL, rc); } - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(boost::ref(OnDataAvailable), GetSelf())); - Event::Post(ev); + Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf())); } /** diff --git a/base/tcpserver.cpp b/base/tcpserver.cpp index c4b863e54..2da1a73f0 100644 --- a/base/tcpserver.cpp +++ b/base/tcpserver.cpp @@ -91,7 +91,5 @@ void TcpServer::HandleReadable(void) TcpClient::Ptr client = m_ClientFactory(fd); - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(boost::ref(OnNewClient), GetSelf(), client)); - Event::Post(ev); + Event::Post(boost::bind(boost::ref(OnNewClient), GetSelf(), client)); } diff --git a/base/tlsclient.cpp b/base/tlsclient.cpp index 4c63a9f7e..7ddb76ce0 100644 --- a/base/tlsclient.cpp +++ b/base/tlsclient.cpp @@ -142,9 +142,7 @@ void TlsClient::HandleReadable(void) } post_event: - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(boost::ref(OnDataAvailable), GetSelf())); - Event::Post(ev); + Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf())); } /** @@ -257,11 +255,8 @@ int TlsClient::ValidateCertificateInternal(int ok, X509_STORE_CTX *x509Context) shared_ptr x509Certificate = shared_ptr(x509Context->cert, &TlsClient::NullCertificateDeleter); bool valid = ValidateCertificate((ok != 0), x509Context, x509Certificate); - if (valid) { - Event::Ptr ev = boost::make_shared(); - ev->OnEventDelivered.connect(boost::bind(boost::ref(OnCertificateValidated), GetSelf())); - Event::Post(ev); - } + if (valid) + Event::Post(boost::bind(boost::ref(OnCertificateValidated), GetSelf())); return valid ? 1 : 0; } diff --git a/cib/nagioschecktask.cpp b/cib/nagioschecktask.cpp index b17147d62..5b6dc23fc 100644 --- a/cib/nagioschecktask.cpp +++ b/cib/nagioschecktask.cpp @@ -46,6 +46,10 @@ void NagiosCheckTask::Run(void) void NagiosCheckTask::ProcessFinishedHandler(void) { + time_t now; + time(&now); + GetResult().SetExecutionEnd(now); + string output = m_Process->GetOutput(); boost::algorithm::trim(output); ProcessCheckOutput(output); @@ -71,9 +75,8 @@ void NagiosCheckTask::ProcessFinishedHandler(void) GetResult().SetState(state); - time_t now; time(&now); - GetResult().SetExecutionEnd(now); + GetResult().SetScheduleEnd(now); Finish(); }