Skip to content

Commit

Permalink
Start impl of URB
Browse files Browse the repository at this point in the history
  • Loading branch information
shilangyu committed Nov 12, 2023
1 parent 033a382 commit 42b6673
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __pycache__/

bin/da_proc
target/
.vscode

### C ###
# Prerequisites
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# You can, however, change the list of files that comprise this variable.

include_directories(include)
set(SOURCES src/main.cpp src/perfect_link.cpp src/best_effort_broadcast.cpp)
set(SOURCES src/main.cpp src/perfect_link.cpp src/best_effort_broadcast.cpp src/uniform_reliable_broadcast.cpp)

# DO NOT EDIT THE FOLLOWING LINES
find_package(Threads)
Expand Down
2 changes: 2 additions & 0 deletions src/include/perfect_link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ auto PerfectLink::send(const in_addr_t host,
addr.sin_port = port;

{
// TODO: if all messages inflight were sent to a process that crashed, this
// will lock forever
std::unique_lock lock(_pending_for_ack_mutex);
_pending_for_ack_cv.wait(
lock, [this] { return _pending_for_ack.size() < MAX_IN_FLIGHT; });
Expand Down
45 changes: 45 additions & 0 deletions src/include/uniform_reliable_broadcast.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <netinet/in.h>
#include <tuple>
#include <vector>
#include "best_effort_broadcast.hpp"
#include "perfect_link.hpp"

/// Enforces 3 properties for broadcast communication:
/// 1. Validity - if pi and pj are correct, then every message broadcast by pi
/// is eventually delivered to pj
/// 2. No duplication - no message is delivered more than once
/// 3. No creation - no message is delivered unless it was broadcast
/// 4. Uniform agreement - if any process delivers m, then all correct processes
/// eventually deliver m
class UniformReliableBroadcast {
public:
UniformReliableBroadcast(
const PerfectLink::ProcessIdType id,
const std::vector<std::tuple<in_addr_t, in_port_t>> processes);

/// @brief Binds this broadcast link to a host and port. Once done cannot be
/// done again.
auto bind(const in_addr_t host, const in_port_t port) -> void;

/// @brief Starts listening to incoming broadcast messages. Sends ACKs for new
/// messages. Receives ACKs and resends messages with missing ACKs. Thread
/// safe.
/// @param callback Function that will be called when a message is delivered.
auto listen(PerfectLink::ListenCallback callback) -> void;

/// @brief Broadcasts a message to all processes. The data has to be smaller
/// than about 64KiB. Sending is possible only after performing a bind. At
/// most 8 messages can be packed in a single packet.
template <
typename... Data,
class = std::enable_if_t<
are_equal<std::tuple<std::uint8_t*, std::size_t>, Data...>::value>,
class = std::enable_if_t<(sizeof...(Data) <=
PerfectLink::MAX_MESSAGE_COUNT_IN_PACKET)>>
auto broadcast(Data... datas) -> void;

private:
BestEffortBroadcast _link;
};
11 changes: 11 additions & 0 deletions src/src/uniform_reliable_broadcast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "uniform_reliable_broadcast.hpp"

UniformReliableBroadcast::UniformReliableBroadcast(
const PerfectLink::ProcessIdType id,
const std::vector<std::tuple<in_addr_t, in_port_t>> processes)
: _link(id, processes) {}

auto UniformReliableBroadcast::bind(const in_addr_t host, const in_port_t port)
-> void {
_link.bind(host, port);
}

0 comments on commit 42b6673

Please sign in to comment.