Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [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 | |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 11 | #include "test/network/emulated_network_manager.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "absl/memory/memory.h" |
Niels Möller | 5e7a3ae | 2021-09-09 15:54:42 +0200 | [diff] [blame] | 17 | #include "p2p/base/basic_packet_socket_factory.h" |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 18 | #include "test/network/fake_network_socket_server.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace test { |
| 22 | |
| 23 | EmulatedNetworkManager::EmulatedNetworkManager( |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 24 | TimeController* time_controller, |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 25 | TaskQueueForTest* task_queue, |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 26 | EndpointsContainer* endpoints_container) |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 27 | : task_queue_(task_queue), |
| 28 | endpoints_container_(endpoints_container), |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 29 | sent_first_update_(false), |
Niels Möller | 5e7a3ae | 2021-09-09 15:54:42 +0200 | [diff] [blame] | 30 | start_count_(0) { |
| 31 | auto socket_server = |
| 32 | std::make_unique<FakeNetworkSocketServer>(endpoints_container); |
| 33 | packet_socket_factory_ = |
| 34 | std::make_unique<rtc::BasicPacketSocketFactory>(socket_server.get()); |
| 35 | // Since we pass ownership of the socket server to `network_thread_`, we must |
| 36 | // arrange that it outlives `packet_socket_factory_` which refers to it. |
| 37 | network_thread_ = |
| 38 | time_controller->CreateThread("net_thread", std::move(socket_server)); |
| 39 | } |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 40 | |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 41 | void EmulatedNetworkManager::EnableEndpoint(EmulatedEndpointImpl* endpoint) { |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 42 | RTC_CHECK(endpoints_container_->HasEndpoint(endpoint)) |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 43 | << "No such interface: " << endpoint->GetPeerLocalAddress().ToString(); |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame] | 44 | network_thread_->PostTask([this, endpoint]() { |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 45 | endpoint->Enable(); |
| 46 | UpdateNetworksOnce(); |
| 47 | }); |
| 48 | } |
| 49 | |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 50 | void EmulatedNetworkManager::DisableEndpoint(EmulatedEndpointImpl* endpoint) { |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 51 | RTC_CHECK(endpoints_container_->HasEndpoint(endpoint)) |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 52 | << "No such interface: " << endpoint->GetPeerLocalAddress().ToString(); |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame] | 53 | network_thread_->PostTask([this, endpoint]() { |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 54 | endpoint->Disable(); |
| 55 | UpdateNetworksOnce(); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | // Network manager interface. All these methods are supposed to be called from |
| 60 | // the same thread. |
| 61 | void EmulatedNetworkManager::StartUpdating() { |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 62 | RTC_DCHECK_RUN_ON(network_thread_.get()); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 63 | |
| 64 | if (start_count_) { |
| 65 | // If network interfaces are already discovered and signal is sent, |
| 66 | // we should trigger network signal immediately for the new clients |
| 67 | // to start allocating ports. |
| 68 | if (sent_first_update_) |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame] | 69 | network_thread_->PostTask([this]() { MaybeSignalNetworksChanged(); }); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 70 | } else { |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame] | 71 | network_thread_->PostTask([this]() { UpdateNetworksOnce(); }); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 72 | } |
| 73 | ++start_count_; |
| 74 | } |
| 75 | |
| 76 | void EmulatedNetworkManager::StopUpdating() { |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 77 | RTC_DCHECK_RUN_ON(network_thread_.get()); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 78 | if (!start_count_) |
| 79 | return; |
| 80 | |
| 81 | --start_count_; |
| 82 | if (!start_count_) { |
| 83 | sent_first_update_ = false; |
| 84 | } |
| 85 | } |
| 86 | |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 87 | void EmulatedNetworkManager::GetStats( |
Artem Titov | 3e0b65d | 2020-07-23 02:19:02 +0200 | [diff] [blame] | 88 | std::function<void(std::unique_ptr<EmulatedNetworkStats>)> stats_callback) |
| 89 | const { |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 90 | task_queue_->PostTask([stats_callback, this]() { |
| 91 | stats_callback(endpoints_container_->GetStats()); |
| 92 | }); |
| 93 | } |
| 94 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 95 | void EmulatedNetworkManager::UpdateNetworksOnce() { |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 96 | RTC_DCHECK_RUN_ON(network_thread_.get()); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 97 | |
Niels Möller | d959f3a | 2022-04-19 11:29:19 +0200 | [diff] [blame] | 98 | std::vector<std::unique_ptr<rtc::Network>> networks; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 99 | for (std::unique_ptr<rtc::Network>& net : |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 100 | endpoints_container_->GetEnabledNetworks()) { |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 101 | net->set_default_local_address_provider(this); |
Niels Möller | d959f3a | 2022-04-19 11:29:19 +0200 | [diff] [blame] | 102 | networks.push_back(std::move(net)); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool changed; |
Niels Möller | d959f3a | 2022-04-19 11:29:19 +0200 | [diff] [blame] | 106 | MergeNetworkList(std::move(networks), &changed); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 107 | if (changed || !sent_first_update_) { |
| 108 | MaybeSignalNetworksChanged(); |
| 109 | sent_first_update_ = true; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void EmulatedNetworkManager::MaybeSignalNetworksChanged() { |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 114 | RTC_DCHECK_RUN_ON(network_thread_.get()); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 115 | // If manager is stopped we don't need to signal anything. |
| 116 | if (start_count_ == 0) { |
| 117 | return; |
| 118 | } |
| 119 | SignalNetworksChanged(); |
| 120 | } |
| 121 | |
| 122 | } // namespace test |
| 123 | } // namespace webrtc |