diff --git a/CMakeLists.txt b/CMakeLists.txt index d972a86443ee6092343c0ccacd7516c3b7bd1a7a..23a015951e9a1da6e14f2f39c9a9c2cf15c8e63c 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 48eeef1ea94c17c51d542f44b31d71efbd4e756e..1eb388d799f10596188a0b94ceed0f2a75e5027e 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 70ceeda5a984cde82b0990512c6a3e51f15ac12b..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..7939578160fd85fd0e9abcc26812748300968e0d --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/server/lib/containers/containers.hpp b/server/lib/containers/containers.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fed4e4229f2dae2b92e88e9a28df5c2aecac00c8 --- /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 8895a9ce7c1097ce5ddd6ccc08d6df73af244e62..f5f397496098928bc384f5ca20b704bb2166a5a6 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 0000000000000000000000000000000000000000..e6ca5c0c06dbaf61175f176caff98451e56ffd83 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/server/lib/plugins_loader/plugins_loader.hpp b/server/lib/plugins_loader/plugins_loader.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f134e91370aad8e12ff34db1508717cf8de154b9 --- /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 0000000000000000000000000000000000000000..a088aa500f49bf118690c855bbe9b1b8874e0dc3 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/server/lib/plugins_provider/plugins_provider.hpp b/server/lib/plugins_provider/plugins_provider.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6e13dd7efce6c034d5b6f43fa20c516c61b9dbe8 --- /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 acdb6219969a04f43a7e1a6847f9985fe6be0f6d..b72b99104a2a18caf6e1871bb57b3e56bb5b33c2 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 c057c99ee2d5e4e52f0cb1440441b5c8ccbd8058..0947ed4480b0863fb68d4c999d804e71afdd8a9d 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 03a5a969b1d44047327ed4f5d9e053e2e1f9fcfb..4b34aa8f076de4278447e8237526de806de23da1 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 c60bce74550225c1d88511a301520b12bf4e8b8f..5dc822049d1817158033ecb84437e8bb09d0fc8d 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 0000000000000000000000000000000000000000..1716a7219b86c9fa5bbc45f026a10dcdbd79b77a --- /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 07018e50dbfe07b29132b296cd204a6c774491dc..8c586acce2589dd7f33d553d4c052409499b5b89 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 272ccbf2d852bca882b5e4f94b498b6b9d6c38c2..5beac0497daf151610b0a2aea9ba43de3239b89f 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 e1dee14824ce7118459813d0ae0d893ceed152ff..3afd4e8f9040a036fccfbe2f3dd951b73628bc34 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