-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubject.cpp
More file actions
347 lines (275 loc) · 8.2 KB
/
Copy pathSubject.cpp
File metadata and controls
347 lines (275 loc) · 8.2 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include"RxObserver/Subject.h"
#include"RxObserver/FlowControlStrategy.h"
namespace BaseLib { namespace Concurrent { namespace details
{
// ----------------------------------------------------
// SubjectState
// ----------------------------------------------------
SubjectState::SubjectState()
: flowController_(new FlowControllerAction<Event::Ptr>(FlowControllerPolicy::Default()))
, observers_(new Concurrent::Observers())
, status_()
, filters_()
, subjectId_("default-id")
{}
SubjectState::SubjectState(SubjectId id, FlowControllerPolicy policy)
: flowController_(new FlowControllerAction<Event::Ptr>(policy))
, observers_(new Concurrent::Observers())
, status_()
, filters_()
, subjectId_(id)
{ }
SubjectState::~SubjectState()
{ }
Observers::Ptr SubjectState::Observers() const
{
return observers_;
}
Concurrent::FlowController<Event::Ptr>::Ptr SubjectState::Controller() const
{
return flowController_;
}
Status::SubjectStatus &SubjectState::Status()
{
return status_;
}
const Status::SubjectStatus &SubjectState::Status() const
{
return status_;
}
FilterList &SubjectState::Filters()
{
return filters_;
}
const FilterList &SubjectState::Filters() const
{
return filters_;
}
const SubjectId &SubjectState::Id() const
{
return subjectId_;
}
bool SubjectState::IsInitialized() const
{
return status_.IsActive();
}
void SubjectState::Initialize(Templates::Action0<bool>::Ptr action)
{
bool isAttached = flowController_->Set(action);
if(isAttached)
status_.Activate();
}
// ----------------------------------------------------
// SubjectBase
// ----------------------------------------------------
SubjectBase::SubjectBase(SubjectId id, SubjectPolicy policy)
: Templates::ContextObject<SubjectPolicy, SubjectState>
(
policy,
SubjectState(id, policy.FlowPolicy())
)
{ }
SubjectBase::~SubjectBase()
{ }
SubjectBase::Ptr SubjectBase::GetPtr()
{
return shared_from_this();
}
// ---------------------------------------------------------
// Subject interface
// ---------------------------------------------------------
Subscription::Ptr SubjectBase::Subscribe(std::shared_ptr<ObserverAction> observer)
{
this->state().Observers()->Add(observer);
if(this->config().Replay().IsOnSubscribe() || observer->Policy().Replay().IsOnSubscribe())
{
replayHistoryToObserver(observer, this->state().Controller()->History());
}
observer->SetSubject(this->GetPtr());
return Subscription::Ptr( new Subscription(observer) );
}
bool SubjectBase::Unsubscribe(std::shared_ptr<ObserverAction> observer)
{
return this->state().Observers()->Remove(observer);
}
bool SubjectBase::Block()
{
return this->state().Status().Block();
}
bool SubjectBase::Unblock()
{
return this->state().Status().Unblock();
}
bool SubjectBase::IsBlocked() const
{
return this->state().Status().IsBlocked();
}
bool SubjectBase::IsActive() const
{
return this->state().Status().IsActive();
}
bool SubjectBase::IsEmpty() const
{
return this->state().Observers()->IsEmpty();
}
int SubjectBase::Size() const
{
return this->state().Observers()->Size();
}
bool SubjectBase::UnsubscribeAll()
{
return this->state().Observers()->Unsubscribe();
}
SubjectPolicy SubjectBase::Policy() const
{
return this->config();
}
const Status::SubjectStatus &SubjectBase::StatusConst() const
{
return this->state().Status();
}
SubjectId SubjectBase::Id() const
{
return this->state().Id();
}
// ---------------------------------------------------------
// Reactor methods
// ---------------------------------------------------------
Event::Ptr SubjectBase::Next(Event::Ptr event)
{
bool initialize = Templates::DoubleCheckedLocking::Initialize<SubjectBase>(this);
if(!initialize) return Event::Ptr(); //EventNoOp::Create();
// ----------------------------------------
// Apply ontent filter on entrance
// ----------------------------------------
if(Filter(event))
{
event->Filter();
OnFilteredOut(event);
}
else
{
bool isAdded = this->state().Controller()->Next(event);
if(!isAdded)
{
event->Reject();
OnFilteredOut(event);
}
else if(this->config().Invocation().IsSync())
{
event->WaitFor(this->config().Timeout().AsDuration().InMillis());
}
}
return event;
}
bool SubjectBase::Complete()
{
return this->state().Observers()->Complete() && this->state().Controller()->Complete();
}
bool SubjectBase::Error(Exception exception)
{
this->Cancel();
this->state().Observers()->Error(exception);
return this->state().Controller()->Error(exception);
}
// ---------------------------------------------------------
// Support pull model interaction
// ---------------------------------------------------------
Events SubjectBase::History() const
{
return this->state().Controller()->History();
}
bool SubjectBase::Submit(Event::Ptr event)
{
Events events;
events.push_back(event);
return Submit(events);
}
bool SubjectBase::Submit(Events events)
{
if(this->IsBlocked()) return false;
if(this->state().Observers()->IsEmpty()) {
IWARNING() << "No observers!";
return false;
}
// -----------------------------
// Apply timebased filter
// -----------------------------
Events filteredEvents = Strategy::FilterEventsOnTime::Instance().Perform(events, this->config().MinSeparation());
if(!filteredEvents.empty())
{
for(Concurrent::Event::Ptr event : filteredEvents)
event->Filter();
OnFilteredOut(filteredEvents);
}
//EventResults results = this->state().Observers()->Next(events);
IINFO() << "Submitting " << events.size() << " events to " << this->state().Observers()->Size() << " observers";
// -----------------------------
// Push to observers
// -----------------------------
EventResults results = Strategy::PlayEventsToObservers::Instance().Perform(
this->state().Observers(),
events,
this->config().Computation(),
this->config().Timeout());
// TODO: Anything to do with results? Statistics?
return true;
}
// ---------------------------------------------------------
// Invoke is called from flow controller
// ---------------------------------------------------------
bool SubjectBase::Invoke()
{
if(this->IsBlocked()) return false;
if(this->state().Observers()->IsEmpty()) {
IWARNING() << "No observers!";
return false;
}
Events events = this->state().Controller()->Pull();
if(events.empty()) return false;
return Submit(events);
}
bool SubjectBase::Cancel()
{
return this->state().Status().Deactivate();
}
bool SubjectBase::IsDone() const
{
return !this->state().Status().IsActive();
}
bool SubjectBase::IsSuccess() const
{
return true;
}
// ---------------------------------------------------------
// Initialize methods
// ---------------------------------------------------------
void SubjectBase::Initialize()
{
this->state().Initialize(this->GetPtr());
}
bool SubjectBase::IsInitialized() const
{
return this->state().IsInitialized();
}
// ---------------------------------------------------------
// private implementation
// ---------------------------------------------------------
bool SubjectBase::replayHistoryToObserver(std::shared_ptr<ObserverAction> observer, Events events)
{
if(events.empty()) return false;
Concurrent::Observers::Ptr observerGroup(new Concurrent::Observers());
observerGroup->Add(observer);
FutureTask<EventResults>::Ptr replayTask = Strategy::PlayEventsToObservers::Create(
Strategy::PlayEventsToObservers::InstancePtr(),
observerGroup,
events,
this->config().Computation(),
this->config().Timeout());
ThreadPool::Ptr pool = Concurrent::ThreadPoolFactory::Instance().GetDefault();
//ScheduledFutureTask<EventResults> scheduledFuture =
pool->Schedule<EventResults>(replayTask, TaskPolicy::RunOnceImmediately());
//scheduledFuture.WaitFor(this->config().policy().Timeout().TimeSinceEpoch());
return !events.empty();
}
}}}