daemon & daemonize overhaul
This commit continues the complete replacement of the spaghetti code
mess that was inside daemon/ and daemonize/ which started in #1138, and
looked like a entry level Java programmer threw up inside the code base.
This greatly simplifies it, removing a whole pile of useless abstraction
layers that don't actually abstract anything, and results in
considerably simpler code. (Many of the changes here were also carried
out in #1138; this commit updates them with the merged result which
amends some things from that PR and goes further in some places).
In detail:
- the `--detach` (and related `--pidfile`) options are gone. (--detach
is still handled, but now just prints a fatal error). Detaching a
process is an archaic unix mechanism that has no place on a modern
system. If you *really* want to do it anyway, `nohup lokid &` will do
the job. (The Windows service control code, which is probably seldom
used, is kept because it seems potentially useful for Windows users).
- Many of the `t_whatever` classes in daemon/* are just deleted (mostly
done in #1138); each one was a bunch of junk code that wraps 3-4 lines
but forces an extra layer (not even a generic abstraction, just a
useless wrapper) for no good reason and made the daemon code painfully
hard to understand and work with.
- All of the remaining `t_whatever` classes in daemon/* are either
renamed to `whatever` (because prefixing every class with `t_` is
moronic).
- Some stupid related code (e.g. epee's command handler returning an
unsuitable "usage" string that has to be string modified into what we
want) was replaced with more generic, useful code.
- Replaced boost mutexes/cvs with std ones in epee command handler, and
deleted some commented out code.
- The `--public-node` option handling was terrible: it was being handled
in main, but main doesn't know anything about options, so then it
forced it through the spaghetti objects *beside* the pack of all
options that got passed along. Moved it to a more sane location
(core_rpc_server) and parse it out with some sanity.
- Changed a bunch of std::bind's to lambdas because, at least for small
lambdas (i.e. with only one-or-two pointers for captures) they will
generally be more efficient as the values can be stored in
std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
|
|
|
// Copyright (c) 2018-2020, The Loki Project
|
2019-03-05 22:05:34 +01:00
|
|
|
// Copyright (c) 2014-2019, The Monero Project
|
2023-04-13 15:50:13 +02:00
|
|
|
//
|
2015-01-29 23:10:53 +01:00
|
|
|
// All rights reserved.
|
2023-04-13 15:50:13 +02:00
|
|
|
//
|
2015-01-29 23:10:53 +01:00
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
2023-04-13 15:50:13 +02:00
|
|
|
//
|
2015-01-29 23:10:53 +01:00
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
2023-04-13 15:50:13 +02:00
|
|
|
//
|
2015-01-29 23:10:53 +01:00
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
2023-04-13 15:50:13 +02:00
|
|
|
//
|
2015-01-29 23:10:53 +01:00
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
2023-04-13 15:50:13 +02:00
|
|
|
//
|
2015-01-29 23:10:53 +01:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-06-02 00:30:19 +02:00
|
|
|
#include <optional>
|
2023-04-13 15:50:13 +02:00
|
|
|
|
2017-02-05 23:48:03 +01:00
|
|
|
#include "common/common_fwd.h"
|
2015-01-29 23:10:53 +01:00
|
|
|
#include "daemon/command_parser_executor.h"
|
2023-04-13 15:50:13 +02:00
|
|
|
#include "epee/console_handler.h"
|
2015-01-29 23:10:53 +01:00
|
|
|
|
|
|
|
namespace daemonize {
|
|
|
|
|
daemon & daemonize overhaul
This commit continues the complete replacement of the spaghetti code
mess that was inside daemon/ and daemonize/ which started in #1138, and
looked like a entry level Java programmer threw up inside the code base.
This greatly simplifies it, removing a whole pile of useless abstraction
layers that don't actually abstract anything, and results in
considerably simpler code. (Many of the changes here were also carried
out in #1138; this commit updates them with the merged result which
amends some things from that PR and goes further in some places).
In detail:
- the `--detach` (and related `--pidfile`) options are gone. (--detach
is still handled, but now just prints a fatal error). Detaching a
process is an archaic unix mechanism that has no place on a modern
system. If you *really* want to do it anyway, `nohup lokid &` will do
the job. (The Windows service control code, which is probably seldom
used, is kept because it seems potentially useful for Windows users).
- Many of the `t_whatever` classes in daemon/* are just deleted (mostly
done in #1138); each one was a bunch of junk code that wraps 3-4 lines
but forces an extra layer (not even a generic abstraction, just a
useless wrapper) for no good reason and made the daemon code painfully
hard to understand and work with.
- All of the remaining `t_whatever` classes in daemon/* are either
renamed to `whatever` (because prefixing every class with `t_` is
moronic).
- Some stupid related code (e.g. epee's command handler returning an
unsuitable "usage" string that has to be string modified into what we
want) was replaced with more generic, useful code.
- Replaced boost mutexes/cvs with std ones in epee command handler, and
deleted some commented out code.
- The `--public-node` option handling was terrible: it was being handled
in main, but main doesn't know anything about options, so then it
forced it through the spaghetti objects *beside* the pack of all
options that got passed along. Moved it to a more sane location
(core_rpc_server) and parse it out with some sanity.
- Changed a bunch of std::bind's to lambdas because, at least for small
lambdas (i.e. with only one-or-two pointers for captures) they will
generally be more efficient as the values can be stored in
std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
|
|
|
class command_server {
|
2023-04-13 15:50:13 +02:00
|
|
|
private:
|
|
|
|
command_parser_executor m_parser;
|
|
|
|
epee::console_handlers_binder m_command_lookup;
|
|
|
|
std::optional<oxenmq::OxenMQ> m_omq;
|
2015-01-29 23:10:53 +01:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
public:
|
|
|
|
/// command_server constructor; forwards to command_parser_executor
|
|
|
|
template <typename... T>
|
|
|
|
command_server(T&&... args) : m_parser{std::forward<T>(args)...} {
|
|
|
|
init_commands();
|
|
|
|
}
|
daemon & daemonize overhaul
This commit continues the complete replacement of the spaghetti code
mess that was inside daemon/ and daemonize/ which started in #1138, and
looked like a entry level Java programmer threw up inside the code base.
This greatly simplifies it, removing a whole pile of useless abstraction
layers that don't actually abstract anything, and results in
considerably simpler code. (Many of the changes here were also carried
out in #1138; this commit updates them with the merged result which
amends some things from that PR and goes further in some places).
In detail:
- the `--detach` (and related `--pidfile`) options are gone. (--detach
is still handled, but now just prints a fatal error). Detaching a
process is an archaic unix mechanism that has no place on a modern
system. If you *really* want to do it anyway, `nohup lokid &` will do
the job. (The Windows service control code, which is probably seldom
used, is kept because it seems potentially useful for Windows users).
- Many of the `t_whatever` classes in daemon/* are just deleted (mostly
done in #1138); each one was a bunch of junk code that wraps 3-4 lines
but forces an extra layer (not even a generic abstraction, just a
useless wrapper) for no good reason and made the daemon code painfully
hard to understand and work with.
- All of the remaining `t_whatever` classes in daemon/* are either
renamed to `whatever` (because prefixing every class with `t_` is
moronic).
- Some stupid related code (e.g. epee's command handler returning an
unsuitable "usage" string that has to be string modified into what we
want) was replaced with more generic, useful code.
- Replaced boost mutexes/cvs with std ones in epee command handler, and
deleted some commented out code.
- The `--public-node` option handling was terrible: it was being handled
in main, but main doesn't know anything about options, so then it
forced it through the spaghetti objects *beside* the pack of all
options that got passed along. Moved it to a more sane location
(core_rpc_server) and parse it out with some sanity.
- Changed a bunch of std::bind's to lambdas because, at least for small
lambdas (i.e. with only one-or-two pointers for captures) they will
generally be more efficient as the values can be stored in
std::function's without any memory allocations.
2020-04-02 22:38:52 +02:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
template <typename... T>
|
|
|
|
bool process_command_and_log(T&&... args) {
|
|
|
|
return m_command_lookup.process_command_and_log(std::forward<T>(args)...);
|
|
|
|
}
|
2015-01-29 23:10:53 +01:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
bool start_handling(std::function<void()> exit_handler = {});
|
2015-03-27 13:01:30 +01:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
void stop_handling();
|
2015-03-27 13:01:30 +01:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
private:
|
|
|
|
void init_commands(cryptonote::rpc::core_rpc_server* rpc_server = nullptr);
|
|
|
|
bool help(const std::vector<std::string>& args);
|
2015-01-29 23:10:53 +01:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
std::string get_commands_str();
|
|
|
|
std::string get_command_usage(const std::vector<std::string>& args);
|
2015-01-29 23:10:53 +01:00
|
|
|
};
|
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
} // namespace daemonize
|