Derecho  0.9
Distributed systems toolkit for RDMA
connection_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <map>
5 #include <mutex>
6 
7 #include "../derecho_type_definitions.hpp"
8 #include "locked_reference.hpp"
9 #include <derecho/tcp/tcp.hpp>
10 
11 namespace tcp {
13  std::mutex sockets_mutex;
14 
16  std::unique_ptr<connection_listener> conn_listener;
17  std::map<node_id_t, socket> sockets;
18  bool add_connection(const node_id_t other_id,
19  const std::pair<ip_addr_t, uint16_t>& other_ip_and_port);
21  const std::map<node_id_t, std::pair<ip_addr_t, uint16_t>>& ip_addrs_and_ports);
22 
23 public:
31  const std::map<node_id_t, std::pair<ip_addr_t, uint16_t>> ip_addrs_and_ports);
32  void destroy();
43  bool write(node_id_t node_id, char const* buffer, size_t size);
52  bool write_all(char const* buffer, size_t size);
63  bool read(node_id_t node_id, char* buffer, size_t size);
74  bool add_node(node_id_t new_id,
75  const std::pair<ip_addr_t, uint16_t>& new_ip_addr_and_port);
83  bool delete_node(node_id_t remove_id);
91  bool contains_node(node_id_t node_id);
92 
93  template <class T>
94  bool exchange(node_id_t node_id, T local, T& remote) {
95  std::lock_guard<std::mutex> lock(sockets_mutex);
96  const auto it = sockets.find(node_id);
97  assert(it != sockets.end());
98  return it->second.exchange(local, remote);
99  }
108  int32_t probe_all();
109 
117  void filter_to(const std::vector<node_id_t>& live_nodes_list);
118 
129 };
130 } // namespace tcp
bool contains_node(node_id_t node_id)
Checks whether this connection manager currently has a socket connected to the node with the specifie...
bool add_node(node_id_t new_id, const std::pair< ip_addr_t, uint16_t > &new_ip_addr_and_port)
Adds a TCP connection to a new node.
A little helper class that wraps together a reference and a lock on a related mutex.
bool write_all(char const *buffer, size_t size)
Writes size bytes from a buffer to all the other nodes currently connected, in ascending order of nod...
bool exchange(node_id_t node_id, T local, T &remote)
std::unique_ptr< connection_listener > conn_listener
bool read(node_id_t node_id, char *buffer, size_t size)
Receives size bytes from the node with ID node_id, over the TCP socket connected to that node...
tcp_connections(node_id_t my_id, const std::map< node_id_t, std::pair< ip_addr_t, uint16_t >> ip_addrs_and_ports)
Creates a TCP connection manager for a set of connections to all of the initial set of addresses...
void establish_node_connections(const std::map< node_id_t, std::pair< ip_addr_t, uint16_t >> &ip_addrs_and_ports)
derecho::LockedReference< std::unique_lock< std::mutex >, socket > get_socket(node_id_t node_id)
Gets a locked reference to the TCP socket connected to a particular node.
bool write(node_id_t node_id, char const *buffer, size_t size)
Writes size bytes from a buffer to the node with ID node_id, using the TCP socket connected to that n...
bool delete_node(node_id_t remove_id)
Removes a node from the managed set of TCP connections, closing the socket connected to it...
void filter_to(const std::vector< node_id_t > &live_nodes_list)
Compares the set of TCP connections to a list of known live nodes and removes any connections to node...
uint32_t node_id_t
Type alias for Node IDs in a Derecho group.
std::map< node_id_t, socket > sockets
int32_t probe_all()
Checks all of the TCP connections managed by this object for new incoming data, and returns the ID of...
bool add_connection(const node_id_t other_id, const std::pair< ip_addr_t, uint16_t > &other_ip_and_port)