summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2015-03-27 16:07:27 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2015-03-27 16:07:27 +0300
commitff44c9cd55bff146c6c47277656a967045184c7b (patch)
treeb1d5d59696c8811db0badea0e6dcedca3601b183 /server/src
parentf87e3320c499edccd545ff3bfc244ef5c7216bb5 (diff)
working module loader
basic program options support abitilty to print installed modules info started config support implementation
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main.cpp106
-rw-r--r--server/src/modules_handler.cpp142
2 files changed, 227 insertions, 21 deletions
diff --git a/server/src/main.cpp b/server/src/main.cpp
index 54bb498..67e5fa6 100644
--- a/server/src/main.cpp
+++ b/server/src/main.cpp
@@ -17,36 +17,100 @@
along with UDM. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <boost/program_options.hpp>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/info_parser.hpp>
+#include <boost/filesystem.hpp>
+#include <iostream>
-#include <dlfcn.h>
#include <api_module_metadata_storage.h>
+#include <modules_handler.h>
-core_api module_api;
-void module_load_test()
+core_api *module_api = nullptr;
+
+modules_handler *modules = nullptr;
+
+namespace bpo = boost::program_options;
+namespace bpt = boost::property_tree;
+
+bpt::ptree config;
+
+
+
+int main(int argc, char *argv[])
{
- void *lib = dlopen("./modules/metadata/libflat_files.so", RTLD_LAZY);
- if(!lib)
+ bpo::options_description desc("Available commands and options");
+ desc.add_options()
+ ("help", "this message")
+ ("daemon", "fork to background")
+ ("config", bpo::value<std::string>(), "use specified config file instead of \"~/.config/udm/udm.conf\"")
+ ("list-modules", "list all installed modules")
+ ("run", "start UDM server")
+ ;
+
+ bool daemon = false, run = false;
+ std::string config_path = "~/.config/udm/udm.conf";
+ try{
+
+ bpo::variables_map vm;
+ bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
+ bpo::notify(vm);
+
+ //load config first, as it may be needed for other commands
+ if(vm.count("config"))
+ config_path = vm["config"].as<std::string>();
+
+ if(boost::filesystem::exists(config_path) && boost::filesystem::is_regular(config_path))
+ bpt::read_info(config_path, config); //TODO: finish this
+ else
+ std::cerr<<"failed to load config: \"" + config_path + "\", file does not exists or is not regular file\n";
+
+
+ if(vm.count("help"))
+ {
+ std::cout<<desc<<std::endl;
+ return 0;
+ }
+ if(vm.count("daemon"))
+ {
+ daemon = true;
+ }
+ if(vm.count("list-modules"))
+ {
+ if(!module_api)
+ module_api = new core_api;
+ if(!modules)
+ modules = new modules_handler;
+ std::cout<<modules->list_modules();
+ return 0;
+ }
+ if(vm.count("run"))
+ {
+ run = true;
+ }
+ }
+ catch(std::exception &e)
{
- printf("%s\n", dlerror());
- return;
+ std::cerr<<"error: "<<e.what()<<std::endl;
+ std::cout<<desc<<std::endl;
+ return -1;
}
- void *fptr = dlsym(lib, "load");
- if(!fptr)
+ catch(...)
{
- printf("%s\n", dlerror());
- dlclose(lib);
- return;
+ //noop
+ return -1;
}
- void *(*f)(void);
- f = (void* (*)())fptr;
- module_metadata_storage *m = static_cast<module_metadata_storage*>(f());
- m->load(&module_api);
-}
-
+ if(run)
+ {
+ if(daemon)
+ {
+ //TODO: fork here
+ }
+ }
+ //module_api must be created first
+ std::cerr<<"error: no command specified"<<std::endl;
+ std::cout<<desc<<std::endl;
-int main(int argc, char *argv[])
-{
- module_load_test();
return 0;
}
diff --git a/server/src/modules_handler.cpp b/server/src/modules_handler.cpp
new file mode 100644
index 0000000..a6dbcef
--- /dev/null
+++ b/server/src/modules_handler.cpp
@@ -0,0 +1,142 @@
+/*
+ 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/>.
+
+*/
+
+#include "modules_handler.h"
+#include <unistd.h>
+#include <dlfcn.h>
+#include <string>
+
+#include <boost/filesystem.hpp>
+
+
+#include <iostream>
+
+extern core_api *module_api;
+
+
+void modules_handler::load_metadata_modules(const std::string &path)
+{
+ if(boost::filesystem::exists(path) && boost::filesystem::is_directory(path))
+ {
+ for(boost::filesystem::directory_iterator i(path), end = boost::filesystem::directory_iterator(); i != end; ++i)
+ {
+ void *lib = dlopen(i->path().generic_string().c_str(), RTLD_LAZY);
+ if(!lib)
+ {
+ printf("failed to open library \"%s\" with error: %s\n", i->path().c_str(), dlerror());
+ return;
+ }
+ void *fptr = dlsym(lib, "udm_metadata_module_load");
+ if(!fptr)
+ {
+ printf("failed to find symbol \"udm_metadata_module_load\" in library %s with error %s\n", i->path().c_str(), dlerror());
+ dlclose(lib);
+ return;
+ }
+ void *(*f)(void);
+ f = (void* (*)())fptr;
+ //TODO: aditional sanity checks
+ module_metadata_storage *m = static_cast<module_metadata_storage*>(f());
+ m->load(module_api);
+ metadata_modules.push_back(m);
+ }
+ }
+}
+
+void modules_handler::load_downloader_modules(const std::string &path)
+{
+ if(boost::filesystem::exists(path) && boost::filesystem::is_directory(path))
+ {
+ for(boost::filesystem::directory_iterator i(path), end = boost::filesystem::directory_iterator(); i != end; ++i)
+ {
+ void *lib = dlopen(i->path().generic_string().c_str(), RTLD_LAZY);
+ if(!lib)
+ {
+ printf("failed to open library \"%s\" with error: %s\n", i->path().c_str(), dlerror());
+ return;
+ }
+ void *fptr = dlsym(lib, "udm_downloader_module_load");
+ if(!fptr)
+ {
+ printf("failed to find symbol \"udm_downloader_module_load\" in library %s with error %s\n", i->path().c_str(), dlerror());
+ dlclose(lib);
+ return;
+ }
+ void *(*f)(void);
+ f = (void* (*)())fptr;
+ //TODO: aditional sanity checks
+ module_metadata_storage *m = static_cast<module_metadata_storage*>(f());
+ m->load(module_api);
+ metadata_modules.push_back(m);
+ }
+ }
+}
+
+std::string modules_handler::get_self_path()
+{
+ //TODO: dynamic buffer
+ char buf[1024];
+ readlink("/proc/self/exe", buf, 1023);
+ std::string s (buf);
+ return s;
+}
+
+std::string modules_handler::list_modules()
+{
+ std::string buf;
+ buf += "Installed metadata modules:\n";
+ for(auto i = metadata_modules.begin(), end = metadata_modules.end(); i != end; ++i)
+ {
+ buf += "\t";
+ buf += (*i)->get_module_info().name;
+ buf += " (" + (*i)->get_module_info().description + ") ";
+ buf += "v" + (*i)->get_module_info().version;
+ buf += "\n";
+ }
+ buf += "Installed downloader modules:\n";
+ for(auto i = downloader_modules.begin(), end = downloader_modules.end(); i != end; ++i)
+ {
+ buf += "\t";
+ buf += (*i)->get_module_info().name;
+ buf += " (" + (*i)->get_module_info().description + ") ";
+ buf += "v" + (*i)->get_module_info().version;
+ buf += "\n";
+ }
+ return buf;
+}
+
+
+modules_handler::modules_handler()
+{
+ self_path = get_self_path();
+ boost::filesystem::path p(self_path);
+ std::string self_dir = p.parent_path().generic_string();
+ load_metadata_modules(self_dir + "/modules/metadata");
+ load_metadata_modules("~/.share/udm/modules/metadata");
+ load_metadata_modules("/usr/lib/udm/modules/metadata");
+ load_downloader_modules(self_dir + "/modules/downloader");
+ load_downloader_modules("~/.share/udm/modules/downloader");
+ load_downloader_modules("/usr/lib/udm/modules/downloader");
+}
+
+modules_handler::~modules_handler()
+{
+ //dtor
+}