-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
102 lines (85 loc) · 3.71 KB
/
Copy pathmain.cpp
File metadata and controls
102 lines (85 loc) · 3.71 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
#include "Logging/DefaultLogger.h"
#include "Relay/JsonRpcRelay.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <atomic>
#include <boost/program_options.hpp>
#include <cstdlib>
#include <thread>
#include <vector>
std::atomic<bool> g_ShutdownProgram{false};
void interrupt_handler(int)
{
if (!g_ShutdownProgram) {
LogWrite("Signal sent to stop application. Setting stop flag.", b_sev::info);
g_ShutdownProgram.store(true);
}
}
int main(int argc, char* argv[])
{
g_ShutdownProgram.store(false);
signal(SIGINT, interrupt_handler);
namespace params = boost::program_options;
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
LoggerSingleton::get().add_stream(console_sink, b_sev::info);
params::options_description desc("Program options");
// clang-format off
desc.add_options()("help", "produce help message")
("bind_address", params::value<std::string>(), "Server bind address (e.g., 127.0.0.1 or 0.0.0.0)")
("bind_port", params::value<uint16_t>(),"Server bind port")
("target_address", params::value<std::string>(),"Target address to send requests to that pass")
("target_port", params::value<uint16_t>(),"Target port to send requests that pass")
("filter_kind", params::value<std::string>(),"Filter kind to be used; default is jsonrpc filter")
("filter_options", params::value<std::string>(),"Filter definitions based on the filter you choose (for jsonrpc, it's a comma separated list of allowed methods)")
("threads", params::value<uint32_t>(),"Number of threads to use in the application");
// clang-format on
params::variables_map vm;
params::store(params::parse_command_line(argc, argv, desc), vm);
params::notify(vm);
if (vm.count("help")) {
std::cout << "Basic Command Line Parameter App" << std::endl << desc << std::endl;
return EXIT_SUCCESS;
}
std::string server_bind_address;
uint16_t server_bind_port;
std::string target_bind_address;
uint16_t target_bind_port;
uint32_t thread_count;
std::string filter_options;
try {
server_bind_address = vm["bind_address"].as<std::string>();
server_bind_port = vm["bind_port"].as<uint16_t>();
target_bind_address = vm["target_address"].as<std::string>();
target_bind_port = vm["target_port"].as<uint16_t>();
thread_count = std::thread::hardware_concurrency();
if (vm.find("threads") != vm.cend()) {
thread_count = vm["threads"].as<uint32_t>();
}
if (vm.find("filter_kind") != vm.cend()) {
// currently there's only jsonrpc filter, so this is no-op
}
if (vm.find("filter_options") == vm.cend()) {
throw std::runtime_error("The argument filter_options should be specified");
}
filter_options = vm["filter_options"].as<std::string>();
} catch (std::bad_cast& ex) {
std::cerr << std::endl
<< "Please include all required options. Use the command line `--help` to see them. "
<< std::endl
<< std::endl;
return EXIT_FAILURE;
}
/////////// start the server
JsonRPCFilter filter;
filter.applyOptions(filter_options);
JsonRpcRelay relay(std::move(filter),
server_bind_address,
server_bind_port,
target_bind_address,
target_bind_port,
thread_count);
while (!g_ShutdownProgram.load(std::memory_order_relaxed)) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
relay.stop();
return EXIT_SUCCESS;
}