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 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 12 | #include <memory> |
Sebastian Jansson | 8d3e4bd | 2019-07-31 18:33:17 +0200 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Sebastian Jansson | 8d3e4bd | 2019-07-31 18:33:17 +0200 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | namespace test { |
| 17 | |
| 18 | SimulatedNetworkNode::Builder::Builder() {} |
| 19 | |
| 20 | SimulatedNetworkNode::Builder::Builder(NetworkEmulationManager* net) |
| 21 | : net_(net) {} |
| 22 | |
| 23 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::config( |
| 24 | SimulatedNetwork::Config config) { |
| 25 | config_ = config; |
| 26 | return *this; |
| 27 | } |
| 28 | |
| 29 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::delay_ms( |
| 30 | int queue_delay_ms) { |
| 31 | config_.queue_delay_ms = queue_delay_ms; |
| 32 | return *this; |
| 33 | } |
| 34 | |
| 35 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::capacity_kbps( |
| 36 | int link_capacity_kbps) { |
| 37 | config_.link_capacity_kbps = link_capacity_kbps; |
| 38 | return *this; |
| 39 | } |
| 40 | |
| 41 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::capacity_Mbps( |
| 42 | int link_capacity_Mbps) { |
| 43 | config_.link_capacity_kbps = link_capacity_Mbps * 1000; |
| 44 | return *this; |
| 45 | } |
| 46 | |
| 47 | SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::loss( |
| 48 | double loss_rate) { |
| 49 | config_.loss_percent = std::round(loss_rate * 100); |
| 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | SimulatedNetworkNode SimulatedNetworkNode::Builder::Build() const { |
| 54 | RTC_DCHECK(net_); |
| 55 | return Build(net_); |
| 56 | } |
| 57 | |
| 58 | SimulatedNetworkNode SimulatedNetworkNode::Builder::Build( |
| 59 | NetworkEmulationManager* net) const { |
| 60 | SimulatedNetworkNode res; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 61 | auto behavior = std::make_unique<SimulatedNetwork>(config_); |
Sebastian Jansson | 8d3e4bd | 2019-07-31 18:33:17 +0200 | [diff] [blame] | 62 | res.simulation = behavior.get(); |
| 63 | res.node = net->CreateEmulatedNode(std::move(behavior)); |
| 64 | return res; |
| 65 | } |
| 66 | |
| 67 | } // namespace test |
| 68 | } // namespace webrtc |