CatapultServer  v0.5.0.1 (Elephant)
RemoteApiForwarder.h
Go to the documentation of this file.
1 
21 #pragma once
25 #include "catapult/thread/Future.h"
29 
30 namespace catapult { namespace chain {
31 
34  public:
38  net::PacketIoPicker& packetIoPicker,
39  const model::TransactionRegistry& transactionRegistry,
40  const utils::TimeSpan& timeout,
41  const std::string& operationName)
42  : m_packetIoPicker(packetIoPicker)
43  , m_transactionRegistry(transactionRegistry)
44  , m_timeout(timeout)
45  , m_operationName(operationName)
46  {}
47 
48  public:
50  template<typename TRemoteApiAction, typename TRemoteApiFactory>
51  thread::future<ionet::NodeInteractionResult> processSync(TRemoteApiAction action, TRemoteApiFactory apiFactory) const {
52  auto packetIoPair = m_packetIoPicker.pickOne(m_timeout);
53  if (!packetIoPair) {
54  CATAPULT_LOG_THROTTLE(warning, 60'000) << "no packet io available for operation '" << m_operationName << "'";
55  return thread::make_ready_future(ionet::NodeInteractionResult());
56  }
57 
58  // pass in a non-owning pointer to the registry
59  auto pRemoteApiUnique = apiFactory(*packetIoPair.io(), packetIoPair.node().identityKey(), m_transactionRegistry);
60  auto pRemoteApi = utils::UniqueToShared(std::move(pRemoteApiUnique));
61 
62  // extend the lifetimes of pRemoteApi and packetIoPair until the completion of the action
63  // (pRemoteApi is a pointer so that the reference taken by action is valid throughout the entire asynchronous action)
64  return action(*pRemoteApi).then([pRemoteApi, packetIoPair, operationName = m_operationName](auto&& resultFuture) {
65  auto result = resultFuture.get();
66  CATAPULT_LOG_LEVEL(ionet::NodeInteractionResultCode::Neutral == result ? utils::LogLevel::Trace : utils::LogLevel::Info)
67  << "completed '" << operationName << "' (" << packetIoPair.node() << ") with result " << result;
68  return ionet::NodeInteractionResult(packetIoPair.node().identityKey(), result);
69  });
70  }
71 
72  private:
73  net::PacketIoPicker& m_packetIoPicker;
74  const model::TransactionRegistry& m_transactionRegistry;
75  utils::TimeSpan m_timeout;
76  std::string m_operationName;
77  };
78 }}
NodeInteractionResult.h
MemoryUtils.h
catapult::chain::RemoteApiForwarder::m_transactionRegistry
const model::TransactionRegistry & m_transactionRegistry
Definition: RemoteApiForwarder.h:74
colorPrint.warning
def warning(*args)
Definition: colorPrint.py:10
catapult::utils::TimeSpan
Represents a time duration.
Definition: TimeSpan.h:30
PacketIoPicker.h
catapult::chain::RemoteApiForwarder
Simplifies interacting with remote nodes via apis.
Definition: RemoteApiForwarder.h:33
catapult::thread::future
Provides a way to access the result of an asynchronous operation.
Definition: Future.h:29
TransactionPlugin.h
catapult::chain::RemoteApiForwarder::m_timeout
utils::TimeSpan m_timeout
Definition: RemoteApiForwarder.h:75
catapult::action
std::function< void()> action
An action function.
Definition: functions.h:27
catapult::net::PacketIoPicker
An interface for picking packet io pairs.
Definition: PacketIoPicker.h:30
ThrottleLogger.h
catapult::net::PacketIoPicker::pickOne
virtual ionet::NodePacketIoPair pickOne(const utils::TimeSpan &ioDuration)=0
CATAPULT_LOG_THROTTLE
#define CATAPULT_LOG_THROTTLE(LEVEL, THROTTLE_MILLIS)
Definition: ThrottleLogger.h:76
catapult::chain::RemoteApiForwarder::m_operationName
std::string m_operationName
Definition: RemoteApiForwarder.h:76
TimeSpan.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::model::TransactionRegistry
A registry of transaction plugins.
Definition: TransactionPlugin.h:98
catapult::chain::RemoteApiForwarder::processSync
thread::future< ionet::NodeInteractionResult > processSync(TRemoteApiAction action, TRemoteApiFactory apiFactory) const
Picks a random peer and wraps an api around it using apiFactory. Finally, passes the api to action.
Definition: RemoteApiForwarder.h:51
Future.h
catapult::chain::RemoteApiForwarder::RemoteApiForwarder
RemoteApiForwarder(net::PacketIoPicker &packetIoPicker, const model::TransactionRegistry &transactionRegistry, const utils::TimeSpan &timeout, const std::string &operationName)
Definition: RemoteApiForwarder.h:37
catapult::chain::RemoteApiForwarder::m_packetIoPicker
net::PacketIoPicker & m_packetIoPicker
Definition: RemoteApiForwarder.h:73