zookeeper-cpp
ZooKeeper Client for C++
server.cpp
1 #include "detail/subprocess.hpp"
2 #include "package_registry.hpp"
3 #include "server.hpp"
4 
5 #include <iostream>
6 
7 #include <signal.h>
8 
9 namespace zk::server
10 {
11 
12 server::server(std::string classpath) :
13  _running(true),
14  _worker([this, classpath = std::move(classpath)] () mutable { this->run_process(std::move(classpath)); })
15 { }
16 
17 server::~server() noexcept
18 {
19  shutdown(true);
20 }
21 
22 std::shared_ptr<server> server::create(package_registry& registry)
23 {
24  // TODO: Handle the nullopt case
25  return std::make_shared<server>(registry.find_newest_classpath().value());
26 }
27 
28 void server::shutdown(bool wait_for_stop)
29 {
30  _running = false;
31  if (wait_for_stop)
32  _worker.join();
33 }
34 
35 void server::run_process(std::string classpath)
36 {
37  detail::subprocess proc("java",
38  {
39  "-cp", std::move(classpath),
40  "org.apache.zookeeper.server.quorum.QuorumPeerMain",
41  "2181",
42  "zk-data"
43  }
44  );
45 
46  while (_running)
47  {
48  std::cout << proc.stdout().read();
49  std::cerr << proc.stderr().read();
50  }
51  proc.signal(SIGTERM);
52 }
53 
54 }
optional< std::string > find_newest_classpath() const
Get the classpath for running the newest registered server version.
STL namespace.
Controls a ZooKeeper server process on this local machine.
Definition: server.hpp:20
static std::shared_ptr< server > create(package_registry &registry)
Create a running server process with the best (newest) version from the provided registry.
Definition: server.cpp:22
The package registry tracks configuration of classpaths and JARs needed to run various ZooKeeper vers...