Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
23de48fc
Commit
23de48fc
authored
Aug 06, 2023
by
Samir Halilcevic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add example for caf::after
parent
cc7f3496
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
examples/CMakeLists.txt
examples/CMakeLists.txt
+1
-0
examples/message_passing/after.cpp
examples/message_passing/after.cpp
+59
-0
No files found.
examples/CMakeLists.txt
View file @
23de48fc
...
...
@@ -24,6 +24,7 @@ add_core_example(. hello_world)
add_core_example
(
config read-json
)
# basic message passing primitives
add_core_example
(
message_passing after
)
add_core_example
(
message_passing calculator
)
add_core_example
(
message_passing cell
)
add_core_example
(
message_passing dancing_kirby
)
...
...
examples/message_passing/after.cpp
0 → 100644
View file @
23de48fc
// This example shows how to use caf::after
#include "caf/all.hpp"
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
using
std
::
cout
;
using
std
::
endl
;
using
namespace
caf
;
// Sends random caracters to buddy, and then waits for a letter back
behavior
noisy_buddy
(
event_based_actor
*
self
,
actor
buddy
)
{
using
namespace
std
::
chrono_literals
;
std
::
random_device
rd
;
std
::
minstd_rand
gen
{
rd
()};
const
auto
count
=
std
::
uniform_int_distribution
<>
(
20
,
100
)(
gen
);
std
::
uniform_int_distribution
<>
dis
(
33
,
126
);
for
(
auto
i
=
0
;
i
<
count
;
i
++
)
{
self
->
send
(
buddy
,
static_cast
<
char
>
(
dis
(
gen
)));
}
return
{[
self
](
std
::
string
letter
)
{
cout
<<
"Received a letter:"
<<
endl
<<
letter
<<
endl
;
self
->
quit
();
}};
}
struct
aggregator_state
{
std
::
string
letter
;
caf
::
actor
dest
;
};
// Aggregates incoming characters and stores the sender, replies with the
// reversed string when inactive for 100ms
behavior
aggregator
(
stateful_actor
<
aggregator_state
>*
self
)
{
using
namespace
std
::
chrono_literals
;
return
{
[
=
](
char
c
)
mutable
{
self
->
state
.
dest
=
caf
::
actor_cast
<
caf
::
actor
>
(
self
->
current_sender
());
self
->
state
.
letter
.
push_back
(
c
);
},
// trigger if we dont receive a message for 100ms
caf
::
after
(
100ms
)
>>
[
=
]
{
std
::
reverse
(
self
->
state
.
letter
.
begin
(),
self
->
state
.
letter
.
end
());
self
->
send
(
self
->
state
.
dest
,
self
->
state
.
letter
);
cout
<<
"bye"
<<
endl
;
self
->
quit
();
}};
}
void
caf_main
(
actor_system
&
system
)
{
auto
agg
=
system
.
spawn
(
aggregator
);
system
.
spawn
(
noisy_buddy
,
agg
);
}
CAF_MAIN
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment