Flow 1.0.0
Flow project: Full implementation reference.
net_env_simulator.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
20
21namespace flow::net_flow
22{
23// Implementations.
24
26 seed_type_t random_seed,
27 prob_type_t recv_packet_loss_prob,
28 const Packet_loss_seq& recv_packet_loss_seq,
29 const Latency_range& recv_latency_range,
30 const Latency_seq& recv_latency_seq,
31 prob_type_t recv_packet_dup_prob,
32 const Packet_dup_seq& recv_packet_dup_seq) :
33 log::Log_context(logger_ptr, Flow_log_component::S_NET_FLOW),
34 m_seed((random_seed == 0) ? seed_type_t(util::time_since_posix_epoch().count()) : random_seed),
35 m_rnd_generator(m_seed),
36 m_recv_packet_loss_seq(recv_packet_loss_seq),
37 m_recv_packet_loss_distribution(recv_packet_loss_prob),
38 m_recv_latency_seq(recv_latency_seq),
39 m_recv_latency_distribution_msec(recv_latency_range.first.count(),
40 recv_latency_range.second.count()),
41 m_recv_packet_dup_seq(recv_packet_dup_seq),
42 m_recv_packet_dup_distribution(recv_packet_dup_prob)
43{
44 using boost::chrono::round;
45 using boost::chrono::milliseconds; // Just for the output streaming.
46
47 FLOW_LOG_INFO("Net_env_simulator [" << this << "] started with random seed [" << m_seed << "]; "
48 "receiver packet loss ["
49 << (recv_packet_loss_prob * prob_type_t(100)) << "%]; "
50 "latency range [" << round<milliseconds>(recv_latency_range.first) << ", "
51 << round<milliseconds>(recv_latency_range.second) << "]; "
52 "packet duplication probability ["
53 << (recv_packet_dup_prob * prob_type_t(100)) << "%]"
54 ".");
55}
56
58{
59 if (m_recv_packet_loss_seq.empty())
60 {
61 // Ran out of prescribed outcomes; use randomness.
63 }
64 // else
65 const bool drop = m_recv_packet_loss_seq.front();
67 return drop;
68}
69
71{
72 if (m_recv_packet_dup_seq.empty())
73 {
74 // Ran out of prescribed outcomes; use randomness.
76 }
77 // else
78 const bool dup = m_recv_packet_dup_seq.front();
80 return dup;
81}
82
84{
85 if (m_recv_latency_seq.empty())
86 {
87 // Ran out of prescribed outcomes; use randomness.
89 }
90 // else
91 const Fine_duration latency = m_recv_latency_seq.front();
93 return latency;
94}
95
96} // namespace flow::net_flow
97
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1291
std::queue< Fine_duration > Latency_seq
Short-hand for list of packet latencies.
Net_env_simulator(log::Logger *logger_ptr, seed_type_t random_seed=0, prob_type_t recv_packet_loss_prob=0, const Packet_loss_seq &recv_packet_loss_seq=Packet_loss_seq(), const Latency_range &recv_latency_range=Latency_range(), const Latency_seq &recv_latency_seq=Latency_seq(), prob_type_t recv_packet_dup_prob=0, const Packet_dup_seq &recv_packet_dup_seq=Packet_dup_seq())
Constructs simulator.
Packet_dup_seq m_recv_packet_dup_seq
At a given point in time, the i-th from now (in chronological order) received packet will be duplicat...
std::queue< bool > Packet_dup_seq
Short-hand for list of packet duplication outcomes.
Random_generator m_rnd_generator
Random number generator for various random events like packet loss.
boost::random::bernoulli_distribution< prob_type_t > m_recv_packet_loss_distribution
Returns true or false according to some probability set at construction; used for received packet los...
double prob_type_t
Short-hand for floating point between 0 and 1 used to express probability.
Packet_loss_seq m_recv_packet_loss_seq
At a given point in time, the i-th from now (in chronological order) received packet will be dropped ...
std::pair< Fine_duration, Fine_duration > Latency_range
Short-hand for latency range [low, high].
boost::random::uniform_int_distribution< Fine_duration::rep > m_recv_latency_distribution_msec
Returns random latencies (in units of Fine_duration) within the improper [low, high] range given at c...
Fine_duration received_packet_latency()
Low-level packet was received and not dropped, but should we simulate that it was lagged by an additi...
boost::random::bernoulli_distribution< prob_type_t > m_recv_packet_dup_distribution
Returns true or false according to some probability set at construction; used for received packet dup...
uint32_t seed_type_t
Short-hand for the random seed integer type.
Latency_seq m_recv_latency_seq
At a given point in time, the i-th from now (in chronological order) received packet will be lagged b...
seed_type_t m_seed
Random seed for m_rnd_generator (saved for reproducibility of "random" events).
bool should_duplicate_received_packet()
Low-level packet was received, but should we simulate that it was duplicated? Probability and/or pres...
bool should_drop_received_packet()
Low-level packet was received, but should we simulate that it was dropped before receipt?...
std::queue< bool > Packet_loss_seq
Short-hand for list of packet loss outcomes.
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Definition: log.hpp:197
Flow module containing the API and implementation of the Flow network protocol, a TCP-inspired stream...
Definition: node.cpp:25
boost::chrono::microseconds time_since_posix_epoch()
Get the current POSIX (Unix) time as a duration from the Epoch time point.
Definition: util.cpp:147
Flow_log_component
The flow::log::Component payload enumeration comprising various log components used by Flow's own int...
Definition: common.hpp:632
Fine_clock::duration Fine_duration
A high-res time duration as computed from two Fine_time_pts.
Definition: common.hpp:410