Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
all tests pass!
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelsavannah committed Jan 25, 2019
1 parent a14c931 commit 62fd04e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
30 changes: 22 additions & 8 deletions node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ node::node(std::shared_ptr<bzn::asio::io_context_base> io_context, std::shared_p
void
node::start()
{
this->weak_priv_protobuf_handler =
[weak_self = weak_from_this()](auto msg, auto session)
{
auto strong_self = weak_self.lock();
if (strong_self)
{
strong_self->priv_protobuf_handler(msg, session);
}
else
{
LOG(warning) << "ignoring incoming message because node is gone";
}
};
std::call_once(this->start_once, &node::do_accept, this);
}

Expand Down Expand Up @@ -85,7 +98,7 @@ node::do_accept()
std::shared_ptr<bzn::beast::websocket_stream_base> ws = self->websocket->make_unique_websocket_stream(
self->acceptor_socket->get_tcp_socket());

auto session = std::make_shared<bzn::session>(self->io_context, ++self->session_id_counter, ep, self->chaos, std::bind(&node::priv_protobuf_handler, self, std::placeholders::_1, std::placeholders::_2), self->options->get_ws_idle_timeout());
auto session = std::make_shared<bzn::session>(self->io_context, ++self->session_id_counter, ep, self->chaos, self->weak_priv_protobuf_handler, self->options->get_ws_idle_timeout());
session->accept_connection(std::move(ws));

LOG(info) << "accepting new incomming connection with " << key;
Expand Down Expand Up @@ -127,11 +140,13 @@ node::send_message_str(const boost::asio::ip::tcp::endpoint& ep, std::shared_ptr
auto key = this->key_from_ep(ep);

if (this->sessions.find(key) == this->sessions.end() || !this->sessions.at(key)->is_open()) {
auto session = std::make_shared<bzn::session>(this->io_context, ++this->session_id_counter, ep,
this->chaos, std::bind(&node::priv_protobuf_handler,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2), this->options->get_ws_idle_timeout());
auto session = std::make_shared<bzn::session>(
this->io_context
, ++this->session_id_counter
, ep
, this->chaos
, this->weak_priv_protobuf_handler
, this->options->get_ws_idle_timeout());
session->open_connection(this->websocket);
sessions.insert_or_assign(key, session);
}
Expand All @@ -142,7 +157,6 @@ node::send_message_str(const boost::asio::ip::tcp::endpoint& ep, std::shared_ptr
session->send_message(msg);
}


void
node::send_message(const boost::asio::ip::tcp::endpoint& ep, std::shared_ptr<bzn_envelope> msg)
{
Expand All @@ -163,4 +177,4 @@ std::string
node::key_from_ep(const boost::asio::ip::tcp::endpoint &ep)
{
return ep.address().to_string() + ":" + std::to_string(ep.port());
}
}
2 changes: 2 additions & 0 deletions node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace bzn

std::shared_ptr<bzn::crypto_base> crypto;
std::shared_ptr<bzn::options_base> options;

std::function<void(const bzn_envelope&, std::shared_ptr<bzn::session_base>)> weak_priv_protobuf_handler;
};

} // bzn
2 changes: 2 additions & 0 deletions node/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ void
session::close()
{
// TODO: re-open socket later if we still have messages to send?
LOG(info) << "closing session";

std::lock_guard<std::mutex> lock(this->socket_lock);
if (this->closing)
Expand Down Expand Up @@ -290,4 +291,5 @@ session::~session()
{
LOG(warning) << "dropping session with " << this->write_queue.size() << " messages left in its write queue";
}
LOG(info) << "session destructor run";
}

0 comments on commit 62fd04e

Please sign in to comment.