zmqrpc.cpp raw

   1  // Copyright (c) 2018-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <zmq/zmqrpc.h>
   6  
   7  #include <rpc/server.h>
   8  #include <rpc/util.h>
   9  #include <univalue.h>
  10  #include <zmq/zmqabstractnotifier.h>
  11  #include <zmq/zmqnotificationinterface.h>
  12  
  13  #include <list>
  14  #include <memory>
  15  #include <string>
  16  #include <utility>
  17  #include <vector>
  18  
  19  class JSONRPCRequest;
  20  
  21  namespace {
  22  
  23  static RPCMethod getzmqnotifications()
  24  {
  25      return RPCMethod{
  26          "getzmqnotifications",
  27          "Returns information about the active ZeroMQ notifications.\n",
  28                  {},
  29                  RPCResult{
  30                      RPCResult::Type::ARR, "", "",
  31                      {
  32                          {RPCResult::Type::OBJ, "", "",
  33                          {
  34                              {RPCResult::Type::STR, "type", "Type of notification"},
  35                              {RPCResult::Type::STR, "address", "Address of the publisher"},
  36                              {RPCResult::Type::NUM, "hwm", "Outbound message high water mark"},
  37                          }},
  38                      }
  39                  },
  40                  RPCExamples{
  41                      HelpExampleCli("getzmqnotifications", "")
  42              + HelpExampleRpc("getzmqnotifications", "")
  43                  },
  44          [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
  45  {
  46      UniValue result(UniValue::VARR);
  47      if (g_zmq_notification_interface != nullptr) {
  48          for (const auto* n : g_zmq_notification_interface->GetActiveNotifiers()) {
  49              UniValue obj(UniValue::VOBJ);
  50              obj.pushKV("type", n->GetType());
  51              obj.pushKV("address", n->GetAddress());
  52              obj.pushKV("hwm", n->GetOutboundMessageHighWaterMark());
  53              result.push_back(std::move(obj));
  54          }
  55      }
  56  
  57      return result;
  58  },
  59      };
  60  }
  61  
  62  const CRPCCommand commands[]{
  63      {"zmq", &getzmqnotifications},
  64  };
  65  
  66  } // anonymous namespace
  67  
  68  void RegisterZMQRPCCommands(CRPCTable& t)
  69  {
  70      for (const auto& c : commands) {
  71          t.appendCommand(c.name, &c);
  72      }
  73  }
  74