-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandController.h
210 lines (155 loc) · 5.97 KB
/
CommandController.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#pragma once
#include"RxCommand/IncludeExtLibs.h"
#include"RxCommand/CommandSubscription.h"
#include"RxCommand/CommandBase.h"
#include"RxCommand/CommandControllerBase.h"
#include"RxCommand/Commands.h"
#include"RxCommand/Command.h"
#include"RxCommand/Strategy/ComputationStrategy.h"
#include"RxCommand/CommandPolicy.h"
#include"RxCommand/CommandControllerPolicy.h"
#include"RxCommand/Export.h"
namespace Reactor { namespace details
{
// ---------------------------------------------------------------
// CommandControllerState
// ---------------------------------------------------------------
class DLL_STATE CommandControllerState
{
public:
CommandControllerState() {}
CommandControllerState(RxThreadPool::Ptr threadPool);
virtual ~CommandControllerState();
const CommandsGroup& commands() const;
CommandsGroup& commands();
const CommandsGroup& fallback() const;
CommandsGroup& fallback();
bool SetCommands(const CommandsGroup &commands);
bool SetFallback(const CommandsGroup &commands);
RxThreadPool::Ptr ControllerPool() const;
RxThreadPool::Ptr CommandPool() const;
bool SetCommandPool(RxThreadPool::Ptr commandPool);
const Templates::BooleanTrigger& Trigger() const;
Templates::BooleanTrigger& Trigger();
Mutex &schedulingMutex() {
return schedulingMutex_;
}
private:
RxThreadPool::Ptr controllerPool_;
RxThreadPool::Ptr commandPool_;
CommandsGroup commands_;
CommandsGroup fallback_;
Templates::BooleanTrigger triggered_;
Mutex schedulingMutex_;
};
// ---------------------------------------------------------------
// CommandController
// ---------------------------------------------------------------
class DLL_STATE CommandController
: public Reactor::CommandController
, public Templates::LockableType<CommandController>
, protected Templates::ContextObjectShared<CommandControllerPolicy, CommandControllerState, Status::ExecutionStatus>
, public ENABLE_SHARED_FROM_THIS(CommandController)
{
private:
static const int64 ACQUIRE_LOCK_TIMEOUT_IN_MS = 5000;
public:
CommandController(CommandControllerPolicy policy, RxThreadPool::Ptr threadPool);
virtual ~CommandController();
CLASS_TRAITS(CommandController)
CommandController::Ptr GetPtr();
// ---------------------------------------------------------------
// Interface Runnable
// ---------------------------------------------------------------
virtual void run();
virtual std::string GetName() const;
// ---------------------------------------------------------------
// Interface CommandControllerBase
// ---------------------------------------------------------------
virtual bool SetCommandThreadPool(RxThreadPool::Ptr commandThreadPool);
virtual bool SetCommands(const CommandsGroup &commands);
virtual bool SetFallback(const CommandsGroup &commands);
virtual bool AddCommand(CommandBase::Ptr command);
virtual bool AddFallback(CommandBase::Ptr command);
virtual CommandsGroup GetCommands() const;
virtual CommandsGroup GetFallback() const;
virtual bool Shutdown();
virtual bool Cancel();
virtual void Resume();
virtual void Suspend();
virtual bool Interrupt();
virtual void Trigger();
virtual bool Reset();
virtual bool IsDone() const;
virtual bool IsSuccess() const;
virtual bool IsExecuting() const;
virtual bool IsCancelled() const;
virtual bool IsTimeout() const;
virtual bool IsInterrupted() const;
virtual bool IsSuspended() const;
virtual bool IsPolicyViolated() const;
virtual bool AreCommandsExecuting() const;
virtual int NumCommandsExecuting() const;
virtual Duration TimeoutIn() const;
virtual Status::ExecutionStatus& Status();
virtual const Status::ExecutionStatus& StatusConst() const;
virtual IList<CommandBase::Ptr> Schedule();
protected:
// -----------------------------------------------------------
// Private implementation
// -----------------------------------------------------------
void reset();
void stopScheduling();
void scheduleController();
bool nextGroupExecution();
IList<CommandBase::Ptr> scheduleFromRun();
IList<CommandBase::Ptr> schedule();
/**
* Execute all commands that can be executed according to policies
*/
IList<CommandBase::Ptr> scheduleReady();
/**
* Execute the commands if they are not currently executing
* Note! "Not executing" should be enforced by computation strategies.
*/
IList<CommandBase::Ptr> executeCommands(const IList<CommandBase::Ptr> &commands);
/**
* Execute templated command
*/
virtual bool executeCommand(CommandBase::Ptr command) = 0;
/**
* Compute max wait time based on timeout and interval policy: "The time when a command should be done with the current execution and is ready for another execution".
*/
Duration computeMaxWaitTimeMs() const;
/**
* Check if controller has timed out according to policy, i.e., no more time left to finish all commands.
*/
bool isTimeout() const;
/**
* Check if commands group is done.
*/
bool isCommandGroupDone() const;
/**
* Check if controller is cancelled, timed out, or if there are any more commands left executing.
*/
bool isDoneForever() const;
/**
* Check if commands are successfully executed.
*/
bool isSuccess() const;
/**
* Controller times out, stop all commands.
*/
void doTimeout();
/**
* Cancel all attached commands and set execution state.
*/
bool shutdown();
// -----------------------------------------------------------
// private functions to set state, perform callbacks, etc
// -----------------------------------------------------------
virtual void finished() = 0;
virtual void finishedWithError(GeneralException exception) = 0;
virtual void finishedWithFatalError(GeneralException exception) = 0;
};
}}