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

RPC handlers can now access server context

Our network interface now relies on Thallium's support for `providers`
(https://mochi.readthedocs.io/en/latest/thallium/09_providers.html).
This allows using the member functions from a class as RPC handlers,
which in turn allows RPC handers to access the class' data members.

Thus, we have heavily refactored `scord`:

- Create a new `scord::rpc_server` class that inherits both from
`network::server` and `network::provider`. This enables the
aforementioned functionality.
- The RPC handlers for `scord` are now member functions of
`scord::rpc_server` instead of free functions.
- The managers used by `scord` (i.e. the `job_manager`,
`adhoc_storage_manager`, and `pfs_storage_manager` are now members of
`scord::rpc_server` instead of singletons, since they no longer need to
be accessed globally.

Similarly for `scord-ctl`:
- Create a new `scord_ctl::rpc_server` class that inherits both from
`network::server` and `network::provider`. This enables the
aforementioned functionality.
- The RPC handlers for `scord-ctl` are now member functions of
`scord_ctl::rpc_server` instead of free functions.

Additionally, we have also moved the `network` and `logger` namespaces
outside of `scord`, since this code is shared by both `scord` and
`scord-ctl`.
parent b4e9cbde
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -32,24 +32,24 @@ logger_setup(const char* ident, logger_type type, const char* log_file) {
    constexpr auto get_cxx_type = [](logger_type t) {
        switch(t) {
            case CONSOLE_LOGGER:
                return scord::logger_type::console;
                return logger::logger_type::console;
            case CONSOLE_COLOR_LOGGER:
                return scord::logger_type::console_color;
                return logger::logger_type::console_color;
            case FILE_LOGGER:
                return scord::logger_type::file;
                return logger::logger_type::file;
            case SYSLOG_LOGGER:
                return scord::logger_type::syslog;
                return logger::logger_type::syslog;
            default:
                return scord::logger_type::console;
                return logger::logger_type::console;
        }
    };
    scord::logger::create_global_logger(ident, get_cxx_type(type), log_file);
    logger::create_global_logger(ident, get_cxx_type(type), log_file);
}

void
logger_log(enum logger_level level, const char* fmt, ...) {

    if(const auto logger = scord::logger::get_global_logger(); logger) {
    if(const auto logger = logger::get_global_logger(); logger) {

        std::array<char, LOGGER_MSG_MAX_LEN> msg; // NOLINT
        va_list args;
@@ -79,7 +79,7 @@ logger_log(enum logger_level level, const char* fmt, ...) {

void
logger_destroy() {
    if(scord::logger::get_global_logger()) {
        scord::logger::destroy_global_logger();
    if(logger::get_global_logger()) {
        logger::destroy_global_logger();
    }
}
+31 −36
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ ptr(const T* p) {

namespace fs = std::filesystem;

namespace scord {
namespace logger {

enum logger_type {
    console,
@@ -64,7 +64,7 @@ class logger_config {
public:
    logger_config() = default;

    explicit logger_config(std::string ident, scord::logger_type type,
    explicit logger_config(std::string ident, logger_type type,
                           std::optional<fs::path> log_file = {})
        : m_ident(std::move(ident)), m_type(type),
          m_log_file(std::move(log_file)) {}
@@ -74,7 +74,7 @@ public:
        return m_ident;
    }

    scord::logger_type
    logger_type
    type() const {
        return m_type;
    }
@@ -86,7 +86,7 @@ public:

private:
    std::string m_ident;
    scord::logger_type m_type = console_color;
    logger_type m_type = console_color;
    std::optional<fs::path> m_log_file;
};

@@ -165,30 +165,6 @@ public:
        spdlog::shutdown();
    }

    // the following static functions can be used to interact
    // with a globally registered logger instance

    template <typename... Args>
    static inline void
    create_global_logger(Args&&... args) {
        global_logger() = std::make_shared<logger>(args...);
    }

    static inline void
    register_global_logger(logger&& lg) {
        global_logger() = std::make_shared<logger>(std::move(lg));
    }

    static inline std::shared_ptr<logger>&
    get_global_logger() {
        return global_logger();
    }

    static inline void
    destroy_global_logger() {
        global_logger().reset();
    }

    // the following member functions can be used to interact
    // with a specific logger instance
    inline void
@@ -308,17 +284,36 @@ public:
    }

private:
    static std::shared_ptr<logger>&
    global_logger() {
    std::shared_ptr<spdlog::logger> m_internal_logger;
    std::string m_type;
};

// the following static functions can be used to interact
// with a globally registered logger instance

static inline std::shared_ptr<logger>&
get_global_logger() {
    static std::shared_ptr<logger> s_global_logger;
    return s_global_logger;
}

private:
    std::shared_ptr<spdlog::logger> m_internal_logger;
    std::string m_type;
};
template <typename... Args>
static inline void
create_global_logger(Args&&... args) {
    get_global_logger() = std::make_shared<logger>(args...);
}

static inline void
register_global_logger(logger&& lg) {
    get_global_logger() = std::make_shared<logger>(std::move(lg));
}

static inline void
destroy_global_logger() {
    get_global_logger().reset();
}


} // namespace scord
} // namespace logger

#endif /* SCORD_LOGGER_HPP */
+1 −7
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@

#define LOGGER_INFO(...)                                                       \
    do {                                                                       \
        using scord::logger;                                                   \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->info(__VA_ARGS__);                    \
        }                                                                      \
@@ -42,7 +41,6 @@

#define LOGGER_DEBUG(...)                                                      \
    do {                                                                       \
        using scord::logger;                                                   \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->debug(__VA_ARGS__);                   \
        }                                                                      \
@@ -50,7 +48,6 @@

#define LOGGER_FLUSH()                                                         \
    do {                                                                       \
        using scord::logger;                                                   \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->flush();                              \
        }                                                                      \
@@ -69,7 +66,6 @@

#define LOGGER_WARN(...)                                                       \
    do {                                                                       \
        using scord::logger;                                                   \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->warn(__VA_ARGS__);                    \
        }                                                                      \
@@ -77,7 +73,6 @@

#define LOGGER_ERROR(...)                                                      \
    do {                                                                       \
        using scord::logger;                                                   \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->error(__VA_ARGS__);                   \
        }                                                                      \
@@ -85,7 +80,6 @@

#define LOGGER_ERRNO(...)                                                      \
    do {                                                                       \
        using scord::logger;                                                   \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->error_errno(__VA_ARGS__);             \
        }                                                                      \
@@ -93,7 +87,7 @@

#define LOGGER_CRITICAL(...)                                                   \
    do {                                                                       \
        using scord::logger;                                                   \
        using logger::logger;                                                  \
        if(logger::get_global_logger()) {                                      \
            logger::get_global_logger()->critical(__VA_ARGS__);                \
        }                                                                      \
+2 −2
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@

using namespace std::literals;

namespace scord::network {
namespace network {


client::client(const std::string& protocol)
@@ -56,4 +56,4 @@ client::self_address() const noexcept {
    }
}

} // namespace scord::network
} // namespace network
+5 −5
Original line number Diff line number Diff line
@@ -22,13 +22,13 @@
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#ifndef SCORD_CLIENT_HPP
#define SCORD_CLIENT_HPP
#ifndef NETWORK_CLIENT_HPP
#define NETWORK_CLIENT_HPP

#include <optional>
#include <thallium.hpp>

namespace scord::network {
namespace network {

class endpoint;

@@ -45,6 +45,6 @@ private:
    std::shared_ptr<thallium::engine> m_engine;
};

} // namespace scord::network
} // namespace network

#endif // SCORD_CLIENT_HPP
#endif // NETWORK_CLIENT_HPP
Loading