if (m_ShuttingDown)
break;
- vector<Event::Ptr> events;
-
- Event::Wait(&events, boost::get_system_time() + boost::posix_time::seconds(sleep));
-
- for (vector<Event::Ptr>::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));
}
}
void Finish(void)
{
- Event::Ptr ev = boost::make_shared<Event>();
- ev->OnEventDelivered.connect(boost::bind(&T::FinishForwarder, static_cast<shared_ptr<T> >(GetSelf())));
- Event::Post(ev);
+ Event::Post(boost::bind(boost::cref(OnTaskCompleted), static_cast<shared_ptr<T> >(GetSelf())));
}
bool m_Finished;
-
-private:
- static void FinishForwarder(const shared_ptr<T>& task)
- {
- task->OnTaskCompleted(task);
- }
};
}
using namespace icinga;
-deque<Event::Ptr> Event::m_Events;
+vector<Event> Event::m_Events;
condition_variable Event::m_EventAvailable;
mutex Event::m_Mutex;
-bool Event::Wait(vector<Event::Ptr> *events, const system_time& wait_until)
+Event::Event(const function<void ()>& callback)
+ : m_Callback(callback)
+{ }
+
+void Event::ProcessEvents(const system_time& wait_until)
{
- mutex::scoped_lock lock(m_Mutex);
+ vector<Event> events;
+
+ {
+ mutex::scoped_lock lock(m_Mutex);
+
+ while (m_Events.empty()) {
+ if (!m_EventAvailable.timed_wait(lock, wait_until))
+ return;
+ }
- while (m_Events.empty()) {
- if (!m_EventAvailable.timed_wait(lock, wait_until))
- return false;
+ events.swap(m_Events);
}
-
- vector<Event::Ptr> result;
- std::copy(m_Events.begin(), m_Events.end(), back_inserter(*events));
- m_Events.clear();
- return true;
+ vector<Event>::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<void ()>& callback)
{
if (Application::IsMainThread()) {
- ev->OnEventDelivered();
+ callback();
return;
}
+ Event ev(callback);
+
{
mutex::scoped_lock lock(m_Mutex);
m_Events.push_back(ev);
namespace icinga
{
-class I2_BASE_API Event : public Object
+class I2_BASE_API Event
{
public:
- typedef shared_ptr<Event> Ptr;
- typedef weak_ptr<Event> WeakPtr;
+ static void ProcessEvents(const system_time& wait_until);
+ static void Post(const function<void ()>& callback);
- static bool Wait(vector<Event::Ptr> *events, const system_time& wait_until);
- static void Post(const Event::Ptr& ev);
+private:
+ Event(const function<void ()>& callback);
- boost::signal<void ()> OnEventDelivered;
+ function<void ()> m_Callback;
-private:
- static deque<Event::Ptr> m_Events;
+ static vector<Event> m_Events;
static condition_variable m_EventAvailable;
static mutex m_Mutex;
};
entry.Facility = facility;
entry.Message = message;
- Event::Ptr ev = boost::make_shared<Event>();
- ev->OnEventDelivered.connect(boost::bind(&Logger::ForwardLogEntry, entry));
- Event::Post(ev);
+ Event::Post(boost::bind(&Logger::ForwardLogEntry, entry));
}
/**
/* nobody can possibly have a valid event subscription when the
destructor has been called */
- if (!from_dtor) {
- Event::Ptr ev = boost::make_shared<Event>();
- ev->OnEventDelivered.connect(boost::bind(boost::ref(OnClosed), GetSelf()));
- Event::Post(ev);
- }
+ if (!from_dtor)
+ Event::Post(boost::bind(boost::ref(OnClosed), GetSelf()));
}
/**
void Socket::HandleSocketError(const exception& ex)
{
if (!OnError.empty()) {
- Event::Ptr ev = boost::make_shared<Event>();
- 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 {
m_RecvQueue->Write(NULL, rc);
}
- Event::Ptr ev = boost::make_shared<Event>();
- ev->OnEventDelivered.connect(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
- Event::Post(ev);
+ Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
}
/**
TcpClient::Ptr client = m_ClientFactory(fd);
- Event::Ptr ev = boost::make_shared<Event>();
- ev->OnEventDelivered.connect(boost::bind(boost::ref(OnNewClient), GetSelf(), client));
- Event::Post(ev);
+ Event::Post(boost::bind(boost::ref(OnNewClient), GetSelf(), client));
}
}
post_event:
- Event::Ptr ev = boost::make_shared<Event>();
- ev->OnEventDelivered.connect(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
- Event::Post(ev);
+ Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
}
/**
shared_ptr<X509> x509Certificate = shared_ptr<X509>(x509Context->cert, &TlsClient::NullCertificateDeleter);
bool valid = ValidateCertificate((ok != 0), x509Context, x509Certificate);
- if (valid) {
- Event::Ptr ev = boost::make_shared<Event>();
- 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;
}
void NagiosCheckTask::ProcessFinishedHandler(void)
{
+ time_t now;
+ time(&now);
+ GetResult().SetExecutionEnd(now);
+
string output = m_Process->GetOutput();
boost::algorithm::trim(output);
ProcessCheckOutput(output);
GetResult().SetState(state);
- time_t now;
time(&now);
- GetResult().SetExecutionEnd(now);
+ GetResult().SetScheduleEnd(now);
Finish();
}