Skip to content

Commit 4dac91f

Browse files
PaulRalnikovAz3git
andauthored
FEAT: parsers for new acrhitecture (#511)
closes #510 --------- Co-authored-by: Alexey Zharkov <62723911+Az3git@users.noreply.github.com>
1 parent 8442480 commit 4dac91f

18 files changed

Lines changed: 397 additions & 107 deletions

source/connection/flow/i_new_flow.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88

99
namespace sim {
1010

11-
struct FlowContext {
11+
struct Endpoints {
12+
std::shared_ptr<IHost> sender;
13+
std::shared_ptr<IHost> receiver;
14+
};
15+
16+
struct FlowContext : public Endpoints {
1217
SizeByte sent_size;
1318
SizeByte delivered_size;
1419
SizeByte retransmit_size;
1520

1621
std::optional<TimeNs> start_time;
1722
std::optional<TimeNs> last_ack_receive_time;
1823
utils::Statistics<TimeNs> rtt_statistics;
19-
20-
std::weak_ptr<IHost> sender;
21-
std::weak_ptr<IHost> receiver;
2224
};
2325

2426
// Transport layer interface for reliable data delivery along single physical
2527
// path
26-
class INewFlow : public virtual Identifiable {
27-
public:
28-
virtual void send(std::vector<PacketInfo> packets) = 0;
28+
class INewFlow : public virtual Identifiable {
29+
public:
30+
virtual void send(std::vector<PacketInfo> packets) = 0;
2931

30-
virtual const FlowContext& get_context() const = 0;
31-
};
32+
virtual const FlowContext& get_context() const = 0;
33+
};
3234

3335
} // namespace sim

source/connection/flow/tcp/new_tcp_flow.cpp

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ void NewTcpFlow::send(std::vector<PacketInfo> packets_info) {
2020
if (packets_info.empty()) {
2121
return;
2222
}
23-
auto exp_endpoints = get_endpoints();
24-
if (!exp_endpoints.has_value()) {
25-
LOG_ERROR(fmt::format("Flow {}; error while sending packets: {}", m_id,
26-
exp_endpoints.error()));
27-
return;
28-
}
29-
auto [sender, receiver] = exp_endpoints.value();
23+
auto sender = m_context.sender;
24+
auto receiver = m_context.receiver;
3025
for (auto info : packets_info) {
3126
Packet data = create_data_packet(std::move(info), sender, receiver);
3227

@@ -42,31 +37,19 @@ NewTcpFlow::NewTcpFlow(Id a_id, std::shared_ptr<IHost> a_sender,
4237
std::shared_ptr<IHost> a_receiver, bool a_ecn_capable,
4338
RTO a_rto)
4439
: m_id(std::move(a_id)),
45-
m_context({SizeByte(0), SizeByte(0), SizeByte(0),
40+
m_context({Endpoints{a_sender, a_receiver}, SizeByte(0), SizeByte(0),
41+
SizeByte(0),
4642

47-
std::nullopt, std::nullopt, utils::Statistics<TimeNs>(),
48-
49-
a_sender, a_receiver}),
43+
std::nullopt, std::nullopt, utils::Statistics<TimeNs>()}),
5044
m_ecn_capable(a_ecn_capable),
5145
m_rto(std::move(a_rto)) {
5246
if (!m_flag_manager.register_flag_by_amount(m_packet_type_label,
5347
PacketType::ENUM_SIZE)) {
54-
throw std::runtime_error("Can not registrate packet type label");
48+
throw std::runtime_error("Can not register packet type label");
5549
}
5650
if (!register_packet_avg_rtt_flag(m_flag_manager)) {
57-
throw std::runtime_error("Can not registrate packet avg rtt label");
58-
}
59-
}
60-
61-
utils::StrExpected<NewTcpFlow::Endpoints> NewTcpFlow::get_endpoints() const {
62-
if (m_context.sender.expired()) {
63-
return std::unexpected("sender pointer expired");
64-
}
65-
66-
if (m_context.receiver.expired()) {
67-
return std::unexpected("receiver pointer expired");
51+
throw std::runtime_error("Can not register packet avg rtt label");
6852
}
69-
return Endpoints{m_context.sender.lock(), m_context.receiver.lock()};
7053
}
7154

7255
Packet NewTcpFlow::create_data_packet(PacketInfo info,
@@ -120,38 +103,21 @@ void NewTcpFlow::set_avg_rtt_if_present(Packet& packet) {
120103
}
121104

122105
void NewTcpFlow::send_data_packet(Packet data) {
123-
if (m_context.sender.expired()) {
124-
LOG_ERROR(fmt::format(
125-
"Flow {}: sender pointer expired; could not send data packet {}",
126-
m_id, data.to_string()));
127-
return;
128-
}
129-
auto sender = m_context.sender.lock();
130-
131106
TimeNs now = Scheduler::get_instance().get_current_time();
132107

133108
Scheduler::get_instance().add<Timeout>(now + m_rto.current,
134109
shared_from_this(), data);
135110
m_context.sent_size += data.size;
136111

137112
data.sent_time = now;
138-
sender->enqueue_packet(std::move(data));
113+
m_context.sender->enqueue_packet(std::move(data));
139114
}
140115

141116
void NewTcpFlow::process_data_packet(const Packet& data,
142117
PacketCallback callback) {
143-
auto exp_endpoints = get_endpoints();
144-
if (!exp_endpoints.has_value()) {
145-
LOG_ERROR(
146-
fmt::format("Flow {}: error while processing data packet {}: {}",
147-
m_id, data.to_string(), exp_endpoints.error()));
148-
return;
149-
}
150-
auto [sender, receiver] = exp_endpoints.value();
151-
152118
Packet ack = data;
153-
ack.source_id = receiver->get_id();
154-
ack.dest_id = sender->get_id();
119+
ack.source_id = m_context.receiver->get_id();
120+
ack.dest_id = m_context.sender->get_id();
155121
ack.size = SizeByte(1);
156122
ack.ttl = M_MAX_TTL;
157123
ack.flags.set_flag(m_packet_type_label, PacketType::ACK)
@@ -173,7 +139,7 @@ void NewTcpFlow::process_data_packet(const Packet& data,
173139
callback);
174140
};
175141

176-
receiver->enqueue_packet(ack);
142+
m_context.receiver->enqueue_packet(ack);
177143
}
178144

179145
void NewTcpFlow::process_ack(const Packet& ack, SizeByte data_packet_size,
@@ -189,8 +155,9 @@ void NewTcpFlow::process_ack(const Packet& ack, SizeByte data_packet_size,
189155
update_rto_on_ack();
190156

191157
if (!m_ack_monitor.confirm_one(ack.packet_num)) {
192-
LOG_WARN(fmt::format("Flow {} got ack {} that confirms nothing; ignored", m_id,
193-
ack.to_string()));
158+
LOG_WARN(
159+
fmt::format("Flow {} got ack {} that confirms nothing; ignored",
160+
m_id, ack.to_string()));
194161
return;
195162
}
196163

@@ -217,7 +184,7 @@ void NewTcpFlow::update_rto_on_ack() {
217184
void NewTcpFlow::on_timeout(Packet data) {
218185
if (m_ack_monitor.is_confirmed(data.packet_num)) {
219186
LOG_INFO(fmt::format(
220-
"Flow {}: packet {} is confirmed when timeout reashed; no "
187+
"Flow {}: packet {} is confirmed when timeout reached; no "
221188
"retransmit",
222189
m_id, data.packet_num));
223190
return;
@@ -247,7 +214,7 @@ class NewTcpFlow::Timeout : public Event {
247214
void operator()() {
248215
if (m_flow.expired()) {
249216
LOG_ERROR(
250-
"Could not run TCP flow timout event: flow pointer expired");
217+
"Could not run TCP flow timeout event: flow pointer expired");
251218
return;
252219
}
253220
m_flow.lock()->on_timeout(std::move(m_packet));

source/connection/flow/tcp/new_tcp_flow.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ class NewTcpFlow : public INewFlow,
2424
std::shared_ptr<IHost> a_receiver, bool a_ecn_capable,
2525
RTO a_rto);
2626

27-
struct Endpoints {
28-
std::shared_ptr<IHost> sender;
29-
std::shared_ptr<IHost> receiver;
30-
};
31-
32-
utils::StrExpected<Endpoints> get_endpoints() const;
33-
3427
Packet create_data_packet(PacketInfo info, std::shared_ptr<IHost> sender,
3528
std::shared_ptr<IHost> receiver);
3629

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#include "./config_node_with_preset.hpp"
22

3-
namespace sim{
3+
namespace sim {
44

5-
ConfigNodeWithPreset::ConfigNodeWithPreset(ConfigNode a_node, std::optional<ConfigNode> a_presets_node, std::optional<ConfigNode> a_preset)
6-
: m_node(std::move(a_node)), m_presets_node(std::move(a_presets_node)), m_preset(std::move(a_preset)){}
5+
ConfigNodeWithPreset::ConfigNodeWithPreset(
6+
ConfigNode a_node, std::optional<ConfigNode> a_presets_node,
7+
std::optional<ConfigNode> a_preset)
8+
: m_node(std::move(a_node)),
9+
m_presets_node(std::move(a_presets_node)),
10+
m_preset(std::move(a_preset)) {}
711

8-
ConfigNodeWithPresetExpected ConfigNodeWithPreset::operator[](std::string_view key) const{
12+
ConfigNodeWithPresetExpected ConfigNodeWithPreset::operator[](
13+
std::string_view key) const {
914
const ConfigNodeExpected child_node = m_node[key];
10-
if (!child_node.has_value()){
15+
if (!child_node.has_value()) {
1116
// m_node hasn't key. Tries to find key in m_preset
12-
if (!m_preset.has_value()){
17+
if (!m_preset.has_value()) {
1318
// m_preset doesn't exist. Tries to find preset for this node
14-
if (!m_presets_node.has_value()){
19+
if (!m_presets_node.has_value()) {
1520
// m_presets_node doesn't exist. Returns message about it
1621
std::stringstream ss;
1722
ss << "Key error: node " << m_node << ":\n";
@@ -20,7 +25,7 @@ ConfigNodeWithPresetExpected ConfigNodeWithPreset::operator[](std::string_view k
2025
}
2126
// m_presets_node exist. Tries to find field 'preset-name' in m_node
2227
ConfigNodeExpected node_preset_name = m_node["preset-name"];
23-
if (!node_preset_name.has_value()){
28+
if (!node_preset_name.has_value()) {
2429
// config hasn't field 'preset-name'. Returns message about it
2530
std::stringstream ss;
2631
ss << "Key error: node " << m_node << ":\n";
@@ -29,48 +34,66 @@ ConfigNodeWithPresetExpected ConfigNodeWithPreset::operator[](std::string_view k
2934
}
3035
// field 'preset-name' was found. Tries to convert it to string
3136
ConfigNode presets_node = m_presets_node.value();
32-
utils::StrExpected<std::string> preset_name = node_preset_name.value().as<std::string>();
33-
if (!preset_name.has_value()){
34-
// field 'preset-name' couldn't be converted to string. Returns message about it
37+
utils::StrExpected<std::string> preset_name =
38+
node_preset_name.value().as<std::string>();
39+
if (!preset_name.has_value()) {
40+
// field 'preset-name' couldn't be converted to string. Returns
41+
// message about it
3542
std::stringstream ss;
3643
ss << "Error: field 'preset-name' in node " << m_node << '\n';
3744
ss << "couldn't be converted to string";
3845
return std::unexpected(ss.str());
3946
}
4047
// conversion is successful. tries to find preset in m_presets_node
4148
ConfigNodeExpected preset_node = presets_node[preset_name.value()];
42-
if (!preset_node.has_value()){
43-
// m_presets_node hasn't preset with the specified name in config. Retruns message about it
49+
if (!preset_node.has_value()) {
50+
// m_presets_node hasn't preset with the specified name in
51+
// config. Returns message about it
4452
std::stringstream ss;
45-
ss << "Error: preset named '" << preset_name.value() << "' of node " << m_node << '\n';
46-
ss << "can not be found in presets node:" << presets_node << ":\n";
53+
ss << "Error: preset named '" << preset_name.value()
54+
<< "' of node " << m_node << '\n';
55+
ss << "can not be found in presets node:" << presets_node
56+
<< ":\n";
4757
return std::unexpected(ss.str());
4858
}
4959
// preset was found successfull. put the found value in m_preset
5060
m_preset.emplace(preset_node.value());
5161
}
5262
// tries to find key in preset
5363
ConfigNodeExpected key_node = m_preset.value()[key];
54-
if (!key_node.has_value()){
64+
if (!key_node.has_value()) {
5565
// m_preset hasn't key. Returns message about it
5666
std::stringstream ss;
57-
ss << "Key error: preset '" << m_preset.value().get_name().value() << "' ";
67+
ss << "Key error: preset '" << m_preset.value().get_name().value()
68+
<< "' ";
5869
ss << "does not have key: " << key;
5970
return std::unexpected(ss.str());
6071
}
61-
// key was found in preset
62-
return ConfigNodeWithPreset(key_node.value(), m_presets_node, m_preset);
72+
// key was found in preset; use key_node as main node; DROP PRESET NODE
73+
return ConfigNodeWithPreset(key_node.value(), m_presets_node,
74+
std::nullopt);
6375
}
64-
// key was found in m_node
65-
return ConfigNodeWithPreset(child_node.value(), m_presets_node, m_preset);
76+
// key was found in m_node: go to child value; DROP PRESET NODE
77+
return ConfigNodeWithPreset(child_node.value(), m_presets_node,
78+
std::nullopt);
6679
}
6780

68-
const std::string& ConfigNodeWithPreset::get_name_or_throw() const{
81+
const std::string& ConfigNodeWithPreset::get_name_or_throw() const {
6982
return m_node.get_name_or_throw();
7083
}
7184

72-
const ConfigNode& ConfigNodeWithPreset::get_node() const noexcept{
85+
std::runtime_error ConfigNodeWithPreset::create_parsing_error(
86+
std::string_view error) const {
87+
return m_node.create_parsing_error(error);
88+
}
89+
90+
const ConfigNode& ConfigNodeWithPreset::get_node() const noexcept {
7391
return m_node;
7492
}
7593

76-
} // namespace sim
94+
const std::optional<ConfigNode> ConfigNodeWithPreset::get_presets_node()
95+
const noexcept {
96+
return m_presets_node;
97+
}
98+
99+
} // namespace sim

source/parser/config_reader/config_node_with_preset.hpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@
22

33
#include "./config_node.hpp"
44

5-
namespace sim{
5+
namespace sim {
66

77
class ConfigNodeWithPreset;
88

99
using ConfigNodeWithPresetExpected = utils::StrExpected<ConfigNodeWithPreset>;
1010

1111
// Config node that supports preset inheritance.
12-
class ConfigNodeWithPreset{
12+
class ConfigNodeWithPreset {
1313
public:
14-
1514
explicit ConfigNodeWithPreset(
1615
ConfigNode a_node,
1716
std::optional<ConfigNode> a_presets_node = std::nullopt,
18-
std::optional<ConfigNode> a_preset = std::nullopt
19-
);
17+
std::optional<ConfigNode> a_preset = std::nullopt);
2018

2119
ConfigNodeWithPresetExpected operator[](std::string_view key) const;
2220

2321
// Returns the base config node.
2422
const ConfigNode& get_node() const noexcept;
2523

26-
// Converts m_node to type T. Probably this method will return message about error
24+
const std::optional<ConfigNode> get_presets_node() const noexcept;
25+
26+
// Converts m_node to type T. Probably this method will return message about
27+
// error
2728
template <typename T>
2829
[[nodiscard]] utils::StrExpected<T> as() const noexcept {
2930
return m_node.as<T>();
@@ -36,14 +37,17 @@ class ConfigNodeWithPreset{
3637

3738
[[nodiscard]] const std::string& get_name_or_throw() const;
3839

39-
private:
40+
[[nodiscard]] std::runtime_error create_parsing_error(
41+
std::string_view error) const;
4042

43+
private:
4144
// m_node contains information about config node and probably preset name
4245
// m_preset - preset node which that is used to supplement fields of m_node
43-
// m_presets_node - node it which preset node should be searched (id it needs)
46+
// m_presets_node - node it which preset node should be searched (id it
47+
// needs)
4448
const ConfigNode m_node;
4549
const std::optional<ConfigNode> m_presets_node;
4650
mutable std::optional<ConfigNode> m_preset;
4751
};
4852

49-
} // namespace sim
53+
} // namespace sim

0 commit comments

Comments
 (0)