Skip to content

Commit fa307e1

Browse files
PaulRalnikovAz3git
andauthored
FEAT: refactor Simulator class (#513)
closes #512 --------- Co-authored-by: Alexey Zharkov <62723911+Az3git@users.noreply.github.com>
1 parent 4dac91f commit fa307e1

27 files changed

Lines changed: 576 additions & 136 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if(BUILD_TESTS)
5252

5353
target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/external/yaml-cpp/include)
5454
target_include_directories(${TEST_NAME} PUBLIC "source")
55+
target_compile_definitions(${TEST_NAME} PRIVATE PROJECT_ROOT_DIR="${CMAKE_SOURCE_DIR}")
5556

5657
target_compile_options(${TEST_NAME} PRIVATE -Wall -Wextra)
5758
target_link_libraries(${TEST_NAME} gtest gmock gtest_main yaml-cpp matplot)

source/link/link.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ Link::Link(Id a_id, std::weak_ptr<IDevice> a_from, std::weak_ptr<IDevice> a_to,
2626
}
2727
}
2828

29-
Link::Link(LinkInitArgs args)
30-
: Link(args.id.value_or_throw(),
31-
IdentifierFactory::get_instance().get_object<IDevice>(
32-
args.from_id.value_or_throw()),
33-
IdentifierFactory::get_instance().get_object<IDevice>(
34-
args.to_id.value_or_throw()),
35-
args.speed.value_or_throw(), args.delay.value_or_throw(),
36-
args.max_from_egress_buffer_size.value_or_throw(),
37-
args.max_to_ingress_buffer_size.value_or_throw()) {}
38-
3929
void Link::schedule_arrival(Packet packet) {
4030
if (m_to.expired()) {
4131
LOG_WARN("Destination device pointer is expired");

source/link/link.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,12 @@
99

1010
namespace sim {
1111

12-
struct LinkInitArgs {
13-
utils::StrExpected<Id> id = std::unexpected("Missing id");
14-
utils::StrExpected<Id> from_id = std::unexpected("Missing from id");
15-
utils::StrExpected<Id> to_id = std::unexpected("Missing to id");
16-
utils::StrExpected<SpeedGbps> speed = std::unexpected("Missing speed");
17-
utils::StrExpected<TimeNs> delay = std::unexpected("Missing delay");
18-
utils::StrExpected<SizeByte> max_from_egress_buffer_size =
19-
std::unexpected("Missing max from egress buffer size");
20-
utils::StrExpected<SizeByte> max_to_ingress_buffer_size =
21-
std::unexpected("Missing max to ingress buffer size");
22-
;
23-
};
24-
2512
class Link : public ILink, public std::enable_shared_from_this<Link> {
2613
public:
2714
Link(Id a_id, std::weak_ptr<IDevice> a_from, std::weak_ptr<IDevice> a_to,
2815
SpeedGbps a_speed = SpeedGbps(1), TimeNs a_delay = TimeNs(0),
2916
SizeByte a_max_from_egress_buffer_size = SizeByte(4096),
3017
SizeByte a_max_to_ingress_buffer_size = SizeByte(4096));
31-
explicit Link(LinkInitArgs args);
3218
~Link() = default;
3319

3420
void schedule_arrival(Packet packet) final;

source/parser/config_reader/config_node.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ std::ostream& operator<<(std::ostream& out, const ConfigNode& node) {
4747
return out;
4848
}
4949

50+
std::string ConfigNode::to_string() const {
51+
std::stringstream ss;
52+
ss << *this;
53+
return ss.str();
54+
}
55+
5056
YAML::NodeType::value ConfigNode::Type() const { return m_node.Type(); }
5157

5258
bool ConfigNode::IsNull() const noexcept { return m_node.IsNull(); }

source/parser/config_reader/config_node.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class ConfigNode {
3232

3333
friend std::ostream& operator<<(std::ostream& out, const ConfigNode& node);
3434

35+
std::string to_string() const;
36+
3537
// yaml-cpp functional
3638

3739
[[nodiscard]] YAML::NodeType::value Type() const;

source/parser/network_parser.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "network_parser.hpp"
2+
3+
#include "parser/simulation/new-connection/connection_parser.hpp"
4+
#include "parser/topology/topology_parser.hpp"
5+
#include "relative_path_parser.hpp"
6+
7+
namespace sim {
8+
9+
Network parse_network(const std::filesystem::path& path) {
10+
ConfigNode node = load_file(path);
11+
12+
std::string topology_config_path =
13+
parse_relative_path(node, "topology_config_path", path.parent_path());
14+
15+
const ConfigNode topology_config = load_file(topology_config_path);
16+
17+
Topology topology = parse_topology(topology_config);
18+
19+
const ConfigNode& connections_node = node["connections"].value_or_throw();
20+
21+
NetworkContext ctx(std::move(topology), utils::IdTable<INewConnection>{});
22+
23+
std::optional<ConfigNode> presets_node = node["presets"].to_optional();
24+
std::optional<ConfigNode> connection_presets_node =
25+
presets_node ? presets_node.value()["connection"].to_optional()
26+
: std::nullopt;
27+
28+
for (const auto& connection_node : connections_node) {
29+
ConfigNodeWithPreset conn_node_with_preset(connection_node,
30+
connection_presets_node);
31+
32+
std::shared_ptr<INewConnection> connection = parse_i_connection(
33+
conn_node_with_preset, ctx.topology.get_context().hosts_table);
34+
Id conn_id = connection->get_id();
35+
if (!ctx.connections_table.emplace(conn_id, connection).second) {
36+
throw connections_node.create_parsing_error(
37+
fmt::format("Two connection with same name: {}", conn_id));
38+
}
39+
}
40+
41+
return Network{std::move(ctx)};
42+
}
43+
44+
} // namespace sim

source/parser/network_parser.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
#include <filesystem>
3+
4+
#include "simulator/network/network.hpp"
5+
6+
namespace sim {
7+
8+
Network parse_network(const std::filesystem::path &path);
9+
10+
}

source/parser/parser.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "parser/topology/host/host_parser.hpp"
99
#include "parser/topology/link/link_parser.hpp"
1010
#include "parser/topology/switch/switch_parser.hpp"
11-
#include "preset_storage.hpp"
1211

1312
namespace sim {
1413

@@ -43,8 +42,8 @@ Simulator YamlParser::build_simulator_from_config(
4342

4443
parse_if_present(topology_config["hosts"],
4544
[this, &hosts_presets_node](ConfigNode node) {
46-
process_hosts(node, hosts_presets_node);
47-
});
45+
process_hosts(node, hosts_presets_node);
46+
});
4847

4948
const ConfigNode packet_spraying_node =
5049
topology_config["packet-spraying"].value_or_throw();
@@ -56,10 +55,11 @@ Simulator YamlParser::build_simulator_from_config(
5655
}
5756
}
5857

59-
parse_if_present(topology_config["switches"],
60-
[this, &switches_presets_node, &packet_spraying_node](ConfigNode node) {
61-
process_switches(node, switches_presets_node, packet_spraying_node);
62-
});
58+
parse_if_present(
59+
topology_config["switches"],
60+
[this, &switches_presets_node, &packet_spraying_node](ConfigNode node) {
61+
process_switches(node, switches_presets_node, packet_spraying_node);
62+
});
6363

6464
std::optional<ConfigNode> links_presets_node = std::nullopt;
6565
if (topology_presets_node.has_value()) {
@@ -82,41 +82,50 @@ Simulator YamlParser::build_simulator_from_config(
8282
return std::move(m_simulator);
8383
}
8484

85-
void YamlParser::process_hosts(const ConfigNode &hosts_node, const std::optional<ConfigNode> &hosts_presets_node) {
85+
void YamlParser::process_hosts(
86+
const ConfigNode &hosts_node,
87+
const std::optional<ConfigNode> &hosts_presets_node) {
8688
process_identifiables<IHost>(
8789
hosts_node,
8890
[this](std::shared_ptr<IHost> host) {
8991
return m_simulator.add_host(host);
9092
},
91-
[&hosts_presets_node](const ConfigNode& hosts_node){
92-
return HostParser::parse_i_host(ConfigNodeWithPreset(hosts_node, hosts_presets_node));
93+
[&hosts_presets_node](const ConfigNode &hosts_node) {
94+
return HostParser::parse_i_host(
95+
ConfigNodeWithPreset(hosts_node, hosts_presets_node));
9396
},
9497
"Can not add host.");
9598
}
9699

97-
void YamlParser::process_switches(const ConfigNode &switches_node,
98-
const std::optional<ConfigNode> &switches_presets_node,
99-
const ConfigNode &packet_spraying_node) {
100+
void YamlParser::process_switches(
101+
const ConfigNode &switches_node,
102+
const std::optional<ConfigNode> &switches_presets_node,
103+
const ConfigNode &packet_spraying_node) {
100104
process_identifiables<ISwitch>(
101105
switches_node,
102106
[this](std::shared_ptr<ISwitch> swtch) {
103107
return m_simulator.add_switch(swtch);
104108
},
105-
[&packet_spraying_node, &switches_presets_node](const ConfigNode &switch_node) {
106-
return SwitchParser::parse_i_switch(ConfigNodeWithPreset(switch_node, switches_presets_node), packet_spraying_node);
109+
[&packet_spraying_node,
110+
&switches_presets_node](const ConfigNode &switch_node) {
111+
return SwitchParser::parse_i_switch(
112+
ConfigNodeWithPreset(switch_node, switches_presets_node),
113+
packet_spraying_node);
107114
},
108115
"Can not add switch.");
109116
}
110117

111-
void YamlParser::process_links(const ConfigNode &links_node,
112-
const std::optional<ConfigNode> &link_presets_node) {
118+
void YamlParser::process_links(
119+
const ConfigNode &links_node,
120+
const std::optional<ConfigNode> &link_presets_node) {
113121
process_identifiables<ILink>(
114122
links_node,
115123
[this](std::shared_ptr<ILink> link) {
116124
return m_simulator.add_link(link);
117125
},
118126
[&link_presets_node](const ConfigNode &link_node) {
119-
return LinkParser::parse_i_link(ConfigNodeWithPreset(link_node, link_presets_node));
127+
return LinkParser::parse_i_link(
128+
ConfigNodeWithPreset(link_node, link_presets_node));
120129
},
121130
"Can not add link.");
122131
}

source/parser/preset_storage.hpp

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
5+
#include "parser/config_reader/config_node.hpp"
6+
7+
namespace sim {
8+
inline std::filesystem::path parse_relative_path(const ConfigNode& node,
9+
std::string_view field_name,
10+
std::filesystem::path prefix) {
11+
return prefix /
12+
node[field_name].value_or_throw().as_or_throw<std::string>();
13+
}
14+
} // namespace sim

0 commit comments

Comments
 (0)