Unverified Commit 732a8a68 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #813

Port auto-connect changes to cleanup branch
parents 67b7161c 7bedcd63
......@@ -123,12 +123,15 @@ public:
friend class abstract_actor;
/// The number of actors implictly spawned by the actor system on startup.
static constexpr size_t num_internal_actors = 2;
static constexpr size_t num_internal_actors = 3;
/// Returns the ID of an internal actor by its name.
/// @pre x in {'SpawnServ', 'ConfigServ', 'StreamServ'}
/// @pre x in {'SpawnServ', 'ConfigServ', 'PeerServ', 'StreamServ'}
static constexpr size_t internal_actor_id(atom_value x) {
return x == atom("SpawnServ") ? 0 : (x == atom("ConfigServ") ? 1 : 2);
return x == atom("SpawnServ") ? 0
: (x == atom("ConfigServ") ? 1
: (x == atom("PeerServ") ? 2
: 3));
}
/// Returns the internal actor for dynamic spawn operations.
......@@ -142,6 +145,11 @@ public:
return internal_actors_[internal_actor_id(atom("ConfigServ"))];
}
/// Returns the internal actor for storing the addresses of its peers.
inline const strong_actor_ptr& peer_serv() const {
return internal_actors_[internal_actor_id(atom("PeerServ"))];
}
actor_system() = delete;
actor_system(const actor_system&) = delete;
actor_system& operator=(const actor_system&) = delete;
......@@ -569,6 +577,11 @@ private:
internal_actors_[internal_actor_id(atom("ConfigServ"))] = std::move(x);
}
/// Sets the internal actor for storing the peer addresses.
inline void peer_serv(strong_actor_ptr x) {
internal_actors_[internal_actor_id(atom("PeerServ"))] = std::move(x);
}
// -- member variables -------------------------------------------------------
/// Used to generate ascending actor IDs.
......
......@@ -166,6 +166,93 @@ behavior spawn_serv_impl(stateful_actor<spawn_serv_state>* self) {
};
}
// -- peer server --------------------------------------------------------------
// A peer server keeps track of the addresses to reach its peers. All addresses
// for a given node are stored under the string representation of its node id.
// When an entry is requested that does not exist, the requester is subscribed
// to the key. When the entry is set, it get a copy and is removed from the
// subscribers.
struct peer_state {
using key_type = std::string;
using mapped_type = message;
using subscriber_set = std::unordered_set<strong_actor_ptr>;
using topic_set = std::unordered_set<std::string>;
std::unordered_map<key_type, std::pair<mapped_type, subscriber_set>> data;
std::unordered_map<strong_actor_ptr, topic_set> subscribers;
static const char* name;
};
const char* peer_state::name = "peer_server";
behavior peer_serv_impl(stateful_actor<peer_state>* self) {
CAF_LOG_TRACE("");
std::string wildcard = "*";
auto unsubscribe_all = [=](actor subscriber) {
auto& subscribers = self->state.subscribers;
auto ptr = actor_cast<strong_actor_ptr>(subscriber);
auto i = subscribers.find(ptr);
if (i == subscribers.end())
return;
for (auto& key : i->second)
self->state.data[key].second.erase(ptr);
subscribers.erase(i);
};
self->set_down_handler([=](down_msg& dm) {
CAF_LOG_TRACE(CAF_ARG(dm));
auto ptr = actor_cast<strong_actor_ptr>(dm.source);
if (ptr)
unsubscribe_all(actor_cast<actor>(std::move(ptr)));
});
return {
// Set a key/value pair.
[=](put_atom, const std::string& key, message& msg) {
CAF_LOG_TRACE(CAF_ARG(key) << CAF_ARG(msg));
if (key == wildcard || key.empty())
return;
auto& vp = self->state.data[key];
vp.first = std::move(msg);
for (auto& subscriber_ptr : vp.second) {
// We never put a nullptr in our map.
auto subscriber = actor_cast<actor>(subscriber_ptr);
if (subscriber != self->current_sender()) {
self->send(subscriber, key, vp.first);
self->state.subscribers[subscriber_ptr].erase(key);
}
}
self->state.data[key].second.clear();
},
// Get a key/value pair.
[=](get_atom, std::string& key) {
auto sender = actor_cast<strong_actor_ptr>(self->current_sender());
if (sender) {
CAF_LOG_TRACE(CAF_ARG(key));
// Get the value ...
if (key == wildcard || key.empty())
return;
auto d = self->state.data.find(key);
if (d != self->state.data.end()) {
self->send(actor_cast<actor>(sender), std::move(key),
d->second.first);
return;
}
// ... or sub if it is not available.
CAF_LOG_TRACE(CAF_ARG(key) << CAF_ARG(sender));
self->state.data[key].second.insert(sender);
auto& subscribers = self->state.subscribers;
auto s = subscribers.find(sender);
if (s != subscribers.end()) {
s->second.insert(key);
} else {
self->monitor(sender);
subscribers.emplace(sender, peer_state::topic_set{key});
}
}
}
};
}
// -- stream server ------------------------------------------------------------
// The stream server acts as a man-in-the-middle for all streams that cross the
......@@ -292,10 +379,12 @@ actor_system::actor_system(actor_system_config& cfg)
static constexpr auto Flags = hidden + lazy_init;
spawn_serv(actor_cast<strong_actor_ptr>(spawn<Flags>(spawn_serv_impl)));
config_serv(actor_cast<strong_actor_ptr>(spawn<Flags>(config_serv_impl)));
peer_serv(actor_cast<strong_actor_ptr>(spawn<Flags>(peer_serv_impl)));
// fire up remaining modules
registry_.start();
registry_.put(atom("SpawnServ"), spawn_serv());
registry_.put(atom("ConfigServ"), config_serv());
registry_.put(atom("PeerServ"), peer_serv());
for (auto& mod : modules_)
if (mod)
mod->start();
......
......@@ -80,14 +80,8 @@ public:
std::vector<strong_actor_ptr>& forwarding_stack,
message& msg) = 0;
/// Called whenever BASP learns the ID of a remote node
/// to which it does not have a direct connection.
virtual void learned_new_node_directly(const node_id& nid,
bool was_known_indirectly) = 0;
/// Called whenever BASP learns the ID of a remote node
/// to which it does not have a direct connection.
virtual void learned_new_node_indirectly(const node_id& nid) = 0;
/// Called whenever BASP learns the ID of a remote node.
virtual void learned_new_node(const node_id& nid) = 0;
/// Called if a heartbeat was received from `nid`
virtual void handle_heartbeat(const node_id& nid) = 0;
......@@ -107,9 +101,19 @@ public:
return namespace_.system().config();
}
/// Send messages that were buffered while connectivity establishment
/// was pending using `hdl`.
virtual void send_buffered_messages(execution_unit* ctx, node_id nid,
connection_handle hdl) = 0;
/// Returns a reference to the sent buffer.
virtual buffer_type& get_buffer(connection_handle hdl) = 0;
/// Returns a reference to a buffer to be sent to node with `nid`.
/// If communication with the node is esstablished, it picks the first
/// available handle, otherwise a buffer for a pending message is returned.
virtual buffer_type& get_buffer(node_id nid) = 0;
/// Flushes the underlying write buffer of `hdl`.
virtual void flush(connection_handle hdl) = 0;
......@@ -135,15 +139,15 @@ public:
void handle_heartbeat(execution_unit* ctx);
/// Returns a route to `target` or `none` on error.
optional<routing_table::route> lookup(const node_id& target);
routing_table::lookup_result lookup(const node_id& target);
/// Flushes the underlying buffer of `path`.
void flush(const routing_table::route& path);
void flush(connection_handle hdl);
/// Sends a BASP message and implicitly flushes the output buffer of `r`.
/// This function will update `hdr.payload_len` if a payload was written.
void write(execution_unit* ctx, const routing_table::route& r,
header& hdr, payload_writer* writer = nullptr);
void write(execution_unit* ctx, connection_handle hdl, header& hdr,
payload_writer* writer = nullptr);
/// Adds a new actor to the map of published actors.
void add_published_actor(uint16_t port,
......@@ -200,7 +204,7 @@ public:
buffer_type& out_buf, optional<uint16_t> port);
/// Writes the client handshake to `buf`.
static void write_client_handshake(execution_unit* ctx, buffer_type& buf,
void write_client_handshake(execution_unit* ctx, buffer_type& buf,
const node_id& this_node,
const std::string& app_identifier);
......
......@@ -24,6 +24,7 @@
#include "caf/callback.hpp"
#include "caf/io/abstract_broker.hpp"
#include "caf/io/basp/buffer_type.hpp"
#include "caf/io/network/interfaces.hpp"
#include "caf/node_id.hpp"
namespace caf {
......@@ -36,70 +37,90 @@ namespace basp {
/// BASP peer and provides both direct and indirect paths.
class routing_table {
public:
using endpoint = std::pair<uint16_t, network::address_listing>;
explicit routing_table(abstract_broker* parent);
virtual ~routing_table();
/// Describes a routing path to a node.
struct route {
const node_id& next_hop;
connection_handle hdl;
/// Result for a lookup of a node.
struct lookup_result {
/// Tracks whether the node is already known.
bool known;
/// Servant handle to communicate with the node -- if already created.
optional<connection_handle> hdl;
};
/// Describes a function object for erase operations that
/// is called for each indirectly lost connection.
using erase_callback = callback<const node_id&>;
/// Returns a route to `target` or `none` on error.
optional<route> lookup(const node_id& target);
/// Returns the ID of the peer connected via `hdl` or
/// Returns the ID of the peer reachable via `hdl` or
/// `none` if `hdl` is unknown.
node_id lookup_direct(const connection_handle& hdl) const;
/// Returns the handle offering a direct connection to `nid` or
/// `invalid_connection_handle` if no direct connection to `nid` exists.
optional<connection_handle> lookup_direct(const node_id& nid) const;
node_id lookup(const connection_handle& hdl) const;
/// Returns the next hop that would be chosen for `nid`
/// or `none` if there's no indirect route to `nid`.
node_id lookup_indirect(const node_id& nid) const;
/// Returns the state for communication with `nid` along with a handle
/// if communication is established or `none` if `nid` is unknown.
lookup_result lookup(const node_id& nid) const;
/// Adds a new direct route to the table.
/// Adds a new endpoint to the table.
/// @pre `hdl != invalid_connection_handle && nid != none`
void add_direct(const connection_handle& hdl, const node_id& nid);
void add(const node_id& nid, const connection_handle& hdl);
/// Adds a new indirect route to the table.
bool add_indirect(const node_id& hop, const node_id& dest);
/// Add a new endpoint to the table.
/// @pre `origin != none && nid != none`
void add(const node_id& nid, const node_id& origin);
/// Blacklist the route to `dest` via `hop`.
void blacklist(const node_id& hop, const node_id& dest);
/// Adds a new endpoint to the table that has no attached information.
/// that propagated information about the node.
/// @pre `nid != none`
void add(const node_id& nid);
/// Removes a direct connection and calls `cb` for any node
/// that became unreachable as a result of this operation,
/// including the node that is assigned as direct path for `hdl`.
void erase_direct(const connection_handle& hdl, erase_callback& cb);
void erase(const connection_handle& hdl, erase_callback& cb);
/// Removes any entry for indirect connection to `dest` and returns
/// `true` if `dest` had an indirect route, otherwise `false`.
bool erase_indirect(const node_id& dest);
/// Queries whether `dest` is reachable.
/// Queries whether `dest` is reachable directly.
bool reachable(const node_id& dest);
/// Removes all direct and indirect routes to `dest` and calls
/// `cb` for any node that became unreachable as a result of this
/// operation, including `dest`.
/// @returns the number of removed routes (direct and indirect)
size_t erase(const node_id& dest, erase_callback& cb);
/// Returns the parent broker.
inline abstract_broker* parent() {
return parent_;
}
/// Set the forwarding node that first mentioned `hdl`.
bool origin(const node_id& nid, const node_id& origin);
/// Get the forwarding node that first mentioned `hdl`
/// or `none` if the node is unknown.
optional<node_id> origin(const node_id& nid);
/// Set the handle for communication with `nid`.
bool handle(const node_id& nid, const connection_handle& hdl);
/// Get the handle for communication with `nid`
/// or `none` if the node is unknown.
optional<connection_handle> handle(const node_id& nid);
/// Get the addresses to reach `nid` or `none` if the node is unknown.
optional<const endpoint&> address(const node_id& nid);
/// Returns the local autoconnect endpoint.
const endpoint& autoconnect_endpoint();
/// Set the local autoconenct endpoint.
void autoconnect_endpoint(uint16_t, network::address_listing);
public:
/// Entry to bundle information for a remote endpoint.
struct node_info {
/// Handle for the node if communication is established.
optional<connection_handle> hdl;
/// The endpoint who told us about the node.
optional<node_id> origin;
};
template <class Map, class Fallback>
typename Map::mapped_type
get_opt(const Map& m, const typename Map::key_type& k, Fallback&& x) const {
......@@ -109,16 +130,10 @@ public:
return std::forward<Fallback>(x);
}
using node_id_set = std::unordered_set<node_id>;
using indirect_entries = std::unordered_map<node_id, // dest
node_id_set>; // hop
abstract_broker* parent_;
std::unordered_map<connection_handle, node_id> direct_by_hdl_;
std::unordered_map<node_id, connection_handle> direct_by_nid_;
indirect_entries indirect_;
indirect_entries blacklist_;
std::unordered_map<connection_handle, node_id> nid_by_hdl_;
std::unordered_map<node_id, node_info> node_information_base_;
endpoint autoconnect_endpoint_;
};
/// @}
......
......@@ -77,17 +77,17 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
std::vector<strong_actor_ptr>& stages, message& msg);
// performs bookkeeping such as managing `spawn_servers`
void learned_new_node(const node_id& nid);
void learned_new_node(const node_id& nid) override;
// inherited from basp::instance::callee
void learned_new_node_directly(const node_id& nid,
bool was_indirectly_before) override;
void send_buffered_messages(execution_unit* ctx, node_id nid,
connection_handle hdl) override;
// inherited from basp::instance::callee
void learned_new_node_indirectly(const node_id& nid) override;
buffer_type& get_buffer(connection_handle hdl) override;
// inherited from basp::instance::callee
buffer_type& get_buffer(connection_handle hdl) override;
buffer_type& get_buffer(node_id nid) override;
// inherited from basp::instance::callee
void flush(connection_handle hdl) override;
......@@ -102,6 +102,9 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
/// Cleans up any state for `hdl`.
void cleanup(connection_handle hdl);
/// Try to establish a connection to node with `nid`.
void connect(const node_id& nid);
// pointer to ourselves
broker* self;
......@@ -146,6 +149,9 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
// actor
void handle_down_msg(down_msg&);
// buffer messages for nodes while connectivity is established
std::unordered_map<node_id, std::vector<buffer_type>> pending_connectivity;
static const char* name;
};
......
......@@ -46,13 +46,8 @@ struct connection_helper_state {
static const char* name;
};
behavior datagram_connection_broker(broker* self,
uint16_t port,
network::address_listing addresses,
actor system_broker);
behavior connection_helper(stateful_actor<connection_helper_state>* self,
actor b);
actor system_broker);
} // namespace io
} // namespace caf
......@@ -64,25 +64,34 @@ strong_actor_ptr basp_broker_state::make_proxy(node_id nid, actor_id aid) {
CAF_ASSERT(nid != this_node());
if (nid == none || aid == invalid_actor_id)
return nullptr;
// this member function is being called whenever we deserialize a
// This member function is being called whenever we deserialize a
// payload received from a remote node; if a remote node A sends
// us a handle to a third node B, then we assume that A offers a route to B
if (nid != this_context->id
&& !instance.tbl().lookup_direct(nid)
&& instance.tbl().add_indirect(this_context->id, nid))
learned_new_node_indirectly(nid);
// we need to tell remote side we are watching this actor now;
// use a direct route if possible, i.e., when talking to a third node
auto path = instance.tbl().lookup(nid);
if (!path) {
// this happens if and only if we don't have a path to `nid`
// and current_context_->hdl has been blacklisted
CAF_LOG_DEBUG("cannot create a proxy instance for an actor "
// us a handle to a third node B, then we assume that A can tell us
// how to contact B.
// TODO: This should probably happen somewhere else, but is usually only
// performed in `finalize_handshake` which is only called on receipt
// of server handshakes.
if (this_context->id == none)
this_context->id = instance.tbl().lookup(this_context->hdl);
auto lr = instance.tbl().lookup(nid);
if (nid != this_context->id && !lr.known) {
instance.tbl().add(nid, this_context->id);
connect(nid);
}
// We need to tell remote side we are watching this actor now;
// use a direct route if possible, i.e., when talking to a third node.
// TODO: Communication setup might still be in progress.
/*
if (lr.known && !lr.hdl) {
// This happens if and only if we don't have a path to `nid`
// and current_context_->hdl has been blacklisted.
CAF_LOG_INFO("cannot create a proxy instance for an actor "
"running on a node we don't have a route to");
return nullptr;
}
// create proxy and add functor that will be called if we
// receive a kill_proxy_instance message
*/
// Create proxy and add functor that will be called if we
// receive a kill_proxy_instance message.
auto mm = &system().middleman();
actor_config cfg;
auto res = make_actor<forwarding_actor_proxy, strong_actor_ptr>(
......@@ -90,22 +99,27 @@ strong_actor_ptr basp_broker_state::make_proxy(node_id nid, actor_id aid) {
strong_actor_ptr selfptr{self->ctrl()};
res->get()->attach_functor([=](const error& rsn) {
mm->backend().post([=] {
// using res->id() instead of aid keeps this actor instance alive
// Using res->id() instead of aid keeps this actor instance alive
// until the original instance terminates, thus preventing subtle
// bugs with attachables
// bugs with attachables.
auto bptr = static_cast<basp_broker*>(selfptr->get());
if (!bptr->getf(abstract_actor::is_terminated_flag))
bptr->state.proxies().erase(nid, res->id(), rsn);
});
});
CAF_LOG_DEBUG("successfully created proxy instance, "
CAF_LOG_INFO("successfully created proxy instance, "
"write announce_proxy_instance:"
<< CAF_ARG(nid) << CAF_ARG(aid)
<< CAF_ARG2("hdl", this_context->hdl));
// tell remote side we are monitoring this actor now
instance.write_announce_proxy(self->context(), get_buffer(this_context->hdl),
nid, aid);
instance.flush(*path);
<< CAF_ARG(nid) << CAF_ARG(aid));
// TODO: Can it happen that things have changed here?
lr = instance.tbl().lookup(nid);
if (lr.hdl) {
auto hdl = std::move(*lr.hdl);
// Tell remote side we are monitoring this actor now.
instance.write_announce_proxy(self->context(), get_buffer(hdl), nid, aid);
flush(hdl);
} else {
instance.write_announce_proxy(self->context(), get_buffer(nid), nid, aid);
}
mm->notify<hook::new_remote_actor>(res);
return res;
}
......@@ -150,15 +164,21 @@ void basp_broker_state::purge_state(const node_id& nid) {
void basp_broker_state::send_kill_proxy_instance(const node_id& nid,
actor_id aid, error rsn) {
CAF_LOG_TRACE(CAF_ARG(nid) << CAF_ARG(aid) << CAF_ARG(rsn));
auto path = instance.tbl().lookup(nid);
if (!path) {
auto res = instance.tbl().lookup(nid);
if (!res.known) {
CAF_LOG_INFO("cannot send exit message for proxy, no route to host:"
<< CAF_ARG(nid));
return;
}
instance.write_kill_proxy(self->context(), get_buffer(path->hdl), nid, aid,
if (res.hdl) {
auto hdl = std::move(*res.hdl);
instance.write_kill_proxy(self->context(), get_buffer(hdl), nid, aid,
rsn);
instance.flush(hdl);
} else {
instance.write_kill_proxy(self->context(), get_buffer(nid), nid, aid,
rsn);
instance.flush(*path);
}
}
void basp_broker_state::proxy_announced(const node_id& nid, actor_id aid) {
......@@ -331,8 +351,8 @@ void basp_broker_state::learned_new_node(const node_id& nid) {
auto msg = make_message(sys_atom::value, get_atom::value, "info");
return sink(name_atm, stages, msg);
});
auto path = instance.tbl().lookup(nid);
if (!path) {
auto res = instance.tbl().lookup(nid);
if (!res.known) {
CAF_LOG_ERROR("learned_new_node called, but no route to nid");
return;
}
......@@ -343,38 +363,42 @@ void basp_broker_state::learned_new_node(const node_id& nid) {
invalid_actor_id};
// writing std::numeric_limits<actor_id>::max() is a hack to get
// this send-to-named-actor feature working with older CAF releases
instance.write(self->context(), get_buffer(path->hdl),
hdr, &writer);
instance.flush(*path);
}
void basp_broker_state::learned_new_node_directly(const node_id& nid,
bool was_indirectly_before) {
CAF_ASSERT(this_context != nullptr);
CAF_LOG_TRACE(CAF_ARG(nid));
if (!was_indirectly_before)
learned_new_node(nid);
if (res.hdl) {
auto hdl = std::move(*res.hdl);
instance.write(self->context(), get_buffer(hdl), hdr, &writer);
instance.flush(hdl);
} else {
instance.write(self->context(), get_buffer(nid), hdr, &writer);
}
}
void basp_broker_state::learned_new_node_indirectly(const node_id& nid) {
void basp_broker_state::connect(const node_id& nid) {
// TODO: Split this by functionality, address query & connecting?
CAF_ASSERT(this_context != nullptr);
CAF_LOG_TRACE(CAF_ARG(nid));
learned_new_node(nid);
if (!automatic_connections)
return;
// this member function gets only called once, after adding a new
// indirect connection to the routing table; hence, spawning
// our helper here exactly once and there is no need to track
// in-flight connection requests
auto path = instance.tbl().lookup(nid);
if (!path) {
CAF_LOG_ERROR("learned_new_node_indirectly called, but no route to nid");
if (instance.tbl().lookup(nid).hdl) {
CAF_LOG_ERROR("establish_communication called with established connection");
return;
}
if (path->next_hop == nid) {
CAF_LOG_ERROR("learned_new_node_indirectly called with direct connection");
auto origin = instance.tbl().origin(nid);
if (!origin) {
CAF_LOG_ERROR("establish_communication called, but no node known "
"to ask for contact information");
return;
}
auto ehdl = instance.tbl().handle(*origin);
if (!ehdl) {
CAF_LOG_ERROR("establish_communication called, but node with contact "
"information is no longer reachable");
return;
}
auto hdl = std::move(*ehdl);
using namespace detail;
auto try_connect = [&](std::string item) {
auto tmp = get_or(config(), "middleman.attach-utility-actors", false)
......@@ -382,7 +406,7 @@ void basp_broker_state::learned_new_node_indirectly(const node_id& nid) {
: system().spawn<detached + hidden>(connection_helper, self);
system().registry().put(tmp.id(), actor_cast<strong_actor_ptr>(tmp));
auto writer = make_callback([&item](serializer& sink) -> error {
auto name_atm = atom("ConfigServ");
auto name_atm = atom("PeerServ");
std::vector<actor_id> stages;
auto msg = make_message(get_atom::value, std::move(item));
return sink(name_atm, stages, msg);
......@@ -391,11 +415,11 @@ void basp_broker_state::learned_new_node_indirectly(const node_id& nid) {
basp::header::named_receiver_flag, 0,
make_message_id().integer_value(), tmp.id(),
invalid_actor_id};
instance.write(self->context(), get_buffer(path->hdl),
instance.write(self->context(), get_buffer(hdl),
hdr, &writer);
instance.flush(*path);
flush(hdl);
};
try_connect("basp.default-connectivity-tcp");
try_connect(to_string(nid));
}
void basp_broker_state::set_context(connection_handle hdl) {
......@@ -421,7 +445,7 @@ void basp_broker_state::cleanup(connection_handle hdl) {
purge_state(nid);
return none;
});
instance.tbl().erase_direct(hdl, cb);
instance.tbl().erase(hdl, cb);
// Remove the context for `hdl`, making sure clients receive an error in case
// this connection was closed during handshake.
auto i = ctx.find(hdl);
......@@ -441,10 +465,32 @@ basp_broker_state::get_buffer(connection_handle hdl) {
return self->wr_buf(hdl);
}
basp_broker_state::buffer_type&
basp_broker_state::get_buffer(node_id nid) {
auto res = instance.tbl().lookup(nid);
if (res.known && res.hdl)
return get_buffer(*res.hdl);
auto& msgs = pending_connectivity[nid];
msgs.emplace_back();
return msgs.back();
}
void basp_broker_state::flush(connection_handle hdl) {
self->flush(hdl);
}
void basp_broker_state::send_buffered_messages(execution_unit*,
node_id nid,
connection_handle hdl) {
if (pending_connectivity.count(nid) > 0) {
for (auto& msg : pending_connectivity[nid]) {
auto& buf = get_buffer(hdl);
buf.insert(buf.end(), msg.begin(), msg.end());
}
}
flush(hdl);
}
/******************************************************************************
* basp_broker *
******************************************************************************/
......@@ -466,6 +512,18 @@ behavior basp_broker::make_behavior() {
if (res) {
auto port = res->second;
auto addrs = network::interfaces::list_addresses(false);
// Remove link local addresses. These don't work for autoconnects.
for (auto& p : addrs) {
auto& vec = p.second;
vec.erase(std::remove_if(std::begin(vec), std::end(vec),
[](const std::string& str) {
return str.find("fe80") == 0;
}),
vec.end());
}
// Set this as the propagated autoconnect endpoint.
state.instance.tbl().autoconnect_endpoint(port, addrs);
// Add a config serv entry.
auto config_server = system().registry().get(atom("ConfigServ"));
send(actor_cast<actor>(config_server), put_atom::value,
"basp.default-connectivity-tcp",
......@@ -533,8 +591,8 @@ behavior basp_broker::make_behavior() {
<< ", " << CAF_ARG(msg));
if (!src)
return sec::invalid_argument;
auto path = this->state.instance.tbl().lookup(dest_node);
if (!path) {
auto lr = this->state.instance.tbl().lookup(dest_node);
if (!lr.known) {
CAF_LOG_ERROR("no route to receiving node");
return sec::no_route_to_receiving_node;
}
......@@ -546,9 +604,14 @@ behavior basp_broker::make_behavior() {
basp::header hdr{basp::message_type::dispatch_message,
basp::header::named_receiver_flag, 0,
cme->mid.integer_value(), src->id(), invalid_actor_id};
state.instance.write(context(), state.get_buffer(path->hdl),
hdr, &writer);
state.instance.flush(*path);
if (lr.hdl) {
auto hdl = std::move(*lr.hdl);
state.instance.write(context(), state.get_buffer(hdl), hdr, &writer);
state.instance.flush(hdl);
} else {
state.instance.write(context(), state.get_buffer(dest_node), hdr,
&writer);
}
return delegated<message>();
},
// received from underlying broker implementation
......@@ -628,10 +691,10 @@ behavior basp_broker::make_behavior() {
-> std::tuple<node_id, std::string, uint16_t> {
std::string addr;
uint16_t port = 0;
auto hdl = state.instance.tbl().lookup_direct(x);
if (hdl) {
addr = remote_addr(*hdl);
port = remote_port(*hdl);
auto lr = state.instance.tbl().lookup(x);
if (lr.known && lr.hdl) {
addr = remote_addr(*lr.hdl);
port = remote_port(*lr.hdl);
}
return std::make_tuple(x, std::move(addr), port);
},
......
......@@ -34,94 +34,45 @@ auto autoconnect_timeout = std::chrono::minutes(10);
const char* connection_helper_state::name = "connection_helper";
behavior datagram_connection_broker(broker* self, uint16_t port,
network::address_listing addresses,
actor system_broker) {
auto& mx = self->system().middleman().backend();
auto& this_node = self->system().node();
auto app_id = get_or(self->config(), "middleman.app-identifier",
defaults::middleman::app_identifier);
for (auto& kvp : addresses) {
for (auto& addr : kvp.second) {
auto eptr = mx.new_remote_udp_endpoint(addr, port);
if (eptr) {
auto hdl = (*eptr)->hdl();
self->add_datagram_servant(std::move(*eptr));
basp::instance::write_client_handshake(self->context(),
self->wr_buf(hdl), this_node,
app_id);
}
}
}
return {
[=](new_datagram_msg& msg) {
auto hdl = msg.handle;
self->send(system_broker, std::move(msg), self->take(hdl), port);
self->quit();
},
after(autoconnect_timeout) >> [=]() {
CAF_LOG_TRACE(CAF_ARG(""));
// nothing heard in about 10 minutes... just a call it a day, then
CAF_LOG_INFO("aborted direct connection attempt after 10min");
self->quit(exit_reason::user_shutdown);
}
};
}
behavior connection_helper(stateful_actor<connection_helper_state>* self,
actor b) {
CAF_LOG_TRACE(CAF_ARG(b));
self->monitor(b);
actor system_broker) {
CAF_LOG_TRACE(CAF_ARG(system_broker));
self->monitor(system_broker);
self->set_down_handler([=](down_msg& dm) {
CAF_LOG_TRACE(CAF_ARG(dm));
self->quit(std::move(dm.reason));
});
return {
// this config is send from the remote `ConfigServ`
// this config is send from the remote `PeerServ`
[=](const std::string& item, message& msg) {
CAF_LOG_TRACE(CAF_ARG(item) << CAF_ARG(msg));
CAF_LOG_DEBUG("received requested config:" << CAF_ARG(msg));
CAF_LOG_DEBUG("received requested address:" << CAF_ARG(msg));
// whatever happens, we are done afterwards
self->quit();
msg.apply({
[&](uint16_t port, network::address_listing& addresses) {
if (item == "basp.default-connectivity-tcp") {
[&](basp::routing_table::endpoint& ep) {
auto port = ep.first;
auto& addrs = ep.second;
auto& mx = self->system().middleman().backend();
for (auto& kvp : addresses) {
for (auto& kvp : addrs) {
for (auto& addr : kvp.second) {
auto hdl = mx.new_tcp_scribe(addr, port);
if (hdl) {
// gotcha! send scribe to our BASP broker
// to initiate handshake etc.
// Gotcha! Send scribe to our broker to initiate handshake etc.
CAF_LOG_INFO("connected directly:" << CAF_ARG(addr));
self->send(b, connect_atom::value, *hdl, port);
self->send(system_broker, connect_atom::value, *hdl, port);
return;
}
}
}
CAF_LOG_INFO("could not connect to node directly");
} else if (item == "basp.default-connectivity-udp") {
auto& sys = self->system();
// create new broker to try addresses for communication via UDP
if (get_or(sys.config(), "middleman.attach-utility-actors", false))
self->system().middleman().spawn_broker<hidden>(
datagram_connection_broker, port, std::move(addresses), b
);
else
self->system().middleman().spawn_broker<detached + hidden>(
datagram_connection_broker, port, std::move(addresses), b
);
} else {
CAF_LOG_INFO("aborted direct connection attempt, unknown item: "
<< CAF_ARG(item));
}
CAF_LOG_ERROR("could not connect to node ");
}
});
},
after(autoconnect_timeout) >> [=] {
CAF_LOG_TRACE(CAF_ARG(""));
// nothing heard in about 10 minutes... just a call it a day, then
CAF_LOG_INFO("aborted direct connection attempt after 10min");
CAF_LOG_INFO("aborted connection attempt after 10min");
self->quit(exit_reason::user_shutdown);
}
};
......
......@@ -55,7 +55,7 @@ connection_state instance::handle(execution_unit* ctx,
callee_.purge_state(nid);
return none;
});
tbl_.erase_direct(dm.handle, cb);
tbl_.erase(dm.handle, cb);
return close_connection;
};
std::vector<char>* payload = nullptr;
......@@ -86,27 +86,27 @@ connection_state instance::handle(execution_unit* ctx,
void instance::handle_heartbeat(execution_unit* ctx) {
CAF_LOG_TRACE("");
for (auto& kvp: tbl_.direct_by_hdl_) {
for (auto& kvp: tbl_.nid_by_hdl_) {
CAF_LOG_TRACE(CAF_ARG(kvp.first) << CAF_ARG(kvp.second));
write_heartbeat(ctx, callee_.get_buffer(kvp.first));
callee_.flush(kvp.first);
}
}
optional<routing_table::route> instance::lookup(const node_id& target) {
routing_table::lookup_result instance::lookup(const node_id& target) {
return tbl_.lookup(target);
}
void instance::flush(const routing_table::route& path) {
callee_.flush(path.hdl);
void instance::flush(connection_handle hdl) {
callee_.flush(hdl);
}
void instance::write(execution_unit* ctx, const routing_table::route& r,
void instance::write(execution_unit* ctx, connection_handle hdl,
header& hdr, payload_writer* writer) {
CAF_LOG_TRACE(CAF_ARG(hdr));
CAF_ASSERT(hdr.payload_len == 0 || writer != nullptr);
write(ctx, callee_.get_buffer(r.hdl), hdr, writer);
flush(r);
write(ctx, callee_.get_buffer(hdl), hdr, writer);
flush(hdl);
}
void instance::add_published_actor(uint16_t port,
......@@ -169,8 +169,8 @@ bool instance::dispatch(execution_unit* ctx, const strong_actor_ptr& sender,
CAF_LOG_TRACE(CAF_ARG(sender) << CAF_ARG(receiver)
<< CAF_ARG(mid) << CAF_ARG(msg));
CAF_ASSERT(receiver && system().node() != receiver->node());
auto path = lookup(receiver->node());
if (!path) {
auto lr = lookup(receiver->node());
if (!lr.known) {
notify<hook::message_sending_failed>(sender, receiver, mid, msg);
return false;
}
......@@ -180,9 +180,14 @@ bool instance::dispatch(execution_unit* ctx, const strong_actor_ptr& sender,
});
header hdr{message_type::dispatch_message, 0, 0, mid.integer_value(),
sender ? sender->id() : invalid_actor_id, receiver->id()};
write(ctx, callee_.get_buffer(path->hdl), hdr, &writer);
flush(*path);
notify<hook::message_sent>(sender, path->next_hop, receiver, mid, msg);
if (lr.hdl) {
auto hdl = std::move(*lr.hdl);
write(ctx, callee_.get_buffer(hdl), hdr, &writer);
flush(hdl);
} else {
write(ctx, callee_.get_buffer(receiver->node()), hdr, &writer);
}
notify<hook::message_sent>(sender, receiver->node(), receiver, mid, msg);
return true;
}
......@@ -224,7 +229,7 @@ void instance::write_server_handshake(execution_unit* ctx, buffer_type& out_buf,
auto writer = make_callback([&](serializer& sink) -> error {
auto appid = get_or(callee_.config(), "middleman.app-identifier",
defaults::middleman::app_identifier);
if (auto err = sink(this_node_, appid))
if (auto err = sink(this_node_, appid, tbl_.autoconnect_endpoint()))
return err;
if (pa != nullptr) {
auto i = pa->first ? pa->first->id() : invalid_actor_id;
......@@ -247,7 +252,8 @@ void instance::write_client_handshake(execution_unit* ctx,
CAF_LOG_TRACE(CAF_ARG(this_node) << CAF_ARG(app_identifier));
auto writer = make_callback([&](serializer& sink) -> error {
return sink(const_cast<node_id&>(this_node),
const_cast<std::string&>(app_identifier));
const_cast<std::string&>(app_identifier),
tbl_.autoconnect_endpoint());
});
header hdr{message_type::client_handshake, 0, 0, 0, invalid_actor_id,
invalid_actor_id};
......@@ -295,6 +301,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
switch (hdr.operation) {
case message_type::server_handshake: {
node_id source_node;
basp::routing_table::endpoint autoconn_addr;
actor_id aid = invalid_actor_id;
std::set<std::string> sigs;
if (!payload_valid()) {
......@@ -303,7 +310,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
} else {
binary_deserializer bd{ctx, *payload};
std::string remote_appid;
if (bd(source_node, remote_appid))
if (bd(source_node, remote_appid, autoconn_addr))
return false;
auto appid = get_or(callee_.config(), "middleman.app-identifier",
defaults::middleman::app_identifier);
......@@ -320,25 +327,28 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
callee_.finalize_handshake(source_node, aid, sigs);
return false;
}
auto lr = tbl_.lookup(source_node);
// Close redundant connections.
if (tbl_.lookup_direct(source_node)) {
if (lr.hdl) {
CAF_LOG_DEBUG("close redundant connection:" << CAF_ARG(source_node));
callee_.finalize_handshake(source_node, aid, sigs);
return false;
}
// Add new route to this node.
CAF_LOG_DEBUG("new connection:" << CAF_ARG(source_node));
tbl_.add_direct(hdl, source_node);
// write handshake as client in response
auto path = tbl_.lookup(source_node);
if (!path) {
CAF_LOG_ERROR("no route to host after server handshake");
return false;
}
write_client_handshake(ctx, callee_.get_buffer(path->hdl));
callee_.learned_new_node_directly(source_node, false);
if (lr.known)
tbl_.handle(source_node, hdl);
else
tbl_.add(source_node, hdl);
// Store autoconnect address.
auto peer_server = system().registry().get(atom("PeerServ"));
anon_send(actor_cast<actor>(peer_server), put_atom::value,
to_string(source_node), make_message(std::move(autoconn_addr)));
write_client_handshake(ctx, callee_.get_buffer(hdl));
flush(hdl);
callee_.learned_new_node(source_node);
callee_.finalize_handshake(source_node, aid, sigs);
flush(*path);
callee_.send_buffered_messages(ctx, source_node, hdl);
break;
}
case message_type::client_handshake: {
......@@ -349,7 +359,9 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
binary_deserializer bd{ctx, *payload};
node_id source_node;
std::string remote_appid;
if (bd(source_node, remote_appid))
basp::routing_table::endpoint autoconn_addr;
// TODO: Read addrs separately.
if (bd(source_node, remote_appid, autoconn_addr))
return false;
auto appid = get_if<std::string>(&callee_.config(),
"middleman.app-identifier");
......@@ -358,22 +370,32 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
CAF_LOG_ERROR("app identifier mismatch");
return false;
}
if (tbl_.lookup_direct(source_node)) {
auto lr = tbl_.lookup(source_node);
if (lr.hdl) {
CAF_LOG_DEBUG("received second client handshake:"
<< CAF_ARG(source_node));
break;
}
// Add route to this node.
// Add this node to our contacts.
CAF_LOG_DEBUG("new connection:" << CAF_ARG(source_node));
tbl_.add_direct(hdl, source_node);
callee_.learned_new_node_directly(source_node, false);
// Either add a new node or add the handle to a known one.
if (lr.known)
tbl_.handle(source_node, hdl);
else
tbl_.add(source_node, hdl);
callee_.learned_new_node(source_node);
callee_.send_buffered_messages(ctx, source_node, hdl);
// Store autoconnect address.
auto peer_server = system().registry().get(atom("PeerServ"));
anon_send(actor_cast<actor>(peer_server), put_atom::value,
to_string(source_node), make_message(std::move(autoconn_addr)));
break;
}
case message_type::dispatch_message: {
// Sanity checks.
if (!payload_valid())
return false;
auto source_node = tbl_.lookup_direct(hdl);
auto source_node = tbl_.lookup(hdl);
if (source_node == none) {
CAF_LOG_ERROR("received dispatch_message before handshake");
return false;
......@@ -402,7 +424,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
break;
}
case message_type::announce_proxy: {
auto source_node = tbl_.lookup_direct(hdl);
auto source_node = tbl_.lookup(hdl);
if (source_node == none) {
CAF_LOG_ERROR("received announce_proxy before handshake");
return false;
......@@ -414,7 +436,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
// Sanity checks.
if (!payload_valid())
return false;
auto source_node = tbl_.lookup_direct(hdl);
auto source_node = tbl_.lookup(hdl);
if (source_node == none) {
CAF_LOG_ERROR("received announce_proxy before handshake");
return false;
......@@ -430,7 +452,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
break;
}
case message_type::heartbeat: {
auto source_node = tbl_.lookup_direct(hdl);
auto source_node = tbl_.lookup(hdl);
if (source_node == none) {
CAF_LOG_ERROR("received announce_proxy before handshake");
return false;
......
......@@ -389,7 +389,8 @@ void middleman::init(actor_system_config& cfg) {
.add_message_type<accept_handle>("@accept_handle")
.add_message_type<connection_handle>("@connection_handle")
.add_message_type<connection_passivated_msg>("@connection_passivated_msg")
.add_message_type<acceptor_passivated_msg>("@acceptor_passivated_msg");
.add_message_type<acceptor_passivated_msg>("@acceptor_passivated_msg")
.add_message_type<basp::routing_table::endpoint>("@autoconnect_endpoint");
// compute and set ID for this network node
node_id this_node{node_id::data::create_singleton()};
system().node_.swap(this_node);
......
......@@ -33,123 +33,96 @@ routing_table::~routing_table() {
// nop
}
optional<routing_table::route> routing_table::lookup(const node_id& target) {
auto hdl = lookup_direct(target);
if (hdl)
return route{target, *hdl};
// pick first available indirect route
auto i = indirect_.find(target);
if (i != indirect_.end()) {
auto& hops = i->second;
while (!hops.empty()) {
auto& hop = *hops.begin();
hdl = lookup_direct(hop);
if (hdl)
return route{hop, *hdl};
hops.erase(hops.begin());
}
}
return none;
node_id routing_table::lookup(const connection_handle& hdl) const {
return get_opt(nid_by_hdl_, hdl, none);
}
node_id routing_table::lookup_direct(const connection_handle& hdl) const {
return get_opt(direct_by_hdl_, hdl, none);
routing_table::lookup_result routing_table::lookup(const node_id& nid) const {
auto i = node_information_base_.find(nid);
if (i != node_information_base_.end())
return {true, i->second.hdl};
return {false, none};
}
optional<connection_handle>
routing_table::lookup_direct(const node_id& nid) const {
auto i = direct_by_nid_.find(nid);
if (i != direct_by_nid_.end())
return i->second;
return none;
void routing_table::erase(const connection_handle& hdl, erase_callback& cb) {
auto i = nid_by_hdl_.find(hdl);
if (i == nid_by_hdl_.end())
return;
cb(i->second);
parent_->parent().notify<hook::connection_lost>(i->second);
node_information_base_.erase(i->second);
nid_by_hdl_.erase(i->first);
// TODO: Look through other nodes and remove this one as an origin?
}
node_id routing_table::lookup_indirect(const node_id& nid) const {
auto i = indirect_.find(nid);
if (i == indirect_.end())
return none;
if (i->second.empty())
return none;
return *i->second.begin();
void routing_table::add(const node_id& nid, const connection_handle& hdl) {
CAF_ASSERT(nid_by_hdl_.count(hdl) == 0);
CAF_ASSERT(node_information_base_.count(nid) == 0);
nid_by_hdl_.emplace(hdl, nid);
node_information_base_[nid] = node_info{hdl, none};
parent_->parent().notify<hook::new_connection_established>(nid);
}
void routing_table::blacklist(const node_id& hop, const node_id& dest) {
blacklist_[dest].emplace(hop);
auto i = indirect_.find(dest);
if (i == indirect_.end())
return;
i->second.erase(hop);
if (i->second.empty())
indirect_.erase(i);
void routing_table::add(const node_id& nid, const node_id& origin) {
CAF_ASSERT(node_information_base_.count(nid) == 0);
node_information_base_[nid] = node_info{none, origin};
// TODO: Some new related hook?
//parent_->parent().notify<hook::new_connection_established>(nid);
}
void routing_table::erase_direct(const connection_handle& hdl,
erase_callback& cb) {
auto i = direct_by_hdl_.find(hdl);
if (i == direct_by_hdl_.end())
return;
cb(i->second);
parent_->parent().notify<hook::connection_lost>(i->second);
direct_by_nid_.erase(i->second);
direct_by_hdl_.erase(i->first);
void routing_table::add(const node_id& nid) {
//CAF_ASSERT(hdl_by_nid_.count(nid) == 0);
CAF_ASSERT(node_information_base_.count(nid) == 0);
node_information_base_[nid] = node_info{none, none};
// TODO: Some new related hook?
//parent_->parent().notify<hook::new_connection_established>(nid);
}
bool routing_table::erase_indirect(const node_id& dest) {
auto i = indirect_.find(dest);
if (i == indirect_.end())
bool routing_table::reachable(const node_id& dest) {
auto i = node_information_base_.find(dest);
if (i != node_information_base_.end())
return i->second.hdl != none;
return false;
}
bool routing_table::origin(const node_id& nid, const node_id& origin) {
auto i = node_information_base_.find(nid);
if (i == node_information_base_.end())
return false;
if (parent_->parent().has_hook())
for (auto& nid : i->second)
parent_->parent().notify<hook::route_lost>(nid, dest);
indirect_.erase(i);
i->second.origin = origin;
return true;
}
void routing_table::add_direct(const connection_handle& hdl,
const node_id& nid) {
CAF_ASSERT(direct_by_hdl_.count(hdl) == 0);
CAF_ASSERT(direct_by_nid_.count(nid) == 0);
direct_by_hdl_.emplace(hdl, nid);
direct_by_nid_.emplace(nid, hdl);
parent_->parent().notify<hook::new_connection_established>(nid);
optional<node_id> routing_table::origin(const node_id& nid) {
auto i = node_information_base_.find(nid);
if (i == node_information_base_.end())
return none;
return i->second.origin;
}
bool routing_table::add_indirect(const node_id& hop, const node_id& dest) {
auto i = blacklist_.find(dest);
if (i == blacklist_.end() || i->second.count(hop) == 0) {
auto& hops = indirect_[dest];
auto added_first = hops.empty();
hops.emplace(hop);
parent_->parent().notify<hook::new_route_added>(hop, dest);
return added_first;
}
return false; // blacklisted
bool routing_table::handle(const node_id& nid, const connection_handle& hdl) {
auto i = node_information_base_.find(nid);
if (i == node_information_base_.end())
return false;
i->second.hdl = hdl;
nid_by_hdl_.emplace(hdl, nid);
return true;
}
bool routing_table::reachable(const node_id& dest) {
return direct_by_nid_.count(dest) > 0 || indirect_.count(dest) > 0;
optional<connection_handle> routing_table::handle(const node_id& nid) {
auto i = node_information_base_.find(nid);
if (i == node_information_base_.end())
return none;
return i->second.hdl;
}
const routing_table::endpoint& routing_table::autoconnect_endpoint() {
return autoconnect_endpoint_;
}
size_t routing_table::erase(const node_id& dest, erase_callback& cb) {
cb(dest);
size_t res = 0;
auto i = indirect_.find(dest);
if (i != indirect_.end()) {
res = i->second.size();
for (auto& nid : i->second) {
cb(nid);
parent_->parent().notify<hook::route_lost>(nid, dest);
}
indirect_.erase(i);
}
auto hdl = lookup_direct(dest);
if (hdl) {
direct_by_hdl_.erase(*hdl);
direct_by_nid_.erase(dest);
parent_->parent().notify<hook::connection_lost>(dest);
++res;
}
return res;
void routing_table::autoconnect_endpoint(uint16_t port,
network::address_listing addrs) {
autoconnect_endpoint_ = {port, std::move(addrs)};
}
} // namespace basp
......
......@@ -25,6 +25,8 @@
#include <thread>
#include <vector>
#include "caf/test/io_dsl.hpp"
#include "caf/all.hpp"
#include "caf/io/all.hpp"
......@@ -39,6 +41,14 @@ using std::string;
using ping_atom = atom_constant<atom("ping")>;
using pong_atom = atom_constant<atom("pong")>;
using set_atom = atom_constant<atom("set")>;
using begin_atom = atom_constant<atom("begin")>;
using middle_atom = atom_constant<atom("middle")>;
using end_atom = atom_constant<atom("end")>;
using msg_atom = atom_constant<atom("msg")>;
using done_atom = atom_constant<atom("shutdown")>;
/*
This test checks whether automatic connections work as expected
......@@ -65,223 +75,282 @@ using pong_atom = atom_constant<atom("pong")>;
*/
/*
std::thread run_prog(const char* arg, uint16_t port, bool use_asio) {
return detail::run_sub_unit_test(invalid_actor,
test::engine::path(),
test::engine::max_runtime(),
CAF_XSTR(CAF_SUITE),
use_asio,
{"--port=" + std::to_string(port), arg});
}
namespace {
constexpr uint16_t port_earth = 12340;
constexpr uint16_t port_mars = 12341;
constexpr uint16_t port_jupiter = 12342;
// we run the same code on all three nodes, a simple ping-pong client
struct testee_state {
std::set<actor> buddies;
uint16_t port = 0;
const char* name = "testee";
// Used for the tests with the test backend.
class config : public actor_system_config {
public:
config() {
load<caf::io::middleman, io::network::test_multiplexer>();
set("scheduler.policy", caf::atom("testing"));
set("middleman.detach-utility-actors", false);
set("middleman.enable-automatic-connections", true);
set("middleman.enable-tcp", true);
set("middleman.enable-udp", false);
}
};
// Used for the tests with the default multiplexer backend.
class simple_config : public actor_system_config {
public:
simple_config() {
load<caf::io::middleman>();
set("middleman.enable-automatic-connections", true);
set("middleman.enable-tcp", true);
set("middleman.enable-udp", false);
}
};
behavior testee(stateful_actor<testee_state>* self) {
class fixture {
public:
fixture() : earth(cfg_earth), mars(cfg_mars), jupiter(cfg_jupiter) {
CAF_MESSAGE("Earth : " << to_string(earth.node()));
CAF_MESSAGE("Mars : " << to_string(mars.node()));
CAF_MESSAGE("Jupiter: " << to_string(jupiter.node()));
}
simple_config cfg_earth;
simple_config cfg_mars;
simple_config cfg_jupiter;
actor_system earth{cfg_earth};
actor_system mars{cfg_mars};
actor_system jupiter{cfg_jupiter};
};
struct cache {
actor tmp;
};
behavior test_actor(stateful_actor<cache>* self, std::string location,
bool quit_directly) {
return {
[self](ping_atom, actor buddy, bool please_broadcast) -> message {
if (please_broadcast)
for (auto& x : self->state.buddies)
if (x != buddy)
send_as(buddy, x, ping_atom::value, buddy, false);
self->state.buddies.emplace(std::move(buddy));
return make_message(pong_atom::value, self);
[=](set_atom, actor val) {
self->state.tmp = val;
},
[self](pong_atom, actor buddy) {
self->state.buddies.emplace(std::move(buddy));
[=](begin_atom) {
CAF_REQUIRE(self->state.tmp);
CAF_MESSAGE("starting messaging on " << location);
self->send(self->state.tmp, middle_atom::value, self);
},
[self](put_atom, uint16_t new_port) {
self->state.port = new_port;
[=](middle_atom, actor start) {
CAF_REQUIRE(self->state.tmp);
CAF_MESSAGE("forwaring message on " << location);
self->send(self->state.tmp, end_atom::value, start, self);
},
[self](get_atom) {
return self->state.port;
[=](end_atom, actor start, actor middle) {
CAF_MESSAGE("message arrived on " << location);
if (quit_directly) {
CAF_MESSAGE("telling other nodes to quit from " << location);
self->send(start, done_atom::value);
self->send(middle, done_atom::value);
self->send(self, done_atom::value);
} else {
CAF_MESSAGE("telling intermediate node to quit from " << location);
self->state.tmp = start;
self->send(middle, done_atom::value);
}
},
[=](msg_atom) {
CAF_REQUIRE(self->state.tmp);
CAF_MESSAGE("telling tmp actor to quit from " << location);
self->send(self->state.tmp, done_atom::value);
self->send(self, done_atom::value);
},
[=](done_atom) {
CAF_MESSAGE("actor on " << location << " is quitting");
self->quit();
}
};
}
void run_earth(bool use_asio, bool as_server, uint16_t pub_port) {
scoped_actor self{system};
struct captain : hook {
public:
captain(actor parent) : parent_(std::move(parent)) {
// nop
}
} // namespace <anonymous>
void new_connection_established_cb(const node_id& node) override {
anon_send(parent_, put_atom::value, node);
call_next<hook::new_connection_established>(node);
}
void new_remote_actor_cb(const actor_addr& addr) override {
anon_send(parent_, put_atom::value, addr);
call_next<hook::new_remote_actor>(addr);
}
CAF_TEST_FIXTURE_SCOPE(autoconn_tcp_simple_test, fixture)
void connection_lost_cb(const node_id& dest) override {
anon_send(parent_, delete_atom::value, dest);
}
CAF_TEST(build_triangle_simple_tcp) {
CAF_MESSAGE("setting up Earth");
auto on_earth = earth.spawn(test_actor, "Earth", true);
auto earth_port = earth.middleman().publish(on_earth, 0);
CAF_REQUIRE(earth_port);
CAF_MESSAGE("Earth reachable via " << *earth_port);
CAF_MESSAGE("setting up Mars");
auto from_earth = mars.middleman().remote_actor("localhost", *earth_port);
CAF_REQUIRE(from_earth);
auto on_mars = mars.spawn(test_actor, "Mars", true);
anon_send(on_mars, set_atom::value, *from_earth);
auto mars_port = mars.middleman().publish(on_mars, 0);
CAF_REQUIRE(mars_port);
CAF_MESSAGE("Mars reachable via " << *mars_port);
CAF_MESSAGE("setting up Jupiter");
auto from_mars = jupiter.middleman().remote_actor("localhost", *mars_port);
CAF_REQUIRE(from_mars);
auto on_jupiter = jupiter.spawn(test_actor, "Jupiter", true);
anon_send(on_jupiter, set_atom::value, *from_mars);
CAF_MESSAGE("forwarding an actor from Jupiter to Earth via Mars");
anon_send(on_jupiter, begin_atom::value);
jupiter.await_all_actors_done();
mars.await_all_actors_done();
earth.await_all_actors_done();
}
private:
actor parent_;
};
middleman::instance()->add_hook<captain>(self);
auto aut = system.spawn(testee);
auto port = publish(aut, pub_port);
CAF_MESSAGE("published testee at port " << port);
std::thread mars_process;
std::thread jupiter_process;
// launch process for Mars
if (!as_server) {
CAF_MESSAGE("launch process for Mars");
mars_process = run_prog("--mars", port, use_asio);
}
CAF_MESSAGE("wait for Mars to connect");
node_id mars;
self->receive(
[&](put_atom, const node_id& nid) {
mars = nid;
CAF_MESSAGE(CAF_ARG(mars));
}
);
actor_addr mars_addr;
uint16_t mars_port;
self->receive_while([&] { return mars_addr == invalid_actor_addr; })(
[&](put_atom, const actor_addr& addr) {
auto hdl = actor_cast<actor>(addr);
self->request(hdl, sys_atom::value, get_atom::value, "info").then(
[&](ok_atom, const string&, const actor_addr&, const string& name) {
if (name != "testee")
return;
mars_addr = addr;
CAF_MESSAGE(CAF_ARG(mars_addr));
self->request(actor_cast<actor>(mars_addr), get_atom::value).then(
[&](uint16_t mp) {
CAF_MESSAGE("mars published its actor at port " << mp);
mars_port = mp;
}
);
}
);
}
);
// launch process for Jupiter
if (!as_server) {
CAF_MESSAGE("launch process for Jupiter");
jupiter_process = run_prog("--jupiter", mars_port, use_asio);
}
CAF_MESSAGE("wait for Jupiter to connect");
self->receive(
[](put_atom, const node_id& jupiter) {
CAF_MESSAGE(CAF_ARG(jupiter));
}
);
actor_addr jupiter_addr;
self->receive_while([&] { return jupiter_addr == invalid_actor_addr; })(
[&](put_atom, const actor_addr& addr) {
auto hdl = actor_cast<actor>(addr);
self->request(hdl, sys_atom::value, get_atom::value, "info").then(
[&](ok_atom, const string&, const actor_addr&, const string& name) {
if (name != "testee")
return;
jupiter_addr = addr;
CAF_MESSAGE(CAF_ARG(jupiter_addr));
}
);
}
);
CAF_MESSAGE("shutdown Mars");
anon_send_exit(mars_addr, exit_reason::kill);
if (mars_process.joinable())
mars_process.join();
self->receive(
[&](delete_atom, const node_id& nid) {
CAF_CHECK(nid == mars);
CAF_TEST(break_triangle_simple_tcp) {
actor on_earth;
actor on_jupiter;
{
simple_config conf;
actor_system mars(conf);
// Earth.
CAF_MESSAGE("setting up Earth");
on_earth = earth.spawn(test_actor, "Earth", false);
auto earth_port = earth.middleman().publish(on_earth, 0);
CAF_REQUIRE(earth_port);
CAF_MESSAGE("Earth reachable via " << *earth_port);
// Mars.
CAF_MESSAGE("setting up Mars");
auto from_earth = mars.middleman().remote_actor("localhost", *earth_port);
CAF_REQUIRE(from_earth);
auto on_mars = mars.spawn(test_actor, "Mars", false);
anon_send(on_mars, set_atom::value, *from_earth);
auto mars_port = mars.middleman().publish(on_mars, 0);
CAF_REQUIRE(mars_port);
CAF_MESSAGE("Mars reachable via " << *mars_port);
// Jupiter.
CAF_MESSAGE("setting up Jupiter");
auto from_mars = jupiter.middleman().remote_actor("localhost", *mars_port);
CAF_REQUIRE(from_mars);
on_jupiter = jupiter.spawn(test_actor, "Jupiter", false);
anon_send(on_jupiter, set_atom::value, *from_mars);
// Trigger the connection setup.
CAF_MESSAGE("forwarding an actor from Jupiter to Earth via Mars");
anon_send(on_jupiter, begin_atom::value);
mars.await_all_actors_done();
// Leaving the scope will shutdown Mars.
}
);
CAF_MESSAGE("check whether we still can talk to Jupiter");
self->send(aut, ping_atom::value, self, true);
std::set<actor_addr> found;
int i = 0;
self->receive_for(i, 2)(
[&](pong_atom, const actor&) {
found.emplace(self->current_sender());
}
);
std::set<actor_addr> expected{aut.address(), jupiter_addr};
CAF_CHECK(found == expected);
CAF_MESSAGE("shutdown Jupiter");
anon_send_exit(jupiter_addr, exit_reason::kill);
if (jupiter_process.joinable())
jupiter_process.join();
anon_send_exit(aut, exit_reason::kill);
// Let the remaining nodes communicate.
anon_send(on_earth, msg_atom::value);
jupiter.await_all_actors_done();
earth.await_all_actors_done();
}
void run_mars(uint16_t port_to_earth, uint16_t pub_port) {
auto aut = system.spawn(testee);
auto port = publish(aut, pub_port);
anon_send(aut, put_atom::value, port);
CAF_MESSAGE("published testee at port " << port);
auto earth = remote_actor("localhost", port_to_earth);
send_as(aut, earth, ping_atom::value, aut, false);
}
CAF_TEST_FIXTURE_SCOPE_END()
void run_jupiter(uint16_t port_to_mars) {
auto aut = system.spawn(testee);
auto mars = remote_actor("localhost", port_to_mars);
send_as(aut, mars, ping_atom::value, aut, true);
}
*/
CAF_TEST_FIXTURE_SCOPE(autoconn_tcp_test,
belt_fixture<test_coordinator_fixture<config>>)
CAF_TEST(triangle_setup) {
// this unit test is temporarily disabled until problems
// with OBS are sorted out or new actor_system API is in place
CAF_TEST(build_triangle_tcp) {
CAF_MESSAGE("Earth : " << to_string(earth.sys.node()));
CAF_MESSAGE("Mars : " << to_string(mars.sys.node()));
CAF_MESSAGE("Jupiter: " << to_string(jupiter.sys.node()));
// Earth.
CAF_MESSAGE("setting up Earth");
auto on_earth = earth.sys.spawn(test_actor, "Earth", true);
CAF_MESSAGE("run initialization code");
exec_all();
CAF_MESSAGE("prepare connection");
prepare_connection(earth, mars, "earth", port_earth);
CAF_MESSAGE("publish dummy on earth");
earth.publish(on_earth, port_earth);
// Mars.
CAF_MESSAGE("setting up Mars");
auto from_earth = mars.remote_actor("earth", port_earth);
CAF_REQUIRE(from_earth);
auto on_mars = mars.sys.spawn(test_actor, "Mars", true);
anon_send(on_mars, set_atom::value, from_earth);
CAF_MESSAGE("run initialization code");
exec_all();
CAF_MESSAGE("prepare connection");
prepare_connection(mars, jupiter, "mars", port_mars);
CAF_MESSAGE("publish dummy on mars");
mars.publish(on_mars, port_mars);
// Jupiter
CAF_MESSAGE("setting up Jupiter");
auto from_mars = jupiter.remote_actor("mars", port_mars);
CAF_REQUIRE(from_mars);
auto on_jupiter = jupiter.sys.spawn(test_actor, "Jupiter", true);
anon_send(on_jupiter, set_atom::value, from_mars);
exec_all();
// This handle will be created by the test multiplexer for the automatically
// opened socket when automatic connections are enabled.
auto hdl_jupiter = accept_handle::from_int(std::numeric_limits<int64_t>::max());
// Prepare automatic connection between Jupiter and Earth.
prepare_connection(jupiter, earth, "jupiter", port_jupiter, hdl_jupiter);
// Add the address information for this test to the config server on Mars.
auto mars_config_server = mars.sys.registry().get(atom("PeerServ"));
network::address_listing interfaces{
{network::protocol::ipv4, std::vector<std::string>{"jupiter"}}
};
basp::routing_table::endpoint addrs{port_jupiter, interfaces};
anon_send(actor_cast<actor>(mars_config_server), put_atom::value,
to_string(jupiter.sys.node()), make_message(addrs));
// Trigger the automatic connection setup.
CAF_MESSAGE("forwarding an actor from Jupiter to Earth via Mars");
anon_send(on_jupiter, begin_atom::value);
exec_all();
}
/*
CAF_TEST(triangle_setup) {
uint16_t port = 0;
uint16_t publish_port = 0;
auto argv = test::engine::argv();
auto argc = test::engine::argc();
auto r = message_builder(argv, argv + argc).extract_opts({
{"port,p", "port of remote side (when running mars or jupiter)", port},
{"mars", "run mars"},
{"jupiter", "run jupiter"},
{"use-asio", "use ASIO network backend (if available)"},
{"server,s", "run in server mode (don't run clients)", publish_port}
});
// check arguments
bool is_mars = r.opts.count("mars") > 0;
bool is_jupiter = r.opts.count("jupiter") > 0;
bool has_port = r.opts.count("port") > 0;
if (((is_mars || is_jupiter) && !has_port) || (is_mars && is_jupiter)) {
CAF_ERROR("need a port when running Mars or Jupiter and cannot "
"both at the same time");
return;
}
// enable automatic connections
anon_send(whereis(atom("ConfigServ")), put_atom::value,
"middleman.enable-automatic-connections", make_message(true));
auto use_asio = r.opts.count("use-asio") > 0;
# ifdef CAF_USE_ASIO
if (use_asio) {
CAF_MESSAGE("enable ASIO backend");
set_middleman<network::asio_multiplexer>();
}
# endif // CAF_USE_ASIO
auto as_server = r.opts.count("server") > 0;
if (is_mars)
run_mars(port, publish_port);
else if (is_jupiter)
run_jupiter(port);
else
run_earth(use_asio, as_server, publish_port);
await_all_actors_done();
shutdown();
CAF_TEST(break_triangle_tcp) {
CAF_MESSAGE("Earth : " << to_string(earth.sys.node()));
CAF_MESSAGE("Mars : " << to_string(mars.sys.node()));
CAF_MESSAGE("Jupiter: " << to_string(jupiter.sys.node()));
// Earth.
CAF_MESSAGE("setting up Earth");
auto on_earth = earth.sys.spawn(test_actor, "Earth", false);
CAF_MESSAGE("run initialization code");
exec_all();
CAF_MESSAGE("prepare connection");
prepare_connection(earth, mars, "earth", port_earth);
CAF_MESSAGE("publish dummy on earth");
earth.publish(on_earth, port_earth);
// Mars.
CAF_MESSAGE("setting up Mars");
auto from_earth = mars.remote_actor("earth", port_earth);
CAF_REQUIRE(from_earth);
auto on_mars = mars.sys.spawn(test_actor, "Mars", false);
anon_send(on_mars, set_atom::value, from_earth);
CAF_MESSAGE("run initialization code");
exec_all();
CAF_MESSAGE("prepare connection");
prepare_connection(mars, jupiter, "mars", port_mars);
CAF_MESSAGE("publish dummy on mars");
mars.publish(on_mars, port_mars);
// Jupiter.
CAF_MESSAGE("setting up Jupiter");
auto from_mars = jupiter.remote_actor("mars", port_mars);
CAF_REQUIRE(from_mars);
auto on_jupiter = jupiter.sys.spawn(test_actor, "Jupiter", false);
anon_send(on_jupiter, set_atom::value, from_mars);
exec_all();
// This handle will be created by the test multiplexer for the automatically
// opened socket when automatic connections are enabled.
auto hdl_jupiter = accept_handle::from_int(std::numeric_limits<int64_t>::max());
// Prepare automatic connection between Jupiter and Earth.
prepare_connection(jupiter, earth, "jupiter", port_jupiter, hdl_jupiter);
// Add the address information for this test to the config server on Mars.
auto mars_config_server = mars.sys.registry().get(atom("PeerServ"));
network::address_listing interfaces{
{network::protocol::ipv4, std::vector<std::string>{"jupiter"}}
};
basp::routing_table::endpoint addrs{port_jupiter, interfaces};
anon_send(actor_cast<actor>(mars_config_server), put_atom::value,
to_string(jupiter.sys.node()), make_message(addrs));
// Trigger the automatic connection setup between the edge nodes.
CAF_MESSAGE("forwarding an actor from Jupiter to Earth via Mars");
anon_send(on_jupiter, begin_atom::value);
exec_all();
// Shutdown the basp broker of the intermediate node.
using caf::io::basp_broker;
auto bhdl = mars.mm.named_broker<basp_broker>(caf::atom("BASP"));
auto bactor =
dynamic_cast<basp_broker*>(caf::actor_cast<caf::abstract_actor*>(bhdl));
anon_send_exit(bactor, exit_reason::kill);
exec_all();
// Let the remaining nodes communicate.
anon_send(on_earth, msg_atom::value);
exec_all();
}
*/
CAF_TEST_FIXTURE_SCOPE_END()
......@@ -75,7 +75,6 @@ constexpr uint64_t default_operation_data = make_message_id().integer_value();
constexpr auto basp_atom = caf::atom("BASP");
constexpr auto spawn_serv_atom = caf::atom("SpawnServ");
constexpr auto config_serv_atom = caf::atom("ConfigServ");
} // namespace <anonymous>
......@@ -263,7 +262,8 @@ public:
void connect_node(node& n,
optional<accept_handle> ax = none,
actor_id published_actor_id = invalid_actor_id,
const set<string>& published_actor_ifs = std::set<std::string>{}) {
const set<string>& published_actor_ifs = set<string>{},
const basp::routing_table::endpoint& autoconn = {}) {
auto src = ax ? *ax : ahdl_;
CAF_MESSAGE("connect remote node " << n.name
<< ", connection ID = " << n.connection.id()
......@@ -276,10 +276,10 @@ public:
mock(hdl,
{basp::message_type::client_handshake, 0, 0, 0, invalid_actor_id,
invalid_actor_id},
n.id, std::string{})
n.id, std::string{}, basp::routing_table::endpoint{})
.receive(hdl, basp::message_type::server_handshake, no_flags, any_vals,
basp::version, published_actor_id, invalid_actor_id, this_node(),
std::string{}, published_actor_id, published_actor_ifs)
std::string{}, autoconn, published_actor_id, published_actor_ifs)
// upon receiving our client handshake, BASP will check
// whether there is a SpawnServ actor on this node
.receive(hdl, basp::message_type::dispatch_message,
......@@ -289,9 +289,9 @@ public:
make_message(sys_atom::value, get_atom::value, "info"));
// test whether basp instance correctly updates the
// routing table upon receiving client handshakes
auto path = unbox(tbl().lookup(n.id));
CAF_CHECK_EQUAL(path.hdl, n.connection);
CAF_CHECK_EQUAL(path.next_hop, n.id);
auto lr = tbl().lookup(n.id);
CAF_REQUIRE(lr.hdl);
CAF_CHECK_EQUAL(*lr.hdl, n.connection);
}
std::pair<basp::header, buffer> read_from_out_buf(connection_handle hdl) {
......@@ -480,7 +480,8 @@ CAF_TEST(non_empty_server_handshake) {
basp::header expected{basp::message_type::server_handshake, 0, 0,
basp::version, self()->id(), invalid_actor_id};
to_buf(expected_buf, expected, nullptr, this_node(), std::string{},
self()->id(), set<string>{"caf::replies_to<@u16>::with<@u16>"});
basp::routing_table::endpoint{}, self()->id(),
set<string>{"caf::replies_to<@u16>::with<@u16>"});
CAF_CHECK_EQUAL(hexstr(buf), hexstr(expected_buf));
}
......@@ -566,10 +567,12 @@ CAF_TEST(remote_actor_and_send) {
mock(jupiter().connection,
{basp::message_type::server_handshake, 0, 0, basp::version,
jupiter().dummy_actor->id(), invalid_actor_id},
jupiter().id, std::string{}, jupiter().dummy_actor->id(), uint32_t{0})
jupiter().id, std::string{}, basp::routing_table::endpoint{},
jupiter().dummy_actor->id(), uint32_t{0})
.receive(jupiter().connection, basp::message_type::client_handshake,
no_flags, any_vals, no_operation_data, invalid_actor_id,
invalid_actor_id, this_node(), std::string{})
invalid_actor_id, this_node(), std::string{},
basp::routing_table::endpoint{})
.receive(jupiter().connection, basp::message_type::dispatch_message,
basp::header::named_receiver_flag, any_vals,
default_operation_data, any_vals, invalid_actor_id,
......@@ -656,17 +659,10 @@ CAF_TEST_FIXTURE_SCOPE_END()
CAF_TEST_FIXTURE_SCOPE(basp_tests_with_autoconn, autoconn_enabled_fixture)
CAF_TEST_DISABLED(automatic_connection) {
// this tells our BASP broker to enable the automatic connection feature
//anon_send(aut(), ok_atom::value,
// "middleman.enable-automatic-connections", make_message(true));
//mpx()->exec_runnable(); // process publish message in basp_broker
// jupiter [remote hdl 0] -> mars [remote hdl 1] -> earth [this_node]
// (this node receives a message from jupiter via mars and responds via mars,
// but then also establishes a connection to jupiter directly)
CAF_TEST(read_address_after_handshake) {
auto check_node_in_tbl = [&](node& n) {
auto hdl = tbl().lookup_direct(n.id);
CAF_REQUIRE(hdl);
auto lr = tbl().lookup(n.id);
CAF_REQUIRE(lr.hdl);
};
mpx()->provide_scribe("jupiter", 8080, jupiter().connection);
CAF_CHECK(mpx()->has_pending_scribe("jupiter", 8080));
......@@ -676,80 +672,32 @@ CAF_TEST_DISABLED(automatic_connection) {
publish(self(), 4242);
mpx()->flush_runnables(); // process publish message in basp_broker
CAF_MESSAGE("connect to mars");
connect_node(mars(), ax, self()->id());
//CAF_CHECK_EQUAL(tbl().lookup_direct(mars().id).id(), mars().connection.id());
auto ep = instance().tbl().autoconnect_endpoint();
connect_node(mars(), ax, self()->id(), set<string>{}, ep);
check_node_in_tbl(mars());
// TODO: this use case is no longer possible. Nodes are required to open a
// connection first, before sending messages (forwarding has been
// removed).
CAF_MESSAGE("simulate that an actor from jupiter "
"sends a message to us via mars");
mock(mars().connection,
{basp::message_type::dispatch_message, 0, 0, 0,
jupiter().dummy_actor->id(), self()->id()},
std::vector<actor_id>{}, make_message("hello from jupiter!"))
.receive(mars().connection, basp::message_type::dispatch_message,
basp::header::named_receiver_flag, any_vals,
default_operation_data, any_vals, invalid_actor_id,
spawn_serv_atom, std::vector<actor_id>{},
make_message(sys_atom::value, get_atom::value, "info"))
.receive(mars().connection, basp::message_type::dispatch_message,
basp::header::named_receiver_flag, any_vals,
default_operation_data,
any_vals, // actor ID of an actor spawned by the BASP broker
invalid_actor_id, config_serv_atom, std::vector<actor_id>{},
make_message(get_atom::value, "basp.default-connectivity-tcp"))
.receive(mars().connection, basp::message_type::announce_proxy, no_flags,
no_payload, no_operation_data, invalid_actor_id,
jupiter().dummy_actor->id());
CAF_CHECK_EQUAL(mpx()->output_buffer(mars().connection).size(), 0u);
CAF_CHECK_EQUAL(tbl().lookup_indirect(jupiter().id), mars().id);
CAF_CHECK_EQUAL(tbl().lookup_indirect(mars().id), none);
auto connection_helper_actor = sys.latest_actor_id();
CAF_CHECK_EQUAL(mpx()->output_buffer(mars().connection).size(), 0u);
// create a dummy config server and respond to the name lookup
CAF_MESSAGE("receive ConfigServ of jupiter");
network::address_listing res;
res[network::protocol::ipv4].emplace_back("jupiter");
mock(mars().connection,
{basp::message_type::dispatch_message, 0, 0, 0, invalid_actor_id,
connection_helper_actor},
std::vector<actor_id>{},
make_message("basp.default-connectivity-tcp",
make_message(uint16_t{8080}, std::move(res))));
// our connection helper should now connect to jupiter and
// send the scribe handle over to the BASP broker
while (mpx()->has_pending_scribe("jupiter", 8080)) {
CAF_MESSAGE("Look for mars address information in our peer server");
auto peer_server = sys.registry().get(atom("PeerServ"));
CAF_MESSAGE("Send request");
self()->send(actor_cast<actor>(peer_server), get_atom::value,
to_string(mars().id));
// process get request and send answer
do {
sched.run();
mpx()->flush_runnables();
}
CAF_REQUIRE(mpx()->output_buffer(mars().connection).empty());
// send handshake from jupiter
mock(jupiter().connection,
{basp::message_type::server_handshake, 0, 0, basp::version,
jupiter().dummy_actor->id(), invalid_actor_id},
std::string{}, jupiter().dummy_actor->id(), uint32_t{0})
.receive(jupiter().connection, basp::message_type::client_handshake,
no_flags, 1u, no_operation_data, invalid_actor_id,
invalid_actor_id, std::string{});
CAF_CHECK_EQUAL(tbl().lookup_indirect(jupiter().id), none);
CAF_CHECK_EQUAL(tbl().lookup_indirect(mars().id), none);
check_node_in_tbl(jupiter());
check_node_in_tbl(mars());
CAF_MESSAGE("receive message from jupiter");
} while (self()->mailbox().empty());
CAF_MESSAGE("Process reply");
self()->receive(
[](const std::string& str) -> std::string {
CAF_CHECK_EQUAL(str, "hello from jupiter!");
return "hello from earth!";
[&](const std::string& item, message& msg) {
// Check that we got an entry under the name of our peer.
CAF_REQUIRE_EQUAL(item, to_string(mars().id));
msg.apply(
[&](basp::routing_table::endpoint& ep) {
// The addresses of our dummy node, thus empty.
CAF_CHECK(ep.second.empty());
}
);
}
);
mpx()->exec_runnable(); // process forwarded message in basp_broker
CAF_MESSAGE("response message must take direct route now");
mock().receive(jupiter().connection, basp::message_type::dispatch_message,
no_flags, any_vals, default_operation_data, self()->id(),
jupiter().dummy_actor->id(), std::vector<actor_id>{},
make_message("hello from earth!"));
CAF_CHECK_EQUAL(mpx()->output_buffer(mars().connection).size(), 0u);
}
CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment