From 8d3e4bd9a6c5ca684e01e1daa1784a3bdaf9f125 Mon Sep 17 00:00:00 2001 From: Sebastian Jansson Date: Wed, 31 Jul 2019 18:33:17 +0200 Subject: [PATCH] Adds simulated network node builder. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:10839 Change-Id: I8fd7efc928e418bee3871c3870adb6d9061348fe Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/147274 Commit-Queue: Sebastian Jansson Reviewed-by: Erik Språng Cr-Commit-Position: refs/heads/master@{#28724} --- test/network/BUILD.gn | 2 + test/network/network_emulation_manager.cc | 4 ++ test/network/network_emulation_manager.h | 3 + test/network/simulated_network_node.cc | 69 +++++++++++++++++++++++ test/network/simulated_network_node.h | 42 ++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 test/network/simulated_network_node.cc create mode 100644 test/network/simulated_network_node.h diff --git a/test/network/BUILD.gn b/test/network/BUILD.gn index 92a53c7ac5..be372f1654 100644 --- a/test/network/BUILD.gn +++ b/test/network/BUILD.gn @@ -30,6 +30,8 @@ rtc_source_set("emulated_network") { "network_emulation.h", "network_emulation_manager.cc", "network_emulation_manager.h", + "simulated_network_node.cc", + "simulated_network_node.h", "traffic_route.cc", "traffic_route.h", ] diff --git a/test/network/network_emulation_manager.cc b/test/network/network_emulation_manager.cc index d2f6947c81..e0366798c3 100644 --- a/test/network/network_emulation_manager.cc +++ b/test/network/network_emulation_manager.cc @@ -87,6 +87,10 @@ EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode( return out; } +SimulatedNetworkNode::Builder NetworkEmulationManagerImpl::NodeBuilder() { + return SimulatedNetworkNode::Builder(this); +} + EmulatedEndpoint* NetworkEmulationManagerImpl::CreateEndpoint( EmulatedEndpointConfig config) { absl::optional ip = config.ip; diff --git a/test/network/network_emulation_manager.h b/test/network/network_emulation_manager.h index 118c701ac8..fbe8a23ca9 100644 --- a/test/network/network_emulation_manager.h +++ b/test/network/network_emulation_manager.h @@ -31,6 +31,7 @@ #include "test/network/emulated_network_manager.h" #include "test/network/fake_network_socket_server.h" #include "test/network/network_emulation.h" +#include "test/network/simulated_network_node.h" #include "test/network/traffic_route.h" #include "test/time_controller/time_controller.h" @@ -48,6 +49,8 @@ class NetworkEmulationManagerImpl : public NetworkEmulationManager { EmulatedNetworkNode* CreateEmulatedNode( std::unique_ptr network_behavior) override; + SimulatedNetworkNode::Builder NodeBuilder(); + EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) override; void EnableEndpoint(EmulatedEndpoint* endpoint) override; void DisableEndpoint(EmulatedEndpoint* endpoint) override; diff --git a/test/network/simulated_network_node.cc b/test/network/simulated_network_node.cc new file mode 100644 index 0000000000..e4c5d9091e --- /dev/null +++ b/test/network/simulated_network_node.cc @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#include "test/network/simulated_network_node.h" + +#include + +#include "absl/memory/memory.h" + +namespace webrtc { +namespace test { + +SimulatedNetworkNode::Builder::Builder() {} + +SimulatedNetworkNode::Builder::Builder(NetworkEmulationManager* net) + : net_(net) {} + +SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::config( + SimulatedNetwork::Config config) { + config_ = config; + return *this; +} + +SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::delay_ms( + int queue_delay_ms) { + config_.queue_delay_ms = queue_delay_ms; + return *this; +} + +SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::capacity_kbps( + int link_capacity_kbps) { + config_.link_capacity_kbps = link_capacity_kbps; + return *this; +} + +SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::capacity_Mbps( + int link_capacity_Mbps) { + config_.link_capacity_kbps = link_capacity_Mbps * 1000; + return *this; +} + +SimulatedNetworkNode::Builder& SimulatedNetworkNode::Builder::loss( + double loss_rate) { + config_.loss_percent = std::round(loss_rate * 100); + return *this; +} + +SimulatedNetworkNode SimulatedNetworkNode::Builder::Build() const { + RTC_DCHECK(net_); + return Build(net_); +} + +SimulatedNetworkNode SimulatedNetworkNode::Builder::Build( + NetworkEmulationManager* net) const { + SimulatedNetworkNode res; + auto behavior = absl::make_unique(config_); + res.simulation = behavior.get(); + res.node = net->CreateEmulatedNode(std::move(behavior)); + return res; +} + +} // namespace test +} // namespace webrtc diff --git a/test/network/simulated_network_node.h b/test/network/simulated_network_node.h new file mode 100644 index 0000000000..e374c59b75 --- /dev/null +++ b/test/network/simulated_network_node.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef TEST_NETWORK_SIMULATED_NETWORK_NODE_H_ +#define TEST_NETWORK_SIMULATED_NETWORK_NODE_H_ + +#include "api/test/network_emulation_manager.h" +#include "call/simulated_network.h" + +namespace webrtc { +namespace test { +// Helper struct to simplify creation of simulated network behaviors. +struct SimulatedNetworkNode { + SimulatedNetwork* simulation; + EmulatedNetworkNode* node; + class Builder { + public: + Builder(); + explicit Builder(NetworkEmulationManager* net); + Builder& config(SimulatedNetwork::Config config); + Builder& delay_ms(int queue_delay_ms); + Builder& capacity_kbps(int link_capacity_kbps); + Builder& capacity_Mbps(int link_capacity_Mbps); + Builder& loss(double loss_rate); + SimulatedNetworkNode Build() const; + SimulatedNetworkNode Build(NetworkEmulationManager* net) const; + + private: + NetworkEmulationManager* const net_ = nullptr; + SimulatedNetwork::Config config_; + }; +}; +} // namespace test +} // namespace webrtc + +#endif // TEST_NETWORK_SIMULATED_NETWORK_NODE_H_