diff options
-rw-r--r-- | server/include/main.h | 5 | ||||
-rw-r--r-- | server/include/modules_handler.h | 2 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/flat_files.cbp | 1 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/main.cpp | 100 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/main.h | 1 | ||||
-rw-r--r-- | server/src/api_core.cpp | 12 | ||||
-rw-r--r-- | server/src/main.cpp | 36 | ||||
-rw-r--r-- | server/src/modules_handler.cpp | 12 |
8 files changed, 162 insertions, 7 deletions
diff --git a/server/include/main.h b/server/include/main.h index 5d93569..71769b8 100644 --- a/server/include/main.h +++ b/server/include/main.h @@ -22,17 +22,22 @@ #ifndef MAIN_H_INCLUDED #define MAIN_H_INCLUDED +#include "api_module_metadata_storage.h" + #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/info_parser.hpp> namespace bpt = boost::property_tree; struct runtime_config_s{ + //TODO: define metadata and data storage modules per module alongside with default ones bpt::ptree config_file; + module_metadata_storage *default_metadata_storage; short verbosity; runtime_config_s() { verbosity = 0; + default_metadata_storage = nullptr; } }; diff --git a/server/include/modules_handler.h b/server/include/modules_handler.h index c5e91b4..5696053 100644 --- a/server/include/modules_handler.h +++ b/server/include/modules_handler.h @@ -33,6 +33,8 @@ class modules_handler void load_modules(); void on_modules_loaded(); std::string list_modules(); + std::list<module_base*> &get_metadata_modules(); + std::list<module_base*> &get_downloader_modules(); ~modules_handler(); private: void load_metadata_modules(const std::string &path); diff --git a/server/modules/metadata/flat_files/flat_files.cbp b/server/modules/metadata/flat_files/flat_files.cbp index 3081897..038dcf0 100644 --- a/server/modules/metadata/flat_files/flat_files.cbp +++ b/server/modules/metadata/flat_files/flat_files.cbp @@ -40,6 +40,7 @@ <Linker> <Add library="boost_filesystem" /> <Add library="boost_system" /> + <Add library="boost_serialization" /> </Linker> <Unit filename="main.cpp" /> <Unit filename="main.h" /> diff --git a/server/modules/metadata/flat_files/main.cpp b/server/modules/metadata/flat_files/main.cpp index baaa933..f1583c5 100644 --- a/server/modules/metadata/flat_files/main.cpp +++ b/server/modules/metadata/flat_files/main.cpp @@ -22,12 +22,52 @@ #include "main.h" #include <iostream> +#include <fstream> #include <boost/filesystem.hpp> #include <boost/bind.hpp> +#include <boost/archive/binary_oarchive.hpp> +#include <boost/archive/binary_iarchive.hpp> +#include <boost/serialization/vector.hpp> + +std::string replace_home_var(const std::string &path) +{ + std::string home = getenv("HOME"), tmp_path = path; + for(std::string::size_type p1 = tmp_path.find("~"); p1 != std::string::npos; p1 = tmp_path.find("~", p1)) + tmp_path.replace(p1, 1, home); + return tmp_path; + +} + + void storage_impl::on_modules_loaded() { - //settings = api->get_module_settings(this); //you can get module settings here, by default settings loaded automatically after this function call + //just checking if we working fine via core + //test set +/* { + std::string d = "some string\ndfjoghdfyigehygerityertoweuyrtghyuewitgweyuitgweiurtgeiutrerytiewgytyewuirtgewrgyutygergytiu"; + std::vector<char> data; + for(auto i = d.begin(), end = d.end(); i != end; ++i) + { + data.push_back(*i); + } + api->metadata_set(this, "test_setting", data); + } */ + //test get +/* { + std::vector<char> data; + api->metadata_get(this, "test_setting", data); + std::string s; + for(auto i = data.begin(), end = data.end(); i != end; ++i) + { + s.push_back(*i); + } + std::cout<<"we have read:\n"<<s<<std::endl; + } */ + //test remove +/* { + api->metadata_remove(this, "test_setting"); + } */ } @@ -39,8 +79,8 @@ void storage_impl::load(core_api *a) 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.on_modules_loaded = boost::bind(&storage_impl::on_modules_loaded, this); - std::cout<<"flat_files metadata module succesfully loaded\n"; + 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 } @@ -49,27 +89,77 @@ const module_info &storage_impl::get_module_info() return info; } + + void storage_impl::set_module_settings(const std::map<std::string, std::string> &settings) { this->settings = settings; + parsed_data_path = replace_home_var(this->settings["data_path"]); } + bool storage_impl::set(const std::string &module_name, const std::string &setting_name, const std::vector<char> &data) { - //TODO + //print data +/* std::cout<<"printing data in metadata_flat_files set api:\n"; + for(auto i = data.begin(), end = data.end(); i != end; ++i) + { + std::cout<<*i; + } + std::cout<<std::endl; */ + std::string out_file_path = parsed_data_path; + out_file_path += "/"; + out_file_path += module_name; + if(!boost::filesystem::exists(out_file_path)) + { + //TODO: validation + boost::filesystem::create_directories(out_file_path); + } + else if(!boost::filesystem::is_directory(out_file_path)) + { + //error + return false; + } + out_file_path += "/"; + out_file_path += setting_name; + std::ofstream os(out_file_path, std::ios::binary); + boost::archive::binary_oarchive ar(os); + ar<<data; return true; } bool storage_impl::get(const std::string &module_name, const std::string &setting_name, std::vector<char> &data) { - //TODO + std::string in_file_path = parsed_data_path; + in_file_path += "/"; + in_file_path += module_name; + in_file_path += "/"; + in_file_path += setting_name; + if(!boost::filesystem::exists(in_file_path) || !boost::filesystem::is_regular(in_file_path)) + { + //data does not exists + return false; + } + std::ifstream is(in_file_path, std::ios::binary); + boost::archive::binary_iarchive ar(is); + ar>>data; return true; } bool storage_impl::remove(const std::string &module_name, const std::string &setting_name) { //TODO + std::string file_path = parsed_data_path; + file_path += "/"; + file_path += module_name; + file_path += "/"; + file_path += setting_name; + if(!boost::filesystem::exists(file_path) || !boost::filesystem::is_regular(file_path)) + { + return false; + } + boost::filesystem::remove(file_path); return true; } diff --git a/server/modules/metadata/flat_files/main.h b/server/modules/metadata/flat_files/main.h index 97a2723..0798674 100644 --- a/server/modules/metadata/flat_files/main.h +++ b/server/modules/metadata/flat_files/main.h @@ -44,6 +44,7 @@ class storage_impl: public module_metadata_storage module_info info; core_api *api = nullptr; std::map<std::string, std::string> settings; + std::string parsed_data_path; }; diff --git a/server/src/api_core.cpp b/server/src/api_core.cpp index 6cee810..c8f0032 100644 --- a/server/src/api_core.cpp +++ b/server/src/api_core.cpp @@ -29,19 +29,29 @@ extern runtime_config_s runtime_config; bool core_api::metadata_set(module_base *m, const std::string &setting_name, const std::vector<char> &data) { - //TODO + //TODO: respect per module metadata modules definition + //print data +/* std::cout<<"printing data in core set api:\n"; + for(auto i = data.begin(), end = data.end(); i != end; ++i) + { + std::cout<<*i; + } + std::cout<<std::endl; */ + runtime_config.default_metadata_storage->set(m->get_module_info().name, setting_name, data); return true; } bool core_api::metadata_get(module_base *m, const std::string &setting_name, std::vector<char> &data) { //TODO + runtime_config.default_metadata_storage->get(m->get_module_info().name, setting_name, data); return true; } bool core_api::metadata_remove(module_base *m, const std::string &setting_name) { //TODO + runtime_config.default_metadata_storage->remove(m->get_module_info().name, setting_name); return true; } diff --git a/server/src/main.cpp b/server/src/main.cpp index 8cf9efc..13b291b 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -116,11 +116,47 @@ int main(int argc, char *argv[]) } if(run) { + if(!module_api) + module_api = new core_api; + if(!modules) + { + modules = new modules_handler; + modules->load_modules(); + } + + std::string default_metadata_module_name = runtime_config.config_file.get<std::string>("default_metadata_module", ""); + if(default_metadata_module_name == "") + { + if(!modules->get_metadata_modules().empty()) + runtime_config.default_metadata_storage = static_cast<module_metadata_storage*>(*(modules->get_metadata_modules().begin())); + else + std::cerr<<"Error: Metadata storage modules not installed'n"; + } + else + { + for(auto i = modules->get_metadata_modules().begin(), end = modules->get_metadata_modules().end(); i != end; ++i) + { + if((*i)->get_module_info().name == default_metadata_module_name) + runtime_config.default_metadata_storage = static_cast<module_metadata_storage*>(*i); + } + if(!runtime_config.default_metadata_storage) + { + std::cerr<<"Error: Failed to load metadata storage module named \""<<default_metadata_module_name<<"\" as default metadata storage module\n"; + } + } + if(!runtime_config.default_metadata_storage) + { + std::cerr<<"Error: Failed to set default metadata storage module\n"; + return -1; + } + modules->on_modules_loaded(); //call second initialization stage in modules + if(daemon) { //TODO: fork here } //TODO: run here + return 0; //stub for now } std::cerr<<"error: no command specified"<<std::endl; std::cout<<desc<<std::endl; diff --git a/server/src/modules_handler.cpp b/server/src/modules_handler.cpp index b897aa6..818539b 100644 --- a/server/src/modules_handler.cpp +++ b/server/src/modules_handler.cpp @@ -174,7 +174,6 @@ void modules_handler::load_modules() load_downloader_modules(self_dir + "/modules/downloader"); load_downloader_modules(replace_home_var("~/.share/udm/modules/downloader")); load_downloader_modules("/usr/lib/udm/modules/downloader"); - on_modules_loaded(); load_modules_settings(); } @@ -200,6 +199,17 @@ void modules_handler::load_modules_settings() (*i)->set_module_settings(module_api->get_module_settings(*i)); } +std::list<module_base*> &modules_handler::get_metadata_modules() +{ + return metadata_modules; +} + +std::list<module_base*> &modules_handler::get_downloader_modules() +{ + return downloader_modules; +} + + modules_handler::~modules_handler() { //dtor |