Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 1 addition & 37 deletions source/device/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,7 @@

namespace sim {

Host::Host(Id a_id) : m_router(a_id) {}

bool Host::add_inlink(std::shared_ptr<ILink> link) {
if (!is_valid_link(link)) {
return false;
}
return m_router.add_inlink(link);
}

bool Host::add_outlink(std::shared_ptr<ILink> link) {
if (!is_valid_link(link)) {
return false;
}
m_router.add_outlink(link);
return true;
}

bool Host::update_routing_table(Id dest_id, std::shared_ptr<ILink> link,
size_t paths_count) {
if (!is_valid_link(link)) {
return false;
}
m_router.update_routing_table(dest_id, link, paths_count);
return true;
}

std::shared_ptr<ILink> Host::next_inlink() { return m_router.next_inlink(); };

std::shared_ptr<ILink> Host::get_link_to_destination(Packet packet) const {
return m_router.get_link_to_destination(packet);
};
Host::Host(Id a_id) : RoutingModule(a_id) {}

bool Host::notify_about_arrival(Time arrival_time) {
return m_process_scheduler.notify_about_arriving(arrival_time,
Expand Down Expand Up @@ -134,10 +104,4 @@ Time Host::send_packet() {
return total_processing_time;
}

std::set<std::shared_ptr<ILink>> Host::get_outlinks() {
return m_router.get_outlinks();
}

Id Host::get_id() const { return m_router.get_id(); }

} // namespace sim
12 changes: 1 addition & 11 deletions source/device/host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@

namespace sim {

class Host : public IHost, public std::enable_shared_from_this<Host> {
class Host : public IHost, public RoutingModule, public std::enable_shared_from_this<Host> {
public:
Host(Id id);
~Host() = default;

bool add_inlink(std::shared_ptr<ILink> link) final;
bool add_outlink(std::shared_ptr<ILink> link) final;
bool update_routing_table(Id dest_id, std::shared_ptr<ILink> link,
size_t paths_count = 1) final;
std::shared_ptr<ILink> next_inlink() final;
std::shared_ptr<ILink> get_link_to_destination(Packet packet) const final;
std::set<std::shared_ptr<ILink>> get_outlinks() final;
bool notify_about_arrival(Time arrive_time) final;

DeviceType get_type() const final;
Expand All @@ -29,11 +22,8 @@ class Host : public IHost, public std::enable_shared_from_this<Host> {

void enqueue_packet(Packet packet) final;

Id get_id() const final;

private:
std::queue<Packet> m_nic_buffer;
RoutingModule m_router;
SchedulingModule<IHost, Process> m_process_scheduler;
SchedulingModule<IHost, SendData> m_send_data_scheduler;
};
Expand Down
16 changes: 16 additions & 0 deletions source/device/interfaces/i_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "i_processing_device.hpp"
#include "i_routing_device.hpp"

namespace sim {

class IDevice : public virtual IRoutingDevice, public virtual IProcessingDevice {
public:
virtual ~IDevice() = default;

// Returns true if the total number of packets in inlinks change from 0 to 1
virtual bool notify_about_arrival(Time arrival_time) = 0;
};

} // namespace sim
5 changes: 2 additions & 3 deletions source/device/interfaces/i_host.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once
#include "i_processing_device.hpp"
#include "i_routing_device.hpp"
#include "i_device.hpp"

namespace sim {

class IHost : public IRoutingDevice, public IProcessingDevice {
class IHost : public virtual IDevice {
public:
virtual ~IHost() = default;

Expand Down
3 changes: 0 additions & 3 deletions source/device/interfaces/i_routing_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class IRoutingDevice : public Identifiable {
virtual std::shared_ptr<ILink> get_link_to_destination(Packet packet) const = 0;
virtual std::shared_ptr<ILink> next_inlink() = 0;
virtual std::set<std::shared_ptr<ILink>>get_outlinks() = 0;

// Returns true if the total number of packets in inlinks change from 0 to 1
virtual bool notify_about_arrival(Time arrival_time) = 0;
};

} // namespace sim
6 changes: 2 additions & 4 deletions source/device/interfaces/i_switch.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once

#include "device/interfaces/i_routing_device.hpp"
#include "device/interfaces/i_processing_device.hpp"
#include "device/interfaces/i_device.hpp"

namespace sim {

class ISwitch : public IRoutingDevice,
public IProcessingDevice {
class ISwitch : public virtual IDevice {
public:
virtual ~ISwitch() = default;
};
Expand Down
16 changes: 11 additions & 5 deletions source/device/routing_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "link/i_link.hpp"
#include "logger/logger.hpp"
#include "utils/validation.hpp"

namespace sim {

Expand All @@ -15,6 +16,10 @@ RoutingModule::RoutingModule(Id a_id, std::unique_ptr<IHasher> a_hasher)
Id RoutingModule::get_id() const { return m_id; }

bool RoutingModule::add_inlink(std::shared_ptr<ILink> link) {
if (!is_valid_link(link)) {
return false;
}

if (m_id != link->get_to()->get_id()) {
LOG_WARN(
"Link destination device is incorrect (expected current device)");
Expand All @@ -33,6 +38,9 @@ bool RoutingModule::add_inlink(std::shared_ptr<ILink> link) {
}

bool RoutingModule::add_outlink(std::shared_ptr<ILink> link) {
if (!is_valid_link(link)) {
return false;
}
if (m_id != link->get_from()->get_id()) {
LOG_WARN("Outlink source is not our device");
return false;
Expand All @@ -48,6 +56,9 @@ bool RoutingModule::add_outlink(std::shared_ptr<ILink> link) {
bool RoutingModule::update_routing_table(Id dest_id,
std::shared_ptr<ILink> link,
size_t paths_count) {
if (!is_valid_link(link)) {
return false;
}
if (m_id != link->get_from()->get_id()) {
LOG_WARN("Link source device is incorrect (expected current device)");
return false;
Expand Down Expand Up @@ -127,9 +138,4 @@ void RoutingModule::correctify_outlinks() {
[](std::weak_ptr<ILink> link) { return link.expired(); });
}

bool RoutingModule::notify_about_arrival(Time arrival_time) {
(void)arrival_time;
return false;
};

} // namespace sim
5 changes: 2 additions & 3 deletions source/device/routing_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace sim {

class RoutingModule : public IRoutingDevice {
class RoutingModule : public virtual IRoutingDevice {
public:
RoutingModule(Id a_id = "", std::unique_ptr<IHasher> a_hasher = nullptr);
~RoutingModule() = default;
virtual ~RoutingModule() = default;

Id get_id() const final;
bool add_inlink(std::shared_ptr<ILink> link) final;
Expand All @@ -21,7 +21,6 @@ class RoutingModule : public IRoutingDevice {
std::shared_ptr<ILink> next_inlink() final;
std::shared_ptr<ILink> get_link_to_destination(Packet packet) const final;
std::set<std::shared_ptr<ILink>> get_outlinks() final;
bool notify_about_arrival(Time arrival_time) final;

void correctify_inlinks();
void correctify_outlinks();
Expand Down
37 changes: 1 addition & 36 deletions source/device/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,15 @@

#include <iostream>

#include "device/routing_module.hpp"
#include "logger/logger.hpp"
#include "utils/validation.hpp"

namespace sim {

Switch::Switch(Id a_id, ECN&& a_ecn)
: m_router(std::make_unique<RoutingModule>(a_id)),
: RoutingModule(a_id),
m_ecn(std::move(a_ecn)) {}

bool Switch::add_inlink(std::shared_ptr<ILink> link) {
if (!is_valid_link(link)) {
return false;
}
return m_router->add_inlink(link);
}

bool Switch::add_outlink(std::shared_ptr<ILink> link) {
if (!is_valid_link(link)) {
return false;
}
return m_router->add_outlink(link);
}

bool Switch::update_routing_table(Id dest_id, std::shared_ptr<ILink> link,
size_t paths_count) {
if (!is_valid_link(link)) {
return false;
}
return m_router->update_routing_table(dest_id, link, paths_count);
}

std::shared_ptr<ILink> Switch::next_inlink() { return m_router->next_inlink(); }

std::shared_ptr<ILink> Switch::get_link_to_destination(Packet packet) const {
return m_router->get_link_to_destination(packet);
}

bool Switch::notify_about_arrival(Time arrival_time) {
return m_process_scheduler.notify_about_arriving(arrival_time,
weak_from_this());
Expand Down Expand Up @@ -103,10 +74,4 @@ Time Switch::process() {
return total_processing_time;
}

std::set<std::shared_ptr<ILink>> Switch::get_outlinks() {
return m_router->get_outlinks();
}

Id Switch::get_id() const { return m_router->get_id(); }

} // namespace sim
13 changes: 2 additions & 11 deletions source/device/switch.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#pragma once

#include "device/routing_module.hpp"
#include "device/interfaces/i_switch.hpp"
#include "device/scheduling_module.hpp"
#include "ecn.hpp"
#include "event/process.hpp"

namespace sim {

class Switch : public ISwitch, public std::enable_shared_from_this<Switch> {
class Switch : public ISwitch, public RoutingModule, public std::enable_shared_from_this<Switch> {
public:
Switch(Id a_id, ECN&& a_ecn = ECN(1.0, 1.0, 0.0));
~Switch() = default;

bool add_inlink(std::shared_ptr<ILink> link) final;
bool add_outlink(std::shared_ptr<ILink> link) final;
bool update_routing_table(Id dest_id, std::shared_ptr<ILink> link,
size_t paths_count = 1) final;
std::shared_ptr<ILink> next_inlink() final;
std::shared_ptr<ILink> get_link_to_destination(Packet packet) const final;
std::set<std::shared_ptr<ILink>> get_outlinks() final;
bool notify_about_arrival(Time arrival_time) final;

DeviceType get_type() const final;
Expand All @@ -28,10 +22,7 @@ class Switch : public ISwitch, public std::enable_shared_from_this<Switch> {
// The iterator over ingress buffers is stored in m_next_link.
Time process() final;

Id get_id() const final;

private:
std::unique_ptr<IRoutingDevice> m_router;
SchedulingModule<ISwitch, Process> m_process_scheduler;
ECN m_ecn;
};
Expand Down
6 changes: 3 additions & 3 deletions source/link/i_link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <optional>

#include "device/interfaces/i_routing_device.hpp"
#include "device/interfaces/i_device.hpp"

namespace sim {

Expand All @@ -20,8 +20,8 @@ class ILink : public Identifiable {
virtual void schedule_arrival(Packet packet) = 0;

virtual std::optional<Packet> get_packet() = 0;
virtual std::shared_ptr<IRoutingDevice> get_from() const = 0;
virtual std::shared_ptr<IRoutingDevice> get_to() const = 0;
virtual std::shared_ptr<IDevice> get_from() const = 0;
virtual std::shared_ptr<IDevice> get_to() const = 0;

virtual Size get_max_from_egress_buffer_size() const = 0;
virtual Size get_from_egress_queue_size() const = 0;
Expand Down
8 changes: 4 additions & 4 deletions source/link/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace sim {

Link::Link(Id a_id, std::weak_ptr<IRoutingDevice> a_from,
std::weak_ptr<IRoutingDevice> a_to, std::uint32_t a_speed_gbps,
Link::Link(Id a_id, std::weak_ptr<IDevice> a_from,
std::weak_ptr<IDevice> a_to, std::uint32_t a_speed_gbps,
Time a_delay, Size a_max_from_egress_buffer_size,
Size a_max_to_ingress_buffer_size)
: m_id(a_id),
Expand Down Expand Up @@ -54,7 +54,7 @@ std::optional<Packet> Link::get_packet() {
return packet;
};

std::shared_ptr<IRoutingDevice> Link::get_from() const {
std::shared_ptr<IDevice> Link::get_from() const {
if (m_from.expired()) {
LOG_WARN("Source device pointer is expired");
return nullptr;
Expand All @@ -63,7 +63,7 @@ std::shared_ptr<IRoutingDevice> Link::get_from() const {
return m_from.lock();
};

std::shared_ptr<IRoutingDevice> Link::get_to() const {
std::shared_ptr<IDevice> Link::get_to() const {
if (m_to.expired()) {
LOG_WARN("Destination device pointer is expired");
return nullptr;
Expand Down
12 changes: 6 additions & 6 deletions source/link/link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace sim {

class Link : public ILink, public std::enable_shared_from_this<Link> {
public:
Link(Id a_id, std::weak_ptr<IRoutingDevice> a_from,
std::weak_ptr<IRoutingDevice> a_to, std::uint32_t a_speed_gbps = 1,
Link(Id a_id, std::weak_ptr<IDevice> a_from,
std::weak_ptr<IDevice> a_to, std::uint32_t a_speed_gbps = 1,
Time a_delay = 0, Size a_max_from_egress_buffer_size = 4096,
Size a_max_to_ingress_buffer_size = 4096);
~Link() = default;
Expand All @@ -20,8 +20,8 @@ class Link : public ILink, public std::enable_shared_from_this<Link> {

std::optional<Packet> get_packet() final;

std::shared_ptr<IRoutingDevice> get_from() const final;
std::shared_ptr<IRoutingDevice> get_to() const final;
std::shared_ptr<IDevice> get_from() const final;
std::shared_ptr<IDevice> get_to() const final;

Size get_from_egress_queue_size() const final;
Size get_max_from_egress_buffer_size() const final;
Expand Down Expand Up @@ -63,8 +63,8 @@ class Link : public ILink, public std::enable_shared_from_this<Link> {
void start_head_packet_sending();

Id m_id;
std::weak_ptr<IRoutingDevice> m_from;
std::weak_ptr<IRoutingDevice> m_to;
std::weak_ptr<IDevice> m_from;
std::weak_ptr<IDevice> m_to;
std::uint32_t m_speed_gbps;

Time m_propagation_delay;
Expand Down
4 changes: 2 additions & 2 deletions source/parser/identifiable_parser/parse_link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ std::shared_ptr<Link> Parser<Link>::parse_object(
Id from_id = value_node["from"].as<Id>();
Id to_id = value_node["to"].as<Id>();
auto from_ptr =
IdentifierFactory::get_instance().get_object<IRoutingDevice>(from_id);
IdentifierFactory::get_instance().get_object<IDevice>(from_id);
auto to_ptr =
IdentifierFactory::get_instance().get_object<IRoutingDevice>(to_id);
IdentifierFactory::get_instance().get_object<IDevice>(to_id);

if (from_ptr == nullptr) {
LOG_ERROR("Failed to find link's source");
Expand Down
Loading