8 #include <system_error>
11 #include <sys/select.h>
13 #include "classpath.hpp"
14 #include "configuration.hpp"
15 #include "detail/event_handle.hpp"
16 #include "detail/subprocess.hpp"
21 static void validate_settings(
const configuration& settings)
23 if (settings.is_minimal())
27 else if (!settings.source_file())
29 throw std::invalid_argument(
"Configuration has not been saved to a file");
35 _shutdown_event(std::make_unique<detail::event_handle>())
37 validate_settings(settings);
38 _worker = std::thread([
this, packages = std::move(packages), settings = std::move(settings)] ()
40 this->run_process(packages, settings);
49 server::~server() noexcept
54 void server::shutdown(
bool wait_for_stop)
56 _running.store(
false, std::memory_order_release);
57 _shutdown_event->notify_one();
59 if (wait_for_stop && _worker.joinable())
63 static void wait_for_event(
int fd1,
int fd2,
int fd3)
68 FD_SET(fd1, &read_fds);
69 FD_SET(fd2, &read_fds);
70 FD_SET(fd3, &read_fds);
72 int nfds = std::max(std::max(fd1, fd2), fd3) + 1;
73 int rc = ::select(nfds, &read_fds,
nullptr,
nullptr,
nullptr);
79 throw std::system_error(errno, std::system_category(),
"select");
83 void server::run_process(
const classpath& packages,
const configuration& settings)
85 detail::subprocess::argument_list args = {
"-cp", packages.command_line(),
86 "org.apache.zookeeper.server.quorum.QuorumPeerMain",
88 if (settings.is_minimal())
90 args.emplace_back(std::to_string(settings.client_port()));
91 args.emplace_back(settings.data_directory().value());
95 args.emplace_back(settings.source_file().value());
98 detail::subprocess proc(
"java", std::move(args));
100 auto drain_pipes = [&] ()
102 bool read_anything =
true;
103 while (read_anything)
105 read_anything =
false;
107 auto out = proc.stdout().read();
110 read_anything =
true;
114 auto err = proc.stderr().read();
117 read_anything =
true;
123 while (_running.load(std::memory_order_acquire))
125 wait_for_event(proc.stdout().native_read_handle(),
126 proc.stderr().native_read_handle(),
127 _shutdown_event->native_handle()
Represents a collection of JARs or other Java entities that should be provided as the --classpath to ...
Controls a ZooKeeper server process on this local machine.
Controls the import of future and promise types.
void shutdown(bool wait_for_stop=false)
Initiate shutting down the server process.
Represents a configuration which should be run by server instance.