-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_reply.cpp
More file actions
104 lines (84 loc) · 2.73 KB
/
Copy pathrequest_reply.cpp
File metadata and controls
104 lines (84 loc) · 2.73 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
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <nlohmann/json.hpp>
#include "kinopio/kinopio.hpp"
namespace {
std::string resolveServer() {
if (const char* value = std::getenv("KINOPIO_NATS_URL")) {
return value;
}
return "nats://demo.nats.io:4222";
}
} // namespace
int main() {
kinopio::KinopioOptions options;
options.servers = {resolveServer()};
kinopio::KinopioHub hub(options);
hub.connected(std::chrono::seconds(5));
auto calculator = hub.getScope("math").getVariable("calculator");
auto service = calculator.serve([](const kinopio::Payload& request, const kinopio::MessageMetadata&) -> kinopio::Payload {
const auto& json = std::get<nlohmann::json>(request);
std::string operation = json.value("operation", "");
int a = json.value("a", 0);
int b = json.value("b", 0);
nlohmann::json result{
{"operation", operation},
{"inputs", {{"a", a}, {"b", b}}},
{"timestamp", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()}
};
if (operation == "add") {
result["result"] = a + b;
} else if (operation == "subtract") {
result["result"] = a - b;
} else if (operation == "multiply") {
result["result"] = a * b;
} else if (operation == "divide") {
if (b != 0) {
result["result"] = a / b;
} else {
result["result"] = "Error: Division by zero";
}
} else {
throw std::runtime_error("Unknown operation: " + operation);
}
return result;
});
std::cout << "Calculator service is running\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
auto response1 = calculator.req(nlohmann::json{
{"operation", "add"},
{"a", 10},
{"b", 5}
});
std::cout << "Addition result: " << std::get<nlohmann::json>(response1).dump() << '\n';
auto response2 = calculator.req(nlohmann::json{
{"operation", "multiply"},
{"a", 7},
{"b", 3}
});
std::cout << "Multiplication result: " << std::get<nlohmann::json>(response2).dump() << '\n';
auto response3 = calculator.req(nlohmann::json{
{"operation", "divide"},
{"a", 20},
{"b", 4}
});
std::cout << "Division result: " << std::get<nlohmann::json>(response3).dump() << '\n';
try {
auto error_response = calculator.req(nlohmann::json{
{"operation", "invalid"},
{"a", 1},
{"b", 2}
});
} catch (const std::exception& e) {
std::cout << "Expected error: " << e.what() << '\n';
}
std::cout << "All requests completed!\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
service.unsubscribe();
hub.dispose();
return 0;
}