83 lines
1.9 KiB
C++
83 lines
1.9 KiB
C++
/*
|
|
* EventNotifier.cpp
|
|
*
|
|
* Created on: May 17, 2018
|
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
|
*/
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include "utilities/config.h"
|
|
|
|
#include "EventNotifier.h"
|
|
|
|
namespace Input
|
|
{
|
|
|
|
EventNotifier::EventNotifier(IBlockingQueue<Event>* queue) :
|
|
is_running(true),
|
|
event_queue(queue)
|
|
{
|
|
this->notify_thread = std::thread(&EventNotifier::notify, this);
|
|
|
|
CLOG(INFO, INPUT_LOGGER) << "Created EventNotifier and started thread";
|
|
}
|
|
|
|
EventNotifier::~EventNotifier()
|
|
{
|
|
this->is_running = false;
|
|
|
|
Event end_event(0, 0, "END");
|
|
this->event_queue->push(end_event);
|
|
|
|
this->notify_thread.join();
|
|
|
|
delete this->event_queue;
|
|
}
|
|
|
|
void EventNotifier::register_event_handler(IEventHandler* handler)
|
|
{
|
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
|
this->event_handlers.insert(handler);
|
|
}
|
|
|
|
void EventNotifier::unregister_event_handler(IEventHandler* handler)
|
|
{
|
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
|
this->event_handlers.erase(handler);
|
|
}
|
|
|
|
void EventNotifier::distribute_event(std::shared_ptr<Event> event)
|
|
{
|
|
this->event_queue->push(*event);
|
|
}
|
|
|
|
void EventNotifier::notify()
|
|
{
|
|
while(this->is_running)
|
|
{
|
|
Event event = this->event_queue->pop();
|
|
|
|
// TODO schoener machen
|
|
if(event.name == "END")
|
|
{
|
|
return;
|
|
}
|
|
|
|
// getting a guard and calling all registered handlers
|
|
std::lock_guard event_handler_guard(this->event_handler_mutex);
|
|
for(auto handler : this->event_handlers)
|
|
{
|
|
boost::thread handler_caller(boost::bind(&IEventHandler::handle, handler, event));
|
|
|
|
if(!handler_caller.timed_join(boost::posix_time::milliseconds(HANDLER_TIMEOUT)))
|
|
{
|
|
CLOG(WARNING, INPUT_LOGGER) << "Handler " << typeid(handler).name() << " didn't finish in "
|
|
<< HANDLER_TIMEOUT << " milliseconds. Aborting Execution!";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|