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
b9ee7c0a
Commit
b9ee7c0a
authored
Nov 09, 2016
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `response_promise::delegate`, close #512
parent
53c92ec6
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
82 additions
and
47 deletions
+82
-47
libcaf_core/caf/check_typed_input.hpp
libcaf_core/caf/check_typed_input.hpp
+1
-1
libcaf_core/caf/response_promise.hpp
libcaf_core/caf/response_promise.hpp
+30
-1
libcaf_core/caf/response_type.hpp
libcaf_core/caf/response_type.hpp
+1
-0
libcaf_core/caf/typed_response_promise.hpp
libcaf_core/caf/typed_response_promise.hpp
+7
-0
libcaf_core/src/response_promise.cpp
libcaf_core/src/response_promise.cpp
+5
-3
libcaf_core/test/typed_response_promise.cpp
libcaf_core/test/typed_response_promise.cpp
+38
-42
No files found.
libcaf_core/caf/check_typed_input.hpp
View file @
b9ee7c0a
...
...
@@ -21,7 +21,7 @@
#define CAF_CHECK_TYPED_INPUT_HPP
#include "caf/fwd.hpp"
#include "caf/
typed_actor
.hpp"
#include "caf/
replies_to
.hpp"
#include "caf/detail/type_list.hpp"
#include "caf/detail/typed_actor_util.hpp"
...
...
libcaf_core/caf/response_promise.hpp
View file @
b9ee7c0a
...
...
@@ -26,6 +26,8 @@
#include "caf/message.hpp"
#include "caf/actor_addr.hpp"
#include "caf/message_id.hpp"
#include "caf/response_type.hpp"
#include "caf/check_typed_input.hpp"
namespace
caf
{
...
...
@@ -54,7 +56,34 @@ public:
response_promise
>::
type
deliver
(
T
&&
x
,
Ts
&&
...
xs
)
{
return
deliver_impl
(
make_message
(
std
::
forward
<
T
>
(
x
),
std
::
forward
<
Ts
>
(
xs
)...));
return
deliver_impl
(
make_message
(
std
::
forward
<
T
>
(
x
),
std
::
forward
<
Ts
>
(
xs
)...));
}
/// Satisfies the promise by delegating to another actor.
template
<
message_priority
P
=
message_priority
::
normal
,
class
Handle
=
actor
,
class
...
Ts
>
typename
response_type
<
typename
Handle
::
signatures
,
detail
::
implicit_conversions_t
<
typename
std
::
decay
<
Ts
>::
type
>
...
>::
delegated_type
delegate
(
const
Handle
&
dest
,
Ts
&&
...
xs
)
{
static_assert
(
sizeof
...(
Ts
)
>
0
,
"nothing to delegate"
);
using
token
=
detail
::
type_list
<
typename
detail
::
implicit_conversions
<
typename
std
::
decay
<
Ts
>::
type
>::
type
...
>
;
static_assert
(
response_type_unbox
<
signatures_of_t
<
Handle
>
,
token
>::
valid
,
"receiver does not accept given message"
);
if
(
dest
)
{
auto
mid
=
P
==
message_priority
::
high
?
id_
.
with_high_priority
()
:
id_
;
dest
->
enqueue
(
make_mailbox_element
(
std
::
move
(
source_
),
mid
,
std
::
move
(
stages_
),
std
::
forward
<
Ts
>
(
xs
)...),
ctx_
);
}
return
{};
}
/// Satisfies the promise by sending an error response message.
...
...
libcaf_core/caf/response_type.hpp
View file @
b9ee7c0a
...
...
@@ -23,6 +23,7 @@
#include <tuple>
#include "caf/fwd.hpp"
#include "caf/replies_to.hpp"
#include "caf/detail/type_list.hpp"
...
...
libcaf_core/caf/typed_response_promise.hpp
View file @
b9ee7c0a
...
...
@@ -67,6 +67,13 @@ public:
return
*
this
;
}
template
<
message_priority
P
=
message_priority
::
normal
,
class
Handle
=
actor
,
class
...
Us
>
typed_response_promise
delegate
(
const
Handle
&
dest
,
Us
&&
...
xs
)
{
promise_
.
template
delegate
<
P
>(
dest
,
std
::
forward
<
Us
>
(
xs
)...);
return
*
this
;
}
/// Satisfies the promise by sending an error response message.
/// For non-requests, nothing is done.
inline
typed_response_promise
deliver
(
error
x
)
{
...
...
libcaf_core/src/response_promise.cpp
View file @
b9ee7c0a
...
...
@@ -20,9 +20,11 @@
#include <utility>
#include <algorithm>
#include "caf/local_actor.hpp"
#include "caf/response_promise.hpp"
#include "caf/logger.hpp"
#include "caf/local_actor.hpp"
namespace
caf
{
response_promise
::
response_promise
()
...
...
@@ -68,9 +70,9 @@ response_promise response_promise::deliver_impl(message msg) {
return
*
this
;
}
if
(
self_
)
CAF_LOG_
ERROR
(
"response promise already satisfied"
);
CAF_LOG_
INFO
(
"response promise already satisfied"
);
else
CAF_LOG_
ERROR
(
"invalid response promise"
);
CAF_LOG_
INFO
(
"invalid response promise"
);
return
*
this
;
}
...
...
libcaf_core/test/typed_response_promise.cpp
View file @
b9ee7c0a
...
...
@@ -148,54 +148,21 @@ CAF_TEST(typed_response_promise) {
typed_response_promise
<
int
>
resp
;
CAF_MESSAGE
(
"trigger 'invalid response promise' error"
);
resp
.
deliver
(
1
);
// delivers on an invalid promise has no effect
self
->
request
(
foo
,
infinite
,
get_atom
::
value
,
42
).
receive
(
[](
int
x
)
{
CAF_CHECK_EQUAL
(
x
,
84
);
},
ERROR_HANDLER
);
self
->
request
(
foo
,
infinite
,
get_atom
::
value
,
42
,
52
).
receive
(
[](
int
x
,
int
y
)
{
CAF_CHECK_EQUAL
(
x
,
84
);
CAF_CHECK_EQUAL
(
y
,
104
);
},
ERROR_HANDLER
);
self
->
request
(
foo
,
infinite
,
get_atom
::
value
,
3.14
,
3.14
).
receive
(
[](
double
x
,
double
y
)
{
CAF_CHECK_EQUAL
(
x
,
3.14
*
2
);
CAF_CHECK_EQUAL
(
y
,
3.14
*
2
);
},
[](
const
error
&
err
)
{
CAF_ERROR
(
"unexpected error response message received: "
<<
to_string
(
err
));
}
);
auto
f
=
make_function_view
(
foo
);
CAF_CHECK_EQUAL
(
f
(
get_atom
::
value
,
42
),
84
);
CAF_CHECK_EQUAL
(
f
(
get_atom
::
value
,
42
,
52
),
std
::
make_tuple
(
84
,
104
));
CAF_CHECK_EQUAL
(
f
(
get_atom
::
value
,
3.14
,
3.14
),
std
::
make_tuple
(
6.28
,
6.28
));
}
CAF_TEST
(
typed_response_promise_chained
)
{
auto
composed
=
foo
*
foo
*
foo
;
self
->
request
(
composed
,
infinite
,
1
).
receive
(
[](
int
v
)
{
CAF_CHECK_EQUAL
(
v
,
8
);
},
[](
const
error
&
err
)
{
CAF_ERROR
(
"unexpected error response message received: "
<<
to_string
(
err
));
}
);
auto
f
=
make_function_view
(
foo
*
foo
*
foo
);
CAF_CHECK_EQUAL
(
f
(
1
),
8
);
}
// verify that only requests get an error response message
CAF_TEST
(
error_response_message
)
{
self
->
request
(
foo
,
infinite
,
get_atom
::
value
,
3.14
).
receive
(
[](
double
)
{
CAF_ERROR
(
"unexpected ordinary response message received"
);
},
[](
error
&
err
)
{
CAF_CHECK_EQUAL
(
err
.
code
(),
static_cast
<
uint8_t
>
(
sec
::
unexpected_message
));
}
);
auto
f
=
make_function_view
(
foo
);
CAF_CHECK_EQUAL
(
f
(
get_atom
::
value
,
3.14
),
sec
::
unexpected_message
);
self
->
send
(
foo
,
get_atom
::
value
,
42
);
self
->
receive
(
[](
int
x
)
{
...
...
@@ -208,7 +175,7 @@ CAF_TEST(error_response_message) {
self
->
send
(
foo
,
get_atom
::
value
,
3.14
);
self
->
receive
(
[
&
](
error
&
err
)
{
CAF_CHECK_EQUAL
(
err
.
code
(),
static_cast
<
uint8_t
>
(
sec
::
unexpected_message
)
);
CAF_CHECK_EQUAL
(
err
,
sec
::
unexpected_message
);
self
->
send
(
self
,
message
{});
}
);
...
...
@@ -230,4 +197,33 @@ CAF_TEST(satisfied_promise) {
);
}
CAF_TEST
(
delegating_promises
)
{
using
task
=
std
::
pair
<
typed_response_promise
<
int
>
,
int
>
;
struct
state
{
std
::
vector
<
task
>
tasks
;
};
using
bar_actor
=
typed_actor
<
replies_to
<
int
>::
with
<
int
>
,
reacts_to
<
ok_atom
>>
;
auto
bar_fun
=
[](
bar_actor
::
stateful_pointer
<
state
>
self
,
foo_actor
worker
)
->
bar_actor
::
behavior_type
{
return
{
[
=
](
int
x
)
->
typed_response_promise
<
int
>
{
auto
&
tasks
=
self
->
state
.
tasks
;
tasks
.
emplace_back
(
self
->
make_response_promise
<
int
>
(),
x
);
self
->
send
(
self
,
ok_atom
::
value
);
return
tasks
.
back
().
first
;
},
[
=
](
ok_atom
)
{
auto
&
tasks
=
self
->
state
.
tasks
;
if
(
!
tasks
.
empty
())
{
auto
&
task
=
tasks
.
back
();
task
.
first
.
delegate
(
worker
,
task
.
second
);
tasks
.
pop_back
();
}
}
};
};
auto
f
=
make_function_view
(
system
.
spawn
(
bar_fun
,
foo
));
CAF_CHECK_EQUAL
(
f
(
42
),
84
);
}
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