---
title: llarp/rpc/lokid_rpc_client.hpp

---

# llarp/rpc/lokid_rpc_client.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[llarp::rpc](Namespaces/namespacellarp_1_1rpc.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::rpc::LokidRpcClient](Classes/structllarp_1_1rpc_1_1LokidRpcClient.md)** <br>The [LokidRpcClient]() uses loki-mq to talk to make API requests to lokid.  |




## Source code

```cpp
#pragma once

#include <llarp/router_id.hpp>

#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <llarp/crypto/types.hpp>
#include <llarp/dht/key.hpp>
#include <llarp/service/name.hpp>

namespace llarp
{
  struct AbstractRouter;

  namespace rpc
  {
    using LMQ_ptr = std::shared_ptr<oxenmq::OxenMQ>;

    struct LokidRpcClient : public std::enable_shared_from_this<LokidRpcClient>
    {
      explicit LokidRpcClient(LMQ_ptr lmq, std::weak_ptr<AbstractRouter> r);

      void
      ConnectAsync(oxenmq::address url);

      SecretKey
      ObtainIdentityKey();

      uint64_t
      BlockHeight() const
      {
        return m_BlockHeight;
      }

      void
      LookupLNSNameHash(
          dht::Key_t namehash,
          std::function<void(std::optional<service::EncryptedName>)> resultHandler);

      void
      InformConnection(RouterID router, bool success);

     private:
      void
      Connected();

      void
      Command(std::string_view cmd);

      void
      UpdateServiceNodeList();

      template <typename HandlerFunc_t, typename Args_t>
      void
      Request(std::string_view cmd, HandlerFunc_t func, const Args_t& args)
      {
        m_lokiMQ->request(*m_Connection, std::move(cmd), std::move(func), args);
      }

      template <typename HandlerFunc_t>
      void
      Request(std::string_view cmd, HandlerFunc_t func)
      {
        m_lokiMQ->request(*m_Connection, std::move(cmd), std::move(func));
      }

      // Handles a service node list update; takes the "service_node_states" object of an oxend
      // "get_service_nodes" rpc request.
      void
      HandleNewServiceNodeList(const nlohmann::json& json);

      // Handles request from lokid for peer stats on a specific peer
      void
      HandleGetPeerStats(oxenmq::Message& msg);

      // Handles notification of a new block
      void
      HandleNewBlock(oxenmq::Message& msg);

      std::optional<oxenmq::ConnectionID> m_Connection;
      LMQ_ptr m_lokiMQ;

      std::weak_ptr<AbstractRouter> m_Router;
      std::atomic<bool> m_UpdatingList;
      std::string m_LastUpdateHash;

      std::unordered_map<RouterID, PubKey> m_KeyMap;

      uint64_t m_BlockHeight;
    };

  }  // namespace rpc
}  // namespace llarp
```


-------------------------------

Updated on 2022-11-08 at 16:19:49 +0000
