Skip to content

Commit

Permalink
[FOLD] Revert TrustedPublisherServer io service model
Browse files Browse the repository at this point in the history
  • Loading branch information
bachase committed Oct 22, 2017
1 parent 4f28f38 commit 80b1093
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
20 changes: 16 additions & 4 deletions src/test/app/ValidatorSite_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,24 @@ class ValidatorSite_test : public beast::unit_test::suite
env.timeKeeper().now() + 3600s;

TrustedPublisherServer server1(
ep1, pubSigningKeys1, manifest1, sequence,
expiration, version, list1);
ep1,
env.app().getIOService(),
pubSigningKeys1,
manifest1,
sequence,
expiration,
version,
list1);

TrustedPublisherServer server2(
ep2, pubSigningKeys2, manifest2, sequence,
expiration, version, list2);
ep2,
env.app().getIOService(),
pubSigningKeys2,
manifest2,
sequence,
expiration,
version,
list2);

{
// fetch single site
Expand Down
29 changes: 15 additions & 14 deletions src/test/jtx/TrustedPublisherServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,22 @@ class TrustedPublisherServer
using resp_type = beast::http::response<beast::http::string_body>;
using error_code = boost::system::error_code;

boost::asio::io_service ios_;
boost::optional<boost::asio::io_service::work> work_;
std::thread iosThread_;

socket_type sock_;
boost::asio::ip::tcp::acceptor acceptor_;

std::string list_;

public:
TrustedPublisherServer(endpoint_type const& ep,
TrustedPublisherServer(
endpoint_type const& ep,
boost::asio::io_service& ios,
std::pair<PublicKey, SecretKey> keys,
std::string const& manifest,
int sequence,
NetClock::time_point expiration,
int version,
std::vector<PublicKey> const& validators)
: work_{ios_}
, iosThread_([&]() { this->ios_.run(); })
, sock_(ios_)
, acceptor_(ios_)
: sock_(ios), acceptor_(ios)
{
std::string data = "{\"sequence\":" + std::to_string(sequence) +
",\"expiration\":" +
Expand Down Expand Up @@ -90,34 +85,37 @@ class TrustedPublisherServer
boost::asio::ip::tcp::acceptor::reuse_address(true), ec);
acceptor_.bind(ep);
acceptor_.listen(boost::asio::socket_base::max_connections);
acceptor_.async_accept(sock_, [this](error_code ec) { on_accept(ec); });
acceptor_.async_accept(
sock_,
std::bind(
&TrustedPublisherServer::on_accept, this, std::placeholders::_1));
}

~TrustedPublisherServer()
{
error_code ec;
acceptor_.close(ec);
work_ = boost::none;
ios_.stop();
iosThread_.join();
}

endpoint_type
local_endpoint() const
{
return acceptor_.local_endpoint();
}

private:
struct lambda
{
int id;
TrustedPublisherServer& self;
socket_type sock;
boost::asio::io_service::work work;

lambda(int id_, TrustedPublisherServer& self_, socket_type&& sock_)
: id(id_)
, self(self_)
, sock(std::move(sock_))
, work(sock.get_io_service())
{
}

Expand All @@ -138,7 +136,10 @@ class TrustedPublisherServer

static int id_ = 0;
std::thread{lambda{++id_, *this, std::move(sock_)}}.detach();
acceptor_.async_accept(sock_, [this](error_code ec) { on_accept(ec); });
acceptor_.async_accept(
sock_,
std::bind(
&TrustedPublisherServer::on_accept, this, std::placeholders::_1));
}

void
Expand Down
17 changes: 16 additions & 1 deletion src/test/rpc/ValidatorRPC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <BeastConfig.h>
#include <ripple/app/main/BasicApp.h>
#include <ripple/app/misc/ValidatorSite.h>
#include <ripple/beast/unit_test.h>
#include <ripple/core/ConfigSections.h>
Expand Down Expand Up @@ -263,8 +264,22 @@ class ValidatorRPC_test : public beast::unit_test::suite
// 0 port means to use OS port selection
endpoint_type ep{address_type::from_string("127.0.0.1"), 0};

// Manage single thread io_service for server
struct Worker : BasicApp
{
Worker() : BasicApp(1) {}
};
Worker w;

TrustedPublisherServer server(
ep, publisherSigningKeys, manifest, 1, expiration, 1, keys);
ep,
w.get_io_service(),
publisherSigningKeys,
manifest,
1,
expiration,
1,
keys);

endpoint_type const & local_ep = server.local_endpoint();
std::string siteURI = "http://127.0.0.1:" +
Expand Down

0 comments on commit 80b1093

Please sign in to comment.