diff options
-rw-r--r-- | protocol/udm.proto | 7 | ||||
-rw-r--r-- | server/include/api_core.h | 5 | ||||
-rw-r--r-- | server/include/api_module_base.h | 22 | ||||
-rw-r--r-- | server/include/api_module_downloader.h | 24 | ||||
-rw-r--r-- | server/include/settings.h | 40 | ||||
-rw-r--r-- | server/modules/downloaders/curl/main.cpp | 8 | ||||
-rw-r--r-- | server/modules/downloaders/curl/main.h | 8 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/main.cpp | 8 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/main.h | 8 | ||||
-rw-r--r-- | server/src/api_core.cpp | 6 | ||||
-rw-r--r-- | server/src/modules_handler.cpp | 29 | ||||
-rw-r--r-- | server/src/server_session.cpp | 30 |
12 files changed, 135 insertions, 60 deletions
diff --git a/protocol/udm.proto b/protocol/udm.proto index a801b00..0825dc1 100644 --- a/protocol/udm.proto +++ b/protocol/udm.proto @@ -176,8 +176,9 @@ message setting_info { optional string default_value = 1 [default = "empty"]; optional string minimal_value = 2 [default = "not set"]; optional string maximal_value = 3 [default = "not set"]; - repeated string dependencies = 4; //list containing all settings which must be turned on for this setting - repeated string blockers = 5; //list of settings which must be turned of for this setting + optional string description = 4; + repeated string dependencies = 5; //list containing all settings which must be turned on for this setting + repeated string blockers = 6; //list of settings which must be turned of for this setting } message setting { @@ -209,7 +210,7 @@ message module_info //general module info for client including settings, downloa required SERVER_MODULE_TYPE type = 1; required string name = 2; //unique (used as module id) required string version = 3; - optional string decription = 4 [default = "no description specified"]; + optional string description = 4 [default = "no description specified"]; repeated setting settings = 5; repeated module_download_ui_element_info download_info_ui = 6; //always complete here repeated module_download_ui_element_info download_creation_ui = 7; diff --git a/server/include/api_core.h b/server/include/api_core.h index 341bfc2..9af26f7 100644 --- a/server/include/api_core.h +++ b/server/include/api_core.h @@ -25,14 +25,15 @@ #include <vector> #include <map> -class module_base; +#include "settings.h" +class module_base; class core_api { public: //core - virtual std::map<std::string, std::string> get_module_settings(module_base *m); + virtual std::map<std::string, setting_s> get_module_settings(module_base *m); virtual std::map<std::string, std::string> get_core_settings(); //metadata diff --git a/server/include/api_module_base.h b/server/include/api_module_base.h index 89fc4dd..d8acf9b 100644 --- a/server/include/api_module_base.h +++ b/server/include/api_module_base.h @@ -21,25 +21,35 @@ #ifndef API_MODULE_BASE_H_INCLUDED #define API_MODULE_BASE_H_INCLUDED -#include <api_core.h> +#include "api_core.h" +#include "settings.h" #include <boost/function.hpp> -struct module_info + + +struct module_info_base { std::string name, description, version; //module name must be set and must be unique, description and version is optional - std::map<std::string, std::string> default_settings; //set all settings supported by module here with default values to save in config for editing later + std::map<std::string, setting_s> default_settings; //set all settings supported by module here with default values to save in config for editing later boost::function<void()> on_modules_loaded; //if set, this function will be called after basic initialization of all installed modules, so installed modules can be partially used here, for example metadata storage will be available }; + + class module_base { public: virtual void load(core_api *a) = 0; - virtual const module_info &get_module_info() = 0; - virtual void set_module_settings(const std::map<std::string, std::string> &settings) = 0; //will called with settings stored in config + virtual const module_info_base &get_module_info() = 0; + virtual void set_module_settings(const std::map<std::string, setting_s> &settings) = 0; //will called with settings stored in config + virtual std::map<std::string, setting_s> &get_runtime_module_settings() //get runtime module_settings, works faster than core_api function + { + return settings; + } virtual ~module_base() = 0; protected: - std::map<std::string, std::string> settings; + std::map<std::string, setting_s> settings; + core_api *api = nullptr; }; diff --git a/server/include/api_module_downloader.h b/server/include/api_module_downloader.h index ae91943..d980d62 100644 --- a/server/include/api_module_downloader.h +++ b/server/include/api_module_downloader.h @@ -27,16 +27,16 @@ //i decided to avoid protobuf dependency in modules, so here is a copy/past of some protobuf declared structures required by downloader module enum MODULE_UI_ELEMENT_TYPE_e { - UI_EMPTY = 0, //helper type to set empty element - UI_STR = 1, //ui elements containing strings - UI_INTEGER, //numeric only ui elements (use strings instead ?) - UI_PROGRESS_BAR, //generic progress bar - UI_WINDOW, //ui window ... - UI_GROUP //empty ui element to group children together in ui (tabs can be implemented using this type) + UI_EMPTY_ = 0, //helper type to set empty element + UI_STR_ = 1, //ui elements containing strings + UI_INTEGER_, //numeric only ui elements (use strings instead ?) + UI_PROGRESS_BAR_, //generic progress bar + UI_WINDOW_, //ui window ... + UI_GROUP_ //empty ui element to group children together in ui (tabs can be implemented using this type) }; struct module_download_ui_element_info_s { - MODULE_UI_ELEMENT_TYPE_e type = UI_EMPTY; + MODULE_UI_ELEMENT_TYPE_e type = UI_EMPTY_; std::list<module_download_ui_element_info_s> children; std::string name; // name can be non unique int id; //internal element id used to get element value (should be unique for every loaded module) @@ -51,7 +51,7 @@ struct module_download_menu_element_info_s { }; -struct downloader_module_info : public module_info //downloader_module_info must be returned via module_base::get_module_info() for downloader module +struct downloader_module_info : public module_info_base //downloader_module_info must be returned via module_base::get_module_info() for downloader module { std::list<module_download_menu_element_info_s> download_root_menu, download_content_menu; //downloader module can add additional menu entries for download root and download content if content management supported by protocol std::list<module_download_ui_element_info_s> download_info_ui, download_creation_ui; //downloader module should set all data fields required for download creation, also may set data fields for info in ui in client @@ -59,15 +59,15 @@ struct downloader_module_info : public module_info //downloader_module_info must }; enum DOWNLOAD_CONTENT_ENTRY_TYPE_s { - DOWNLOAD_CONTENT_ENTRY_TYPE_FILE = 0, - DOWNLOAD_CONTENT_ENTRY_TYPE_DIRECTORY = 1 + DOWNLOAD_CONTENT_ENTRY_TYPE_FILE_ = 0, + DOWNLOAD_CONTENT_ENTRY_TYPE_DIRECTORY_ = 1 }; struct download_content_entry_s { //here is basic content entry description, this may be file or directory, i have set basic attributes required, but additional may be specified in info_map std::string name; //file/dir name, required - DOWNLOAD_CONTENT_ENTRY_TYPE_s type = DOWNLOAD_CONTENT_ENTRY_TYPE_FILE; + DOWNLOAD_CONTENT_ENTRY_TYPE_s type = DOWNLOAD_CONTENT_ENTRY_TYPE_FILE_; std::list<download_content_entry_s> children; int64_t size = 0, downloaded = 0; //file/dir size, downloaded size, optional std::map<std::string, std::string> info_map; //any additional info @@ -93,6 +93,8 @@ public: virtual bool execute_action_on_download(int download_id, int action_id) = 0; //execute module defined action on download, by id received via add_download virtual std::list<download_s> get_downloads(std::map<int, std::string> params = std::map<int, std::string>()) = 0; //this function must return list of downloads, additional options may be passed via params map virtual download_s get_download_info(int download_id, std::map<int, std::string> params = std::map<int, std::string>()) = 0; //same as above, except this one is for single download, not a complete list +protected: + downloader_module_info info; }; diff --git a/server/include/settings.h b/server/include/settings.h new file mode 100644 index 0000000..ba8f017 --- /dev/null +++ b/server/include/settings.h @@ -0,0 +1,40 @@ +/* + Copyright © 2015 Gluzskiy Alexandr (sss) + + This file is part of Unknown Download Manager (UDM). + + UDM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + UDM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with UDM. If not, see <http://www.gnu.org/licenses/>. + +*/ + + + +#ifndef SETTINGS_H +#define SETTINGS_H + +#include <list> + +struct setting_info_s { + std::string default_value = "empty", minimal_value = "not set", maximal_value = "not set", description; + std::list<std::string> dependencies, blockers; //list containing all settings which must be turned on for this setting, list of settings which must be turned of for this setting +}; + +struct setting_s { + setting_info_s info; + std::string value; +}; + + + +#endif diff --git a/server/modules/downloaders/curl/main.cpp b/server/modules/downloaders/curl/main.cpp index 66f89b4..23c4591 100644 --- a/server/modules/downloaders/curl/main.cpp +++ b/server/modules/downloaders/curl/main.cpp @@ -57,21 +57,21 @@ void downloader::load(core_api *a) module_download_ui_element_info_s e; e.id = 0; e.name = "Url"; - e.type = MODULE_UI_ELEMENT_TYPE_e::UI_STR; + e.type = MODULE_UI_ELEMENT_TYPE_e::UI_STR_; info.download_creation_ui.push_back(e); e.id = 1; e.name = "Download path"; - e.type = MODULE_UI_ELEMENT_TYPE_e::UI_STR; + e.type = MODULE_UI_ELEMENT_TYPE_e::UI_STR_; info.download_creation_ui.push_back(e); info.on_modules_loaded = boost::bind(&downloader::on_modules_loaded, this); //optional definition of function which is called after all modules loaded } -const module_info &downloader::get_module_info() +const module_info_base &downloader::get_module_info() { return info; } -void downloader::set_module_settings(const std::map<std::string, std::string> &settings) +void downloader::set_module_settings(const std::map<std::string, setting_s> &settings) { this->settings = settings; } diff --git a/server/modules/downloaders/curl/main.h b/server/modules/downloaders/curl/main.h index aff5cb3..d97e72e 100644 --- a/server/modules/downloaders/curl/main.h +++ b/server/modules/downloaders/curl/main.h @@ -37,8 +37,8 @@ public: //core module api downloader(); void load(core_api *a); - const module_info &get_module_info(); - void set_module_settings(const std::map<std::string, std::string> &settings); //will called with settings stored in config + const module_info_base &get_module_info(); + void set_module_settings(const std::map<std::string, setting_s> &settings); //will called with settings stored in config ~downloader(); //downloader module api int add_download(std::map<int, std::string> params); //add download, this function must return unique for current session and current downloader download id @@ -50,9 +50,7 @@ public: download_s get_download_info(int download_id, std::map<int, std::string> params = std::map<int, std::string>()); //same as above, except this one is for single download, not a complete list private: void on_modules_loaded(); - downloader_module_info info; - core_api *api = nullptr; - std::map<std::string, std::string> settings; //settings name, val +// downloader_module_info info; std::map<int, my_download> downloads; //map of id, download }; diff --git a/server/modules/metadata/flat_files/main.cpp b/server/modules/metadata/flat_files/main.cpp index f1583c5..d0e1040 100644 --- a/server/modules/metadata/flat_files/main.cpp +++ b/server/modules/metadata/flat_files/main.cpp @@ -78,23 +78,23 @@ void storage_impl::load(core_api *a) info.name = "flat_files_metadata"; info.description = "this module provide metadata storage in flat files"; info.version = "0.0.0.1draft"; - info.default_settings["data_path"] = "~/.local/share/udm/metadata"; + info.default_settings["data_path"].value = "~/.local/share/udm/metadata"; info.on_modules_loaded = boost::bind(&storage_impl::on_modules_loaded, this); //optional definition of function which is called after all modules loaded //std::cout<<"flat_files metadata module succesfully loaded\n"; //working fine } -const module_info &storage_impl::get_module_info() +const module_info_base &storage_impl::get_module_info() { return info; } -void storage_impl::set_module_settings(const std::map<std::string, std::string> &settings) +void storage_impl::set_module_settings(const std::map<std::string, setting_s> &settings) { this->settings = settings; - parsed_data_path = replace_home_var(this->settings["data_path"]); + parsed_data_path = replace_home_var(this->settings["data_path"].value); } diff --git a/server/modules/metadata/flat_files/main.h b/server/modules/metadata/flat_files/main.h index 0798674..b315d62 100644 --- a/server/modules/metadata/flat_files/main.h +++ b/server/modules/metadata/flat_files/main.h @@ -32,8 +32,8 @@ class storage_impl: public module_metadata_storage storage_impl(); //module base void load(core_api *a); - const module_info &get_module_info(); - void set_module_settings(const std::map<std::string, std::string> &settings); + const module_info_base &get_module_info(); + void set_module_settings(const std::map<std::string, setting_s> &settings); //metadata module bool set(const std::string &module_name, const std::string &setting_name, const std::vector<char> &data); bool get(const std::string &module_name, const std::string &setting_name, std::vector<char> &data); @@ -41,9 +41,7 @@ class storage_impl: public module_metadata_storage private: void on_modules_loaded(); - module_info info; - core_api *api = nullptr; - std::map<std::string, std::string> settings; + module_info_base info; std::string parsed_data_path; }; diff --git a/server/src/api_core.cpp b/server/src/api_core.cpp index b407928..f9fe772 100644 --- a/server/src/api_core.cpp +++ b/server/src/api_core.cpp @@ -53,15 +53,15 @@ bool core_api::metadata_remove(module_base *m, const std::string &setting_name) return true; } -std::map<std::string, std::string> core_api::get_module_settings(module_base *m) +std::map<std::string, setting_s> core_api::get_module_settings(module_base *m) { - std::map<std::string, std::string> settings; + std::map<std::string, setting_s> settings; std::string module = "modules."; module += m->get_module_info().name; try{ for(auto i : runtime_config.config_file.get_child(module)) { - settings[i.first] = i.second.get_value<std::string>(""); + settings[i.first].value = i.second.get_value<std::string>(""); } } catch(...) diff --git a/server/src/modules_handler.cpp b/server/src/modules_handler.cpp index 32c3b9f..6d351bb 100644 --- a/server/src/modules_handler.cpp +++ b/server/src/modules_handler.cpp @@ -137,7 +137,7 @@ std::string modules_handler::list_modules_single_type_internal(const std::list<m buf += "\n\t\t"; buf += i1.first; buf += " = "; - buf += i1.second; + buf += i1.second.value; } } buf += "\n\n"; @@ -147,12 +147,13 @@ std::string modules_handler::list_modules_single_type_internal(const std::list<m void modules_handler::sync_module_settings(module_base *m) { + //update config file with available modules settings for(auto i : m->get_module_info().default_settings) { std::string setting = "modules." + m->get_module_info().name + "." + i.first; - std::string current_val = runtime_config.config_file.get<std::string>(setting, "not set"), default_val = i.second; - if(current_val == "not set" || current_val != default_val) - runtime_config.config_file.put(setting, i.second); + std::string current_val = runtime_config.config_file.get<std::string>(setting, "not set"); + if(current_val == "not set") + runtime_config.config_file.put(setting, i.second.value); } } @@ -177,24 +178,24 @@ void modules_handler::load_modules() void modules_handler::on_modules_loaded() { - for(auto i = metadata_modules.begin(), end = metadata_modules.end(); i != end; ++i) + for(auto i : metadata_modules) { - if(!(*i)->get_module_info().on_modules_loaded.empty()) - (*i)->get_module_info().on_modules_loaded(); + if(!i->get_module_info().on_modules_loaded.empty()) + i->get_module_info().on_modules_loaded(); } - for(auto i = downloader_modules.begin(), end = downloader_modules.end(); i != end; ++i) + for(auto i : downloader_modules) { - if(!(*i)->get_module_info().on_modules_loaded.empty()) - (*i)->get_module_info().on_modules_loaded(); + if(!i->get_module_info().on_modules_loaded.empty()) + i->get_module_info().on_modules_loaded(); } } void modules_handler::load_modules_settings() { - for(auto i = metadata_modules.begin(), end = metadata_modules.end(); i != end; ++i) - (*i)->set_module_settings(module_api->get_module_settings(*i)); - for(auto i = downloader_modules.begin(), end = downloader_modules.end(); i != end; ++i) - (*i)->set_module_settings(module_api->get_module_settings(*i)); + for(auto i : metadata_modules) + i->set_module_settings(module_api->get_module_settings(i)); + for(auto i : downloader_modules) + i->set_module_settings(module_api->get_module_settings(i)); } std::list<module_base*> &modules_handler::get_metadata_modules() diff --git a/server/src/server_session.cpp b/server/src/server_session.cpp index 64f2cfe..3affc5f 100644 --- a/server/src/server_session.cpp +++ b/server/src/server_session.cpp @@ -26,8 +26,10 @@ #include "utilities.h" #include "socket_wraper.h" #include "client.h" +#include "modules_handler.h" extern std::map<std::string, client> clients; +extern modules_handler *modules; @@ -183,8 +185,6 @@ void server_session::handle_command(client_msg *msg) } else client_ = i->second; - - //set auth token } send_message(&m); @@ -192,11 +192,35 @@ void server_session::handle_command(client_msg *msg) break; case CLIENT_MSG_TYPE::CLIENT_MODULES_REQUEST: { - //TODO: + server_msg msg; + msg.set_type(SERVER_MSG_TYPE::SERVER_MODULES_REPLY); + for(auto i : modules->get_downloader_modules()) + { + module_info *mi = msg.add_server_modules_reply(); + mi->set_name(i->get_module_info().name); + mi->set_description(i->get_module_info().description); + mi->set_version(i->get_module_info().version); + for(auto ms : i->get_runtime_module_settings()) + { + setting *msi = mi->add_settings(); + msi->set_name(ms.first); + msi->set_value(ms.second.value); + for(auto di : ms.second.info.dependencies) + msi->mutable_info()->add_dependencies(di); + for(auto bi : ms.second.info.blockers) + msi->mutable_info()->add_blockers(bi); + msi->mutable_info()->set_default_value(ms.second.info.default_value); + msi->mutable_info()->set_minimal_value(ms.second.info.minimal_value); + msi->mutable_info()->set_maximal_value(ms.second.info.maximal_value); + msi->mutable_info()->set_description(ms.second.info.description); + } + } + send_message(&msg); } break; case CLIENT_MSG_TYPE::CLIENT_CORE_INFO_REQUEST: { + //TODO: settings values here server_msg msg; msg.set_type(SERVER_MSG_TYPE::SERVER_CORE_INFO_REPLY); msg.mutable_server_core_info_reply()->set_version(1); |