MOTION  0.01
Framework for mixed-protocol multi-party computation
communication_layer.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020 Lennart Braun
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <atomic>
26 #include <cstddef>
27 #include <functional>
28 #include <memory>
29 #include <vector>
30 
31 // Undefine Windows macros that collide with function names in MOTION.
32 #ifdef SendMessage
33 #undef SendMessage
34 #endif
35 
36 #ifdef GetMessage
37 #undef GetMessage
38 #endif
39 
41 #include "transport.h"
42 
43 namespace encrypto::motion {
44 
45 class Logger;
46 
47 } // namespace encrypto::motion
48 
50 
51 class MessageHandler;
52 struct TransportStatistics;
53 
54 // Central interface for all communication related functionality
55 //
56 // Allows to send messages to other parties and to register handlers for
57 // specific message types.
59  public:
60  CommunicationLayer(std::size_t my_id, std::vector<std::unique_ptr<Transport>>&& transports);
61  CommunicationLayer(std::size_t my_id, std::vector<std::unique_ptr<Transport>>&& transports,
62  std::shared_ptr<Logger> logger);
64 
65  std::size_t GetNumberOfParties() const { return number_of_parties_; }
66  std::size_t GetMyId() const { return my_id_; }
67 
68  // Start communication
69  void Start();
70  void Synchronize();
71 
72  // Send a message to a specified party
73  void SendMessage(std::size_t party_id, std::vector<std::uint8_t>&& message);
74  void SendMessage(std::size_t party_id, const std::vector<std::uint8_t>& message);
75  void SendMessage(std::size_t party_id, std::shared_ptr<const std::vector<std::uint8_t>> message);
76  void SendMessage(std::size_t party_id, flatbuffers::FlatBufferBuilder&& message_builder);
77 
78  // Send a message to all other parties
79  void BroadcastMessage(std::vector<std::uint8_t>&& message);
80  void BroadcastMessage(const std::vector<std::uint8_t>& message);
81  void BroadcastMessage(std::shared_ptr<const std::vector<std::uint8_t>> message);
82  void BroadcastMessage(flatbuffers::FlatBufferBuilder&& message_builder);
83 
84  // Factory function for creating message handlers
86  std::function<std::shared_ptr<MessageHandler>(std::size_t party_id)>;
87  // Register message handlers for given types
89  const std::vector<MessageType>& message_types);
90  // Deregister any message handler registered for the given types
91  void DeregisterMessageHandler(const std::vector<MessageType>& message_types);
92  // Return the message handler registered for the given type.
93  // Throws if no handler was registered for this type.
94  MessageHandler& GetMessageHandler(std::size_t party_id, MessageType type);
95 
96  // Register handler to be called if no matchin handler is installed.
98  MessageHandler& GetFallbackMessageHandler(std::size_t party_id);
99 
100  // shutdown the communication layer
101  void Shutdown();
102 
103  std::vector<TransportStatistics> GetTransportStatistics() const noexcept;
104 
105  void SetLogger(std::shared_ptr<Logger> logger);
106 
107  private:
109 
110  std::size_t my_id_;
111  std::size_t number_of_parties_;
112  std::unique_ptr<CommunicationLayerImplementation> implementation_;
113  bool is_started_;
114  bool is_shutdown_;
115  std::shared_ptr<Logger> logger_;
116 };
117 
118 // Create a set of communication layers connected by dummy transports
119 std::vector<std::unique_ptr<CommunicationLayer>> MakeDummyCommunicationLayers(
120  std::size_t number_of_parties);
121 
122 // Create a set of communication layers connected by local TCP connections
123 std::vector<std::unique_ptr<CommunicationLayer>> MakeLocalTcpCommunicationLayers(
124  std::size_t number_of_parties, bool ipv6 = true);
125 
126 } // namespace encrypto::motion::communication
sync_handler.h
encrypto::motion::communication::CommunicationLayer::Synchronize
void Synchronize()
Definition: communication_layer.cpp:302
message.h
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::message_handlers_
std::vector< MessageHandlerMap > message_handlers_
Definition: communication_layer.cpp:81
encrypto::motion::communication::MakeLocalTcpCommunicationLayers
std::vector< std::unique_ptr< CommunicationLayer > > MakeLocalTcpCommunicationLayers(std::size_t number_of_parties, bool ipv6)
Definition: communication_layer.cpp:526
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::send_queues_
std::vector< SynchronizedFiberQueue< message_t > > send_queues_
Definition: communication_layer.cpp:75
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::fallback_message_handlers_
std::vector< std::shared_ptr< MessageHandler > > fallback_message_handlers_
Definition: communication_layer.cpp:82
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation
Definition: communication_layer.cpp:49
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::SendTerminationMessages
void SendTerminationMessages()
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::my_id_
std::size_t my_id_
Definition: communication_layer.cpp:62
encrypto::motion::communication::CommunicationLayer::~CommunicationLayer
~CommunicationLayer()
Definition: communication_layer.cpp:276
encrypto::motion::Logger
Definition: logger.h:40
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::receive_threads_
std::vector< std::thread > receive_threads_
Definition: communication_layer.cpp:76
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::Shutdown
void Shutdown()
Definition: communication_layer.cpp:233
encrypto::motion::communication::GetMessage
const encrypto::motion::communication::Message * GetMessage(const void *buf)
Definition: message_generated.h:146
encrypto::motion::communication::SynchronizationHandler
Definition: sync_handler.h:40
geninput.type
type
Definition: geninput.py:149
encrypto::motion::ThreadSetName
void ThreadSetName(std::thread &thread, const std::string &name)
Definition: thread.cpp:29
encrypto::motion::communication::CommunicationLayer::CommunicationLayer
CommunicationLayer(std::size_t my_id, std::vector< std::unique_ptr< Transport >> &&transports)
Definition: communication_layer.cpp:250
encrypto::motion::communication::CommunicationLayer::RegisterFallbackMessageHandler
void RegisterFallbackMessageHandler(MessageHandlerFunction)
Definition: communication_layer.cpp:449
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::start_promise_
std::promise< void > start_promise_
Definition: communication_layer.cpp:65
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::sync_handler_
std::shared_ptr< SynchronizationHandler > sync_handler_
Definition: communication_layer.cpp:84
encrypto::motion::communication::MessageType::kSynchronizationMessage
@ kSynchronizationMessage
encrypto::motion::communication::DummyTransport::MakeTransportPair
static std::pair< std::unique_ptr< DummyTransport >, std::unique_ptr< DummyTransport > > MakeTransportPair()
Definition: dummy_transport.cpp:37
transport.h
synchronized_queue.h
encrypto::motion::communication::CommunicationLayer::GetMessageHandler
MessageHandler & GetMessageHandler(std::size_t party_id, MessageType type)
Definition: communication_layer.cpp:434
encrypto::motion::communication::CommunicationLayer::Start
void Start()
Definition: communication_layer.cpp:281
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::initialize
void initialize(std::size_t my_id, std::size_t number_of_parties)
encrypto::motion::communication::VerifyMessageBuffer
bool VerifyMessageBuffer(flatbuffers::Verifier &verifier)
Definition: message_generated.h:154
encrypto::motion::communication::CommunicationLayer::RegisterMessageHandler
void RegisterMessageHandler(MessageHandlerFunction, const std::vector< MessageType > &message_types)
Definition: communication_layer.cpp:393
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::MessageHandlerMap
std::unordered_map< MessageType, std::shared_ptr< MessageHandler > > MessageHandlerMap
Definition: communication_layer.cpp:79
encrypto::motion::communication::MakeDummyCommunicationLayers
std::vector< std::unique_ptr< CommunicationLayer > > MakeDummyCommunicationLayers(std::size_t number_of_parties)
Definition: communication_layer.cpp:503
encrypto::motion::communication::TcpSetupHelper
Definition: tcp_transport.h:69
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::start_sfuture_
std::shared_future< void > start_sfuture_
Definition: communication_layer.cpp:66
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::CommunicationLayerImplementation
CommunicationLayerImplementation(std::size_t my_id, std::vector< std::unique_ptr< Transport >> &&transports, std::shared_ptr< Logger > logger)
Definition: communication_layer.cpp:89
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::logger_
std::shared_ptr< Logger > logger_
Definition: communication_layer.cpp:86
communication_layer.h
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::message_handlers_mutex_
std::shared_mutex message_handlers_mutex_
Definition: communication_layer.cpp:80
encrypto::motion::communication::CommunicationLayer::GetNumberOfParties
std::size_t GetNumberOfParties() const
Definition: communication_layer.h:65
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::continue_communication_
std::atomic< bool > continue_communication_
Definition: communication_layer.cpp:67
thread.h
encrypto::motion::communication::TcpPartiesConfiguration
std::vector< TcpConnectionConfiguration > TcpPartiesConfiguration
Definition: tcp_transport.h:62
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::number_of_parties_
std::size_t number_of_parties_
Definition: communication_layer.cpp:63
logger.h
encrypto::motion::communication::CommunicationLayer::GetTransportStatistics
std::vector< TransportStatistics > GetTransportStatistics() const noexcept
Definition: communication_layer.cpp:482
encrypto::motion
Definition: algorithm_description.cpp:35
dummy_transport.h
encrypto::motion::communication::CommunicationLayer::Shutdown
void Shutdown()
Definition: communication_layer.cpp:467
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::ReceiveTask
void ReceiveTask(std::size_t party_id)
Definition: communication_layer.cpp:151
encrypto::motion::communication::CommunicationLayer::BroadcastMessage
void BroadcastMessage(std::vector< std::uint8_t > &&message)
Definition: communication_layer.cpp:353
encrypto::motion::communication::CommunicationLayer::SendMessage
void SendMessage(std::size_t party_id, std::vector< std::uint8_t > &&message)
Definition: communication_layer.cpp:331
encrypto::motion::communication::CommunicationLayer::GetMyId
std::size_t GetMyId() const
Definition: communication_layer.h:66
pooled_work_stealing
Definition: pooled_work_stealing.hpp:35
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::transports_
std::vector< std::unique_ptr< Transport > > transports_
Definition: communication_layer.cpp:69
encrypto::motion::communication::MessageHandler
Definition: message_handler.h:38
encrypto::motion::communication::CommunicationLayer
Definition: communication_layer.h:58
message_handler.h
encrypto::motion::communication::EnumNameMessageType
const char * EnumNameMessageType(MessageType e)
Definition: message_generated.h:76
encrypto::motion::communication::BuildMessage
flatbuffers::FlatBufferBuilder BuildMessage(MessageType message_type, const std::vector< uint8_t > *payload)
Definition: message.cpp:32
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::message_t
std::variant< std::vector< std::uint8_t >, std::shared_ptr< const std::vector< std::uint8_t > >> message_t
Definition: communication_layer.cpp:73
encrypto::motion::communication
Definition: backend.h:37
tcp_transport.h
constants.h
encrypto::motion::communication::CommunicationLayer::GetFallbackMessageHandler
MessageHandler & GetFallbackMessageHandler(std::size_t party_id)
Definition: communication_layer.cpp:460
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
encrypto::motion::communication::MessageType
MessageType
Definition: message_generated.h:16
encrypto::motion::communication::MessageType::kTerminationMessage
@ kTerminationMessage
message_generated.h
encrypto::motion::communication::CommunicationLayer::DeregisterMessageHandler
void DeregisterMessageHandler(const std::vector< MessageType > &message_types)
Definition: communication_layer.cpp:414
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::send_threads_
std::vector< std::thread > send_threads_
Definition: communication_layer.cpp:77
encrypto::motion::communication::CommunicationLayer::MessageHandlerFunction
std::function< std::shared_ptr< MessageHandler >(std::size_t party_id)> MessageHandlerFunction
Definition: communication_layer.h:86
encrypto::motion::communication::TcpSetupHelper::SetupConnections
std::vector< std::unique_ptr< Transport > > SetupConnections()
Definition: tcp_transport.cpp:203
encrypto::motion::communication::CommunicationLayer::SetLogger
void SetLogger(std::shared_ptr< Logger > logger)
Definition: communication_layer.cpp:494
encrypto::motion::communication::CommunicationLayer::CommunicationLayerImplementation::SendTask
void SendTask(std::size_t party_id)
Definition: communication_layer.cpp:115