-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.cpp
More file actions
152 lines (122 loc) · 4.94 KB
/
example1.cpp
File metadata and controls
152 lines (122 loc) · 4.94 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stop_token>
#include <iostream>
#include <thread>
#include "nplex-cpp/client.hpp"
#include "logger.hpp"
static std::string to_iso8601(std::chrono::milliseconds ms_since_epoch)
{
using namespace std::chrono;
auto secs = duration_cast<seconds>(ms_since_epoch);
auto millis = duration_cast<milliseconds>(ms_since_epoch - secs).count();
std::ostringstream oss;
std::time_t t = secs.count();
std::tm tm_utc;
gmtime_r(&t, &tm_utc);
oss << std::put_time(&tm_utc, "%Y-%m-%dT%H:%M:%S");
oss << '.' << std::setw(3) << std::setfill('0') << millis << 'Z';
return oss.str();
}
static void print_database_content(const nplex::client_ptr &cli)
{
size_t counter = 0;
auto tx = cli->create_tx();
std::cout << std::endl;
std::cout << "Nplex database content at rev " << tx->rev() << std::endl;
std::cout << "-------------------------------------------" << std::endl;
tx->for_each([&counter](const nplex::key_t &key, const nplex::value_t &value) {
std::cout << ++counter << ". ";
std::cout << key << " = " << value.data() << std::endl;
std::cout << " rev: " << value.rev() << std::endl;
std::cout << " timestamp: " << to_iso8601(value.timestamp()) << std::endl;
std::cout << " user: " << value.user() << std::endl;
std::cout << " tx_type: " << value.tx_type() << std::endl;
return true;
});
//tx->discard();
}
static void measure_latency(const nplex::client_ptr &cli)
{
auto ping = cli->ping("Hello Nplex!");
std::cout << "Ping response received in " << ping.get().count() << " microseconds" << std::endl;
}
static const char *to_str(nplex::session_t::code_e code)
{
switch (code)
{
case nplex::session_t::code_e::CONNECTED: return "CONNECTED";
case nplex::session_t::code_e::CLOSED_BY_SERVER: return "CLOSED_BY_SERVER";
case nplex::session_t::code_e::CLOSED_BY_USER: return "CLOSED_BY_USER";
case nplex::session_t::code_e::COMM_ERROR: return "COMM_ERROR";
case nplex::session_t::code_e::CON_LOST: return "CON_LOST";
case nplex::session_t::code_e::EXCD_LIMITS: return "EXCD_LIMITS";
default: return "UNKNOWN";
}
}
static void print_active_sessions(const nplex::client_ptr &cli)
{
auto future = cli->fetch_sessions(false);
auto sessions = future.get();
std::cout << std::endl;
std::cout << "List of active sessions" << std::endl;
std::cout << "-----------------------" << std::endl;
std::size_t index = 0;
for (const auto &session : sessions)
{
std::cout << ++index << ". ";
std::cout << "user: " << session.user << std::endl;
std::cout << " address: " << session.address << std::endl;
std::cout << " code: " << to_str(session.code) << std::endl;
std::cout << " since: " << to_iso8601(session.since) << std::endl;
std::cout << " until: " << (session.until.count() ? to_iso8601(session.until) : std::string{"-"}) << std::endl;
}
}
static void update_content(const nplex::client_ptr &cli)
{
auto tx = cli->create_tx(nplex::transaction::isolation_e::SERIALIZABLE, false);
int num = tx->read_or("rct.gates.3.open", "0")->as_number_or<int>(42);
num++;
tx->upsert("rct.gates.3.open", std::to_string(num)); // text content
tx->upsert("rct.gates.3.num", {reinterpret_cast<char*>(&num), sizeof(num)}); // binary content
tx->set_type(42);
try {
auto submit_result = tx->submit().get();
if (submit_result == nplex::transaction::submit_e::COMMITTED)
std::cout << "Transaction committed successfully" << std::endl;
else
std::cout << "Transaction rejected: " << static_cast<int>(submit_result) << std::endl;
} catch (const std::exception &e) {
std::cerr << "Transaction failed: " << e.what() << std::endl;
}
}
int main()
{
try {
// Configure connection parameters
nplex::params_t params = {
.servers = "localhost:14022, localhost:8081",
.user = "admin",
.password = "s3cr3t"
};
// Create client instance
auto cli = nplex::client::create(params);
// Attach logger
auto log = std::make_shared<logger>(nplex::logger::log_level_e::TRACE);
cli->set_logger(log);
// Start the nplex event loop in a dedicated thread
std::jthread worker([cli](std::stop_token st) {
cli->run(std::move(st));
});
cli->wait_for_synced();
print_active_sessions(cli);
print_database_content(cli);
measure_latency(cli);
update_content(cli);
// Close the database, alternatively you can call `cli->close();`
//worker.request_stop();
return EXIT_SUCCESS;
}
catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << '\n';
return EXIT_FAILURE;
}
}