CatapultServer  v0.5.0.1 (Elephant)
HandlerFactory.h
Go to the documentation of this file.
1 
21 #pragma once
26 
27 namespace catapult { namespace handlers {
28 
30  template<typename TRequestTraits>
32  public:
34  static constexpr auto Packet_Type = TRequestTraits::Packet_Type;
35 
36  public:
39  template<typename TResultsProducerFactory>
40  static void RegisterZero(ionet::ServerPacketHandlers& handlers, TResultsProducerFactory resultsProducerFactory) {
41  auto maxPacketDataSize = handlers.maxPacketDataSize();
42  handlers.registerHandler(Packet_Type, [resultsProducerFactory, maxPacketDataSize](const auto& packet, auto& context) {
43  if (!IsPacketValid(packet, Packet_Type))
44  return;
45 
46  // always send a response (even if empty) in order to always acknowledge the request
47  SetResponse(context, maxPacketDataSize, resultsProducerFactory());
48  });
49  }
50 
53  template<typename TResultsProducerFactory>
54  static void RegisterOne(ionet::ServerPacketHandlers& handlers, TResultsProducerFactory resultsProducerFactory) {
55  auto maxPacketDataSize = handlers.maxPacketDataSize();
56  handlers.registerHandler(Packet_Type, [resultsProducerFactory, maxPacketDataSize](const auto& packet, auto& context) {
58  if (!info.IsValid)
59  return;
60 
61  // always send a response (even if empty) in order to always acknowledge the request
62  SetResponse(context, maxPacketDataSize, resultsProducerFactory(info.Range));
63  });
64  }
65 
66  private:
67  template<typename TProducer>
68  static void SetResponse(ionet::ServerPacketHandlerContext& context, uint32_t maxPacketDataSize, TProducer&& producer) {
69  auto builder = ionet::PacketPayloadBuilder(Packet_Type, maxPacketDataSize);
70  Append(AppendAccessor<TRequestTraits>(), builder, producer);
71  context.response(builder.build());
72  }
73 
74  private:
75  enum class AppendType { Entities, Values };
76  using EntitiesAppendFlag = std::integral_constant<AppendType, AppendType::Entities>;
77  using ValuesAppendFlag = std::integral_constant<AppendType, AppendType::Values>;
78 
79  template<typename T, typename = void>
81 
82  template<typename T>
83  struct AppendAccessor<T, utils::traits::is_type_expression_t<decltype(T::Should_Append_As_Values)>> : ValuesAppendFlag {};
84 
85  private:
86  template<typename TProducer>
87  static void Append(EntitiesAppendFlag, ionet::PacketPayloadBuilder& builder, TProducer& producer) {
88  builder.appendGeneratedEntities(producer);
89  }
90 
91  template<typename TProducer>
92  static void Append(ValuesAppendFlag, ionet::PacketPayloadBuilder& builder, TProducer& producer) {
93  builder.appendGeneratedValues(producer);
94  }
95 
96  private:
97  template<typename TRange>
98  struct PacketInfo {
99  public:
100  explicit PacketInfo(TRange&& range)
101  : Range(std::move(range))
102  , IsValid(!Range.empty())
103  {}
104 
105  public:
106  TRange Range;
107  bool IsValid;
108  };
109 
110  private:
111  static auto ProcessRequest(const ionet::Packet& packet) {
112  using RequestStructureType = typename TRequestTraits::RequestStructureType;
113  using RequestRangeType = model::EntityRange<RequestStructureType>;
114 
115  return TRequestTraits::Packet_Type != packet.Type
116  ? PacketInfo<RequestRangeType>(RequestRangeType())
117  : PacketInfo<RequestRangeType>(ionet::ExtractFixedSizeStructuresFromPacket<RequestStructureType>(packet));
118  }
119  };
120 }}
catapult::ionet::PacketPayloadBuilder::appendGeneratedValues
bool appendGeneratedValues(TValueGenerator &&generator)
Definition: PacketPayloadBuilder.h:125
catapult::handlers::BatchHandlerFactory::PacketInfo
Definition: HandlerFactory.h:98
catapult::handlers::BatchHandlerFactory::Append
static void Append(ValuesAppendFlag, ionet::PacketPayloadBuilder &builder, TProducer &producer)
Definition: HandlerFactory.h:92
catapult::handlers::BatchHandlerFactory::Packet_Type
static constexpr auto Packet_Type
Packet type supported by the created handler.
Definition: HandlerFactory.h:34
PacketEntityUtils.h
catapult::ionet::Packet
A packet header with a data payload.
Definition: Packet.h:32
catapult::handlers::BatchHandlerFactory::PacketInfo::PacketInfo
PacketInfo(TRange &&range)
Definition: HandlerFactory.h:100
catapult::handlers::BatchHandlerFactory::AppendAccessor
Definition: HandlerFactory.h:80
catapult::handlers::BatchHandlerFactory::AppendType::Entities
catapult::handlers::BatchHandlerFactory::ValuesAppendFlag
std::integral_constant< AppendType, AppendType::Values > ValuesAppendFlag
Definition: HandlerFactory.h:77
catapult::model::EntityRange
Represents a range of entities.
Definition: EntityRange.h:31
catapult::handlers::BatchHandlerFactory::AppendType::Values
catapult::ionet::PacketHeader::Type
PacketType Type
Type of the packet.
Definition: PacketHeader.h:35
catapult::handlers::BatchHandlerFactory::EntitiesAppendFlag
std::integral_constant< AppendType, AppendType::Entities > EntitiesAppendFlag
Definition: HandlerFactory.h:76
catapult::ionet::ServerPacketHandlers::registerHandler
void registerHandler(PacketType type, const PacketHandler &handler)
Registers a handler for the specified packet type.
Definition: PacketHandlers.cpp:100
catapult::handlers::BatchHandlerFactory::PacketInfo::IsValid
bool IsValid
Definition: HandlerFactory.h:107
catapult::handlers::BatchHandlerFactory::RegisterZero
static void RegisterZero(ionet::ServerPacketHandlers &handlers, TResultsProducerFactory resultsProducerFactory)
Definition: HandlerFactory.h:40
PacketHandlers.h
catapult::ionet::PacketPayloadBuilder
A packet payload builder for creating payloads composed of heterogeneous data.
Definition: PacketPayloadBuilder.h:29
catapult::ionet::ServerPacketHandlerContext::response
const PacketPayload & response() const
Gets the response associated with this context.
Definition: PacketHandlers.cpp:46
Packet_Type
static constexpr auto Packet_Type
Definition: RemoteNodeApi.cpp:33
PacketPayloadBuilder.h
catapult::handlers::BatchHandlerFactory::ProcessRequest
static auto ProcessRequest(const ionet::Packet &packet)
Definition: HandlerFactory.h:111
catapult::handlers::BatchHandlerFactory::SetResponse
static void SetResponse(ionet::ServerPacketHandlerContext &context, uint32_t maxPacketDataSize, TProducer &&producer)
Definition: HandlerFactory.h:68
catapult::ionet::PacketPayloadBuilder::appendGeneratedEntities
bool appendGeneratedEntities(TEntityGenerator &&generator)
Appends all entities produced by generator to the payload.
Definition: PacketPayloadBuilder.h:67
forwardsValidation.info
def info(*args)
Definition: forwardsValidation.py:12
catapult::utils::traits::is_type_expression_t
typename is_type_expression< T, Enable >::type is_type_expression_t
true if the expression is valid and evaluates to a type, false otherwise.
Definition: Traits.h:98
EntityRange.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::handlers::BatchHandlerFactory
Factory for creating a handler that returns batch of entities.
Definition: HandlerFactory.h:31
catapult::handlers::BatchHandlerFactory::RegisterOne
static void RegisterOne(ionet::ServerPacketHandlers &handlers, TResultsProducerFactory resultsProducerFactory)
Definition: HandlerFactory.h:54
catapult::ionet::ServerPacketHandlers::maxPacketDataSize
uint32_t maxPacketDataSize() const
Gets the max packet data size.
Definition: PacketHandlers.cpp:76
catapult::ionet::IsPacketValid
constexpr bool IsPacketValid(const Packet &packet, PacketType type)
Checks if packet is valid with type.
Definition: Packet.h:75
catapult::handlers::BatchHandlerFactory::Append
static void Append(EntitiesAppendFlag, ionet::PacketPayloadBuilder &builder, TProducer &producer)
Definition: HandlerFactory.h:87
catapult::ionet::ServerPacketHandlers
A collection of packet handlers where there is at most one handler per packet type.
Definition: PacketHandlers.h:62
catapult::handlers::BatchHandlerFactory::PacketInfo::Range
TRange Range
Definition: HandlerFactory.h:106
catapult::handlers::BatchHandlerFactory::AppendType
AppendType
Definition: HandlerFactory.h:75
catapult::ionet::ServerPacketHandlerContext
Context passed to a server packet handler function.
Definition: PacketHandlers.h:32