-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActorComposer.h
197 lines (159 loc) · 5.21 KB
/
ActorComposer.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
#pragma once
#include "Processor.h"
#include "ActorController.h"
#include "Actor.h"
namespace RxActor {
template <typename T>
class ActorComposer
{
private:
const ChannelHandle handle_;
ActorGraph* actorGraph;
public:
ActorComposer(ChannelHandle handle)
: handle_(handle)
{ }
virtual ~ActorComposer()
{ }
class ProcessorComposer;
class ProcessorContextComposer
{
public:
ProcessorContextComposer(ProcessorComposer* composer)
: composer_(composer)
{ }
virtual ~ProcessorContextComposer()
{ }
ProcessorContextComposer* OnPreStartDo(std::function<void()> onPreStart)
{
onPreStart_ = onPreStart;
return this;
}
ProcessorContextComposer* OnPostStopDo(std::function<void()> onPostStop)
{
onPostStop_ = onPostStop;
return this;
}
ProcessorContextComposer* OnPreRestartDo(std::function<void(Exception)> onPreRestart)
{
onPreRestart_ = onPreRestart;
return this;
}
ProcessorContextComposer* OnPostRestartDo(std::function<void(Exception)> onPostRestart)
{
onPostRestart_ = onPostRestart;
return this;
}
ProcessorContextPtr Build()
{
return std::make_shared<ProcessorContextInvoker>(onPreStart_, onPostStop_, onPreRestart_, onPostRestart_);
}
ProcessorComposer* Then()
{
return composer_;
}
private:
std::function<void()> onPreStart_ = [] { };
std::function<void()> onPostStop_ = [] { };
std::function<void(Exception)> onPreRestart_ = [](Exception e) { };
std::function<void(Exception)> onPostRestart_ = [](Exception e) { };
private:
ProcessorComposer* composer_;
};
/**
* Build a std::function<void (T) > processor
*/
class ProcessorComposer
{
public:
ProcessorComposer(ActorComposer<T>* composer)
: composer_(composer)
, contextComposer_(std::make_unique<ProcessorContextComposer>(this))
{ }
virtual ~ProcessorComposer()
{ }
std::unique_ptr<ProcessorContextComposer>& Context()
{
return contextComposer_;
}
/**
* Compose an actor system. An ActorComposer should support:
* - Content-based routing
* - Message filter on content, etc
* - Dynamic router
* - Message transformation
* - Selective consumers (based on routers).
*
* For example: composer.actor(a).routeTo(b).or(c).or(d).criteria(routerFunction);
*/
ActorComposer<T>* Process(const ActorId& actorId, std::function<bool(RxActor::Actor<T>*, T)> processor)
{
// std::shared_ptr<details::Actor<T>> actor = std::make_shared<details::Actor<T>>(
// composer_.handle_,
// actorId,
// std::make_shared<ProcessorInvoker<T>>(
// contextComposer_.Build(),
// processor
// ),
// Supervisor(RxActor::DeciderAlgorithm),
// ActorPolicy{SupervisorPolicy{SupervisorStrategy::AllForOne, Policy::Criterion::All()}, TaskPolicy::Default()}
//
// );
// TODO: composer_->actorGraph->Add(actor);
return composer_;
}
ProcessorComposer* RouteTo(std::function<bool(RxActor::Actor<T>*, T)> router)
{
return this;
}
void Producer(std::function<T ()> producer)
{
// Function that provides input to actor
}
/**
*
* Build lambda collection that works as follows:
*
* input: value
*
* if(condition(value)) A();
* if(condition(value)) B();
* if(condition(value)) C();
*/
// ProcessorComposer* ReceiverWithCondition(std::function<> condition)
// {
// return this;
// }
ProcessorComposer* SupervisorPolicy(int maxNrOfRetries, Duration withinTimeRange)
{
return this;
}
ProcessorComposer* SupervisorConditions(std::function<Policy::ReactionKind(BaseLib::Exception)> condition)
{
// Add to list of conditions
return this;
}
private:
std::unique_ptr<ProcessorContextComposer> contextComposer_;
private:
ActorComposer<T> * composer_;
std::function<bool(RxActor::Actor<T>*, T)> processor_;
std::list<std::function<bool(RxActor::Actor<T>*, T)>> conditions_;
// TODO: List of routers based on conditions?
// TODO: filter list
// TODO: Supervisor strategy builder
};
static std::unique_ptr<ActorComposer<T>> From(ChannelHandle handle)
{
return std::make_unique<ActorComposer>(handle);
}
std::unique_ptr<ProcessorComposer> Processor()
{
return std::make_unique<ProcessorComposer>(this);
}
ActorController<T> Build()
{
return ActorController<T>(actorGraph, new ActorControllerPolicy());
}
};
}