Sebastian Jansson | 8d3e4bd | 2019-07-31 18:33:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "test/network/simulated_network_node.h" |
| 11 | |
| 12 | #include <utility> |
| 13 | |
| 14 | #include "absl/memory/memory.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
| 19 | SimulatedNetworkNode::Builder::Builder() {} |
| 20 | |
| 21 | SimulatedNetworkNode::Builder::Builder(NetworkEmulationManager* net) |
| 22 | : net_(net) {} |
| 23 | |
| 24 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::config( |
| 25 | SimulatedNetwork::Config config) { |
| 26 | config_ = config; |
| 27 | return *this; |
| 28 | } |
| 29 | |
| 30 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::delay_ms( |
| 31 | int queue_delay_ms) { |
| 32 | config_.queue_delay_ms = queue_delay_ms; |
| 33 | return *this; |
| 34 | } |
| 35 | |
| 36 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::capacity_kbps( |
| 37 | int link_capacity_kbps) { |
| 38 | config_.link_capacity_kbps = link_capacity_kbps; |
| 39 | return *this; |
| 40 | } |
| 41 | |
| 42 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::capacity_Mbps( |
| 43 | int link_capacity_Mbps) { |
| 44 | config_.link_capacity_kbps = link_capacity_Mbps * 1000; |
| 45 | return *this; |
| 46 | } |
| 47 | |
| 48 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::loss( |
| 49 | double loss_rate) { |
| 50 | config_.loss_percent = std::round(loss_rate * 100); |
| 51 | return *this; |
| 52 | } |
| 53 | |
| 54 | SimulatedNetworkNode SimulatedNetworkNode::Builder::Build() const { |
| 55 | RTC_DCHECK(net_); |
| 56 | return Build(net_); |
| 57 | } |
| 58 | |
| 59 | SimulatedNetworkNode SimulatedNetworkNode::Builder::Build( |
| 60 | NetworkEmulationManager* net) const { |
| 61 | SimulatedNetworkNode res; |
| 62 | auto behavior = absl::make_unique<SimulatedNetwork>(config_); |
| 63 | res.simulation = behavior.get(); |
| 64 | res.node = net->CreateEmulatedNode(std::move(behavior)); |
| 65 | return res; |
| 66 | } |
| 67 | |
| 68 | } // namespace test |
| 69 | } // namespace webrtc |