Skip to content

Commit

Permalink
Add BestEffortBroadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
shilangyu committed Nov 6, 2023
1 parent 405478d commit 2ba0034
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
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)
set(SOURCES src/main.cpp src/perfect_link.cpp src/best_effort_broadcast.cpp)

# DO NOT EDIT THE FOLLOWING LINES
find_package(Threads)
Expand Down
50 changes: 50 additions & 0 deletions src/include/best_effort_broadcast.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <netinet/in.h>
#include <tuple>
#include <vector>
#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
class BestEffortBroadcast {
public:
BestEffortBroadcast(
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 send(Data... datas) -> void;

private:
PerfectLink _link;
const std::vector<std::tuple<in_addr_t, in_port_t>> _processes;
};

template <typename... Data, class, class>
auto BestEffortBroadcast::send(Data... datas) -> void {
for (auto& [host, port] : _processes) {
_link.send(host, port, datas...);
}
}
16 changes: 16 additions & 0 deletions src/src/best_effort_broadcast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "best_effort_broadcast.hpp"
#include "perfect_link.hpp"

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

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

auto BestEffortBroadcast::listen(PerfectLink::ListenCallback callback) -> void {
_link.listen(callback);
}
1 change: 1 addition & 0 deletions src/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mutex>
#include <thread>
#include <vector>
#include "best_effort_broadcast.hpp"
#include "common.hpp"
#include "parser.hpp"
#include "perfect_link.hpp"
Expand Down

0 comments on commit 2ba0034

Please sign in to comment.