Initial commit
This commit is contained in:
Executable
+276
@@ -0,0 +1,276 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <wfr/common/types/WfrTypes.h>
|
||||
#include <wfr/common/types/WfsTypes.h>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace wfr {
|
||||
|
||||
namespace wfs {
|
||||
// Forward declarations
|
||||
class Workflow;
|
||||
class Activity;
|
||||
class ActivityInstance;
|
||||
class AtomicActivityImplementation;
|
||||
}
|
||||
|
||||
|
||||
// main namespace
|
||||
namespace eventsystem {
|
||||
|
||||
|
||||
/**
|
||||
* Event Types.
|
||||
**/
|
||||
enum class EventType
|
||||
: int { GeneralLoggingEvent,
|
||||
WorkflowStatusEvent,
|
||||
ActivityStatusEvent,
|
||||
ActivityInstanceStatusEvent,
|
||||
ComputingResourceStatusEvent,
|
||||
ComponentStatusEvent };
|
||||
|
||||
/**
|
||||
* Abstract event base class.
|
||||
**/
|
||||
class Event {
|
||||
|
||||
// Invariant parameters that can be accessed directly
|
||||
public:
|
||||
const EventType eventType;
|
||||
const wfr_timestamp_sc_t timestamp;
|
||||
|
||||
// Public functions
|
||||
public:
|
||||
Event(EventType type, const wfr_timestamp_sc_t& timestamp) noexcept
|
||||
: eventType(type), timestamp(timestamp) {}
|
||||
virtual ~Event() noexcept {}
|
||||
|
||||
virtual std::string toString() const noexcept = 0;
|
||||
|
||||
template<class T>
|
||||
T& as() noexcept { return *static_cast<T*>(this); }
|
||||
|
||||
template<class T>
|
||||
const T& as() const noexcept { return *static_cast<const T*>(this); }
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Event for logging arbitrary messages
|
||||
**/
|
||||
class GeneralLoggingEvent : public Event {
|
||||
protected:
|
||||
std::string logMessage;
|
||||
|
||||
public:
|
||||
GeneralLoggingEvent( const std::string& message,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now() ) noexcept
|
||||
: Event(EventType::GeneralLoggingEvent, timestamp), logMessage(message) {}
|
||||
|
||||
const std::string& getMessage() const noexcept { return logMessage; }
|
||||
|
||||
virtual std::string toString() const noexcept override;
|
||||
|
||||
static std::shared_ptr<GeneralLoggingEvent> fireEvent( const std::string& message,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now(),
|
||||
bool noLocking = false ) noexcept;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Event to indicate that the state of a workflow changed.
|
||||
**/
|
||||
class WorkflowStatusEvent : public Event {
|
||||
protected:
|
||||
wfr::wfs::Workflow& workflow;
|
||||
wfr::wfs::WorkflowStatus oldStatus;
|
||||
wfr::wfs::WorkflowStatus newStatus;
|
||||
|
||||
public:
|
||||
WorkflowStatusEvent( wfr::wfs::Workflow& workflow,
|
||||
wfr::wfs::WorkflowStatus& oldStatus,
|
||||
wfr::wfs::WorkflowStatus& newStatus,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now() ) noexcept
|
||||
: Event(EventType::WorkflowStatusEvent, timestamp), workflow(workflow), oldStatus(oldStatus), newStatus(newStatus) {}
|
||||
|
||||
wfr::wfs::WorkflowStatus getNewStatus() const noexcept { return newStatus; }
|
||||
|
||||
wfr::wfs::WorkflowStatus getOldStatus() const noexcept { return oldStatus; }
|
||||
|
||||
wfr::wfs::Workflow& getWorkflow() const noexcept { return workflow; }
|
||||
|
||||
virtual std::string toString() const noexcept override;
|
||||
|
||||
static std::shared_ptr<WorkflowStatusEvent> fireEvent( wfr::wfs::Workflow& workflow,
|
||||
wfr::wfs::WorkflowStatus& oldStatus,
|
||||
wfr::wfs::WorkflowStatus& newStatus,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now(),
|
||||
bool noLocking = false ) noexcept;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Event to indicate that the state of an activity changed.
|
||||
**/
|
||||
class ActivityStatusEvent : public Event {
|
||||
protected:
|
||||
wfr::wfs::Activity& activity;
|
||||
wfr::wfs::ActivityStatus oldStatus;
|
||||
wfr::wfs::ActivityStatus newStatus;
|
||||
|
||||
public:
|
||||
ActivityStatusEvent( wfr::wfs::Activity& activity,
|
||||
wfr::wfs::ActivityStatus oldStatus,
|
||||
wfr::wfs::ActivityStatus newStatus,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now() ) noexcept
|
||||
: Event(EventType::ActivityStatusEvent, timestamp), activity(activity), oldStatus(oldStatus), newStatus(newStatus) {}
|
||||
|
||||
wfr::wfs::Activity& getActivity() const noexcept { return activity; }
|
||||
|
||||
wfr::wfs::ActivityStatus getNewStatus() const noexcept { return newStatus; }
|
||||
|
||||
wfr::wfs::ActivityStatus getOldStatus() const noexcept { return oldStatus; }
|
||||
|
||||
virtual std::string toString() const noexcept override;
|
||||
|
||||
static std::shared_ptr<ActivityStatusEvent> fireEvent( wfr::wfs::Activity& activity,
|
||||
wfr::wfs::ActivityStatus oldStatus,
|
||||
wfr::wfs::ActivityStatus newStatus,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now(),
|
||||
bool noLocking = false ) noexcept;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Event to indicate that the state of an activity instance changed.
|
||||
**/
|
||||
class ActivityInstanceStatusEvent : public Event {
|
||||
protected:
|
||||
wfr::wfs::ActivityInstance& activityInstance;
|
||||
wfr::wfs::ActivityInstanceStatus oldStatus;
|
||||
wfr::wfs::ActivityInstanceStatus newStatus;
|
||||
|
||||
public:
|
||||
ActivityInstanceStatusEvent( wfr::wfs::ActivityInstance& activityInstance,
|
||||
wfr::wfs::ActivityInstanceStatus oldStatus,
|
||||
wfr::wfs::ActivityInstanceStatus newStatus,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now() ) noexcept
|
||||
: Event(EventType::ActivityInstanceStatusEvent, timestamp), activityInstance(activityInstance), oldStatus(oldStatus), newStatus(newStatus) {}
|
||||
|
||||
wfr::wfs::ActivityInstance& getActivityInstance() const noexcept { return activityInstance; }
|
||||
|
||||
wfr::wfs::ActivityInstanceStatus getNewStatus() const noexcept { return newStatus; }
|
||||
|
||||
wfr::wfs::ActivityInstanceStatus getOldStatus() const noexcept { return oldStatus; }
|
||||
|
||||
virtual std::string toString() const noexcept override;
|
||||
|
||||
static std::shared_ptr<ActivityInstanceStatusEvent> fireEvent( wfr::wfs::ActivityInstance& activity,
|
||||
wfr::wfs::ActivityInstanceStatus oldStatus,
|
||||
wfr::wfs::ActivityInstanceStatus newStatus,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now(),
|
||||
bool noLocking = false ) noexcept;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Event to indicate that the state of a WFR component changed.
|
||||
*/
|
||||
class ComponentStatusEvent : public Event {
|
||||
protected:
|
||||
wfs::ComponentType componentType;
|
||||
wfs::ComponentStatus oldStatus;
|
||||
wfs::ComponentStatus newStatus;
|
||||
wfr::wfs::Workflow* workflow;
|
||||
|
||||
public:
|
||||
ComponentStatusEvent( wfs::ComponentType componentType,
|
||||
wfs::ComponentStatus oldStatus,
|
||||
wfs::ComponentStatus newStatus,
|
||||
wfr::wfs::Workflow* wf = nullptr,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now()) noexcept
|
||||
: Event(EventType::ComponentStatusEvent, timestamp), componentType(componentType), oldStatus(oldStatus), newStatus(newStatus), workflow(wf) {}
|
||||
|
||||
wfs::ComponentType getComponentType() const noexcept { return componentType; }
|
||||
|
||||
wfs::ComponentStatus getNewStatus() const noexcept { return newStatus; }
|
||||
|
||||
wfs::ComponentStatus getOldStatus() const noexcept { return oldStatus; }
|
||||
|
||||
wfr::wfs::Workflow* getWorkflow() const noexcept { return workflow; }
|
||||
|
||||
virtual std::string toString() const noexcept override;
|
||||
|
||||
static std::shared_ptr<ComponentStatusEvent> fireEvent( wfs::ComponentType componentType,
|
||||
wfs::ComponentStatus oldStatus,
|
||||
wfs::ComponentStatus newStatus,
|
||||
wfr::wfs::Workflow* wf = nullptr,
|
||||
const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now(),
|
||||
bool noLocking = false ) noexcept;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Event to indicate that the state of a computing resource changed.
|
||||
**/
|
||||
class ComputingResourceStatusEvent : public Event {
|
||||
protected:
|
||||
wfr::wfs::Workflow* workflow;
|
||||
wfr::wfs::ActivityImplementationType deviceType;
|
||||
wfr_id_t deviceID;
|
||||
wfr::wfs::ComputingResourceStatus oldStatus;
|
||||
wfr::wfs::ComputingResourceStatus newStatus;
|
||||
wfr::wfs::ActivityInstance* executedTask;
|
||||
wfr::wfs::AtomicActivityImplementation* executedImpl;
|
||||
wfr_size_t deviceSlotCount;
|
||||
|
||||
public:
|
||||
ComputingResourceStatusEvent(wfr::wfs::Workflow* wf, wfr::wfs::ActivityImplementationType deviceType, wfr_id_t deviceID,
|
||||
wfr::wfs::ComputingResourceStatus oldStatus, wfr::wfs::ComputingResourceStatus newStatus,
|
||||
wfr::wfs::ActivityInstance* executedTask = nullptr, wfr::wfs::AtomicActivityImplementation* executedImpl = nullptr,
|
||||
wfr_size_t deviceSlotCount = 0,
|
||||
wfr_timestamp_sc_t timestamp = std::chrono::system_clock::now()) noexcept
|
||||
: Event(EventType::ComputingResourceStatusEvent, timestamp), workflow(wf), deviceType(deviceType),
|
||||
deviceID(deviceID), oldStatus(oldStatus), newStatus(newStatus), executedTask(executedTask),
|
||||
executedImpl(executedImpl), deviceSlotCount(deviceSlotCount) {}
|
||||
|
||||
wfr::wfs::ActivityImplementationType getDeviceType() const noexcept { return deviceType; }
|
||||
|
||||
wfr::wfs::Workflow* getWorkflow() const noexcept { return workflow; }
|
||||
|
||||
wfr_id_t getDeviceID() const noexcept { return deviceID; }
|
||||
|
||||
wfr::wfs::ComputingResourceStatus getNewStatus() const noexcept { return newStatus; }
|
||||
|
||||
wfr::wfs::ComputingResourceStatus getOldStatus() const noexcept { return oldStatus; }
|
||||
|
||||
wfr::wfs::ActivityInstance* getExecutedTask() const noexcept { return executedTask; }
|
||||
|
||||
wfr::wfs::AtomicActivityImplementation* getExecutedImpl() const noexcept { return executedImpl; }
|
||||
|
||||
wfr_size_t getDeviceSlotCount() const noexcept { return deviceSlotCount; }
|
||||
|
||||
virtual std::string toString() const noexcept override;
|
||||
|
||||
static std::shared_ptr<ComputingResourceStatusEvent> fireEvent( wfr::wfs::Workflow* wf,
|
||||
wfr::wfs::ActivityImplementationType deviceType,
|
||||
wfr_id_t deviceID,
|
||||
wfr::wfs::ComputingResourceStatus oldStatus,
|
||||
wfr::wfs::ComputingResourceStatus newStatus,
|
||||
wfr::wfs::ActivityInstance* executedTask = nullptr,
|
||||
wfr::wfs::AtomicActivityImplementation* executedImpl = nullptr,
|
||||
wfr_size_t deviceSlotCount = 0,
|
||||
wfr_timestamp_sc_t timestamp = std::chrono::system_clock::now(),
|
||||
bool noLocking = false ) noexcept;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace eventsystem */
|
||||
} /* namespace wfr */
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace wfr {
|
||||
namespace eventsystem {
|
||||
|
||||
class Event;
|
||||
|
||||
/*
|
||||
* Event listener interface
|
||||
*/
|
||||
class EventListener {
|
||||
public:
|
||||
virtual ~EventListener() {}
|
||||
|
||||
virtual void notifyEvent(const std::shared_ptr<Event>& event) noexcept = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace eventsystem */
|
||||
} /* namespace wfr */
|
||||
Executable
+85
@@ -0,0 +1,85 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <unordered_set>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <wfr/config.h>
|
||||
|
||||
|
||||
namespace wfr {
|
||||
|
||||
// forward declarations
|
||||
class WorkflowRuntime;
|
||||
|
||||
namespace eventsystem {
|
||||
|
||||
// forward declarations
|
||||
class EventListener;
|
||||
class Event;
|
||||
class OStreamLogger;
|
||||
class SQLiteLogger;
|
||||
|
||||
|
||||
class EventSystem {
|
||||
friend class wfr::WorkflowRuntime;
|
||||
|
||||
// Singleton instances
|
||||
private:
|
||||
static EventSystem singleton;
|
||||
#ifdef WFR_ENABLE_STDLOGGER
|
||||
static std::unique_ptr<OStreamLogger> oStreamLogger;
|
||||
#endif
|
||||
#ifdef WFR_ENABLE_PERFDATABASE
|
||||
static std::unique_ptr<SQLiteLogger> sqliteLogger;
|
||||
#endif
|
||||
|
||||
// Non-public parameters
|
||||
private:
|
||||
std::unordered_set<EventListener*> eventListeners;
|
||||
std::queue<std::shared_ptr<Event>> eventQueue;
|
||||
std::unique_ptr<std::thread> eventThread;
|
||||
std::mutex eventThreadMutex;
|
||||
std::condition_variable eventThreadCondVar;
|
||||
bool stopThread = false;
|
||||
std::atomic_bool threadStarted;
|
||||
bool isFinished = false;
|
||||
|
||||
// Non-public static functions
|
||||
private:
|
||||
static void eventThreadFunc(EventSystem* eventSystem) noexcept;
|
||||
|
||||
static void init(int argc, char** argv);
|
||||
static void finish() noexcept;
|
||||
|
||||
// Non-public functions
|
||||
private:
|
||||
EventSystem() noexcept;
|
||||
|
||||
void finishInstance() noexcept;
|
||||
|
||||
// Public functions
|
||||
public:
|
||||
~EventSystem() noexcept;
|
||||
static EventSystem& getInstance() noexcept { return singleton; }
|
||||
|
||||
/**
|
||||
* Adds an event to the event queue.
|
||||
*
|
||||
* It is discouraged to use this function directly.
|
||||
* Use the static fireEvent function found in each Event class instead (e.g. GeneralLoggingEvent::fireEvent(....)).
|
||||
**/
|
||||
void fireEvent(std::shared_ptr<Event> event, bool noLocking = false) noexcept;
|
||||
|
||||
bool addListener(EventListener& listener) noexcept;
|
||||
bool removeListener(EventListener& listener) noexcept;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace eventsystem */
|
||||
} /* namespace wfr */
|
||||
|
||||
Reference in New Issue
Block a user