-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_leaf.cpp
More file actions
114 lines (97 loc) · 3.49 KB
/
Copy pathauto_leaf.cpp
File metadata and controls
114 lines (97 loc) · 3.49 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
103
104
105
106
107
108
109
110
111
112
113
114
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <nlohmann/json.hpp>
#include "kinopio/kinopio.hpp"
#include "kinopio/leaf.hpp"
namespace {
std::vector<std::string> splitValues(const char* value) {
if (value == nullptr) {
return {};
}
std::vector<std::string> values;
std::stringstream stream(value);
std::string item;
while (std::getline(stream, item, ',')) {
if (!item.empty()) {
values.push_back(item);
}
}
return values;
}
const auto& payloadBase(const kinopio::Payload& payload) {
return static_cast<const kinopio::Payload::Base&>(payload);
}
std::string stateName(kinopio::leaf::AutoLeafState state) {
switch (state) {
case kinopio::leaf::AutoLeafState::discovering:
return "discovering";
case kinopio::leaf::AutoLeafState::followingLeader:
return "following-leader";
case kinopio::leaf::AutoLeafState::leaderMissingGrace:
return "leader-missing-grace";
case kinopio::leaf::AutoLeafState::electing:
return "electing";
case kinopio::leaf::AutoLeafState::startingLeaf:
return "starting-leaf";
case kinopio::leaf::AutoLeafState::leader:
return "leader";
case kinopio::leaf::AutoLeafState::stopped:
return "stopped";
}
return "stopped";
}
} // namespace
int main() {
kinopio::leaf::AutoLeafOptions options;
options.discoveryNamespace = std::getenv("KINOPIO_AUTO_LEAF_NAMESPACE") != nullptr
? std::getenv("KINOPIO_AUTO_LEAF_NAMESPACE")
: "demo";
options.advertiseHost = std::getenv("KINOPIO_AUTO_LEAF_ADVERTISE_HOST") != nullptr
? std::getenv("KINOPIO_AUTO_LEAF_ADVERTISE_HOST")
: "";
options.leafRuntime.upstreamLeafUrls = splitValues(std::getenv("KINOPIO_LEAF_UPSTREAM_URLS"));
if (const auto* discoveryPort = std::getenv("KINOPIO_AUTO_LEAF_DISCOVERY_PORT"); discoveryPort != nullptr) {
options.discoveryPort = static_cast<std::uint16_t>(std::stoi(discoveryPort));
}
auto handle = kinopio::leaf::startAutoLeaf(options);
kinopio::leaf::AutoLeafSnapshot snapshot;
for (int attempt = 0; attempt < 20; ++attempt) {
snapshot = handle.snapshot();
std::cout << "State: " << stateName(snapshot.state) << '\n';
std::cout << "Node ID: " << snapshot.nodeId << '\n';
if (snapshot.leaderNodeId.has_value()) {
std::cout << "Leader ID: " << *snapshot.leaderNodeId << '\n';
}
if (snapshot.leaderLeafUrl.has_value()) {
std::cout << "Leader leaf URL: " << *snapshot.leaderLeafUrl << '\n';
}
if (snapshot.localLeaf.has_value()) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds{250});
}
if (!snapshot.localLeaf.has_value()) {
handle.stop();
return snapshot.state == kinopio::leaf::AutoLeafState::followingLeader ? 0 : 1;
}
kinopio::KinopioOptions hubOptions;
hubOptions.servers = {snapshot.localLeaf->clientUrl};
kinopio::KinopioHub hub(hubOptions);
hub.connected(std::chrono::seconds{2});
auto variable = hub.getScope("demo").getVariable("auto-leaf");
bool received = false;
auto subscription = variable.sub([&received](const kinopio::Payload& payload, const kinopio::MessageMetadata&) {
const auto& json = std::get<nlohmann::json>(payloadBase(payload));
std::cout << "Received payload: " << json.dump() << '\n';
received = json.at("ok").get<bool>();
});
variable.pub(nlohmann::json{{"ok", true}});
std::this_thread::sleep_for(std::chrono::milliseconds{100});
handle.stop();
return received ? 0 : 1;
}