memgraph/tests/distributed/raft/example_server.cpp
Matej Ferencevic fc20ddcd25 RPC refactor
Summary:
Start removal of old logic
Remove more obsolete classes
Move Message class to RPC
Remove client logic from system
Remove messaging namespace
Move protocol from messaging to rpc
Move System from messaging to rpc
Remove unnecessary namespace
Remove System from RPC Client
Split Client and Server into separate files
Start implementing new client logic
First semi-working state
Changed network protocol layout
Rewrite client
Fix client receive bug
Cleanup code of debug lines
Migrate to accessors
Migrate back to binary boost archives
Remove debug logging from server
Disable timeout test
Reduce message_id from uint64_t to uint32_t
Add multiple workers to server
Fix compiler warnings
Apply clang-format

Reviewers: teon.banek, florijan, dgleich, buda, mtomic

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1129
2018-01-24 15:27:40 +01:00

76 lines
2.4 KiB
C++

#include <fstream>
#include <thread>
#include <fmt/format.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "communication/rpc/server.hpp"
#include "messages.hpp"
#include "utils/signals/handler.hpp"
#include "utils/terminate_handler.hpp"
using namespace communication::rpc;
using namespace std::literals::chrono_literals;
DEFINE_string(interface, "127.0.0.1",
"Communication interface on which to listen.");
DEFINE_string(port, "10000", "Communication port on which to listen.");
DEFINE_string(log, "log.txt", "Entries log file");
volatile sig_atomic_t is_shutting_down = 0;
int main(int argc, char **argv) {
google::SetUsageMessage("Raft RPC Server");
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
// Unhandled exception handler init.
std::set_terminate(&terminate_handler);
System server_system(
io::network::Endpoint(FLAGS_interface, stoul(FLAGS_port)));
Server server(server_system, "main");
std::ofstream log(FLAGS_log, std::ios_base::app);
// Handler for regular termination signals.
auto shutdown = [&log]() {
if (is_shutting_down) return;
is_shutting_down = 1;
log.close();
exit(0);
};
// Prevent handling shutdown inside a shutdown. For example, SIGINT handler
// being interrupted by SIGTERM before is_shutting_down is set, thus causing
// double shutdown.
sigset_t block_shutdown_signals;
sigemptyset(&block_shutdown_signals);
sigaddset(&block_shutdown_signals, SIGTERM);
sigaddset(&block_shutdown_signals, SIGINT);
CHECK(SignalHandler::RegisterHandler(Signal::Terminate, shutdown,
block_shutdown_signals))
<< "Unable to register SIGTERM handler!";
CHECK(SignalHandler::RegisterHandler(Signal::Interupt, shutdown,
block_shutdown_signals))
<< "Unable to register SIGINT handler!";
// Example callback.
server.Register<AppendEntry>([&log](const AppendEntryReq &request) {
log << request.val << std::endl;
log.flush();
LOG(INFO) << fmt::format("AppendEntry: {}", request.val);
return std::make_unique<AppendEntryRes>(200, FLAGS_interface,
stol(FLAGS_port));
});
LOG(INFO) << "Raft RPC server started";
// Sleep until shutdown detected.
std::this_thread::sleep_until(
std::chrono::time_point<std::chrono::system_clock>::max());
return 0;
}