8a60149568d36832909a8241f920c91f91bdcf8b
[awesomized/libmemcached] / testing / lib / Cluster.cpp
1 #include "Cluster.hpp"
2
3 #include <sys/wait.h>
4
5 void Cluster::reset() {
6 pids.clear();
7 cluster.clear();
8 for (int i = 0; i < count; ++i) {
9 cluster.push_back(proto);
10 }
11 }
12
13 bool Cluster::start() {
14 bool started = true;
15
16 for (auto &server : cluster) {
17 auto pid = server.start();
18 if (started &= pid.has_value()) {
19 pids[*pid] = &server;
20 }
21 }
22
23 return started;
24 }
25
26 void Cluster::stop() {
27 for (auto &server : cluster) {
28 server.stop();
29 }
30 }
31
32 bool Cluster::isStopped() {
33 for (auto &server : cluster) {
34 if (server.getPid() && !server.tryWait()) {
35 return false;
36 }
37 }
38 return true;
39 }
40
41 bool Cluster::isListening(int max_timeout) {
42 vector<WaitForConn::conn_t> conns;
43
44 for (auto &server : cluster) {
45 auto conn = server.createSocket();
46 if (!conn) {
47 return false;
48 }
49 conns.emplace_back(conn.value());
50 }
51
52 WaitForConn wait_for_conn{
53 std::move(conns),
54 Poll{POLLOUT, 2, max_timeout}
55 };
56 return wait_for_conn();
57 }
58
59 void Cluster::wait() {
60 siginfo_t inf;
61
62 while (!isStopped()) {
63 if (waitid(P_ALL, 0, &inf, WEXITED | WNOWAIT)) {
64 perror("Cluster::wait waitid()");
65 return;
66 }
67
68 if (pids[inf.si_pid]) {
69 pids[inf.si_pid]->wait();
70 }
71 }
72 }