clboss/Boss/Mod/ListpeersAnalyzer.cpp
Ken Sedgwick 0bd255ce1b Convert ListpeerResult to use ConstructedListpeers
Construct a "compatibility struct" to convert `listpeerchannels`
output into legacy `listpeers` format.

Tests written using the legacy listpeers format can use the
`convert_legacy_listpeers` utility to construct a compatibility
struct.

The test_peerjudge_datagatherer malformed test needed to be malformed
differently to achieve the desired effect.
2024-06-02 16:19:56 -07:00

62 lines
1.4 KiB
C++

#include"Boss/Mod/ListpeersAnalyzer.hpp"
#include"Boss/Msg/ListpeersAnalyzedResult.hpp"
#include"Boss/Msg/ListpeersResult.hpp"
#include"Ev/Io.hpp"
#include"Jsmn/Object.hpp"
#include"Json/Out.hpp"
#include"Ln/NodeId.hpp"
#include"S/Bus.hpp"
namespace Boss { namespace Mod {
ListpeersAnalyzer::ListpeersAnalyzer(S::Bus& bus) {
bus.subscribe< Msg::ListpeersResult
>([&bus](Msg::ListpeersResult const& l) {
auto ar = Msg::ListpeersAnalyzedResult();
ar.initial = l.initial;
for (auto peer : l.cpeers) {
auto id = peer.first;
auto connected = peer.second.connected;
auto has_chan = bool(false);
auto chans = peer.second.channels;
for (auto chan : chans) {
if (!chan.is_object() || !chan.has("state"))
continue;
auto state_j = chan["state"];
if (!state_j.is_string())
continue;
auto state = std::string(state_j);
auto prefix = std::string( state.begin()
, state.begin() + 8
);
if ( prefix == "OPENINGD"
|| prefix == "CHANNELD"
) {
has_chan = true;
break;
}
}
if (connected) {
if (has_chan)
ar.connected_channeled.emplace(id);
else
ar.connected_unchanneled.emplace(id);
} else {
if (has_chan)
ar.disconnected_channeled.emplace(id);
else
ar.disconnected_unchanneled.emplace(id);
}
}
return bus.raise(std::move(ar));
});
}
}}