| Index: net/quic/core/congestion_control/simulation/switch.cc
|
| diff --git a/net/quic/core/congestion_control/simulation/switch.cc b/net/quic/core/congestion_control/simulation/switch.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ef0bfdafddea18d0a06ebbe9653b0f29bcb7cd27
|
| --- /dev/null
|
| +++ b/net/quic/core/congestion_control/simulation/switch.cc
|
| @@ -0,0 +1,80 @@
|
| +#include <cinttypes>
|
| +
|
| +#include "base/port.h"
|
| +#include "base/stringprintf.h"
|
| +#include "net/quic/core/congestion_control/simulation/switch.h"
|
| +#include "util/gtl/ptr_util.h"
|
| +
|
| +namespace net {
|
| +namespace simulation {
|
| +
|
| +Switch::Switch(Simulator* simulator,
|
| + std::string name,
|
| + SwitchPortNumber port_count,
|
| + QuicByteCount queue_capacity) {
|
| + for (size_t port_number = 1; port_number <= port_count; port_number++) {
|
| + ports_.emplace_back(simulator, StringPrintf("%s (port %" PRIuS ")",
|
| + name.c_str(), port_number),
|
| + this, port_number, queue_capacity);
|
| + }
|
| +}
|
| +
|
| +Switch::Port::Port(Simulator* simulator,
|
| + std::string name,
|
| + Switch* parent,
|
| + SwitchPortNumber port_number,
|
| + QuicByteCount queue_capacity)
|
| + : Endpoint(simulator, name),
|
| + parent_(parent),
|
| + port_number_(port_number),
|
| + connected_(false),
|
| + queue_(simulator,
|
| + StringPrintf("%s (queue)", name.c_str()),
|
| + queue_capacity) {}
|
| +
|
| +void Switch::Port::AcceptPacket(std::unique_ptr<Packet> packet) {
|
| + parent_->DispatchPacket(port_number_, std::move(packet));
|
| +}
|
| +
|
| +void Switch::Port::EnqueuePacket(std::unique_ptr<Packet> packet) {
|
| + queue_.AcceptPacket(std::move(packet));
|
| +}
|
| +
|
| +UnconstrainedPortInterface* Switch::Port::GetRxPort() {
|
| + return this;
|
| +}
|
| +
|
| +void Switch::Port::SetTxPort(ConstrainedPortInterface* port) {
|
| + queue_.set_tx_port(port);
|
| + connected_ = true;
|
| +}
|
| +
|
| +void Switch::Port::Act() {}
|
| +
|
| +void Switch::DispatchPacket(SwitchPortNumber port_number,
|
| + std::unique_ptr<Packet> packet) {
|
| + Port* source_port = &ports_[port_number - 1];
|
| + const auto source_mapping_it = switching_table_.find(packet->source);
|
| + if (source_mapping_it == switching_table_.end()) {
|
| + switching_table_.emplace(packet->source, source_port);
|
| + }
|
| +
|
| + const auto destination_mapping_it =
|
| + switching_table_.find(packet->destination);
|
| + if (destination_mapping_it != switching_table_.end()) {
|
| + destination_mapping_it->second->EnqueuePacket(std::move(packet));
|
| + return;
|
| + }
|
| +
|
| + // If no mapping is available yet, broadcast the packet to all ports
|
| + // different from the source.
|
| + for (Port& egress_port : ports_) {
|
| + if (!egress_port.connected()) {
|
| + continue;
|
| + }
|
| + egress_port.EnqueuePacket(gtl::MakeUnique<Packet>(*packet));
|
| + }
|
| +}
|
| +
|
| +} // namespace simulation
|
| +} // namespace net
|
|
|