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
6eb61e5a
Commit
6eb61e5a
authored
Sep 02, 2019
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add scheduled_send: sending with absolute timeout
parent
c6904641
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
80 deletions
+87
-80
libcaf_core/caf/mixin/sender.hpp
libcaf_core/caf/mixin/sender.hpp
+55
-67
libcaf_core/test/sender.cpp
libcaf_core/test/sender.cpp
+32
-13
No files found.
libcaf_core/caf/mixin/sender.hpp
View file @
6eb61e5a
...
@@ -60,24 +60,8 @@ public:
...
@@ -60,24 +60,8 @@ public:
template
<
message_priority
P
=
message_priority
::
normal
,
template
<
message_priority
P
=
message_priority
::
normal
,
class
Dest
=
actor
,
class
...
Ts
>
class
Dest
=
actor
,
class
...
Ts
>
void
send
(
const
Dest
&
dest
,
Ts
&&
...
xs
)
{
void
send
(
const
Dest
&
dest
,
Ts
&&
...
xs
)
{
using
detail
::
type_list
;
detail
::
type_list
<
detail
::
strip_and_convert_t
<
Ts
>
...
>
args_token
;
static_assert
(
sizeof
...(
Ts
)
>
0
,
"no message to send"
);
type_check
(
dest
,
args_token
);
using
res_t
=
response_type
<
signatures_of_t
<
Dest
>
,
detail
::
implicit_conversions_t
<
typename
std
::
decay
<
Ts
>::
type
>
...
>
;
static_assert
(
!
statically_typed
<
Subtype
>
()
||
statically_typed
<
Dest
>
(),
"statically typed actors can only send() to other "
"statically typed actors; use anon_send() or request() when "
"communicating with dynamically typed actors"
);
static_assert
(
res_t
::
valid
,
"receiver does not accept given message"
);
static_assert
(
is_void_response
<
typename
res_t
::
type
>::
value
||
response_type_unbox
<
signatures_of_t
<
Subtype
>
,
typename
res_t
::
type
>::
valid
,
"this actor does not accept the response message"
);
if
(
dest
)
if
(
dest
)
dest
->
eq_impl
(
make_message_id
(
P
),
dptr
()
->
ctrl
(),
dest
->
eq_impl
(
make_message_id
(
P
),
dptr
()
->
ctrl
(),
dptr
()
->
context
(),
std
::
forward
<
Ts
>
(
xs
)...);
dptr
()
->
context
(),
std
::
forward
<
Ts
>
(
xs
)...);
...
@@ -100,63 +84,51 @@ public:
...
@@ -100,63 +84,51 @@ public:
template
<
message_priority
P
=
message_priority
::
normal
,
template
<
message_priority
P
=
message_priority
::
normal
,
class
Dest
=
actor
,
class
...
Ts
>
class
Dest
=
actor
,
class
...
Ts
>
void
anon_send
(
const
Dest
&
dest
,
Ts
&&
...
xs
)
{
void
anon_send
(
const
Dest
&
dest
,
Ts
&&
...
xs
)
{
static_assert
(
sizeof
...(
Ts
)
>
0
,
"no message to send"
);
caf
::
anon_send
(
dest
,
std
::
forward
<
Ts
>
(
xs
)...);
using
token
=
}
detail
::
type_list
<
typename
detail
::
implicit_conversions
<
/// Sends a message after an absolute timeout.
typename
std
::
decay
<
Ts
>::
type
template
<
message_priority
P
=
message_priority
::
normal
,
class
Dest
=
actor
,
>::
type
...
>
;
class
...
Ts
>
static_assert
(
response_type_unbox
<
detail
::
enable_if_t
<!
std
::
is_same
<
Dest
,
group
>::
value
>
signatures_of_t
<
Dest
>
,
scheduled_send
(
const
Dest
&
dest
,
actor_clock
::
time_point
timeout
,
token
Ts
&&
...
xs
)
{
>::
valid
,
detail
::
type_list
<
detail
::
strip_and_convert_t
<
Ts
>
...
>
args_token
;
"receiver does not accept given message"
);
type_check
(
dest
,
args_token
);
if
(
dest
)
if
(
dest
)
{
dest
->
eq_impl
(
make_message_id
(
P
),
nullptr
,
auto
&
clock
=
dptr
()
->
system
().
clock
();
dptr
()
->
context
(),
std
::
forward
<
Ts
>
(
xs
)...);
clock
.
schedule_message
(
timeout
,
actor_cast
<
strong_actor_ptr
>
(
dest
),
make_mailbox_element
(
dptr
()
->
ctrl
(),
make_message_id
(
P
),
no_stages
,
std
::
forward
<
Ts
>
(
xs
)...));
}
}
}
/// Sends a message after an absolute timeout. Sends the message immediately
/// if the timeout has already past.
template
<
class
...
Ts
>
void
scheduled_send
(
const
group
&
dest
,
actor_clock
::
time_point
timeout
,
Ts
&&
...
xs
)
{
static_assert
(
!
statically_typed
<
Subtype
>
(),
"statically typed actors are not allowed to send to groups"
);
if
(
dest
)
{
auto
&
clock
=
dptr
()
->
system
().
clock
();
clock
.
schedule_message
(
timeout
,
dest
,
dptr
()
->
ctrl
(),
make_message
(
std
::
forward
<
Ts
>
(
xs
)...));
}
}
/// Sends a message after a relative timeout.
template
<
message_priority
P
=
message_priority
::
normal
,
class
Rep
=
int
,
template
<
message_priority
P
=
message_priority
::
normal
,
class
Rep
=
int
,
class
Period
=
std
::
ratio
<
1
>,
class
Dest
=
actor
,
class
...
Ts
>
class
Period
=
std
::
ratio
<
1
>,
class
Dest
=
actor
,
class
...
Ts
>
detail
::
enable_if_t
<!
std
::
is_same
<
Dest
,
group
>::
value
>
detail
::
enable_if_t
<!
std
::
is_same
<
Dest
,
group
>::
value
>
delayed_send
(
const
Dest
&
dest
,
std
::
chrono
::
duration
<
Rep
,
Period
>
r
time
,
delayed_send
(
const
Dest
&
dest
,
std
::
chrono
::
duration
<
Rep
,
Period
>
r
el_timeout
,
Ts
&&
...
xs
)
{
Ts
&&
...
xs
)
{
using
token
=
detail
::
type_list
<
detail
::
strip_and_convert_t
<
Ts
>
...
>
args_token
;
detail
::
type_list
<
type_check
(
dest
,
args_token
);
typename
detail
::
implicit_conversions
<
typename
std
::
decay
<
Ts
>::
type
>::
type
...
>
;
static_assert
(
!
statically_typed
<
Subtype
>
()
||
statically_typed
<
Dest
>
(),
"statically typed actors are only allowed to send() to other "
"statically typed actors; use anon_send() or request() when "
"communicating with dynamically typed actors"
);
static_assert
(
response_type_unbox
<
signatures_of_t
<
Dest
>
,
token
>::
valid
,
"receiver does not accept given message"
);
// TODO: this only checks one way, we should check for loops
static_assert
(
is_void_response
<
typename
response_type
<
signatures_of_t
<
Dest
>
,
detail
::
implicit_conversions_t
<
typename
std
::
decay
<
Ts
>::
type
>
...
>::
type
>::
value
||
response_type_unbox
<
signatures_of_t
<
Subtype
>
,
typename
response_type
<
signatures_of_t
<
Dest
>
,
detail
::
implicit_conversions_t
<
typename
std
::
decay
<
Ts
>::
type
>
...
>::
type
>::
valid
,
"this actor does not accept the response message"
);
if
(
dest
)
{
if
(
dest
)
{
auto
&
clock
=
dptr
()
->
system
().
clock
();
auto
&
clock
=
dptr
()
->
system
().
clock
();
auto
timeout
=
clock
.
now
()
+
r
time
;
auto
timeout
=
clock
.
now
()
+
r
el_timeout
;
clock
.
schedule_message
(
timeout
,
actor_cast
<
strong_actor_ptr
>
(
dest
),
clock
.
schedule_message
(
timeout
,
actor_cast
<
strong_actor_ptr
>
(
dest
),
make_mailbox_element
(
dptr
()
->
ctrl
(),
make_mailbox_element
(
dptr
()
->
ctrl
(),
make_message_id
(
P
),
no_stages
,
make_message_id
(
P
),
no_stages
,
...
@@ -164,6 +136,7 @@ public:
...
@@ -164,6 +136,7 @@ public:
}
}
}
}
/// Sends a message after a relative timeout.
template
<
class
Rep
=
int
,
class
Period
=
std
::
ratio
<
1
>,
class
Dest
=
actor
,
template
<
class
Rep
=
int
,
class
Period
=
std
::
ratio
<
1
>,
class
Dest
=
actor
,
class
...
Ts
>
class
...
Ts
>
void
delayed_send
(
const
group
&
dest
,
std
::
chrono
::
duration
<
Rep
,
Period
>
rtime
,
void
delayed_send
(
const
group
&
dest
,
std
::
chrono
::
duration
<
Rep
,
Period
>
rtime
,
...
@@ -194,6 +167,21 @@ public:
...
@@ -194,6 +167,21 @@ public:
}
}
private:
private:
template
<
class
Dest
,
class
ArgTypes
>
static
void
type_check
(
const
Dest
&
,
ArgTypes
)
{
static_assert
(
!
statically_typed
<
Subtype
>
()
||
statically_typed
<
Dest
>
(),
"statically typed actors are only allowed to send() to other "
"statically typed actors; use anon_send() or request() when "
"communicating with dynamically typed actors"
);
using
rt
=
response_type_unbox
<
signatures_of_t
<
Dest
>
,
ArgTypes
>
;
static_assert
(
rt
::
valid
,
"receiver does not accept given message"
);
// TODO: this only checks one way, we should check for loops
static_assert
(
is_void_response
<
typename
rt
::
type
>::
value
||
response_type_unbox
<
signatures_of_t
<
Subtype
>
,
typename
rt
::
type
>::
valid
,
"this actor does not accept the response message"
);
}
Subtype
*
dptr
()
{
Subtype
*
dptr
()
{
return
static_cast
<
Subtype
*>
(
this
);
return
static_cast
<
Subtype
*>
(
this
);
}
}
...
...
libcaf_core/test/
delayed_send
.cpp
→
libcaf_core/test/
sender
.cpp
View file @
6eb61e5a
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
* | |___ / ___ \| _| Framework *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* \____/_/ \_|_| *
* *
* *
* Copyright 2011-201
8
Dominik Charousset *
* Copyright 2011-201
9
Dominik Charousset *
* *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* (at your option) under the terms and conditions of the Boost Software *
...
@@ -16,16 +16,14 @@
...
@@ -16,16 +16,14 @@
* http://www.boost.org/LICENSE_1_0.txt. *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
******************************************************************************/
#define CAF_SUITE
delayed_send
#define CAF_SUITE
sender
#include <chrono>
#include "caf/mixin/sender.hpp"
#include "caf/actor_system.hpp"
#include "caf/behavior.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/test/dsl.hpp"
#include "caf/test/dsl.hpp"
#include <chrono>
using
namespace
caf
;
using
namespace
caf
;
using
std
::
chrono
::
seconds
;
using
std
::
chrono
::
seconds
;
...
@@ -41,25 +39,46 @@ behavior testee_impl(event_based_actor* self) {
...
@@ -41,25 +39,46 @@ behavior testee_impl(event_based_actor* self) {
};
};
}
}
struct
fixture
:
test_coordinator_fixture
<>
{
group
grp
;
actor
testee
;
fixture
()
{
grp
=
sys
.
groups
().
anonymous
();
testee
=
sys
.
spawn_in_group
(
grp
,
testee_impl
);
}
~
fixture
()
{
anon_send_exit
(
testee
,
exit_reason
::
user_shutdown
);
}
};
}
// namespace <anonymous>
}
// namespace <anonymous>
CAF_TEST_FIXTURE_SCOPE
(
request_timeout_tests
,
test_coordinator_fixture
<>
)
CAF_TEST_FIXTURE_SCOPE
(
sender_tests
,
fixture
)
CAF_TEST
(
delayed
actor
message
)
{
CAF_TEST
(
delayed
actor
message
)
{
auto
testee
=
sys
.
spawn
(
testee_impl
);
self
->
delayed_send
(
testee
,
seconds
(
1
),
"hello world"
);
self
->
delayed_send
(
testee
,
seconds
(
1
),
"hello world"
);
sched
.
trigger_timeout
();
sched
.
trigger_timeout
();
expect
((
std
::
string
),
from
(
self
).
to
(
testee
).
with
(
"hello world"
));
expect
((
std
::
string
),
from
(
self
).
to
(
testee
).
with
(
"hello world"
));
}
}
CAF_TEST
(
delayed
group
message
)
{
CAF_TEST
(
delayed
group
message
)
{
auto
grp
=
sys
.
groups
().
anonymous
();
auto
testee
=
sys
.
spawn_in_group
(
grp
,
testee_impl
);
self
->
delayed_send
(
grp
,
seconds
(
1
),
"hello world"
);
self
->
delayed_send
(
grp
,
seconds
(
1
),
"hello world"
);
sched
.
trigger_timeout
();
sched
.
trigger_timeout
();
expect
((
std
::
string
),
from
(
self
).
to
(
testee
).
with
(
"hello world"
));
expect
((
std
::
string
),
from
(
self
).
to
(
testee
).
with
(
"hello world"
));
// The group keeps a reference, so we need to shutdown 'manually'.
}
anon_send_exit
(
testee
,
exit_reason
::
user_shutdown
);
CAF_TEST
(
scheduled
actor
message
)
{
self
->
scheduled_send
(
testee
,
self
->
clock
().
now
()
+
seconds
(
1
),
"hello world"
);
sched
.
trigger_timeout
();
expect
((
std
::
string
),
from
(
self
).
to
(
testee
).
with
(
"hello world"
));
}
CAF_TEST
(
scheduled
group
message
)
{
self
->
scheduled_send
(
grp
,
self
->
clock
().
now
()
+
seconds
(
1
),
"hello world"
);
sched
.
trigger_timeout
();
expect
((
std
::
string
),
from
(
self
).
to
(
testee
).
with
(
"hello world"
));
}
}
CAF_TEST_FIXTURE_SCOPE_END
()
CAF_TEST_FIXTURE_SCOPE_END
()
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