bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
transport.h
Go to the documentation of this file.
1#ifndef BITSIM_DISTRIBUTED_TRANSPORT_H
2#define BITSIM_DISTRIBUTED_TRANSPORT_H
3
5
6#include <boost/noncopyable.hpp>
7
8#include <chrono>
9#include <memory>
10#include <mutex>
11#include <optional>
12#include <queue>
13#include <random>
14#include <string>
15#include <vector>
16
17namespace bitsim
18{
19namespace distributed
20{
21
33class Transport : private boost::noncopyable
34{
35 public:
36 virtual ~Transport() = default;
37
41 virtual void publish(const std::string &bytes) = 0;
42
46 virtual std::optional<std::string> receive(std::chrono::milliseconds timeout) = 0;
47};
48
56class InProcessBus : private boost::noncopyable
57{
58 public:
62 std::unique_ptr<Transport> connect();
63
64 private:
65 class Endpoint;
66 void publish_(const Endpoint *from, const std::string &bytes);
67
68 std::mutex mutex_;
69 std::vector<Endpoint *> endpoints_;
70};
71
80class LossyTransport final : public Transport
81{
82 public:
83 struct Config
84 {
87 real_t loss{0.0};
89 };
90
91 LossyTransport(std::unique_ptr<Transport> inner, const Config &config);
92
93 void publish(const std::string &bytes) override { inner_->publish(bytes); }
94
95 std::optional<std::string> receive(std::chrono::milliseconds timeout) override;
96
97 uint_t n_lost() const noexcept { return n_lost_; }
98
99 private:
100 using clock_type = std::chrono::steady_clock;
101 struct Held
102 {
103 clock_type::time_point release;
104 uint_t order;
105 std::string bytes;
106 bool operator>(const Held &other) const
107 {
108 return release > other.release || (release == other.release && order > other.order);
109 }
110 };
111
112 std::unique_ptr<Transport> inner_;
113 Config config_;
114 std::mt19937 rng_;
115 std::priority_queue<Held, std::vector<Held>, std::greater<Held>> held_;
116 uint_t n_held_{0};
117 uint_t n_lost_{0};
118};
119
120} // namespace distributed
121} // namespace bitsim
122
123#endif // BITSIM_DISTRIBUTED_TRANSPORT_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Participants that are threads of one process, joined by queues.
Definition transport.h:57
std::unique_ptr< Transport > connect()
A transport for a new participant.
Definition transport.cpp:37
void publish(const std::string &bytes) override
Send bytes to every other participant.
Definition transport.h:93
uint_t n_lost() const noexcept
Definition transport.h:97
LossyTransport(std::unique_ptr< Transport > inner, const Config &config)
Definition transport.cpp:57
std::optional< std::string > receive(std::chrono::milliseconds timeout) override
The next message from another participant, waiting at most timeout.
Definition transport.cpp:63
How the participants of a distributed simulation reach each other: everything one publishes,...
Definition transport.h:34
virtual void publish(const std::string &bytes)=0
Send bytes to every other participant.
virtual std::optional< std::string > receive(std::chrono::milliseconds timeout)=0
The next message from another participant, waiting at most timeout.
virtual ~Transport()=default
Definition chrono_ghosts.cpp:12
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
uint_t seed
Definition transport.h:88
real_t latency
[s]
Definition transport.h:85
real_t jitter
[s], the standard deviation of a delay added to latency
Definition transport.h:86
real_t loss
the probability that a message is lost
Definition transport.h:87