-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathP2PCommand.cpp
More file actions
818 lines (691 loc) · 21.8 KB
/
Copy pathP2PCommand.cpp
File metadata and controls
818 lines (691 loc) · 21.8 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/**
*
* @file P2PCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/commands/P2PCommand.hpp>
#include <vix/cli/Style.hpp>
#include <vix/cli/util/Console.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/utils/Logger.hpp>
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include <vix/p2p/Bootstrap.hpp>
#include <vix/p2p/Node.hpp>
#include <vix/p2p/P2P.hpp>
#include <vix/p2p/Peer.hpp>
#if defined(_WIN32)
#include <io.h>
#define VIX_ISATTY _isatty
#define VIX_FILENO _fileno
#else
#include <unistd.h>
#define VIX_ISATTY isatty
#define VIX_FILENO fileno
#endif
namespace
{
namespace style = vix::cli::style;
namespace cli_console = vix::cli::util;
using Logger = vix::utils::Logger;
struct SharedLifecycle
{
std::atomic<bool> running{true};
std::atomic<bool> stopping{false};
};
static std::shared_ptr<SharedLifecycle> g_life;
static void on_sigint(int)
{
if (g_life)
g_life->running.store(false);
}
static bool is_tty_stdout()
{
return VIX_ISATTY(VIX_FILENO(stdout)) != 0;
}
static bool has_flag(const std::vector<std::string> &a, const std::string &k)
{
for (const auto &x : a)
if (x == k)
return true;
return false;
}
static std::optional<std::string> arg_value(const std::vector<std::string> &a, const std::string &k)
{
for (std::size_t i = 0; i + 1 < a.size(); ++i)
if (a[i] == k)
return a[i + 1];
const std::string prefix = k + "=";
for (const auto &x : a)
if (x.rfind(prefix, 0) == 0)
return x.substr(prefix.size());
return std::nullopt;
}
static std::string to_lower_copy(std::string s)
{
for (auto &c : s)
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
return s;
}
static std::optional<Logger::Level> parse_log_level(std::string raw)
{
const std::string s = to_lower_copy(std::move(raw));
if (s == "trace")
return Logger::Level::Trace;
if (s == "debug")
return Logger::Level::Debug;
if (s == "info")
return Logger::Level::Info;
if (s == "warn" || s == "warning")
return Logger::Level::Warn;
if (s == "error" || s == "err")
return Logger::Level::Error;
if (s == "critical" || s == "fatal")
return Logger::Level::Critical;
if (s == "off" || s == "none")
return Logger::Level::Off;
return std::nullopt;
}
static void apply_log_level_from_flag(Logger &logger, const std::string &value, bool quiet)
{
if (auto lvl = parse_log_level(value))
{
logger.setLevel(*lvl);
if (!quiet)
style::hint(std::string("log_level=") + value);
return;
}
style::error(std::string("invalid --log-level: ") + value);
style::hint("expected: trace, debug, info, warn, error, critical, off");
}
static std::optional<std::uint64_t> parse_u64(const std::string &s)
{
try
{
std::size_t idx = 0;
unsigned long long v = std::stoull(s, &idx, 10);
if (idx != s.size())
return std::nullopt;
return static_cast<std::uint64_t>(v);
}
catch (...)
{
return std::nullopt;
}
}
static std::optional<std::uint16_t> parse_u16(const std::string &s)
{
auto v = parse_u64(s);
if (!v || *v > 65535ULL)
return std::nullopt;
return static_cast<std::uint16_t>(*v);
}
static std::optional<vix::p2p::PeerEndpoint> parse_endpoint(const std::string &s)
{
std::string x = s;
constexpr const char *kPrefix = "tcp://";
if (x.rfind(kPrefix, 0) == 0)
x = x.substr(std::string(kPrefix).size());
const auto pos = x.rfind(':');
if (pos == std::string::npos || pos == 0 || pos + 1 >= x.size())
return std::nullopt;
vix::p2p::PeerEndpoint ep;
ep.host = x.substr(0, pos);
auto p = parse_u16(x.substr(pos + 1));
if (!p)
return std::nullopt;
ep.port = *p;
ep.scheme = "tcp";
return ep;
}
enum class ConnectReason
{
Manual,
Discovery,
Bootstrap
};
struct ConnectStats
{
std::uint64_t connect_attempts = 0;
std::uint64_t connect_deduped = 0;
std::uint64_t connect_failures = 0;
std::uint64_t backoff_skips = 0;
};
struct ConnectEntry
{
std::uint32_t failures = 0;
std::chrono::steady_clock::time_point backoff_until{};
std::chrono::steady_clock::time_point last_attempt{};
};
class ConnectGuard
{
public:
explicit ConnectGuard(std::shared_ptr<SharedLifecycle> life)
: life_(std::move(life))
{
}
static std::string key_for(const vix::p2p::PeerEndpoint &ep)
{
std::string scheme = ep.scheme.empty() ? "tcp" : to_lower_copy(ep.scheme);
std::ostringstream oss;
oss << scheme << "://" << ep.host << ":" << ep.port;
return oss.str();
}
bool allow_attempt(const vix::p2p::PeerEndpoint &ep, bool manual, bool quiet)
{
if (!life_ || life_->stopping.load())
return false;
const auto now = std::chrono::steady_clock::now();
const std::string key = key_for(ep);
std::lock_guard<std::mutex> lock(mu_);
auto &e = table_[key];
if (e.backoff_until.time_since_epoch().count() != 0 && now < e.backoff_until)
{
++stats_.backoff_skips;
return false;
}
constexpr auto kMinAttemptGap = std::chrono::milliseconds(900);
if (!manual && e.last_attempt.time_since_epoch().count() != 0 && (now - e.last_attempt) < kMinAttemptGap)
{
++stats_.connect_deduped;
return false;
}
e.last_attempt = now;
++stats_.connect_attempts;
(void)quiet;
return true;
}
void mark_failure(const vix::p2p::PeerEndpoint &ep, bool manual)
{
const auto now = std::chrono::steady_clock::now();
const std::string key = key_for(ep);
std::lock_guard<std::mutex> lock(mu_);
auto &e = table_[key];
++stats_.connect_failures;
if (manual)
{
e.failures = std::min<std::uint32_t>(e.failures + 1, 8u);
const std::uint64_t backoff_ms = std::min<std::uint64_t>(2500ULL * (1ULL << std::min<std::uint32_t>(e.failures - 1, 5u)), 12000ULL);
e.backoff_until = now + std::chrono::milliseconds(backoff_ms);
return;
}
e.failures = std::min<std::uint32_t>(e.failures + 1, 10u);
const std::uint64_t backoff_ms = std::min<std::uint64_t>(2000ULL * (1ULL << std::min<std::uint32_t>(e.failures - 1, 6u)), 15000ULL);
e.backoff_until = now + std::chrono::milliseconds(backoff_ms);
}
void mark_success(const vix::p2p::PeerEndpoint &ep)
{
const std::string key = key_for(ep);
std::lock_guard<std::mutex> lock(mu_);
auto it = table_.find(key);
if (it == table_.end())
return;
it->second.failures = 0;
it->second.backoff_until = {};
}
ConnectStats stats() const
{
std::lock_guard<std::mutex> lock(mu_);
return stats_;
}
std::size_t tracked_endpoints() const
{
std::lock_guard<std::mutex> lock(mu_);
return table_.size();
}
private:
std::shared_ptr<SharedLifecycle> life_;
mutable std::mutex mu_;
std::unordered_map<std::string, ConnectEntry> table_;
ConnectStats stats_{};
};
static std::string build_stats_plain(const vix::p2p::NodeStats &st, const ConnectStats &cs, std::size_t tracked)
{
std::ostringstream oss;
oss
<< "peers_total=" << st.peers_total
<< " peers_connected=" << st.peers_connected
<< " handshakes_started=" << st.handshakes_started
<< " handshakes_completed=" << st.handshakes_completed
<< " connect_attempts=" << cs.connect_attempts
<< " connect_deduped=" << cs.connect_deduped
<< " connect_failures=" << cs.connect_failures
<< " backoff_skips=" << cs.backoff_skips
<< " tracked_endpoints=" << tracked;
return oss.str();
}
static void print_stats(bool tui, const std::string &line)
{
if (tui)
{
std::cout << "\r" << style::GRAY << "[vix p2p] " << style::RESET << line << " " << std::flush;
}
else
{
std::cout << style::GRAY << "[vix p2p] " << style::RESET << line << "\n"
<< std::flush;
}
}
static void usage(std::ostream &out)
{
out
<< "vix p2p\n"
<< "Run a peer-to-peer node.\n\n"
<< "Usage\n"
<< " vix p2p --id <node_id> --listen <port> [options]\n\n"
<< "Examples\n"
<< " vix p2p --id A --listen 9001\n"
<< " vix p2p --id B --listen 9002 --connect 127.0.0.1:9001\n\n"
<< "What happens\n"
<< " • Starts a P2P node with TCP transport\n"
<< " • Optionally connects to peers\n"
<< " • Enables discovery and/or bootstrap if configured\n"
<< " • Streams live stats (TUI or logs)\n\n"
<< "Core\n"
<< " --id <node_id> Unique node identifier\n"
<< " --listen <port> TCP listen port\n"
<< " --connect <host:port> Connect to a peer on startup\n"
<< " --connect-delay <ms> Delay before connecting\n"
<< " --run <seconds> Auto-stop after N seconds\n"
<< " --stats-every <ms> Stats interval (default: 1000)\n"
<< " --tui <on|off> Live stats display (auto on TTY)\n"
<< " --quiet Print only final stats\n"
<< " --no-connect Disable auto connect\n\n"
<< "Discovery (UDP)\n"
<< " --discovery <on|off> Enable peer discovery (default: on)\n"
<< " --disc-port <port> Discovery port (default: 37020)\n"
<< " --disc-mode <mode> broadcast|multicast (default: broadcast)\n"
<< " --disc-interval <ms> Discovery interval (default: 2000)\n\n"
<< "Bootstrap (Registry)\n"
<< " --bootstrap <on|off> Enable registry bootstrap (default: off)\n"
<< " --registry <url> Registry endpoint\n"
<< " --boot-interval <sec> Refresh interval (default: 15)\n"
<< " --announce <on|off> Announce node to registry (default: on)\n\n"
<< "Logging\n"
<< " --log-level <level> trace|debug|info|warn|error|critical|off\n\n"
<< "Help\n"
<< " -h, --help\n";
}
static bool parse_on_off(const std::string &s, bool &out)
{
if (s == "on")
{
out = true;
return true;
}
if (s == "off")
{
out = false;
return true;
}
return false;
}
static bool do_connect(
const std::shared_ptr<SharedLifecycle> &life,
ConnectGuard &guard,
vix::p2p::P2PRuntime &p2p,
const vix::p2p::PeerEndpoint &ep,
bool manual,
bool quiet)
{
if (!life || life->stopping.load())
return false;
if (!guard.allow_attempt(ep, manual, quiet))
return false;
try
{
p2p.connect(ep);
guard.mark_success(ep);
return true;
}
catch (...)
{
guard.mark_failure(ep, manual);
return false;
}
}
} // namespace
namespace vix::commands::P2PCommand
{
int run(const std::vector<std::string> &argsIn)
{
g_life = std::make_shared<SharedLifecycle>();
std::signal(SIGINT, on_sigint);
std::ios::sync_with_stdio(false);
auto &logger = Logger::getInstance();
std::vector<std::string> args = argsIn;
if (args.empty() || has_flag(args, "--help") || has_flag(args, "-h"))
return help();
const bool quiet = has_flag(args, "--quiet");
const bool no_connect = has_flag(args, "--no-connect");
if (auto s = arg_value(args, "--log-level"))
apply_log_level_from_flag(logger, *s, quiet);
const auto id_opt = arg_value(args, "--id");
const auto listen_opt = arg_value(args, "--listen");
const auto connect_opt = arg_value(args, "--connect");
std::uint64_t connect_delay_ms = 0;
if (auto s = arg_value(args, "--connect-delay"))
{
auto v = parse_u64(*s);
if (!v)
{
style::error("invalid --connect-delay (ms)");
return 1;
}
connect_delay_ms = *v;
}
std::uint64_t run_seconds = 0;
if (auto s = arg_value(args, "--run"))
{
auto v = parse_u64(*s);
if (!v)
{
style::error("invalid --run (seconds)");
return 1;
}
run_seconds = *v;
}
std::uint64_t stats_every_ms = 1000;
if (auto s = arg_value(args, "--stats-every"))
{
auto v = parse_u64(*s);
if (!v || *v == 0)
{
style::error("invalid --stats-every (ms)");
return 1;
}
stats_every_ms = *v;
}
bool tui = is_tty_stdout();
if (auto s = arg_value(args, "--tui"))
{
if (!parse_on_off(*s, tui))
{
style::error("invalid --tui (expected on|off)");
return 1;
}
}
if (!id_opt || !listen_opt)
{
style::error("missing required options: --id and/or --listen");
style::hint("try: vix p2p --help");
return 1;
}
const auto listen_port_opt = parse_u16(*listen_opt);
if (!listen_port_opt)
{
style::error("invalid --listen port");
return 1;
}
bool discovery_on = true;
if (auto s = arg_value(args, "--discovery"))
{
if (!parse_on_off(*s, discovery_on))
{
style::error("invalid --discovery (expected on|off)");
return 1;
}
}
std::uint16_t disc_port = 37020;
if (auto s = arg_value(args, "--disc-port"))
{
auto v = parse_u16(*s);
if (!v)
{
style::error("invalid --disc-port");
return 1;
}
disc_port = *v;
}
vix::p2p::DiscoveryMode disc_mode = vix::p2p::DiscoveryMode::Broadcast;
if (auto s = arg_value(args, "--disc-mode"))
{
if (*s == "broadcast")
disc_mode = vix::p2p::DiscoveryMode::Broadcast;
else if (*s == "multicast")
disc_mode = vix::p2p::DiscoveryMode::Multicast;
else
{
style::error("invalid --disc-mode (expected broadcast|multicast)");
return 1;
}
}
std::uint32_t disc_interval_ms = 2000;
if (auto s = arg_value(args, "--disc-interval"))
{
auto v = parse_u64(*s);
if (!v || *v == 0 || *v > 0xFFFFFFFFULL)
{
style::error("invalid --disc-interval (ms)");
return 1;
}
disc_interval_ms = static_cast<std::uint32_t>(*v);
}
bool bootstrap_on = false;
if (auto s = arg_value(args, "--bootstrap"))
{
if (!parse_on_off(*s, bootstrap_on))
{
style::error("invalid --bootstrap (expected on|off)");
return 1;
}
}
std::string registry = "http://127.0.0.1:8080/p2p/v1";
if (auto s = arg_value(args, "--registry"))
registry = *s;
std::uint64_t boot_interval_sec = 15;
if (auto s = arg_value(args, "--boot-interval"))
{
auto v = parse_u64(*s);
if (!v || *v == 0)
{
style::error("invalid --boot-interval (seconds)");
return 1;
}
boot_interval_sec = *v;
}
bool announce_on = true;
if (auto s = arg_value(args, "--announce"))
{
if (!parse_on_off(*s, announce_on))
{
style::error("invalid --announce (expected on|off)");
return 1;
}
}
vix::p2p::NodeConfig cfg;
cfg.node_id = *id_opt;
cfg.listen_port = *listen_port_opt;
auto node = vix::p2p::make_tcp_node(cfg);
std::weak_ptr<vix::p2p::Node> wnode = node;
vix::p2p::P2PRuntime p2p(node);
auto life = g_life;
ConnectGuard guard(life);
if (!quiet)
{
style::info("P2P starting");
cli_console::status_line(false, "node", cfg.node_id);
cli_console::status_line(false, "listen", std::to_string(cfg.listen_port));
if (tui)
style::hint("tui=on (single-line stats)");
}
if (bootstrap_on)
{
vix::p2p::BootstrapConfig bc;
bc.self_node_id = cfg.node_id;
bc.self_tcp_port = cfg.listen_port;
bc.registry_url = registry;
bc.poll_interval_ms = static_cast<std::uint32_t>(boot_interval_sec * 1000ULL);
bc.mode = announce_on ? vix::p2p::BootstrapMode::PullAndAnnounce
: vix::p2p::BootstrapMode::PullOnly;
auto boot = vix::p2p::make_http_bootstrap(
bc,
[life, &guard, &p2p, wnode, no_connect](const vix::p2p::BootstrapPeer &p)
{
if (no_connect)
return;
if (!life || life->stopping.load())
return;
auto n = wnode.lock();
if (!n)
return;
vix::p2p::PeerEndpoint ep;
ep.host = p.host;
ep.port = p.tcp_port;
ep.scheme = "tcp";
(void)do_connect(life, guard, p2p, ep, /*manual=*/false, /*quiet=*/true);
});
node->set_bootstrap(boot);
if (!quiet)
{
style::hint(std::string("bootstrap=on registry=") + registry);
style::hint(std::string("announce=") + (announce_on ? "on" : "off"));
}
}
if (discovery_on)
{
vix::p2p::DiscoveryConfig dc;
dc.self_node_id = cfg.node_id;
dc.self_tcp_port = cfg.listen_port;
dc.discovery_port = disc_port;
dc.mode = disc_mode;
dc.announce_interval_ms = disc_interval_ms;
auto disc = vix::p2p::make_udp_discovery(
dc,
[life, &guard, &p2p, wnode, no_connect](const vix::p2p::DiscoveryAnnouncement &a)
{
if (no_connect)
return;
if (!life || life->stopping.load())
return;
auto n = wnode.lock();
if (!n)
return;
vix::p2p::PeerEndpoint ep;
ep.host = a.host;
ep.port = a.port;
ep.scheme = "tcp";
(void)do_connect(life, guard, p2p, ep, /*manual=*/false, /*quiet=*/true);
});
node->set_discovery(disc);
if (!quiet)
{
style::hint(std::string("discovery=on disc_port=") + std::to_string(disc_port));
style::hint(std::string("disc_mode=") + (disc_mode == vix::p2p::DiscoveryMode::Broadcast ? "broadcast" : "multicast"));
}
}
p2p.start();
if (connect_opt)
{
auto ep = parse_endpoint(*connect_opt);
if (!ep)
{
style::error("invalid --connect (expected host:port or tcp://host:port)");
life->stopping.store(true);
life->running.store(false);
p2p.stop();
return 1;
}
if (!quiet)
style::hint(std::string("connect=") + ep->host + ":" + std::to_string(ep->port));
if (connect_delay_ms > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(connect_delay_ms));
(void)do_connect(life, guard, p2p, *ep, /*manual=*/true, quiet);
}
std::thread stopper;
if (run_seconds > 0)
{
stopper = std::thread([life, run_seconds]()
{
std::this_thread::sleep_for(std::chrono::seconds(run_seconds));
if (life)
life->running.store(false); });
}
const auto tick = std::chrono::milliseconds(stats_every_ms);
vix::p2p::NodeStats last{};
std::chrono::steady_clock::time_point last_print{};
while (life->running.load())
{
const auto now = std::chrono::steady_clock::now();
const auto st = p2p.stats();
const auto cs = guard.stats();
const auto tracked = guard.tracked_endpoints();
const bool changed =
(st.peers_total != last.peers_total) ||
(st.peers_connected != last.peers_connected) ||
(st.handshakes_started != last.handshakes_started) ||
(st.handshakes_completed != last.handshakes_completed);
const bool time_ok =
(last_print.time_since_epoch().count() == 0) ||
(now - last_print) >= tick;
if (!quiet && (changed || time_ok))
{
print_stats(tui, build_stats_plain(st, cs, tracked));
last = st;
last_print = now;
}
std::this_thread::sleep_for(std::chrono::milliseconds(80));
}
if (stopper.joinable())
stopper.join();
if (!quiet)
{
if (tui)
std::cout << "\n"
<< std::flush;
style::hint("stopping...");
}
life->stopping.store(true);
node->set_discovery(nullptr);
node->set_bootstrap(nullptr);
p2p.stop();
const auto final_st = p2p.stats();
const auto final_cs = guard.stats();
if (!quiet)
{
style::blank();
vix::cli::util::section(std::cout, "Final");
vix::cli::util::kv(std::cout, "peers_total", std::to_string(final_st.peers_total), 22);
vix::cli::util::kv(std::cout, "peers_connected", std::to_string(final_st.peers_connected), 22);
vix::cli::util::kv(std::cout, "handshakes_started", std::to_string(final_st.handshakes_started), 22);
vix::cli::util::kv(std::cout, "handshakes_completed", std::to_string(final_st.handshakes_completed), 22);
vix::cli::util::kv(std::cout, "connect_attempts", std::to_string(final_cs.connect_attempts), 22);
vix::cli::util::kv(std::cout, "connect_deduped", std::to_string(final_cs.connect_deduped), 22);
vix::cli::util::kv(std::cout, "connect_failures", std::to_string(final_cs.connect_failures), 22);
vix::cli::util::kv(std::cout, "backoff_skips", std::to_string(final_cs.backoff_skips), 22);
vix::cli::util::kv(std::cout, "tracked_endpoints", std::to_string(guard.tracked_endpoints()), 22);
style::success("bye");
}
else
{
std::cout << build_stats_plain(final_st, final_cs, guard.tracked_endpoints()) << "\n"
<< std::flush;
}
return 0;
}
int help()
{
usage(std::cout);
return 0;
}
} // namespace vix::commands::P2PCommand