MakerLabs ACM
actor_model.h
Go to the documentation of this file.
1 /*
2  * Copyright Paul Reimer, 2018
3  *
4  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Unported License.
5  * To view a copy of this license, visit
6  * https://creativecommons.org/licenses/by-nc-sa/4.0/
7  * or send a letter to
8  * Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
9  */
10 
11 #pragma once
12 
13 #include "actor.h"
14 #include "node.h"
15 #include "process.h"
16 
17 #include "actor_model_generated.h"
18 
19 #include <experimental/string_view>
20 
21 namespace ActorModel {
22 
23 using MessageFlatbuffer = flatbuffers::DetachedBuffer;
24 using MutableFlatbuffer = std::vector<uint8_t>;
25 
26 // free functions bound to default node
27 
28 // Generic behaviour convenience functions
29 auto spawn(
30  const Behaviour&& _behaviour,
31  const ExecConfigCallback&& _exec_config_callback = nullptr
32 ) -> Pid;
33 
34 auto spawn_link(
35  const Pid& _initial_link_pid,
36  const Behaviour&& _behaviour,
37  const ExecConfigCallback&& _exec_config_callback = nullptr
38 ) -> Pid;
39 
40 auto process_flag(
41  const Pid& pid,
42  const ProcessFlag flag,
43  const bool flag_setting
44 ) -> bool;
45 
46 auto send(
47  const Pid& pid,
48  const Message& message
49 ) -> bool;
50 
51 auto send(
52  const Pid& pid,
54  const std::experimental::string_view payload = ""
55 ) -> bool;
56 
57 auto send(
58  const Pid& pid,
60  const std::vector<uint8_t>& payload_vec
61 ) -> bool;
62 
63 auto send(
64  const Pid& pid,
66  const flatbuffers::Vector<uint8_t>& payload_fbvec
67 ) -> bool;
68 
69 auto send(
70  const Pid& pid,
72  const MessageFlatbuffer& payload_flatbuffer
73 ) -> bool;
74 
75 auto send_after(
76  const Time time,
77  const Pid& pid,
78  const Message& message
79 ) -> TRef;
80 
81 auto send_after(
82  const Time time,
83  const Pid& pid,
85  const std::experimental::string_view payload = ""
86 ) -> TRef;
87 
88 auto send_after(
89  const Time time,
90  const Pid& pid,
92  const std::vector<uint8_t>& payload_vec
93 ) -> TRef;
94 
95 auto send_after(
96  const Time time,
97  const Pid& pid,
99  const MessageFlatbuffer& payload_flatbuffer
100 ) -> TRef;
101 
102 auto send_interval(
103  const Time time,
104  const Pid& pid,
105  const Message& message
106 ) -> TRef;
107 
108 auto send_interval(
109  const Time time,
110  const Pid& pid,
112  const std::experimental::string_view payload = ""
113 ) -> TRef;
114 
115 auto send_interval(
116  const Time time,
117  const Pid& pid,
119  const std::vector<uint8_t>& payload_vec
120 ) -> TRef;
121 
122 auto send_interval(
123  const Time time,
124  const Pid& pid,
126  const MessageFlatbuffer& payload_flatbuffer
127 ) -> TRef;
128 
129 auto cancel(const TRef tref)
130  -> bool;
131 
132 auto exit(const Pid& pid, const Pid& pid2, const Process::Reason exit_reason)
133  -> bool;
134 
135 auto register_name(const std::experimental::string_view name, const Pid& pid)
136  -> bool;
137 
139  -> bool;
140 
141 auto registered()
143 
145  -> MaybePid;
146 
147 auto module(const std::experimental::string_view module_flatbuffer)
148  -> bool;
149 
150 auto apply(
151  const Pid& pid,
152  const std::experimental::string_view function_name,
154 ) -> ResultUnion;
155 
156 auto apply(
157  const Pid& pid,
158  const std::experimental::string_view module_name,
159  const std::experimental::string_view function_name,
161 ) -> ResultUnion;
162 
163 inline
164 auto matches(
165  const ActorModel::Message& message
166 ) -> bool
167 {
168  return true;
169 }
170 
171 inline
172 auto matches(
173  const ActorModel::Message& message,
175 ) -> bool
176 {
177  return (message.type() and message.type()->string_view() == type);
178 }
179 
180 inline
181 auto matches(
182  const ActorModel::Message& message,
185 ) -> bool
186 {
187  if (matches(message, type) and (message.payload()->size() > 0))
188  {
190  reinterpret_cast<const char*>(message.payload()->data()),
191  message.payload()->size()
192  };
193 
194  return true;
195  }
196 
197  return false;
198 }
199 
200 inline
201 auto matches(
202  const ActorModel::Message& message,
204  std::string& payload
205 ) -> bool
206 {
207  if (matches(message, type) and (message.payload()->size() > 0))
208  {
209  payload.assign(
210  reinterpret_cast<const char*>(message.payload()->data()),
211  message.payload()->size()
212  );
213 
214  return true;
215  }
216 
217  return false;
218 }
219 
220 inline
221 auto matches(
222  const ActorModel::Message& message,
224  MutableFlatbuffer& payload
225 ) -> bool
226 {
227  if (matches(message, type) and (message.payload()->size() > 0))
228  {
229  payload.assign(
230  message.payload()->data(),
231  (message.payload()->data() + message.payload()->size())
232  );
233 
234  return true;
235  }
236 
237  return false;
238 }
239 
240 template<typename TableT>
241 inline
242 auto matches(
243  const ActorModel::Message& message,
245  const TableT*& payload_ptr
246 ) -> bool
247 {
248  if (matches(message, type) and (message.payload()->size() > 0))
249  {
250  flatbuffers::Verifier verifier(
251  message.payload()->data(),
252  message.payload()->size()
253  );
254  auto verified = verifier.VerifyBuffer<TableT>(nullptr);
255 
256  if (verified)
257  {
258  auto fb = flatbuffers::GetRoot<TableT>(
259  message.payload()->data()
260  );
261 
262  if (fb)
263  {
264  payload_ptr = fb;
265  return true;
266  }
267  }
268  }
269 
270  return false;
271 }
272 
273 template<typename TableObjT, class /* SFINAE */ = typename TableObjT::TableType>
274 inline
275 auto matches(
276  const ActorModel::Message& message,
278  TableObjT& obj
279 ) -> bool
280 {
281  if (matches(message, type) and (message.payload()->size() > 0))
282  {
283  flatbuffers::Verifier verifier(
284  message.payload()->data(),
285  message.payload()->size()
286  );
287  auto verified = (
288  verifier.VerifyBuffer<typename TableObjT::TableType>(nullptr)
289  );
290 
291  if (verified)
292  {
293  auto fb = flatbuffers::GetRoot<typename TableObjT::TableType>(
294  message.payload()->data()
295  );
296 
297  if (fb)
298  {
299  fb->UnPackTo(&obj);
300  return true;
301  }
302  }
303  }
304 
305  return false;
306 }
307 
308 } // namespace ActorModel
file_extension fb
Definition: firmware_update.fbs:29
auto send(const Pid &pid, const Message &message) -> bool
Definition: actor_model.cpp:58
delegate< void(ProcessExecutionConfigBuilder &)> ExecConfigCallback
Definition: node.h:30
std::experimental::string_view Reason
Definition: process.h:43
auto register_name(const string_view name, const Pid &pid) -> bool
Definition: actor_model.cpp:232
Definition: behaviour.h:30
auto unregister(const string_view name) -> bool
Definition: actor_model.cpp:239
root_type Message
Definition: actor_model.fbs:125
auto spawn(const ActorBehaviour &&_actor_behaviour, const ExecConfigCallback &&_exec_config_callback) -> Pid
Definition: actor.cpp:27
Definition: actor.cpp:17
size_t TRef
Definition: node.h:33
std::experimental::optional< Pid > MaybePid
Definition: pid.h:20
auto matches(const ActorModel::Message &message) -> bool
Definition: actor_model.h:164
auto module(const std::experimental::string_view module_flatbuffer) -> bool
Definition: actor_model.cpp:267
auto spawn_link(const Pid &_initial_link_pid, const ActorBehaviour &&_actor_behaviour, const ExecConfigCallback &&_exec_config_callback) -> Pid
Definition: actor.cpp:39
flatbuffers::DetachedBuffer MessageFlatbuffer
Definition: actor_model.h:23
Definition: actor_model.fbs:27
std::string string
Definition: oom_killer_actor_behaviour.cpp:24
std::experimental::string_view string_view
Definition: actor_model.cpp:16
ProcessFlag
Definition: actor_model.fbs:22
auto apply(const Pid &pid, const std::experimental::string_view function_name, const std::experimental::string_view args) -> ResultUnion
Definition: actor_model.cpp:274
auto process_flag(const Pid &pid, const ProcessFlag flag, const bool flag_setting) -> bool
Definition: actor_model.cpp:48
UUID::UUID Pid
Definition: pid.h:19
std::chrono::milliseconds Time
Definition: node.h:31
auto exit(const Pid &pid, const Pid &pid2, const Reason exit_reason) -> bool
Definition: actor_model.cpp:260
delegate< ResultUnion(const Pid &, Mailbox &)> Behaviour
Definition: behaviour.h:80
auto send_interval(const Time time, const Pid &pid, const Message &message) -> TRef
Definition: actor_model.cpp:173
auto send_after(const Time time, const Pid &pid, const Message &message) -> TRef
Definition: actor_model.cpp:121
std::unordered_map< string, Pid > NamedProcessRegistry
Definition: node.h:86
auto registered() -> const Node::NamedProcessRegistry
Definition: actor_model.cpp:246
std::vector< uint8_t > MutableFlatbuffer
Definition: actor_model.h:24
auto cancel(const TRef tref) -> bool
Definition: actor_model.cpp:225
auto whereis(const string_view name) -> MaybePid
Definition: actor_model.cpp:253