1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
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 <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <map>
#include <signal.h>
#include "api_module_metadata_storage.h"
#include "modules_handler.h"
#include "utilities.h"
#include "server.h"
#include "main.h"
#include "config.h"
#include "client.h"
#include "download_internal.h"
core_api *module_api = nullptr;
modules_handler *modules = nullptr;
namespace bpo = boost::program_options;
std::map<std::string, client*> clients; //auth token used for key
runtime_config_s runtime_config;
std::map<int, download_internal_s> downloads;
server *serv = nullptr;
//TODO: "core" config section architecture, define base settings
void on_exit()
{
serv->terminate();
bpt::write_info(runtime_config.config_file_path, runtime_config.config_file); //save config on sigint
for(auto m : modules->get_downloader_modules())
delete m;
for(auto m : modules->get_metadata_modules())
delete m;
}
void sig_handler(int sig)
{
switch(sig)
{
default:
{
on_exit();
exit(0);
}
break;
}
}
extern "C" int main(int argc, char *argv[])
{
//handle signals
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGABRT, sig_handler);
//this will not work
signal(SIGKILL, sig_handler);
signal(SIGSTOP, sig_handler);
bpo::options_description desc("Available commands and options");
desc.add_options()
("help,h", "this message")
("daemon,d", "fork to background (must be combined with \"--run\", does nothing alone)")
("config,c", bpo::value<std::string>(), "use specified config file instead of \"~/.config/udm/udm.conf\"")
("list-modules", "list all installed modules (you can set verbosity level, accepted values from 0 to 1, default = 0)")
("verbosity,v", bpo::value<short>()->default_value(0), "set global verbosity level")
("run", "start UDM server")
;
bool daemon = false, run = false;
std::string config_path = "~/.config/udm/udm.conf", parsed_config_path;
try{
bpo::variables_map vm;
bpo::store(bpo::command_line_parser(argc, argv).options(desc).style(bpo::command_line_style::unix_style | bpo::command_line_style::allow_long_disguise).run(), 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>();
{
parsed_config_path = replace_home_var(config_path);
if(boost::filesystem::exists(parsed_config_path) && boost::filesystem::is_regular(parsed_config_path))
bpt::read_info(parsed_config_path, runtime_config.config_file); //TODO: finish this
else
std::cerr<<"failed to load config: \"" + config_path + "\", file does not exists or is not regular file\n";
runtime_config.config_file_path = parsed_config_path;
}
{
auto server_pt = runtime_config.config_file.find("server");
if(server_pt == runtime_config.config_file.not_found())
{
runtime_config.config_file.put("server", "");
server_pt = runtime_config.config_file.find("server");
}
auto it = server_pt->second.find("port");
if(it == server_pt->second.not_found())
server_pt->second.put("port", 6613);
it = server_pt->second.find("password");
if(it == server_pt->second.not_found())
server_pt->second.put("password", "");
it = server_pt->second.find("default_download_directory");
if(it == server_pt->second.not_found())
server_pt->second.put("default_download_directory", "~/udm/downloads");
it = server_pt->second.find("default_metadata_module");
if(it == server_pt->second.not_found())
server_pt->second.put("default_metadata_module", "flat_files_metadata");
it = server_pt->second.find("daemon");
if(it == server_pt->second.not_found())
server_pt->second.put("daemon", false);
it = server_pt->second.find("verbosity");
if(it == server_pt->second.not_found())
server_pt->second.put("verbosity", 0);
it = server_pt->second.find("enable_encryption");
if(it == server_pt->second.not_found())
server_pt->second.put("enable_encryption", false);
it = server_pt->second.find("ssl_certificate");
if(it == server_pt->second.not_found())
server_pt->second.put("ssl_certificate", "~/config/udm/cert.crt");
it = server_pt->second.find("ssl_key");
if(it == server_pt->second.not_found())
server_pt->second.put("ssl_key", "~/config/udm/cert.key");
it = server_pt->second.find("ssl_ca");
if(it == server_pt->second.not_found())
server_pt->second.put("ssl_ca", "~/config/udm/ca.crt");
it = server_pt->second.find("ssl_dh");
if(it == server_pt->second.not_found())
server_pt->second.put("ssl_dh", "~/config/udm/dh.pem");
}
//load all config variables here
runtime_config.settings.verbosity = runtime_config.config_file.get<short>("server.verbosity", 0);
daemon = runtime_config.config_file.get<bool>("server.daemon", false);
//override config from command line here
if(vm.count("verbosity"))
{
runtime_config.settings.verbosity = vm["verbosity"].as<short>();
}
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;
modules->load_modules();
}
std::cout<<modules->list_modules();
bpt::write_info(parsed_config_path, runtime_config.config_file); //save config on exit
return 0;
}
if(vm.count("run"))
{
run = true;
}
}
catch(std::exception &e)
{
std::cerr<<"error: "<<e.what()<<std::endl;
std::cout<<desc<<std::endl;
return -1;
}
catch(...)
{
//noop
return -1;
}
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>("server.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
modules->sync_downloads(downloads);
if(daemon)
{
//TODO: fork here
}
boost::asio::io_service io_service_server;
try{
//TODO: server options ip address, interface name
serv = new server(io_service_server, runtime_config, clients, downloads, runtime_config.config_file.get<int>("server.port", 6613));
}
catch(std::exception &e)
{
//TODO:
}
catch(...)
{
//TODO:
}
//TODO: run in separate thread ?
boost::system::error_code ec;
io_service_server.run(ec);
if(ec)
{
//TODO:
}
//TODO: run here
return 0; //stub for now
}
std::cerr<<"error: no command specified"<<std::endl;
std::cout<<desc<<std::endl;
bpt::write_info(parsed_config_path, runtime_config.config_file); //save config on exit
return 0;
}
|