OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <cinttypes> |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "net/quic/core/congestion_control/simulation/switch.h" |
| 10 |
| 11 using base::StringPrintf; |
| 12 |
| 13 namespace net { |
| 14 namespace simulation { |
| 15 |
| 16 Switch::Switch(Simulator* simulator, |
| 17 std::string name, |
| 18 SwitchPortNumber port_count, |
| 19 QuicByteCount queue_capacity) { |
| 20 for (size_t port_number = 1; port_number <= port_count; port_number++) { |
| 21 ports_.emplace_back( |
| 22 simulator, StringPrintf("%s (port %zu)", name.c_str(), port_number), |
| 23 this, port_number, queue_capacity); |
| 24 } |
| 25 } |
| 26 |
| 27 Switch::~Switch() {} |
| 28 |
| 29 Switch::Port::Port(Simulator* simulator, |
| 30 std::string name, |
| 31 Switch* parent, |
| 32 SwitchPortNumber port_number, |
| 33 QuicByteCount queue_capacity) |
| 34 : Endpoint(simulator, name), |
| 35 parent_(parent), |
| 36 port_number_(port_number), |
| 37 connected_(false), |
| 38 queue_(simulator, |
| 39 StringPrintf("%s (queue)", name.c_str()), |
| 40 queue_capacity) {} |
| 41 |
| 42 void Switch::Port::AcceptPacket(std::unique_ptr<Packet> packet) { |
| 43 parent_->DispatchPacket(port_number_, std::move(packet)); |
| 44 } |
| 45 |
| 46 void Switch::Port::EnqueuePacket(std::unique_ptr<Packet> packet) { |
| 47 queue_.AcceptPacket(std::move(packet)); |
| 48 } |
| 49 |
| 50 UnconstrainedPortInterface* Switch::Port::GetRxPort() { |
| 51 return this; |
| 52 } |
| 53 |
| 54 void Switch::Port::SetTxPort(ConstrainedPortInterface* port) { |
| 55 queue_.set_tx_port(port); |
| 56 connected_ = true; |
| 57 } |
| 58 |
| 59 void Switch::Port::Act() {} |
| 60 |
| 61 void Switch::DispatchPacket(SwitchPortNumber port_number, |
| 62 std::unique_ptr<Packet> packet) { |
| 63 Port* source_port = &ports_[port_number - 1]; |
| 64 const auto source_mapping_it = switching_table_.find(packet->source); |
| 65 if (source_mapping_it == switching_table_.end()) { |
| 66 switching_table_.insert(std::make_pair(packet->source, source_port)); |
| 67 } |
| 68 |
| 69 const auto destination_mapping_it = |
| 70 switching_table_.find(packet->destination); |
| 71 if (destination_mapping_it != switching_table_.end()) { |
| 72 destination_mapping_it->second->EnqueuePacket(std::move(packet)); |
| 73 return; |
| 74 } |
| 75 |
| 76 // If no mapping is available yet, broadcast the packet to all ports |
| 77 // different from the source. |
| 78 for (Port& egress_port : ports_) { |
| 79 if (!egress_port.connected()) { |
| 80 continue; |
| 81 } |
| 82 egress_port.EnqueuePacket(base::MakeUnique<Packet>(*packet)); |
| 83 } |
| 84 } |
| 85 |
| 86 } // namespace simulation |
| 87 } // namespace net |
OLD | NEW |