-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.cpp
More file actions
741 lines (636 loc) · 22 KB
/
application.cpp
File metadata and controls
741 lines (636 loc) · 22 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include <appbase/application.hpp>
#include "signals_handler.hpp"
#include <hive/utilities/logging_config.hpp>
#include <hive/utilities/notifications.hpp>
#include <hive/utilities/data_collector.hpp>
#include <hive/utilities/options_description_ex.hpp>
#include <fc/thread/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/throw_exception.hpp>
#include <boost/assert/source_location.hpp>
#include <boost/scope_exit.hpp>
#include <iostream>
#include <fstream>
#include <thread>
namespace appbase {
[[noreturn]] void throw_plugin_not_found_exception(
const std::string& name, const char* file, unsigned line, const char* func)
{
boost::source_location loc(file, line, func);
boost::throw_exception(std::runtime_error("unable to find plugin: " + name), loc);
}
[[noreturn]] void throw_plugin_state_exception(
const std::string& message, const char* file, unsigned line, const char* func)
{
boost::source_location loc(file, line, func);
boost::throw_exception(std::runtime_error(message), loc);
}
class quoted
{
public:
explicit quoted(const char* s)
: data(s)
{}
friend std::ostream& operator<<(std::ostream& stream, const quoted& q)
{
return stream << "\"" << q.data << "\"";
}
private:
const char* data;
};
namespace bpo = boost::program_options;
using bpo::options_description;
using bpo::variables_map;
using hive::utilities::options_description_ex;
using std::cout;
class application_impl {
public:
application_impl() :
_app_options("Application Options")
{}
const variables_map* _options = nullptr;
options_description_ex _app_options;
options_description_ex _cfg_options;
variables_map _args;
bfs::path _data_dir;
std::unique_ptr<fc::thread> _logging_thread;
/// Notification/status members moved here from the public header (PIMPL)
mutable hive::utilities::notifications::notification_handler_wrapper notification_handler;
hive::utilities::statuses_signal_manager status;
void broadcast(hive::utilities::notifications::notification_t notification) const
{
hive::utilities::notifications::error_handler([&]{
notification_handler.broadcast(std::move(notification));
});
}
};
application::application()
: pre_shutdown_plugins(
[]( abstract_plugin* a, abstract_plugin* b )
{
assert( a && b );
return a->get_pre_shutdown_order() > b->get_pre_shutdown_order();
}
),
my(new application_impl()), handler_wrapper(std::make_unique<signals_handler_wrapper>([this](){ generate_interrupt_request(); }))
{
}
application::~application() { }
void application::init_signals_handler()
{
handler_wrapper->init();
notify_status("signals attached");
}
void application::generate_interrupt_request()
{
notify_status("interrupted");
_is_interrupt_request = true;
}
void application::init_logging_thread()
{
FC_ASSERT( !my->_logging_thread, "Logging thread is already initialized" );
// Initialize logging thread AFTER signals are blocked
// so it inherits the blocked signal mask
my->_logging_thread = std::make_unique<fc::thread>("logging_thread");
}
fc::optional< fc::logging_config > application::load_logging_config()
{
fc::optional< fc::logging_config > logging_config;
const variables_map& args = get_args();
my->_logging_thread->async( [this, args, &logging_config]{
try
{
logging_config =
hive::utilities::load_logging_config( args, data_dir() );
if( logging_config )
fc::configure_logging( *logging_config );
}
catch( const fc::exception& e )
{
wlog( "Error parsing logging config. ${e}", ("e", e.to_string()) );
}
}).wait();
ilog("Logging thread started");
return logging_config;
}
void application::startup() {
std::lock_guard<std::mutex> guard( app_mtx );
if( is_interrupt_request() ) return;
ilog("Startup...");
for (const auto& plugin : initialized_plugins)
{
plugin->startup();
if( is_interrupt_request() )
break;
}
if( !is_interrupt_request() )
{
for( const auto& plugin : initialized_plugins )
{
plugin->finalize_startup();
if( is_interrupt_request() )
break;
}
}
notify_status("chain API ready");
}
void application::set_program_options()
{
std::stringstream data_dir_ss;
data_dir_ss << "Directory containing configuration file config.ini. Default location: $HOME/." << app_name << " or CWD/. " << app_name;
std::stringstream plugins_ss;
for( auto& p : default_plugins )
{
plugins_ss << p << ' ';
}
options_description_ex app_cfg_opts( "Application Config Options" );
options_description_ex app_cli_opts( "Application Command Line Options" );
app_cfg_opts.add_options()
("plugin", bpo::value< vector<string> >()->composing()->default_value(default_plugins, plugins_ss.str())->value_name("plugin-name"), "Plugin(s) to enable, may be specified multiple times");
app_cli_opts.add_options()
("help,h", "Print this help message and exit.")
("version,v", "Print version information.")
("dump-config", "Dump configuration and exit")
("list-plugins", "Print names of all available plugins and exit")
#if BOOST_VERSION >= 106800
("generate-completions", "Generate bash auto-complete script (try: eval \"$(hived --generate-completions)\")")
#endif
("data-dir,d", bpo::value<bfs::path>()->value_name("dir"), data_dir_ss.str().c_str() )
("config,c", bpo::value<bfs::path>()->default_value("config.ini")->value_name("filename"), "Configuration file name relative to data-dir");
my->_cfg_options.add(app_cfg_opts);
my->_app_options.add(app_cfg_opts);
my->_app_options.add(app_cli_opts);
set_plugin_options( &my->_app_options, &my->_cfg_options );
}
void application::set_plugin_options(
bpo::options_description* app_options,
bpo::options_description* cfg_options ) const
{
for(auto& plug : plugins) {
options_description_ex plugin_cli_opts("Command Line Options for " + plug.second->get_name());
options_description_ex plugin_cfg_opts("Config Options for " + plug.second->get_name());
plug.second->set_program_options(plugin_cli_opts, plugin_cfg_opts);
if(plugin_cli_opts.options().size())
app_options->add(plugin_cli_opts);
if(plugin_cfg_opts.options().size())
{
cfg_options->add(plugin_cfg_opts);
for(const boost::shared_ptr<bpo::option_description>& od : plugin_cfg_opts.options())
{
// If the config option is not already present as a cli option, add it.
if( plugin_cli_opts.find_nothrow( od->long_name(), false ) == nullptr )
{
app_options->add( od );
}
}
}
}
}
initialization_result application::initialize_impl( int argc, char** argv,
vector<abstract_plugin*> autostart_plugins, const bpo::variables_map& arg_overrides )
{
std::lock_guard<std::mutex> guard( app_mtx );
if( is_interrupt_request() ) return { initialization_result::ok, true };
try
{
// Due to limitations of boost::program_options::store function (used twice below),
// the overrides need to land first in variables_map container (_args), before actual
// args to be overridden are processed.
my->_args = arg_overrides;
set_program_options();
bpo::store( bpo::parse_command_line( argc, argv, my->_app_options ), my->_args );
if( my->_args.count( "help" ) ) {
cout << my->_app_options << "\n";
return { initialization_result::ok, false };
}
if( my->_args.count( "version" ) )
{
cout << version_info << "\n";
return { initialization_result::ok, false };
}
bfs::path data_dir;
if( my->_args.count("data-dir") )
{
data_dir = my->_args["data-dir"].as<bfs::path>();
if( data_dir.is_relative() )
data_dir = bfs::current_path() / data_dir;
}
else
{
#ifdef WIN32
char* parent = getenv( "APPDATA" );
#else
char* parent = getenv( "HOME" );
#endif
if( parent != nullptr )
{
data_dir = std::string( parent );
}
else
{
data_dir = bfs::current_path();
}
std::stringstream app_dir;
app_dir << '.' << app_name;
data_dir = data_dir / app_dir.str();
}
my->_data_dir = data_dir;
bfs::path config_file_name = data_dir / "config.ini";
if( my->_args.count( "config" ) ) {
config_file_name = my->_args["config"].as<bfs::path>();
if( config_file_name.is_relative() )
config_file_name = data_dir / config_file_name;
}
if(!bfs::exists(config_file_name)) {
write_default_config(config_file_name);
}
#if BOOST_VERSION >= 106800
if(my->_args.count("generate-completions") > 0)
{
generate_completions();
return { initialization_result::ok, false };
}
#endif
bpo::store(bpo::parse_config_file< char >( config_file_name.make_preferred().string().c_str(),
my->_cfg_options, true ), my->_args );
if(my->_args.count("dump-config") > 0)
{
std::cout << "{\n";
std::cout << "\t" << quoted("data-dir") << ": " << quoted(my->_data_dir.string().c_str()) << ",\n";
std::cout << "\t" << quoted("config") << ": " << quoted(config_file_name.string().c_str()) << "\n";
std::cout << "}\n";
return { initialization_result::ok, false };
}
if(my->_args.count("list-plugins") > 0)
{
for(const auto& plugin: plugins)
{
std::cout << plugin.first << "\n";
}
return { initialization_result::ok, false };
}
for (const auto& plugin : autostart_plugins)
if (plugin != nullptr && plugin->get_state() == abstract_plugin::registered)
plugin->initialize(my->_args);
if(my->_args.count("plugin") > 0)
{
auto plugins = my->_args.at("plugin").as<std::vector<std::string>>();
for(auto& arg : plugins)
{
vector<string> names;
boost::split(names, arg, boost::is_any_of(" \t,"));
for(const std::string& name : names)
get_plugin(name).initialize(my->_args);
}
}
bpo::notify(my->_args);
return { initialization_result::ok, true };
}
catch (const boost::program_options::validation_error& e)
{
std::cerr << "Error parsing command line: " << e.what() << "\n";
return { initialization_result::validation_error, false };
}
catch (const boost::program_options::unknown_option& e)
{
std::cerr << "Error parsing command line: " << e.what() << "\n";
return { initialization_result::unrecognised_option, false };
}
catch (const boost::program_options::error_with_option_name& e)
{
std::cerr << "Error parsing command line: " << e.what() << "\n";
return { initialization_result::error_with_option, false };
}
catch (const boost::program_options::error& e)
{
std::cerr << "Error parsing command line: " << e.what() << "\n";
return { initialization_result::unknown_command_line_error, false };
}
}
void application::pre_shutdown( std::string& actual_plugin_name )
{
for( auto& plugin : pre_shutdown_plugins )
{
actual_plugin_name = plugin->get_name();
plugin->pre_shutdown();
}
pre_shutdown_plugins.clear();
}
void application::shutdown( std::string& actual_plugin_name ) {
for(auto ritr = running_plugins.rbegin();
ritr != running_plugins.rend(); ++ritr) {
actual_plugin_name = (*ritr)->get_name();
(*ritr)->shutdown();
}
for(auto ritr = running_plugins.rbegin();
ritr != running_plugins.rend(); ++ritr) {
actual_plugin_name = (*ritr)->get_name();
plugins.erase( actual_plugin_name );
}
running_plugins.clear();
initialized_plugins.clear();
plugins.clear();
}
void application::finish()
{
std::lock_guard<std::mutex> guard( app_mtx );
std::string _actual_plugin_name;
auto plugin_exception_info = [&_actual_plugin_name]()
{
elog("Plugin ${plugin} raised an exception...", ("plugin", _actual_plugin_name));
};
if( !is_finished )
{
try
{
pre_shutdown( _actual_plugin_name );
shutdown( _actual_plugin_name );
if( my->_logging_thread )
{
my->_logging_thread->quit();
my->_logging_thread.reset();
}
is_finished = true;
}
catch( const boost::exception& e )
{
plugin_exception_info();
elog(boost::diagnostic_information(e));
}
catch( std::exception& e )
{
plugin_exception_info();
elog("exception: ${what}", ("what", e.what()));
}
catch(...)
{
plugin_exception_info();
try
{
std::rethrow_exception( std::current_exception() );
}
catch( const std::exception& e )
{
elog("exception: ${what}", ("what", e.what()));
}
}
}
}
void application::wait4interrupt_request()
{
const uint32_t _wait_interval = 200;
while( !is_interrupt_request() )
{
std::this_thread::sleep_for( std::chrono::milliseconds( _wait_interval ) );
}
}
void application::wait( bool log )
{
if( log ) ilog("Wait for interrupt request");
wait4interrupt_request();
if( log ) ilog("Interrupt request has been generated");
if( finish_request )
{
ilog("Attempting to request for application shutdown...");
finish_request();
ilog("Request for application shutdown processed.");
}
if( log ) ilog("Wait for finishing plugins");
finish();
if( log ) ilog("Plugins have been finished");
handler_wrapper->wait4stop( log );
}
bool application::is_thread_closed()
{
return handler_wrapper->is_thread_closed();
}
void application::write_default_config(const bfs::path& cfg_file)
{
if(!bfs::exists(cfg_file.parent_path()))
bfs::create_directories(cfg_file.parent_path());
std::ofstream out_cfg( bfs::path(cfg_file).make_preferred().string());
for(const boost::shared_ptr<bpo::option_description>& od : my->_cfg_options.options())
{
if(!od->description().empty())
{
std::string next_line;
std::stringstream ss( od->description() );
while( getline(ss, next_line, '\n') )
out_cfg << "# " << next_line << "\n";
}
boost::any store;
if(!od->semantic()->apply_default(store))
out_cfg << "# " << od->long_name() << " = \n";
else
{
auto example = od->format_parameter();
if( example.empty() )
{
// This is a boolean switch
out_cfg << "# " << od->long_name() << " = " << "false\n";
}
else if( example.length() <= 7 )
{
// The string is formatted "arg"
out_cfg << "# " << od->long_name() << " = \n";
}
else
{
// The string is formatted "arg (=<interesting part>)"
size_t space_pos = example.find(' ');
if (space_pos != std::string::npos &&
example.length() >= space_pos + 4)
{
example.erase(0, space_pos + 3);
example.erase(example.length()-1);
out_cfg << "# " << od->long_name() << " = " << example << "\n";
}
}
}
out_cfg << "\n";
}
out_cfg.close();
}
void application::generate_completions()
{
// option_description is missing long_names() before 1.68.0, and it's easier to just omit this
// feature for old versions of boost, instead of trying to support it
#if BOOST_VERSION >= 106800
std::vector<string> all_plugin_names;
for (const auto& plugin: plugins)
all_plugin_names.push_back(plugin.first);
// generate a string containing all options, separated by a space
std::vector<std::string> all_options;
std::vector<string> args_that_take_plugin_names;
std::vector<string> args_that_take_directories_names;
std::vector<string> args_that_have_no_completion;
for (const boost::shared_ptr<bpo::option_description>& od : my->_app_options.options())
{
std::vector<std::string> this_parameter_variations;
// option_description doesn't have a direct acessor for the short option, so
// call format_name() which will return a combined short + long "-h [ --help ]"
// string, then parse the short option out
std::string formatted = od->format_name();
if (formatted.length() > 2 &&
formatted[0] == '-' &&
formatted[1] != '-')
this_parameter_variations.push_back(formatted.substr(0, 2));
const std::string* long_name_strings;
size_t long_name_count;
std::tie(long_name_strings, long_name_count) = od->long_names();
for (unsigned i = 0; i < long_name_count; ++i)
this_parameter_variations.push_back(std::string("--" + long_name_strings[i]));
std::copy(this_parameter_variations.begin(), this_parameter_variations.end(), std::back_inserter(all_options));
// we don't have a direct way to get the value_name, so parse it from the
// format_parameter() output
std::string formatted_parameter = od->format_parameter();
size_t space_pos = formatted_parameter.find(' ');
if (space_pos != std::string::npos || !formatted_parameter.empty())
{
std::string value_name;
if (space_pos == std::string::npos)
value_name = formatted_parameter;
else
value_name = formatted_parameter.substr(0, space_pos);
if (value_name == "plugin-name")
std::copy(this_parameter_variations.begin(), this_parameter_variations.end(), std::back_inserter(args_that_take_plugin_names));
else if (value_name == "dir")
std::copy(this_parameter_variations.begin(), this_parameter_variations.end(), std::back_inserter(args_that_take_directories_names));
else
std::copy(this_parameter_variations.begin(), this_parameter_variations.end(), std::back_inserter(args_that_have_no_completion));
}
}
std::cout << "_hived()\n"
<< "{\n"
<< " local hived=$1 cur=$2 prev=$3 words=(\"${COMP_WORDS[@]}\")\n"
<< " case \"${prev}\" in\n";
if (!args_that_take_plugin_names.empty())
std::cout << " " << boost::algorithm::join(args_that_take_plugin_names, "|") << ")\n"
<< " COMPREPLY=( $(compgen -W \"" << boost::algorithm::join(all_plugin_names, " ") << "\" -- \"$cur\") )\n"
<< " return\n"
<< " ;;\n";
if (!args_that_take_directories_names.empty())
std::cout << " " << boost::algorithm::join(args_that_take_directories_names, "|") << ")\n"
<< " COMPREPLY=( $(compgen -A directory -- \"$cur\") )\n"
<< " return\n"
<< " ;;\n";
std::cout << " " << boost::algorithm::join(args_that_have_no_completion, "|") << ")\n"
<< " COMPREPLY=()\n"
<< " return\n"
<< " ;;\n"
<< " esac\n"
<< " COMPREPLY=( $(compgen -W \"" << boost::algorithm::join(all_options, " ") << "\" -- \"$cur\") )\n"
<< "}\n"
<< "complete -F _hived -o filenames hived\n";
#endif
}
abstract_plugin* application::find_plugin( const string& name )const
{
auto itr = plugins.find( name );
if( itr == plugins.end() )
{
return nullptr;
}
return itr->second.get();
}
abstract_plugin& application::get_plugin(const string& name)const
{
auto ptr = find_plugin(name);
if(!ptr)
{
notify_error("Unable to find plugin: " + name);
throw_plugin_not_found_exception(name, __FILE__, __LINE__, __func__);
}
return *ptr;
}
bfs::path application::data_dir()const
{
return my->_data_dir;
}
io_service_wrapper& application::get_io_service()
{
return handler_wrapper->get_io_service_wrapper();
}
void application::add_program_options( const options_description& cli, const options_description& cfg )
{
my->_app_options.add( cli );
my->_app_options.add( cfg );
my->_cfg_options.add( cfg );
}
void application::add_logging_program_options()
{
hive::utilities::options_description_ex options;
hive::utilities::set_logging_program_options( options );
hive::utilities::notifications::add_program_options(options);
add_program_options( hive::utilities::options_description_ex(), options );
}
const variables_map& application::get_args() const
{
return my->_args;
}
std::set< std::string > application::get_plugins_names() const
{
std::set< std::string > res;
for( auto& plugin : initialized_plugins )
res.insert( plugin->get_name() );
return res;
}
void application::notify_status(const fc::string& current_status) const noexcept
{
my->status.save_status(current_status);
my->broadcast(hive::utilities::notifications::notification_t("hived_status", "current_status", current_status));
}
void application::notify_fork(const uint32_t& block_num, const fc::string& block_id) const noexcept
{
my->status.save_fork(block_num, block_id);
my->broadcast(hive::utilities::notifications::notification_t("switching forks", "num", block_num, "id", block_id));
}
void application::notify_webserver(const fc::string& webserver_type, const fc::string& address, const uint16_t port) const noexcept
{
my->status.save_webserver(webserver_type, address, port);
my->broadcast(hive::utilities::notifications::notification_t( "webserver listening",
"type", webserver_type,
"address", address,
"port", port
));
}
void application::notify_error(const fc::string& error_message) const noexcept
{
my->status.save_status(error_message);
my->broadcast(hive::utilities::notifications::notification_t("error", "message", error_message));
}
void application::setup_notifications(const boost::program_options::variables_map &args) const
{
my->notification_handler.setup( hive::utilities::notifications::setup_notifications( args ) );
}
hive::utilities::statuses_signal_manager& application::get_status() { return my->status; }
const hive::utilities::statuses_signal_manager& application::get_status() const { return my->status; }
void application::notify_information(const fc::string& name, const fc::string& key, const fc::variant& value) const noexcept
{
try
{
my->broadcast(hive::utilities::notifications::notification_t( name, key, value ));
my->status.save_information( name, key, value );
}
catch( ... ) {}
}
void application::kill()
{
::kill(getpid(), SIGINT);
}
bool application::quit( bool log )
{
auto _is_thread_closed = is_thread_closed();
if( !_is_thread_closed )
{
if( log ) ilog("Kill application");
kill();
if( log ) ilog("Application has been killed");
}
wait( log );
return is_thread_closed();
}
} /// namespace appbase