Verified Commit bd901bcc authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Improve network::server API

- Add `endpoint lookup(std::string address)` so that a RPC server can
lookup peers for RPC forwarding.
- Add `std::string self_address()` so that a RPC server can determine
its own address string.
parent e1c57a55
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -35,8 +35,8 @@ set_property(TARGET _rpc_client PROPERTY POSITION_INDEPENDENT_CODE ON)
add_library(_rpc_server STATIC)
target_sources(
  _rpc_server
  INTERFACE server.hpp request.hpp serialization.hpp
  PRIVATE server.cpp
  INTERFACE endpoint.hpp server.hpp request.hpp serialization.hpp
  PRIVATE server.cpp endpoint.cpp
)

target_link_libraries(_rpc_server PUBLIC common::logger thallium)
+3 −4
Original line number Diff line number Diff line
@@ -33,13 +33,12 @@ namespace network {


client::client(const std::string& protocol)
    : m_engine(std::make_shared<thallium::engine>(protocol,
                                                  THALLIUM_CLIENT_MODE)) {}
    : m_engine(protocol, THALLIUM_CLIENT_MODE) {}

std::optional<endpoint>
client::lookup(const std::string& address) noexcept {
    try {
        return endpoint{m_engine, m_engine->lookup(address)};
        return endpoint{m_engine, m_engine.lookup(address)};
    } catch(const std::exception& ex) {
        LOGGER_ERROR("client::lookup() failed: {}", ex.what());
        return std::nullopt;
@@ -49,7 +48,7 @@ client::lookup(const std::string& address) noexcept {
std::string
client::self_address() const noexcept {
    try {
        return m_engine->self();
        return m_engine.self();
    } catch(const std::exception& ex) {
        LOGGER_ERROR("client::self_address() failed: {}", ex.what());
        return "unknown"s;
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ public:
    self_address() const noexcept;

private:
    std::shared_ptr<thallium::engine> m_engine;
    thallium::engine m_engine;
};

} // namespace network
+2 −3
Original line number Diff line number Diff line
@@ -28,9 +28,8 @@

namespace network {

endpoint::endpoint(std::shared_ptr<thallium::engine> engine,
                   thallium::endpoint endpoint)
    : m_engine(std::move(engine)), m_endpoint(std::move(endpoint)) {}
endpoint::endpoint(thallium::engine& engine, thallium::endpoint endpoint)
    : m_engine(engine), m_endpoint(std::move(endpoint)) {}

std::string
endpoint::address() const {
+3 −4
Original line number Diff line number Diff line
@@ -34,8 +34,7 @@ namespace network {
class endpoint {

public:
    endpoint(std::shared_ptr<thallium::engine> engine,
             thallium::endpoint endpoint);
    endpoint(thallium::engine& engine, thallium::endpoint endpoint);

    std::string
    address() const;
@@ -45,7 +44,7 @@ public:
    call(const std::string& rpc_name, Args&&... args) const {

        try {
            const auto rpc = m_engine->define(rpc_name);
            const auto rpc = m_engine.define(rpc_name);
            return std::make_optional(
                    rpc.on(m_endpoint)(std::forward<Args>(args)...));
        } catch(const std::exception& ex) {
@@ -65,7 +64,7 @@ public:
    }

private:
    std::shared_ptr<thallium::engine> m_engine;
    mutable thallium::engine m_engine;
    thallium::endpoint m_endpoint;
};

Loading