postgres support

This commit is contained in:
CJ Cobb
2020-12-17 11:43:48 -05:00
parent 4ac6d61b05
commit d6e54c398a
7 changed files with 2059 additions and 47 deletions

View File

@@ -31,17 +31,11 @@
#include <thread>
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(beast::error_code ec, char const* what)
fail(boost::beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
@@ -49,12 +43,13 @@ fail(beast::error_code ec, char const* what)
// Echoes back all received WebSocket messages
class session : public std::enable_shared_from_this<session>
{
websocket::stream<beast::tcp_stream> ws_;
beast::flat_buffer buffer_;
boost::beast::websocket::stream<boost::beast::tcp_stream> ws_;
boost::beast::flat_buffer buffer_;
public:
// Take ownership of the socket
explicit session(tcp::socket&& socket) : ws_(std::move(socket))
explicit session(boost::asio::ip::tcp::socket&& socket)
: ws_(std::move(socket))
{
}
@@ -66,9 +61,10 @@ public:
// on the I/O objects in this session. Although not strictly necessary
// for single-threaded contexts, this example code is written to be
// thread-safe by default.
net::dispatch(
boost::asio::dispatch(
ws_.get_executor(),
beast::bind_front_handler(&session::on_run, shared_from_this()));
boost::beast::bind_front_handler(
&session::on_run, shared_from_this()));
}
// Start the asynchronous operation
@@ -76,24 +72,24 @@ public:
on_run()
{
// Set suggested timeout settings for the websocket
ws_.set_option(websocket::stream_base::timeout::suggested(
beast::role_type::server));
ws_.set_option(boost::beast::websocket::stream_base::timeout::suggested(
boost::beast::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res) {
ws_.set_option(boost::beast::websocket::stream_base::decorator(
[](boost::beast::websocket::response_type& res) {
res.set(
http::field::server,
boost::beast::http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-async");
}));
// Accept the websocket handshake
ws_.async_accept(
beast::bind_front_handler(&session::on_accept, shared_from_this()));
ws_.async_accept(boost::beast::bind_front_handler(
&session::on_accept, shared_from_this()));
}
void
on_accept(beast::error_code ec)
on_accept(boost::beast::error_code ec)
{
if (ec)
return fail(ec, "accept");
@@ -108,16 +104,17 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
beast::bind_front_handler(&session::on_read, shared_from_this()));
boost::beast::bind_front_handler(
&session::on_read, shared_from_this()));
}
void
on_read(beast::error_code ec, std::size_t bytes_transferred)
on_read(boost::beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This indicates that the session was closed
if (ec == websocket::error::closed)
if (ec == boost::beast::websocket::error::closed)
return;
if (ec)
@@ -127,11 +124,12 @@ public:
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
beast::bind_front_handler(&session::on_write, shared_from_this()));
boost::beast::bind_front_handler(
&session::on_write, shared_from_this()));
}
void
on_write(beast::error_code ec, std::size_t bytes_transferred)
on_write(boost::beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -151,14 +149,16 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
boost::asio::io_context& ioc_;
boost::asio::ip::tcp::acceptor acceptor_;
public:
listener(net::io_context& ioc, tcp::endpoint endpoint)
listener(
boost::asio::io_context& ioc,
boost::asio::ip::tcp::endpoint endpoint)
: ioc_(ioc), acceptor_(ioc)
{
beast::error_code ec;
boost::beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
@@ -169,7 +169,7 @@ public:
}
// Allow address reuse
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
if (ec)
{
fail(ec, "set_option");
@@ -185,7 +185,7 @@ public:
}
// Start listening for connections
acceptor_.listen(net::socket_base::max_listen_connections, ec);
acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
if (ec)
{
fail(ec, "listen");
@@ -206,13 +206,13 @@ private:
{
// The new connection gets its own strand
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(
boost::asio::make_strand(ioc_),
boost::beast::bind_front_handler(
&listener::on_accept, shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(boost::beast::error_code ec, boost::asio::ip::tcp::socket socket)
{
if (ec)
{
@@ -265,7 +265,7 @@ main(int argc, char* argv[])
<< " websocket-server-async 0.0.0.0 8080 1\n";
return EXIT_FAILURE;
}
auto const address = net::ip::make_address(argv[1]);
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
auto const config = parse_config(argv[4]);
@@ -286,15 +286,19 @@ main(int argc, char* argv[])
std::cerr << "no etl sources listed in config. exiting..." << std::endl;
return EXIT_FAILURE;
}
ETLSource source{sources[0].as_object(), backend};
NetworkValidatedLedgers nwvl;
ETLSource source{sources[0].as_object(), backend, nwvl};
source.start();
// source.loadInitialLedger(60000000);
// The io_context is required for all I/O
net::io_context ioc{threads};
boost::asio::io_context ioc{threads};
// Create and launch a listening port
std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run();
std::make_shared<listener>(
ioc, boost::asio::ip::tcp::endpoint{address, port})
->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;