-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncScheduler.h
220 lines (186 loc) · 5.76 KB
/
SyncScheduler.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
211
212
213
214
215
216
217
218
219
220
#pragma once
#include"DataReactor/IncludeExtLibs.h"
#include"DataReactor/Handler/RxHandler.h"
#include"DataReactor/Condition/StateCondition.h"
#include"DataReactor/Subject/KeyStateTriggerSubject.h"
#include"DataReactor/Export.h"
namespace Reactor
{
class DLL_STATE SyncSchedulerPolicy
{
public:
SyncSchedulerPolicy(Policy::Timeout timeout = Policy::Timeout::FromMilliseconds(2000))
: timeout_(timeout)
{}
virtual ~SyncSchedulerPolicy()
{}
const Policy::Timeout& timeout() const
{
return timeout_;
}
static SyncSchedulerPolicy Default()
{
return SyncSchedulerPolicy();
}
private:
Policy::Timeout timeout_;
};
/**
* @brief The SyncSchedulerState class
*/
template <typename K, typename StateType>
class SyncSchedulerState
{
public:
SyncSchedulerState(typename BaseLib::Templates::EventSelectorMethod<ISet<K>, K>::Ptr selector, StateType state)
: eventSelector_(selector)
, state_(state)
{}
virtual ~SyncSchedulerState()
{}
typename BaseLib::Templates::EventSelectorMethod<ISet<K>, K>::Ptr& Selector()
{
return eventSelector_;
}
const StateType& State() const
{
return state_;
}
RxData::ObjectAccessProxy<K,K> Access()
{
return RxData::ObjectAccessProxy<K,K>(cache(), GetTypeName());
}
typename RxData::ObjectHome<K,K>::Ptr Home()
{
return cache()->template getOrCreateHome<K,K>(GetTypeName());
}
private:
std::string GetTypeName() const
{
return BaseLib::Utility::GetTypeName<K>();
}
RxData::Cache::Ptr cache()
{
return RxData::CacheFactory::Instance().getOrCreateCache(GetTypeName());
}
private:
typename BaseLib::Templates::EventSelectorMethod<ISet<K>, K>::Ptr eventSelector_;
StateType state_;
};
/**
* @brief The SyncScheduler class
*
* TODO:
* - Is it possible to split the implementation of this class to avoid so many template types?
*
* - IDEA: Could the SyncScheduler be a special case of an RxHandler?
* - RxObserver<StateType, TaskResult> where
* - StateType == StateType
* - K = TaskResult
* - EventObserver is the only special type
*
* - Perhaps RxHandler is the base event handler and this SyncScheduler is a long-running RxTriggerHandler or something?
* - A handler - handles events
* - An event listener - trigger events
* - This is an event listener.
* - This is an external event listener, i.e., Network I/O, etc.
*
* Application:
* - SyncSchedulere, i.e., EventSelector, can be an observer to the value K (AbstractSocket::Ptr) populated by socket acceptor
*
*/
template <typename K, typename StateType>
class SyncScheduler
: public TransitionHandler<StateType>
, public KeyStateTriggerSubject<K, StateType>
, public RxData::ObjectObserver<K>
, public Templates::ContextObject<SyncSchedulerPolicy, SyncSchedulerState<K, StateType> >
{
public:
SyncScheduler(typename Templates::EventSelectorMethod<ISet<K>, K>::Ptr eventSelector,
StateType state,
SyncSchedulerPolicy policy = SyncSchedulerPolicy::Default())
: Templates::ContextObject<SyncSchedulerPolicy, SyncSchedulerState<K, StateType> >
(
policy,
SyncSchedulerState<K, StateType>(eventSelector, state)
)
{
this->state().Home()->Connect(this);
}
virtual ~SyncScheduler()
{
this->state().Home()->Disconnect(this);
}
CLASS_TRAITS(SyncScheduler)
// --------------------------------------------------------
// Interface RxHandler<StateType>
// --------------------------------------------------------
virtual void run()
{
// --------------------------------------------------------
// TODO: Move this to a Command like "LongRunningCommand"?
// IDEA: Use CommandController - links up with rx scheduler
// Attach command with "long running task" qos policy.
// Use a SyncCommandController to trigger the individual event listeners based on i/o "ready events"
// --------------------------------------------------------
try
{
ISet<K> events = this->state().Selector()->Select(this->config().timeout().InMillis());
triggerEvents(events);
}
catch(BaseLib::GeneralException exception)
{
KeyStateTriggerSubject<K,StateType>::OnError(this->state().State(), exception);
}
}
virtual bool Execute(RxThreadPool::Ptr )
{
return false; //this->GetState().Executor()->Execute(threadPool);
}
virtual bool Init(RxThreadPool::Ptr )
{
//this->GetState().Executor()->Connect(threadPool.get());
return true;
}
virtual const StateType& To() const
{
return this->state().State();
}
virtual bool IsTriggered() const
{
return true;
}
virtual bool IsDone() const
{
return false;
}
// --------------------------------------------------------
// Interface ObjectObserver<K>
// --------------------------------------------------------
virtual bool OnObjectCreated(K )
{
// Populate selector?
return true;
}
virtual bool OnObjectDeleted(K )
{
// Remove from selector
return true;
}
virtual bool OnObjectModified(K )
{
// Update selector
return true;
}
private:
void triggerEvents(const ISet<K> &events)
{
if(events.empty()) return;
for(typename ISet<K>::const_iterator it = events.begin(), it_end = events.end(); it != it_end; ++it)
{
KeyStateTriggerSubject<K,StateType>::OnTriggerNext(*it, this->state().State());
}
}
};
}