From 50ead02dec006acb677d3893e9ee5c2cc7b45530 Mon Sep 17 00:00:00 2001 From: Danila Slesarev Date: Fri, 5 May 2023 01:26:01 +0300 Subject: [PATCH] feat(server): started developing plugins loading --- CMakeLists.txt | 1 + server/lib/CMakeLists.txt | 5 +- server/lib/connection/CMakeLists.txt | 3 - server/lib/containers/CMakeLists.txt | 2 + server/lib/containers/containers.cpp | 0 server/lib/containers/containers.hpp | 12 ++++ server/lib/plugins_bundle/plugins_bundle.hpp | 56 +++++++++++-------- server/lib/plugins_loader/CMakeLists.txt | 5 ++ server/lib/plugins_loader/plugins_loader.cpp | 0 server/lib/plugins_loader/plugins_loader.hpp | 21 +++++++ server/lib/plugins_provider/CMakeLists.txt | 5 ++ .../lib/plugins_provider/plugins_provider.cpp | 0 .../lib/plugins_provider/plugins_provider.hpp | 32 +++++++++++ .../response_generators.cpp | 4 +- .../response_generators.hpp | 3 +- server/lib/server/CMakeLists.txt | 2 +- server/lib/server/server.cpp | 6 +- server/lib/session/CMakeLists.txt | 3 + .../connection.cpp => session/session.cpp} | 24 ++------ .../connection.hpp => session/session.hpp} | 18 +++--- server/lib/wrappers/wrappers.hpp | 37 +++++++----- 21 files changed, 164 insertions(+), 75 deletions(-) delete mode 100644 server/lib/connection/CMakeLists.txt create mode 100644 server/lib/containers/CMakeLists.txt create mode 100644 server/lib/containers/containers.cpp create mode 100644 server/lib/containers/containers.hpp create mode 100644 server/lib/plugins_loader/CMakeLists.txt create mode 100644 server/lib/plugins_loader/plugins_loader.cpp create mode 100644 server/lib/plugins_loader/plugins_loader.hpp create mode 100644 server/lib/plugins_provider/CMakeLists.txt create mode 100644 server/lib/plugins_provider/plugins_provider.cpp create mode 100644 server/lib/plugins_provider/plugins_provider.hpp create mode 100644 server/lib/session/CMakeLists.txt rename server/lib/{connection/connection.cpp => session/session.cpp} (66%) rename server/lib/{connection/connection.hpp => session/session.hpp} (53%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d972a86..23a0159 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,5 @@ project(Dict2FlashcardsQT) set(CMAKE_CXX_STANDARD 20) add_subdirectory(server) +add_subdirectory(python_tests) # add_executable(test async_tcp_echo_server.cpp) diff --git a/server/lib/CMakeLists.txt b/server/lib/CMakeLists.txt index 48eeef1..1eb388d 100644 --- a/server/lib/CMakeLists.txt +++ b/server/lib/CMakeLists.txt @@ -1,5 +1,8 @@ add_subdirectory(wrappers) +add_subdirectory(containers) +add_subdirectory(plugins_loader) +add_subdirectory(plugins_provider) add_subdirectory(plugins_bundle) add_subdirectory(response_generators) -add_subdirectory(connection) +add_subdirectory(session) add_subdirectory(server) diff --git a/server/lib/connection/CMakeLists.txt b/server/lib/connection/CMakeLists.txt deleted file mode 100644 index 70ceeda..0000000 --- a/server/lib/connection/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_library(connection connection.cpp) -target_include_directories(connection PUBLIC ./) -target_link_libraries(connection response_generators) diff --git a/server/lib/containers/CMakeLists.txt b/server/lib/containers/CMakeLists.txt new file mode 100644 index 0000000..7939578 --- /dev/null +++ b/server/lib/containers/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(containers containers.cpp) +target_include_directories(containers PUBLIC ./) diff --git a/server/lib/containers/containers.cpp b/server/lib/containers/containers.cpp new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/containers/containers.hpp b/server/lib/containers/containers.hpp new file mode 100644 index 0000000..fed4e42 --- /dev/null +++ b/server/lib/containers/containers.hpp @@ -0,0 +1,12 @@ +#ifndef CONTAINERS_H +#define CONTAINERS_H + +class IContainer { + virtual ~IContainer() = default; +}; + +class Container { + cons +} + +#endif // !CONTAINERS_H diff --git a/server/lib/plugins_bundle/plugins_bundle.hpp b/server/lib/plugins_bundle/plugins_bundle.hpp index 8895a9c..f5f3974 100644 --- a/server/lib/plugins_bundle/plugins_bundle.hpp +++ b/server/lib/plugins_bundle/plugins_bundle.hpp @@ -8,46 +8,54 @@ class PluginsBundle { public: - // clang-format off + PluginsBundle(); auto get_definitions(std::string word) - -> DefinitionsProviderWrapper::provided_type; + -> std::optional; auto get_sentences(std::string word) - -> SentencesProviderWrapper::provided_type; + -> std::optional; auto get_images(std::string word) - -> ImagesProviderWrapper::provided_type; + -> std::optional; auto get_audios(std::string word) - -> AudiosProviderWrapper::provided_type; + -> std::optional; auto save_results(std::string word) - -> FormatProcessorWrapper::provided_type; - // clang-format on + -> std::optional; - auto get_definitions_providers_config(JSON new_config) -> JSON; - auto set_definitions_providers_config(JSON new_config) -> JSON; + auto set_definitions_provider(DefinitionsProviderWrapper &&) -> void; + auto get_definitions_providers_config() -> std::optional; + auto set_definitions_providers_config(nlohmann::json new_config) + -> std::optional; - auto get_sentences_providers_config(JSON new_config) -> JSON; - auto set_sentences_providers_config(JSON new_config) -> JSON; + auto set_sentences_provider(SentencesProviderWrapper &&) -> void; + auto get_sentences_providers_config() -> std::optional; + auto set_sentences_providers_config(nlohmann::json new_config) + -> std::optional; - auto get_images_providers_config(JSON new_config) -> JSON; - auto set_images_providers_config(JSON new_config) -> JSON; + auto set_images_provider(ImagesProviderWrapper &&) -> void; + auto get_images_providers_config() -> std::optional; + auto set_images_providers_config(nlohmann::json new_config) + -> std::optional; - auto get_audios_providers_config(JSON new_config) -> JSON; - auto set_audios_providers_config(JSON new_config) -> JSON; + auto set_audios_provider(AudiosProviderWrapper &&) -> void; + auto get_audios_providers_config() -> std::optional; + auto set_audios_providers_config(nlohmann::json new_config) + -> std::optional; - auto get_format_processors_config(JSON new_config) -> JSON; - auto set_format_processors_config(JSON new_config) -> JSON; + auto set_format_processor(FormatProcessorWrapper &&) -> void; + auto get_format_processors_config() -> std::optional; + auto set_format_processors_config(nlohmann::json new_config) + -> std::optional; private: - // std::optional definitions_provider_ = - // std::nullopt; - // std::optional sentences_provider_ = - // std::nullopt; std::optional images_provider_ = - // std::nullopt; std::optional audios_provider_ = - // std::nullopt; std::optional format_processor_ - // = std::nullopt; + std::optional definitions_provider_ = + std::nullopt; + std::optional sentences_provider_ = std::nullopt; + std::optional images_provider_ = std::nullopt; + std::optional audios_provider_ = std::nullopt; + std::optional format_processor_ = std::nullopt; }; #endif // !PLUGINS_BUNDLE_H diff --git a/server/lib/plugins_loader/CMakeLists.txt b/server/lib/plugins_loader/CMakeLists.txt new file mode 100644 index 0000000..e6ca5c0 --- /dev/null +++ b/server/lib/plugins_loader/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(plugins_loader plugins_loader.cpp) +target_include_directories(plugins_loader PUBLIC ./) +target_link_libraries(plugins_loader + serverside_plugin_wrappers + containers) diff --git a/server/lib/plugins_loader/plugins_loader.cpp b/server/lib/plugins_loader/plugins_loader.cpp new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/plugins_loader/plugins_loader.hpp b/server/lib/plugins_loader/plugins_loader.hpp new file mode 100644 index 0000000..f134e91 --- /dev/null +++ b/server/lib/plugins_loader/plugins_loader.hpp @@ -0,0 +1,21 @@ +#ifndef PLUGINS_LOADER_H +#define PLUGINS_LOADER_H + +#include + +template +class IPluginsLoader { + virtual ~IPluginsLoader() = default; + + virtual auto get(const std::string &plugin_name) -> Wrapper; +}; + +template +class PluginsLoader : public IPluginsLoader { + explicit PluginsLoader(const std::string &plugins_dir) { + } + + private: +}; + +#endif diff --git a/server/lib/plugins_provider/CMakeLists.txt b/server/lib/plugins_provider/CMakeLists.txt new file mode 100644 index 0000000..a088aa5 --- /dev/null +++ b/server/lib/plugins_provider/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(plugins_provider plugins_provider.cpp) +target_include_directories(plugins_provider PUBLIC ./) +target_link_libraries(plugins_provider + plugins_loader + serverside_plugin_wrappers) diff --git a/server/lib/plugins_provider/plugins_provider.cpp b/server/lib/plugins_provider/plugins_provider.cpp new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/plugins_provider/plugins_provider.hpp b/server/lib/plugins_provider/plugins_provider.hpp new file mode 100644 index 0000000..6e13dd7 --- /dev/null +++ b/server/lib/plugins_provider/plugins_provider.hpp @@ -0,0 +1,32 @@ +#ifndef PLUGINS_PROVIDER_H +#define PLUGINS_PROVIDER_H + +#include "wrappers.hpp" +#include +#include + +class IPluginsProvider { + public: + virtual ~IPluginsProvider() = default; + + virtual auto get_definitions_provider(const std::string &name) + -> std::optional; + + virtual auto get_sentences_provider(const std::string &name) + -> std::optional; + + virtual auto get_images_provider(const std::string &name) + -> std::optional; + + virtual auto get_audios_provider(const std::string &name) + -> std::optional; + + virtual auto get_format_processor(const std::string &name) + -> std::optional; + + virtual auto reload_plugins() -> void; +}; + +class PluginsProvider : public IPluginsProvider {}; + +#endif // !PLUGINS_PROVIDER_H diff --git a/server/lib/response_generators/response_generators.cpp b/server/lib/response_generators/response_generators.cpp index acdb621..b72b991 100644 --- a/server/lib/response_generators/response_generators.cpp +++ b/server/lib/response_generators/response_generators.cpp @@ -5,9 +5,7 @@ using nlohmann::json; -ResponceGenerator::ResponceGenerator(std::shared_ptr bundle) - : bundle_(std::move(bundle)) { -} +ResponceGenerator::ResponceGenerator() = default; static auto return_error(const std::string &message) -> json { json dst; diff --git a/server/lib/response_generators/response_generators.hpp b/server/lib/response_generators/response_generators.hpp index c057c99..0947ed4 100644 --- a/server/lib/response_generators/response_generators.hpp +++ b/server/lib/response_generators/response_generators.hpp @@ -24,12 +24,13 @@ class IResponceGenerator { class ResponceGenerator : public IResponceGenerator { public: - explicit ResponceGenerator(std::shared_ptr bundle); + ResponceGenerator(); auto handle(const std::string &request) -> nlohmann::json override; private: std::shared_ptr bundle_; + std::shared_ptr<> plugins_provider_; }; #endif // !RESPONSE_GENERATORS_H diff --git a/server/lib/server/CMakeLists.txt b/server/lib/server/CMakeLists.txt index 03a5a96..4b34aa8 100644 --- a/server/lib/server/CMakeLists.txt +++ b/server/lib/server/CMakeLists.txt @@ -3,6 +3,6 @@ find_package(nlohmann_json 3.7.0 REQUIRED) add_library(plugin_server server.cpp) target_link_libraries(plugin_server serverside_plugin_wrappers - connection + session ) target_include_directories(plugin_server PUBLIC ./) diff --git a/server/lib/server/server.cpp b/server/lib/server/server.cpp index c60bce7..5dc8220 100644 --- a/server/lib/server/server.cpp +++ b/server/lib/server/server.cpp @@ -1,5 +1,6 @@ #include "server.hpp" -#include "connection.hpp" +#include "response_generators.hpp" +#include "session.hpp" #include #include #include @@ -26,7 +27,8 @@ void PluginServer::start_accept() { acceptor_.async_accept( [this](boost::system::error_code ec, tcp::socket socket) { if (!ec) { - std::make_shared(std::move(socket))->start(); + std::make_shared>(std::move(socket)) + ->start(); } start_accept(); }); diff --git a/server/lib/session/CMakeLists.txt b/server/lib/session/CMakeLists.txt new file mode 100644 index 0000000..1716a72 --- /dev/null +++ b/server/lib/session/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(session session.cpp) +target_include_directories(session PUBLIC ./) +target_link_libraries(session response_generators) diff --git a/server/lib/connection/connection.cpp b/server/lib/session/session.cpp similarity index 66% rename from server/lib/connection/connection.cpp rename to server/lib/session/session.cpp index 07018e5..8c586ac 100644 --- a/server/lib/connection/connection.cpp +++ b/server/lib/session/session.cpp @@ -1,24 +1,21 @@ -#include "connection.hpp" +#include "session.hpp" #include #include -#include using nlohmann::json; -TCPConnection::TCPConnection( - boost::asio::ip::tcp::socket socket, - std::unique_ptr response_generator) +Session::Session(boost::asio::ip::tcp::socket socket, + std::unique_ptr response_generator) : socket_(std::move(socket)), response_generator_(std::move(response_generator)) { } -void TCPConnection::start() { +void Session::start() { do_read(); } -void TCPConnection::do_read() { +void Session::do_read() { auto self = shared_from_this(); - // https://stackoverflow.com/questions/3058589/boostasioasync-read-until-reads-all-data-instead-of-just-some boost::asio::async_read_until( socket_, @@ -37,19 +34,10 @@ void TCPConnection::do_read() { json response = response_generator_->handle(result); do_write(response); - - // json parsed_request = json::parse(result); - // assert(parsed_request.is_object()); - // assert(parsed_request.contains("query_type")); - // - // json query_type = parsed_request["query_type"]; - // assert(query_type.is_string()); - // - // parsed_request["query_type"]; }); } -void TCPConnection::do_write(std::string response) { +void Session::do_write(std::string response) { auto self(shared_from_this()); boost::asio::async_write( socket_, diff --git a/server/lib/connection/connection.hpp b/server/lib/session/session.hpp similarity index 53% rename from server/lib/connection/connection.hpp rename to server/lib/session/session.hpp index 272ccbf..5beac04 100644 --- a/server/lib/connection/connection.hpp +++ b/server/lib/session/session.hpp @@ -1,15 +1,19 @@ -#ifndef CONNECTION_H -#define CONNECTION_H +#ifndef SESSION_H +#define SESSION_H #include "response_generators.hpp" #include +#include +#include +#include +#include #include +#include -class TCPConnection : public std::enable_shared_from_this { +class Session : public std::enable_shared_from_this { public: - explicit TCPConnection( - boost::asio::ip::tcp::socket socket, - std::unique_ptr response_generator); + explicit Session(boost::asio::ip::tcp::socket socket, + std::unique_ptr response_generator); void start(); @@ -22,4 +26,4 @@ class TCPConnection : public std::enable_shared_from_this { void do_write(std::string response); }; -#endif // !CONNECTION_H +#endif // SESSION_H diff --git a/server/lib/wrappers/wrappers.hpp b/server/lib/wrappers/wrappers.hpp index e1dee14..3afd4e8 100644 --- a/server/lib/wrappers/wrappers.hpp +++ b/server/lib/wrappers/wrappers.hpp @@ -1,12 +1,14 @@ #ifndef WRAPPERS_H #define WRAPPERS_H +#include #include +#include #include -using JSON = int; // Временная заглушка -// #error "Нужно поменять заглушку типа JSON на настоящий" + + template class IPluginWrapper { @@ -15,9 +17,9 @@ class IPluginWrapper { virtual void load(); virtual auto get(std::string word) -> T; - virtual auto get_config_description() -> JSON; - virtual auto get_default_config() -> JSON; - virtual auto set_config(JSON new_config) -> JSON; + virtual auto get_config_description() -> nlohmann::json; + virtual auto get_default_config() -> nlohmann::json; + virtual auto set_config(nlohmann::json new_config) -> nlohmann::json; virtual void unload(); virtual ~IPluginWrapper() = default; @@ -27,7 +29,7 @@ class IPluginWrapper { auto operator=(IPluginWrapper &&) -> IPluginWrapper & = delete; private: - JSON config; + nlohmann::json config; }; struct Card { @@ -37,26 +39,31 @@ struct Card { std::vector examples; std::vector image_links; std::vector audio_links; - JSON tags; - JSON other; + nlohmann::json tags; + nlohmann::json other; }; -// #error "Нужно раснести оболочки по разным файлам" +#error "Нужно раснести оболочки по разным файлам" -class DefinitionsProviderWrapper : public IPluginWrapper> { - auto get_dictionary_scheme() -> JSON; +class DefinitionsProviderWrapper + : public IPluginWrapper, std::string>> { + auto get_dictionary_scheme() -> nlohmann::json; }; class SentencesProviderWrapper - : public IPluginWrapper> {}; + : public IPluginWrapper, std::string>> { +}; -class ImagesProviderWrapper : public IPluginWrapper> { +class ImagesProviderWrapper + : public IPluginWrapper, std::string>> { }; -class AudiosProviderWrapper : public IPluginWrapper> { +class AudiosProviderWrapper + : public IPluginWrapper, std::string>> { }; -class FormatProcessorWrapper : public IPluginWrapper> { +class FormatProcessorWrapper + : public IPluginWrapper, std::string>> { }; #endif // WRAPPERS_H -- GitLab