-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRxTaskExecutor.h
103 lines (90 loc) · 2.78 KB
/
RxTaskExecutor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include"DataReactor/IncludeExtLibs.h"
#include"DataReactor/Handler/RxHandler.h"
#include"DataReactor/Subject/StateTriggerSubject.h"
#include"DataReactor/Condition/StateCondition.h"
#include"DataReactor/Export.h"
namespace Reactor
{
/**
* @brief The HandlerPolicy class
*/
class DLL_STATE RxTaskExecutorPolicy
{};
template <typename StateType>
class RxTaskExecutorState
{
public:
RxTaskExecutorState(
typename TransitionHandler<StateType>::Ptr handler,
TransitionCondition::Ptr condition)
: handler_(handler)
, condition_(condition)
{}
virtual ~RxTaskExecutorState()
{}
const StateType& State() const
{
return handler_->To();
}
typename TransitionHandler<StateType>::Ptr Handler() const
{
return handler_;
}
TransitionCondition::Ptr Condition()
{
return condition_;
}
private:
typename TransitionHandler<StateType>::Ptr handler_;
TransitionCondition::Ptr condition_;
};
/**
* Strategy performs the algorithm to "Read data from socket" "Create datagrams" "Deserialize data"
* TriggerCondition performs the check "Is the condition to trigger 'create datagram' met?"
* TriggerCondition holds the GuardCondition and a simple conditional check based on the Return value.
* Return value can be a number "Number of bytes available in buffer" or "Number of datagrams created"
*
* This object is thread safe and "stateless" in the sense that only attached commands are executed,
* hence it can be run by many threads concurrently.
*/
template <typename StateType>
class RxTaskExecutor
: public Runnable
, public StateTriggerSubject<StateType>
, public Templates::ContextObject<RxTaskExecutorPolicy, RxTaskExecutorState<StateType> >
{
public:
RxTaskExecutor(
typename TransitionHandler<StateType>::Ptr handler,
TransitionCondition::Ptr condition = TransitionCondition::Ptr())
: Templates::ContextObject<RxTaskExecutorPolicy, RxTaskExecutorState<StateType> >
(
RxTaskExecutorPolicy(), RxTaskExecutorState<StateType>(handler, condition)
)
{ }
virtual ~RxTaskExecutor()
{ }
CLASS_TRAITS(RxTaskExecutor)
/**
* @brief run
*
* TODO: The executed handler is triggered asynchronously and notifies completion through callbacks.
*/
virtual void run()
{
try
{
this->state().Handler()->run();
if(this->state().Handler()->IsDone())
{
StateTriggerSubject<StateType>::OnComplete(this->state().State());
}
}
catch(BaseLib::GeneralException &exception)
{
StateTriggerSubject<StateType>::OnError(this->state().State(), exception);
}
}
};
}