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
44c32c26
Commit
44c32c26
authored
Aug 09, 2023
by
Samir Halilcevic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Integrate review feedback
parent
c2cf530b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
13 deletions
+17
-13
examples/message_passing/after.cpp
examples/message_passing/after.cpp
+17
-13
No files found.
examples/message_passing/after.cpp
View file @
44c32c26
// This example shows how to use caf::after
// This example shows how to use caf::after
#include "caf/all.hpp"
#include "caf/after.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
#include <chrono>
#include <chrono>
#include <iostream>
#include <iostream>
#include <random>
#include <random>
#include <string>
using
std
::
cout
;
using
std
::
cout
;
using
std
::
endl
;
using
std
::
endl
;
...
@@ -12,32 +17,31 @@ using std::endl;
...
@@ -12,32 +17,31 @@ using std::endl;
using
namespace
caf
;
using
namespace
caf
;
// Sends a random number of printable characters to buddy and exits
// Sends a random number of printable characters to buddy and exits
void
generator
(
event_based_actor
*
self
,
actor
buddy
)
{
void
generator
(
event_based_actor
*
self
,
actor
sink
)
{
std
::
random_device
rd
;
std
::
random_device
rd
;
std
::
minstd_rand
gen
{
rd
()};
std
::
minstd_rand
gen
{
rd
()};
const
auto
count
=
std
::
uniform_int_distribution
<>
{
20
,
100
}(
gen
);
const
auto
count
=
std
::
uniform_int_distribution
<>
{
20
,
100
}(
gen
);
std
::
uniform_int_distribution
<>
dis
{
33
,
126
};
std
::
uniform_int_distribution
<>
dis
{
33
,
126
};
for
(
auto
i
=
0
;
i
<
count
;
i
++
)
{
for
(
auto
i
=
0
;
i
<
count
;
i
++
)
{
self
->
send
(
buddy
,
static_cast
<
char
>
(
dis
(
gen
)));
self
->
send
(
sink
,
static_cast
<
char
>
(
dis
(
gen
)));
}
}
}
}
// Collects the incoming characters until
either the awaited_size of characters
// Collects the incoming characters until
no new characters arrive for 500ms.
//
is received, or no new characters arrive for 100ms
//
Prints every 60 characters.
behavior
collector
(
stateful_actor
<
std
::
string
>*
self
,
size_t
awaited_size
)
{
behavior
collector
(
stateful_actor
<
std
::
string
>*
self
)
{
using
namespace
std
::
chrono_literals
;
using
namespace
std
::
chrono_literals
;
self
->
state
.
reserve
(
awaited_size
);
return
{
return
{
[
=
](
char
c
)
{
[
self
](
char
c
)
{
self
->
state
.
push_back
(
c
);
self
->
state
.
push_back
(
c
);
if
(
self
->
state
.
size
()
==
awaited_size
)
{
constexpr
auto
flush_threshold
=
60
;
if
(
self
->
state
.
size
()
==
flush_threshold
)
{
cout
<<
"Received message length: "
<<
self
->
state
.
size
()
<<
endl
cout
<<
"Received message length: "
<<
self
->
state
.
size
()
<<
endl
<<
"Message content: "
<<
self
->
state
<<
endl
;
<<
"Message content: "
<<
self
->
state
<<
endl
;
self
->
quit
();
self
->
state
.
clear
();
}
}
},
},
// trigger if we dont receive a message for 100ms
caf
::
after
(
500ms
)
>>
caf
::
after
(
100ms
)
>>
[
self
]()
{
[
self
]()
{
cout
<<
"Timeout reached!"
<<
endl
;
cout
<<
"Timeout reached!"
<<
endl
;
if
(
!
self
->
state
.
empty
())
{
if
(
!
self
->
state
.
empty
())
{
...
@@ -50,7 +54,7 @@ behavior collector(stateful_actor<std::string>* self, size_t awaited_size) {
...
@@ -50,7 +54,7 @@ behavior collector(stateful_actor<std::string>* self, size_t awaited_size) {
}
}
void
caf_main
(
actor_system
&
system
)
{
void
caf_main
(
actor_system
&
system
)
{
auto
col
=
system
.
spawn
(
collector
,
60
);
auto
col
=
system
.
spawn
(
collector
);
system
.
spawn
(
generator
,
col
);
system
.
spawn
(
generator
,
col
);
}
}
...
...
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