diff --git a/Boss/Mod/ListpeersAnnouncer.cpp b/Boss/Mod/ListpeersAnnouncer.cpp new file mode 100644 index 0000000..8c59581 --- /dev/null +++ b/Boss/Mod/ListpeersAnnouncer.cpp @@ -0,0 +1,38 @@ +#include"Boss/Mod/ListpeersAnnouncer.hpp" +#include"Boss/Mod/Rpc.hpp" +#include"Boss/Msg/Init.hpp" +#include"Boss/Msg/ListpeersResult.hpp" +#include"Boss/Msg/Timer10Minutes.hpp" +#include"Ev/Io.hpp" +#include"Jsmn/Object.hpp" +#include"Json/Out.hpp" +#include"S/Bus.hpp" +#include + +namespace Boss { namespace Mod { + +void ListpeersAnnouncer::start() { + auto do_listpeers = [this](bool initial) { + assert(rpc); + return rpc->command("listpeers" + , Json::Out().start_object().end_object() + ).then([ this + , initial + ](Jsmn::Object result) { + return bus.raise(Msg::ListpeersResult{ + std::move(result), initial + }); + }); + }; + bus.subscribe([this, do_listpeers](Msg::Init const& init) { + rpc = &init.rpc; + return do_listpeers(true); + }); + bus.subscribe([this, do_listpeers](Msg::Timer10Minutes const& _) { + return do_listpeers(false); + }); +} + +}} diff --git a/Boss/Mod/ListpeersAnnouncer.hpp b/Boss/Mod/ListpeersAnnouncer.hpp new file mode 100644 index 0000000..aea0f5c --- /dev/null +++ b/Boss/Mod/ListpeersAnnouncer.hpp @@ -0,0 +1,32 @@ +#ifndef BOSS_MOD_LISTPEERSANNOUNCER_HPP +#define BOSS_MOD_LISTPEERSANNOUNCER_HPP + +namespace Boss { namespace Mod { class Rpc; }} +namespace S { class Bus; } + +namespace Boss { namespace Mod { + +/** class Boss::Mod::ListpeersAnnouncer + * + * @brief announces `listpeers` at `init` and + * every 10 minutes thereafter. + */ +class ListpeersAnnouncer { +private: + S::Bus& bus; + Boss::Mod::Rpc *rpc; + + void start(); + +public: + ListpeersAnnouncer() =delete; + ListpeersAnnouncer(ListpeersAnnouncer const&) =delete; + ListpeersAnnouncer( S::Bus& bus_ + ) : bus(bus_) + , rpc(nullptr) + { start(); } +}; + +}} + +#endif /* BOSS_MOD_LISTPEERSANNOUNCER_HPP */ diff --git a/Boss/Mod/all.cpp b/Boss/Mod/all.cpp index 7cb59b6..7130b8a 100644 --- a/Boss/Mod/all.cpp +++ b/Boss/Mod/all.cpp @@ -6,6 +6,7 @@ #include"Boss/Mod/InitialConnect.hpp" #include"Boss/Mod/Initiator.hpp" #include"Boss/Mod/JsonOutputter.hpp" +#include"Boss/Mod/ListpeersAnnouncer.hpp" #include"Boss/Mod/Manifester.hpp" #include"Boss/Mod/NeedsConnectSolicitor.hpp" #include"Boss/Mod/Timers.hpp" @@ -74,6 +75,7 @@ std::shared_ptr all( std::ostream& cout all->install(bus); all->install(bus); all->install(bus); + all->install(bus); all->install(bus); diff --git a/Boss/Msg/ListpeersResult.hpp b/Boss/Msg/ListpeersResult.hpp new file mode 100644 index 0000000..855ad17 --- /dev/null +++ b/Boss/Msg/ListpeersResult.hpp @@ -0,0 +1,24 @@ +#ifndef BOSS_MSG_LISTPEERSRESULT_HPP +#define BOSS_MSG_LISTPEERSRESULT_HPP + +#include"Jsmn/Object.hpp" + +namespace Boss { namespace Msg { + +/** struct Boss::Msg::ListpeersResult + * + * @brief announced during `init` to inform all + * modules about `listpeers` command result. + * Also announced every 10 minutes. + */ +struct ListpeersResult { + Jsmn::Object result; + /* Whether this listpeers was performed during `init` + * or on the 10-minute timer. + */ + bool initial; +}; + +}} + +#endif /* !defined(BOSS_MSG_LISTPEERSRESULT_HPP) */ diff --git a/Makefile.am b/Makefile.am index 1fb6a7a..e6e283e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,8 @@ libclboss_la_SOURCES = \ Boss/Mod/Initiator.hpp \ Boss/Mod/JsonOutputter.cpp \ Boss/Mod/JsonOutputter.hpp \ + Boss/Mod/ListpeersAnnouncer.cpp \ + Boss/Mod/ListpeersAnnouncer.hpp \ Boss/Mod/Manifester.cpp \ Boss/Mod/Manifester.hpp \ Boss/Mod/NeedsConnectSolicitor.cpp \ @@ -46,6 +48,7 @@ libclboss_la_SOURCES = \ Boss/Msg/Init.hpp \ Boss/Msg/JsonCin.hpp \ Boss/Msg/JsonCout.hpp \ + Boss/Msg/ListpeersResult.hpp \ Boss/Msg/ManifestCommand.hpp \ Boss/Msg/ManifestHook.hpp \ Boss/Msg/ManifestNotification.hpp \