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: ...@@ -123,12 +123,15 @@ public:
friend class abstract_actor; friend class abstract_actor;
/// The number of actors implictly spawned by the actor system on startup. /// 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. /// 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) { 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. /// Returns the internal actor for dynamic spawn operations.
...@@ -142,6 +145,11 @@ public: ...@@ -142,6 +145,11 @@ public:
return internal_actors_[internal_actor_id(atom("ConfigServ"))]; 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() = delete;
actor_system(const actor_system&) = delete; actor_system(const actor_system&) = delete;
actor_system& operator=(const actor_system&) = delete; actor_system& operator=(const actor_system&) = delete;
...@@ -569,6 +577,11 @@ private: ...@@ -569,6 +577,11 @@ private:
internal_actors_[internal_actor_id(atom("ConfigServ"))] = std::move(x); 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 ------------------------------------------------------- // -- member variables -------------------------------------------------------
/// Used to generate ascending actor IDs. /// Used to generate ascending actor IDs.
......
...@@ -166,6 +166,93 @@ behavior spawn_serv_impl(stateful_actor<spawn_serv_state>* self) { ...@@ -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 ------------------------------------------------------------ // -- stream server ------------------------------------------------------------
// The stream server acts as a man-in-the-middle for all streams that cross the // 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) ...@@ -292,10 +379,12 @@ actor_system::actor_system(actor_system_config& cfg)
static constexpr auto Flags = hidden + lazy_init; static constexpr auto Flags = hidden + lazy_init;
spawn_serv(actor_cast<strong_actor_ptr>(spawn<Flags>(spawn_serv_impl))); spawn_serv(actor_cast<strong_actor_ptr>(spawn<Flags>(spawn_serv_impl)));
config_serv(actor_cast<strong_actor_ptr>(spawn<Flags>(config_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 // fire up remaining modules
registry_.start(); registry_.start();
registry_.put(atom("SpawnServ"), spawn_serv()); registry_.put(atom("SpawnServ"), spawn_serv());
registry_.put(atom("ConfigServ"), config_serv()); registry_.put(atom("ConfigServ"), config_serv());
registry_.put(atom("PeerServ"), peer_serv());
for (auto& mod : modules_) for (auto& mod : modules_)
if (mod) if (mod)
mod->start(); mod->start();
......
...@@ -80,14 +80,8 @@ public: ...@@ -80,14 +80,8 @@ public:
std::vector<strong_actor_ptr>& forwarding_stack, std::vector<strong_actor_ptr>& forwarding_stack,
message& msg) = 0; message& msg) = 0;
/// Called whenever BASP learns the ID of a remote node /// Called whenever BASP learns the ID of a remote node.
/// to which it does not have a direct connection. virtual void learned_new_node(const node_id& nid) = 0;
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 if a heartbeat was received from `nid` /// Called if a heartbeat was received from `nid`
virtual void handle_heartbeat(const node_id& nid) = 0; virtual void handle_heartbeat(const node_id& nid) = 0;
...@@ -107,9 +101,19 @@ public: ...@@ -107,9 +101,19 @@ public:
return namespace_.system().config(); 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. /// Returns a reference to the sent buffer.
virtual buffer_type& get_buffer(connection_handle hdl) = 0; 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`. /// Flushes the underlying write buffer of `hdl`.
virtual void flush(connection_handle hdl) = 0; virtual void flush(connection_handle hdl) = 0;
...@@ -135,15 +139,15 @@ public: ...@@ -135,15 +139,15 @@ public:
void handle_heartbeat(execution_unit* ctx); void handle_heartbeat(execution_unit* ctx);
/// Returns a route to `target` or `none` on error. /// 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`. /// 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`. /// Sends a BASP message and implicitly flushes the output buffer of `r`.
/// This function will update `hdr.payload_len` if a payload was written. /// This function will update `hdr.payload_len` if a payload was written.
void write(execution_unit* ctx, const routing_table::route& r, void write(execution_unit* ctx, connection_handle hdl, header& hdr,
header& hdr, payload_writer* writer = nullptr); payload_writer* writer = nullptr);
/// Adds a new actor to the map of published actors. /// Adds a new actor to the map of published actors.
void add_published_actor(uint16_t port, void add_published_actor(uint16_t port,
...@@ -200,9 +204,9 @@ public: ...@@ -200,9 +204,9 @@ public:
buffer_type& out_buf, optional<uint16_t> port); buffer_type& out_buf, optional<uint16_t> port);
/// Writes the client handshake to `buf`. /// 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 node_id& this_node,
const std::string& app_identifier); const std::string& app_identifier);
/// Writes the client handshake to `buf`. /// Writes the client handshake to `buf`.
void write_client_handshake(execution_unit* ctx, buffer_type& buf); void write_client_handshake(execution_unit* ctx, buffer_type& buf);
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "caf/callback.hpp" #include "caf/callback.hpp"
#include "caf/io/abstract_broker.hpp" #include "caf/io/abstract_broker.hpp"
#include "caf/io/basp/buffer_type.hpp" #include "caf/io/basp/buffer_type.hpp"
#include "caf/io/network/interfaces.hpp"
#include "caf/node_id.hpp" #include "caf/node_id.hpp"
namespace caf { namespace caf {
...@@ -36,70 +37,90 @@ namespace basp { ...@@ -36,70 +37,90 @@ namespace basp {
/// BASP peer and provides both direct and indirect paths. /// BASP peer and provides both direct and indirect paths.
class routing_table { class routing_table {
public: public:
using endpoint = std::pair<uint16_t, network::address_listing>;
explicit routing_table(abstract_broker* parent); explicit routing_table(abstract_broker* parent);
virtual ~routing_table(); virtual ~routing_table();
/// Describes a routing path to a node. /// Result for a lookup of a node.
struct route { struct lookup_result {
const node_id& next_hop; /// Tracks whether the node is already known.
connection_handle hdl; bool known;
/// Servant handle to communicate with the node -- if already created.
optional<connection_handle> hdl;
}; };
/// Describes a function object for erase operations that /// Describes a function object for erase operations that
/// is called for each indirectly lost connection. /// is called for each indirectly lost connection.
using erase_callback = callback<const node_id&>; using erase_callback = callback<const node_id&>;
/// Returns a route to `target` or `none` on error. /// Returns the ID of the peer reachable via `hdl` or
optional<route> lookup(const node_id& target);
/// Returns the ID of the peer connected via `hdl` or
/// `none` if `hdl` is unknown. /// `none` if `hdl` is unknown.
node_id lookup_direct(const connection_handle& hdl) const; node_id lookup(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;
/// Returns the next hop that would be chosen for `nid` /// Returns the state for communication with `nid` along with a handle
/// or `none` if there's no indirect route to `nid`. /// if communication is established or `none` if `nid` is unknown.
node_id lookup_indirect(const node_id& nid) const; 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` /// @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. /// Add a new endpoint to the table.
bool add_indirect(const node_id& hop, const node_id& dest); /// @pre `origin != none && nid != none`
void add(const node_id& nid, const node_id& origin);
/// Blacklist the route to `dest` via `hop`. /// Adds a new endpoint to the table that has no attached information.
void blacklist(const node_id& hop, const node_id& dest); /// 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 /// Removes a direct connection and calls `cb` for any node
/// that became unreachable as a result of this operation, /// that became unreachable as a result of this operation,
/// including the node that is assigned as direct path for `hdl`. /// 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 /// Queries whether `dest` is reachable directly.
/// `true` if `dest` had an indirect route, otherwise `false`.
bool erase_indirect(const node_id& dest);
/// Queries whether `dest` is reachable.
bool reachable(const node_id& dest); 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. /// Returns the parent broker.
inline abstract_broker* parent() { inline abstract_broker* parent() {
return 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: 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> template <class Map, class Fallback>
typename Map::mapped_type typename Map::mapped_type
get_opt(const Map& m, const typename Map::key_type& k, Fallback&& x) const { get_opt(const Map& m, const typename Map::key_type& k, Fallback&& x) const {
...@@ -109,16 +130,10 @@ public: ...@@ -109,16 +130,10 @@ public:
return std::forward<Fallback>(x); 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_; abstract_broker* parent_;
std::unordered_map<connection_handle, node_id> direct_by_hdl_; std::unordered_map<connection_handle, node_id> nid_by_hdl_;
std::unordered_map<node_id, connection_handle> direct_by_nid_; std::unordered_map<node_id, node_info> node_information_base_;
indirect_entries indirect_; endpoint autoconnect_endpoint_;
indirect_entries blacklist_;
}; };
/// @} /// @}
......
...@@ -77,17 +77,17 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee { ...@@ -77,17 +77,17 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
std::vector<strong_actor_ptr>& stages, message& msg); std::vector<strong_actor_ptr>& stages, message& msg);
// performs bookkeeping such as managing `spawn_servers` // 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 // inherited from basp::instance::callee
void learned_new_node_directly(const node_id& nid, void send_buffered_messages(execution_unit* ctx, node_id nid,
bool was_indirectly_before) override; connection_handle hdl) override;
// inherited from basp::instance::callee // 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 // 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 // inherited from basp::instance::callee
void flush(connection_handle hdl) override; void flush(connection_handle hdl) override;
...@@ -102,6 +102,9 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee { ...@@ -102,6 +102,9 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
/// Cleans up any state for `hdl`. /// Cleans up any state for `hdl`.
void cleanup(connection_handle hdl); void cleanup(connection_handle hdl);
/// Try to establish a connection to node with `nid`.
void connect(const node_id& nid);
// pointer to ourselves // pointer to ourselves
broker* self; broker* self;
...@@ -146,6 +149,9 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee { ...@@ -146,6 +149,9 @@ struct basp_broker_state : proxy_registry::backend, basp::instance::callee {
// actor // actor
void handle_down_msg(down_msg&); 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; static const char* name;
}; };
......
...@@ -46,13 +46,8 @@ struct connection_helper_state { ...@@ -46,13 +46,8 @@ struct connection_helper_state {
static const char* name; 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, behavior connection_helper(stateful_actor<connection_helper_state>* self,
actor b); actor system_broker);
} // namespace io } // namespace io
} // namespace caf } // namespace caf
This diff is collapsed.
...@@ -34,94 +34,45 @@ auto autoconnect_timeout = std::chrono::minutes(10); ...@@ -34,94 +34,45 @@ auto autoconnect_timeout = std::chrono::minutes(10);
const char* connection_helper_state::name = "connection_helper"; 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, behavior connection_helper(stateful_actor<connection_helper_state>* self,
actor b) { actor system_broker) {
CAF_LOG_TRACE(CAF_ARG(b)); CAF_LOG_TRACE(CAF_ARG(system_broker));
self->monitor(b); self->monitor(system_broker);
self->set_down_handler([=](down_msg& dm) { self->set_down_handler([=](down_msg& dm) {
CAF_LOG_TRACE(CAF_ARG(dm)); CAF_LOG_TRACE(CAF_ARG(dm));
self->quit(std::move(dm.reason)); self->quit(std::move(dm.reason));
}); });
return { return {
// this config is send from the remote `ConfigServ` // this config is send from the remote `PeerServ`
[=](const std::string& item, message& msg) { [=](const std::string& item, message& msg) {
CAF_LOG_TRACE(CAF_ARG(item) << CAF_ARG(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 // whatever happens, we are done afterwards
self->quit(); self->quit();
msg.apply({ msg.apply({
[&](uint16_t port, network::address_listing& addresses) { [&](basp::routing_table::endpoint& ep) {
if (item == "basp.default-connectivity-tcp") { auto port = ep.first;
auto& mx = self->system().middleman().backend(); auto& addrs = ep.second;
for (auto& kvp : addresses) { auto& mx = self->system().middleman().backend();
for (auto& addr : kvp.second) { for (auto& kvp : addrs) {
auto hdl = mx.new_tcp_scribe(addr, port); for (auto& addr : kvp.second) {
if (hdl) { auto hdl = mx.new_tcp_scribe(addr, port);
// gotcha! send scribe to our BASP broker if (hdl) {
// to initiate handshake etc. // Gotcha! Send scribe to our broker to initiate handshake etc.
CAF_LOG_INFO("connected directly:" << CAF_ARG(addr)); 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; 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) >> [=] { after(autoconnect_timeout) >> [=] {
CAF_LOG_TRACE(CAF_ARG("")); CAF_LOG_TRACE(CAF_ARG(""));
// nothing heard in about 10 minutes... just a call it a day, then // 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); self->quit(exit_reason::user_shutdown);
} }
}; };
......
...@@ -55,7 +55,7 @@ connection_state instance::handle(execution_unit* ctx, ...@@ -55,7 +55,7 @@ connection_state instance::handle(execution_unit* ctx,
callee_.purge_state(nid); callee_.purge_state(nid);
return none; return none;
}); });
tbl_.erase_direct(dm.handle, cb); tbl_.erase(dm.handle, cb);
return close_connection; return close_connection;
}; };
std::vector<char>* payload = nullptr; std::vector<char>* payload = nullptr;
...@@ -86,27 +86,27 @@ connection_state instance::handle(execution_unit* ctx, ...@@ -86,27 +86,27 @@ connection_state instance::handle(execution_unit* ctx,
void instance::handle_heartbeat(execution_unit* ctx) { void instance::handle_heartbeat(execution_unit* ctx) {
CAF_LOG_TRACE(""); 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)); CAF_LOG_TRACE(CAF_ARG(kvp.first) << CAF_ARG(kvp.second));
write_heartbeat(ctx, callee_.get_buffer(kvp.first)); write_heartbeat(ctx, callee_.get_buffer(kvp.first));
callee_.flush(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); return tbl_.lookup(target);
} }
void instance::flush(const routing_table::route& path) { void instance::flush(connection_handle hdl) {
callee_.flush(path.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) { header& hdr, payload_writer* writer) {
CAF_LOG_TRACE(CAF_ARG(hdr)); CAF_LOG_TRACE(CAF_ARG(hdr));
CAF_ASSERT(hdr.payload_len == 0 || writer != nullptr); CAF_ASSERT(hdr.payload_len == 0 || writer != nullptr);
write(ctx, callee_.get_buffer(r.hdl), hdr, writer); write(ctx, callee_.get_buffer(hdl), hdr, writer);
flush(r); flush(hdl);
} }
void instance::add_published_actor(uint16_t port, void instance::add_published_actor(uint16_t port,
...@@ -169,8 +169,8 @@ bool instance::dispatch(execution_unit* ctx, const strong_actor_ptr& sender, ...@@ -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_LOG_TRACE(CAF_ARG(sender) << CAF_ARG(receiver)
<< CAF_ARG(mid) << CAF_ARG(msg)); << CAF_ARG(mid) << CAF_ARG(msg));
CAF_ASSERT(receiver && system().node() != receiver->node()); CAF_ASSERT(receiver && system().node() != receiver->node());
auto path = lookup(receiver->node()); auto lr = lookup(receiver->node());
if (!path) { if (!lr.known) {
notify<hook::message_sending_failed>(sender, receiver, mid, msg); notify<hook::message_sending_failed>(sender, receiver, mid, msg);
return false; return false;
} }
...@@ -180,9 +180,14 @@ bool instance::dispatch(execution_unit* ctx, const strong_actor_ptr& sender, ...@@ -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(), header hdr{message_type::dispatch_message, 0, 0, mid.integer_value(),
sender ? sender->id() : invalid_actor_id, receiver->id()}; sender ? sender->id() : invalid_actor_id, receiver->id()};
write(ctx, callee_.get_buffer(path->hdl), hdr, &writer); if (lr.hdl) {
flush(*path); auto hdl = std::move(*lr.hdl);
notify<hook::message_sent>(sender, path->next_hop, receiver, mid, msg); 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; return true;
} }
...@@ -224,7 +229,7 @@ void instance::write_server_handshake(execution_unit* ctx, buffer_type& out_buf, ...@@ -224,7 +229,7 @@ void instance::write_server_handshake(execution_unit* ctx, buffer_type& out_buf,
auto writer = make_callback([&](serializer& sink) -> error { auto writer = make_callback([&](serializer& sink) -> error {
auto appid = get_or(callee_.config(), "middleman.app-identifier", auto appid = get_or(callee_.config(), "middleman.app-identifier",
defaults::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; return err;
if (pa != nullptr) { if (pa != nullptr) {
auto i = pa->first ? pa->first->id() : invalid_actor_id; auto i = pa->first ? pa->first->id() : invalid_actor_id;
...@@ -247,7 +252,8 @@ void instance::write_client_handshake(execution_unit* ctx, ...@@ -247,7 +252,8 @@ void instance::write_client_handshake(execution_unit* ctx,
CAF_LOG_TRACE(CAF_ARG(this_node) << CAF_ARG(app_identifier)); CAF_LOG_TRACE(CAF_ARG(this_node) << CAF_ARG(app_identifier));
auto writer = make_callback([&](serializer& sink) -> error { auto writer = make_callback([&](serializer& sink) -> error {
return sink(const_cast<node_id&>(this_node), 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, header hdr{message_type::client_handshake, 0, 0, 0, invalid_actor_id,
invalid_actor_id}; invalid_actor_id};
...@@ -295,6 +301,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -295,6 +301,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
switch (hdr.operation) { switch (hdr.operation) {
case message_type::server_handshake: { case message_type::server_handshake: {
node_id source_node; node_id source_node;
basp::routing_table::endpoint autoconn_addr;
actor_id aid = invalid_actor_id; actor_id aid = invalid_actor_id;
std::set<std::string> sigs; std::set<std::string> sigs;
if (!payload_valid()) { if (!payload_valid()) {
...@@ -303,7 +310,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -303,7 +310,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
} else { } else {
binary_deserializer bd{ctx, *payload}; binary_deserializer bd{ctx, *payload};
std::string remote_appid; std::string remote_appid;
if (bd(source_node, remote_appid)) if (bd(source_node, remote_appid, autoconn_addr))
return false; return false;
auto appid = get_or(callee_.config(), "middleman.app-identifier", auto appid = get_or(callee_.config(), "middleman.app-identifier",
defaults::middleman::app_identifier); defaults::middleman::app_identifier);
...@@ -320,25 +327,28 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -320,25 +327,28 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
callee_.finalize_handshake(source_node, aid, sigs); callee_.finalize_handshake(source_node, aid, sigs);
return false; return false;
} }
auto lr = tbl_.lookup(source_node);
// Close redundant connections. // Close redundant connections.
if (tbl_.lookup_direct(source_node)) { if (lr.hdl) {
CAF_LOG_DEBUG("close redundant connection:" << CAF_ARG(source_node)); CAF_LOG_DEBUG("close redundant connection:" << CAF_ARG(source_node));
callee_.finalize_handshake(source_node, aid, sigs); callee_.finalize_handshake(source_node, aid, sigs);
return false; return false;
} }
// Add new route to this node. // Add new route to this node.
CAF_LOG_DEBUG("new connection:" << CAF_ARG(source_node)); CAF_LOG_DEBUG("new connection:" << CAF_ARG(source_node));
tbl_.add_direct(hdl, source_node); if (lr.known)
// write handshake as client in response tbl_.handle(source_node, hdl);
auto path = tbl_.lookup(source_node); else
if (!path) { tbl_.add(source_node, hdl);
CAF_LOG_ERROR("no route to host after server handshake"); // Store autoconnect address.
return false; auto peer_server = system().registry().get(atom("PeerServ"));
} anon_send(actor_cast<actor>(peer_server), put_atom::value,
write_client_handshake(ctx, callee_.get_buffer(path->hdl)); to_string(source_node), make_message(std::move(autoconn_addr)));
callee_.learned_new_node_directly(source_node, false); write_client_handshake(ctx, callee_.get_buffer(hdl));
flush(hdl);
callee_.learned_new_node(source_node);
callee_.finalize_handshake(source_node, aid, sigs); callee_.finalize_handshake(source_node, aid, sigs);
flush(*path); callee_.send_buffered_messages(ctx, source_node, hdl);
break; break;
} }
case message_type::client_handshake: { case message_type::client_handshake: {
...@@ -349,7 +359,9 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -349,7 +359,9 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
binary_deserializer bd{ctx, *payload}; binary_deserializer bd{ctx, *payload};
node_id source_node; node_id source_node;
std::string remote_appid; 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; return false;
auto appid = get_if<std::string>(&callee_.config(), auto appid = get_if<std::string>(&callee_.config(),
"middleman.app-identifier"); "middleman.app-identifier");
...@@ -358,22 +370,32 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -358,22 +370,32 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
CAF_LOG_ERROR("app identifier mismatch"); CAF_LOG_ERROR("app identifier mismatch");
return false; 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_LOG_DEBUG("received second client handshake:"
<< CAF_ARG(source_node)); << CAF_ARG(source_node));
break; break;
} }
// Add route to this node. // Add this node to our contacts.
CAF_LOG_DEBUG("new connection:" << CAF_ARG(source_node)); CAF_LOG_DEBUG("new connection:" << CAF_ARG(source_node));
tbl_.add_direct(hdl, source_node); // Either add a new node or add the handle to a known one.
callee_.learned_new_node_directly(source_node, false); 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; break;
} }
case message_type::dispatch_message: { case message_type::dispatch_message: {
// Sanity checks. // Sanity checks.
if (!payload_valid()) if (!payload_valid())
return false; return false;
auto source_node = tbl_.lookup_direct(hdl); auto source_node = tbl_.lookup(hdl);
if (source_node == none) { if (source_node == none) {
CAF_LOG_ERROR("received dispatch_message before handshake"); CAF_LOG_ERROR("received dispatch_message before handshake");
return false; return false;
...@@ -402,7 +424,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -402,7 +424,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
break; break;
} }
case message_type::announce_proxy: { case message_type::announce_proxy: {
auto source_node = tbl_.lookup_direct(hdl); auto source_node = tbl_.lookup(hdl);
if (source_node == none) { if (source_node == none) {
CAF_LOG_ERROR("received announce_proxy before handshake"); CAF_LOG_ERROR("received announce_proxy before handshake");
return false; return false;
...@@ -414,7 +436,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -414,7 +436,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
// Sanity checks. // Sanity checks.
if (!payload_valid()) if (!payload_valid())
return false; return false;
auto source_node = tbl_.lookup_direct(hdl); auto source_node = tbl_.lookup(hdl);
if (source_node == none) { if (source_node == none) {
CAF_LOG_ERROR("received announce_proxy before handshake"); CAF_LOG_ERROR("received announce_proxy before handshake");
return false; return false;
...@@ -430,7 +452,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr, ...@@ -430,7 +452,7 @@ bool instance::handle(execution_unit* ctx, connection_handle hdl, header& hdr,
break; break;
} }
case message_type::heartbeat: { case message_type::heartbeat: {
auto source_node = tbl_.lookup_direct(hdl); auto source_node = tbl_.lookup(hdl);
if (source_node == none) { if (source_node == none) {
CAF_LOG_ERROR("received announce_proxy before handshake"); CAF_LOG_ERROR("received announce_proxy before handshake");
return false; return false;
......
...@@ -389,7 +389,8 @@ void middleman::init(actor_system_config& cfg) { ...@@ -389,7 +389,8 @@ void middleman::init(actor_system_config& cfg) {
.add_message_type<accept_handle>("@accept_handle") .add_message_type<accept_handle>("@accept_handle")
.add_message_type<connection_handle>("@connection_handle") .add_message_type<connection_handle>("@connection_handle")
.add_message_type<connection_passivated_msg>("@connection_passivated_msg") .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 // compute and set ID for this network node
node_id this_node{node_id::data::create_singleton()}; node_id this_node{node_id::data::create_singleton()};
system().node_.swap(this_node); system().node_.swap(this_node);
......
...@@ -33,123 +33,96 @@ routing_table::~routing_table() { ...@@ -33,123 +33,96 @@ routing_table::~routing_table() {
// nop // nop
} }
optional<routing_table::route> routing_table::lookup(const node_id& target) { node_id routing_table::lookup(const connection_handle& hdl) const {
auto hdl = lookup_direct(target); return get_opt(nid_by_hdl_, hdl, none);
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_direct(const connection_handle& hdl) const { routing_table::lookup_result routing_table::lookup(const node_id& nid) const {
return get_opt(direct_by_hdl_, hdl, none); auto i = node_information_base_.find(nid);
if (i != node_information_base_.end())
return {true, i->second.hdl};
return {false, none};
} }
optional<connection_handle> void routing_table::erase(const connection_handle& hdl, erase_callback& cb) {
routing_table::lookup_direct(const node_id& nid) const { auto i = nid_by_hdl_.find(hdl);
auto i = direct_by_nid_.find(nid); if (i == nid_by_hdl_.end())
if (i != direct_by_nid_.end()) return;
return i->second; cb(i->second);
return none; 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 { void routing_table::add(const node_id& nid, const connection_handle& hdl) {
auto i = indirect_.find(nid); CAF_ASSERT(nid_by_hdl_.count(hdl) == 0);
if (i == indirect_.end()) CAF_ASSERT(node_information_base_.count(nid) == 0);
return none; nid_by_hdl_.emplace(hdl, nid);
if (i->second.empty()) node_information_base_[nid] = node_info{hdl, none};
return none; parent_->parent().notify<hook::new_connection_established>(nid);
return *i->second.begin();
} }
void routing_table::blacklist(const node_id& hop, const node_id& dest) { void routing_table::add(const node_id& nid, const node_id& origin) {
blacklist_[dest].emplace(hop); CAF_ASSERT(node_information_base_.count(nid) == 0);
auto i = indirect_.find(dest); node_information_base_[nid] = node_info{none, origin};
if (i == indirect_.end()) // TODO: Some new related hook?
return; //parent_->parent().notify<hook::new_connection_established>(nid);
i->second.erase(hop);
if (i->second.empty())
indirect_.erase(i);
} }
void routing_table::erase_direct(const connection_handle& hdl, void routing_table::add(const node_id& nid) {
erase_callback& cb) { //CAF_ASSERT(hdl_by_nid_.count(nid) == 0);
auto i = direct_by_hdl_.find(hdl); CAF_ASSERT(node_information_base_.count(nid) == 0);
if (i == direct_by_hdl_.end()) node_information_base_[nid] = node_info{none, none};
return; // TODO: Some new related hook?
cb(i->second); //parent_->parent().notify<hook::new_connection_established>(nid);
parent_->parent().notify<hook::connection_lost>(i->second);
direct_by_nid_.erase(i->second);
direct_by_hdl_.erase(i->first);
} }
bool routing_table::erase_indirect(const node_id& dest) { bool routing_table::reachable(const node_id& dest) {
auto i = indirect_.find(dest); auto i = node_information_base_.find(dest);
if (i == indirect_.end()) 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; return false;
if (parent_->parent().has_hook()) i->second.origin = origin;
for (auto& nid : i->second)
parent_->parent().notify<hook::route_lost>(nid, dest);
indirect_.erase(i);
return true; return true;
} }
void routing_table::add_direct(const connection_handle& hdl, optional<node_id> routing_table::origin(const node_id& nid) {
const node_id& nid) { auto i = node_information_base_.find(nid);
CAF_ASSERT(direct_by_hdl_.count(hdl) == 0); if (i == node_information_base_.end())
CAF_ASSERT(direct_by_nid_.count(nid) == 0); return none;
direct_by_hdl_.emplace(hdl, nid); return i->second.origin;
direct_by_nid_.emplace(nid, hdl); }
parent_->parent().notify<hook::new_connection_established>(nid);
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::add_indirect(const node_id& hop, const node_id& dest) { optional<connection_handle> routing_table::handle(const node_id& nid) {
auto i = blacklist_.find(dest); auto i = node_information_base_.find(nid);
if (i == blacklist_.end() || i->second.count(hop) == 0) { if (i == node_information_base_.end())
auto& hops = indirect_[dest]; return none;
auto added_first = hops.empty(); return i->second.hdl;
hops.emplace(hop);
parent_->parent().notify<hook::new_route_added>(hop, dest);
return added_first;
}
return false; // blacklisted
} }
bool routing_table::reachable(const node_id& dest) { const routing_table::endpoint& routing_table::autoconnect_endpoint() {
return direct_by_nid_.count(dest) > 0 || indirect_.count(dest) > 0; return autoconnect_endpoint_;
} }
size_t routing_table::erase(const node_id& dest, erase_callback& cb) { void routing_table::autoconnect_endpoint(uint16_t port,
cb(dest); network::address_listing addrs) {
size_t res = 0; autoconnect_endpoint_ = {port, std::move(addrs)};
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;
} }
} // namespace basp } // namespace basp
......
This diff is collapsed.
...@@ -75,7 +75,6 @@ constexpr uint64_t default_operation_data = make_message_id().integer_value(); ...@@ -75,7 +75,6 @@ constexpr uint64_t default_operation_data = make_message_id().integer_value();
constexpr auto basp_atom = caf::atom("BASP"); constexpr auto basp_atom = caf::atom("BASP");
constexpr auto spawn_serv_atom = caf::atom("SpawnServ"); constexpr auto spawn_serv_atom = caf::atom("SpawnServ");
constexpr auto config_serv_atom = caf::atom("ConfigServ");
} // namespace <anonymous> } // namespace <anonymous>
...@@ -263,7 +262,8 @@ public: ...@@ -263,7 +262,8 @@ public:
void connect_node(node& n, void connect_node(node& n,
optional<accept_handle> ax = none, optional<accept_handle> ax = none,
actor_id published_actor_id = invalid_actor_id, 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_; auto src = ax ? *ax : ahdl_;
CAF_MESSAGE("connect remote node " << n.name CAF_MESSAGE("connect remote node " << n.name
<< ", connection ID = " << n.connection.id() << ", connection ID = " << n.connection.id()
...@@ -276,10 +276,10 @@ public: ...@@ -276,10 +276,10 @@ public:
mock(hdl, mock(hdl,
{basp::message_type::client_handshake, 0, 0, 0, invalid_actor_id, {basp::message_type::client_handshake, 0, 0, 0, invalid_actor_id,
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, .receive(hdl, basp::message_type::server_handshake, no_flags, any_vals,
basp::version, published_actor_id, invalid_actor_id, this_node(), 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 // upon receiving our client handshake, BASP will check
// whether there is a SpawnServ actor on this node // whether there is a SpawnServ actor on this node
.receive(hdl, basp::message_type::dispatch_message, .receive(hdl, basp::message_type::dispatch_message,
...@@ -289,9 +289,9 @@ public: ...@@ -289,9 +289,9 @@ public:
make_message(sys_atom::value, get_atom::value, "info")); make_message(sys_atom::value, get_atom::value, "info"));
// test whether basp instance correctly updates the // test whether basp instance correctly updates the
// routing table upon receiving client handshakes // routing table upon receiving client handshakes
auto path = unbox(tbl().lookup(n.id)); auto lr = tbl().lookup(n.id);
CAF_CHECK_EQUAL(path.hdl, n.connection); CAF_REQUIRE(lr.hdl);
CAF_CHECK_EQUAL(path.next_hop, n.id); CAF_CHECK_EQUAL(*lr.hdl, n.connection);
} }
std::pair<basp::header, buffer> read_from_out_buf(connection_handle hdl) { std::pair<basp::header, buffer> read_from_out_buf(connection_handle hdl) {
...@@ -480,7 +480,8 @@ CAF_TEST(non_empty_server_handshake) { ...@@ -480,7 +480,8 @@ CAF_TEST(non_empty_server_handshake) {
basp::header expected{basp::message_type::server_handshake, 0, 0, basp::header expected{basp::message_type::server_handshake, 0, 0,
basp::version, self()->id(), invalid_actor_id}; basp::version, self()->id(), invalid_actor_id};
to_buf(expected_buf, expected, nullptr, this_node(), std::string{}, 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)); CAF_CHECK_EQUAL(hexstr(buf), hexstr(expected_buf));
} }
...@@ -566,10 +567,12 @@ CAF_TEST(remote_actor_and_send) { ...@@ -566,10 +567,12 @@ CAF_TEST(remote_actor_and_send) {
mock(jupiter().connection, mock(jupiter().connection,
{basp::message_type::server_handshake, 0, 0, basp::version, {basp::message_type::server_handshake, 0, 0, basp::version,
jupiter().dummy_actor->id(), invalid_actor_id}, 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, .receive(jupiter().connection, basp::message_type::client_handshake,
no_flags, any_vals, no_operation_data, invalid_actor_id, 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, .receive(jupiter().connection, basp::message_type::dispatch_message,
basp::header::named_receiver_flag, any_vals, basp::header::named_receiver_flag, any_vals,
default_operation_data, any_vals, invalid_actor_id, default_operation_data, any_vals, invalid_actor_id,
...@@ -656,17 +659,10 @@ CAF_TEST_FIXTURE_SCOPE_END() ...@@ -656,17 +659,10 @@ CAF_TEST_FIXTURE_SCOPE_END()
CAF_TEST_FIXTURE_SCOPE(basp_tests_with_autoconn, autoconn_enabled_fixture) CAF_TEST_FIXTURE_SCOPE(basp_tests_with_autoconn, autoconn_enabled_fixture)
CAF_TEST_DISABLED(automatic_connection) { CAF_TEST(read_address_after_handshake) {
// 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)
auto check_node_in_tbl = [&](node& n) { auto check_node_in_tbl = [&](node& n) {
auto hdl = tbl().lookup_direct(n.id); auto lr = tbl().lookup(n.id);
CAF_REQUIRE(hdl); CAF_REQUIRE(lr.hdl);
}; };
mpx()->provide_scribe("jupiter", 8080, jupiter().connection); mpx()->provide_scribe("jupiter", 8080, jupiter().connection);
CAF_CHECK(mpx()->has_pending_scribe("jupiter", 8080)); CAF_CHECK(mpx()->has_pending_scribe("jupiter", 8080));
...@@ -676,80 +672,32 @@ CAF_TEST_DISABLED(automatic_connection) { ...@@ -676,80 +672,32 @@ CAF_TEST_DISABLED(automatic_connection) {
publish(self(), 4242); publish(self(), 4242);
mpx()->flush_runnables(); // process publish message in basp_broker mpx()->flush_runnables(); // process publish message in basp_broker
CAF_MESSAGE("connect to mars"); CAF_MESSAGE("connect to mars");
connect_node(mars(), ax, self()->id()); auto ep = instance().tbl().autoconnect_endpoint();
//CAF_CHECK_EQUAL(tbl().lookup_direct(mars().id).id(), mars().connection.id()); connect_node(mars(), ax, self()->id(), set<string>{}, ep);
check_node_in_tbl(mars()); check_node_in_tbl(mars());
// TODO: this use case is no longer possible. Nodes are required to open a CAF_MESSAGE("Look for mars address information in our peer server");
// connection first, before sending messages (forwarding has been auto peer_server = sys.registry().get(atom("PeerServ"));
// removed). CAF_MESSAGE("Send request");
CAF_MESSAGE("simulate that an actor from jupiter " self()->send(actor_cast<actor>(peer_server), get_atom::value,
"sends a message to us via mars"); to_string(mars().id));
mock(mars().connection, // process get request and send answer
{basp::message_type::dispatch_message, 0, 0, 0, do {
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)) {
sched.run(); sched.run();
mpx()->flush_runnables(); mpx()->flush_runnables();
} } while (self()->mailbox().empty());
CAF_REQUIRE(mpx()->output_buffer(mars().connection).empty()); CAF_MESSAGE("Process reply");
// 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");
self()->receive( self()->receive(
[](const std::string& str) -> std::string { [&](const std::string& item, message& msg) {
CAF_CHECK_EQUAL(str, "hello from jupiter!"); // Check that we got an entry under the name of our peer.
return "hello from earth!"; 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() 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