-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.cpp
More file actions
86 lines (70 loc) · 2.47 KB
/
Copy pathscope.cpp
File metadata and controls
86 lines (70 loc) · 2.47 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
#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));
// Method 1: Using getScope
auto userScope = hub.getScope("users");
auto onlineUsersVar = userScope.getVariable("online");
auto userCountVar = userScope.getVariable("count");
// Publish to user scope
onlineUsersVar.pub(nlohmann::json{"Alice", "Bob", "Charlie"});
std::cout << "Published online users\n";
userCountVar.pub(nlohmann::json{
{"total", 150},
{"online", 3},
{"registered_today", 5}
});
std::cout << "Published user count\n";
// Method 2: Direct scope.variable access
auto chatMessages = hub.getScope("chat").getVariable("messages");
chatMessages.pub(nlohmann::json{
{"room", "general"},
{"user", "Alice"},
{"message", "Hello everyone!"},
{"timestamp", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()}
});
std::cout << "Published chat message\n";
auto systemHealth = hub.getScope("system").getVariable("health");
systemHealth.pub(nlohmann::json{
{"cpu_usage", 45.2},
{"memory_usage", 68.1},
{"disk_usage", 23.8},
{"status", "healthy"},
{"last_check", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()}
});
std::cout << "Published system health\n";
auto systemLogs = hub.getScope("system").getVariable("logs");
systemLogs.pub(nlohmann::json{
{"level", "error"},
{"message", "Failed to connect to database"},
{"service", "user-service"},
{"timestamp", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()},
{"stack_trace", "Error: Connection timeout..."}
});
std::cout << "Published system log\n";
std::cout << "All data published successfully!\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
hub.dispose();
return 0;
}