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" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | |
| 21 | EmulatedNetworkManager::EmulatedNetworkManager( |
| 22 | Clock* clock, |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 23 | TaskQueueForTest* task_queue, |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 24 | EndpointsContainer* endpoints_container) |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 25 | : task_queue_(task_queue), |
| 26 | endpoints_container_(endpoints_container), |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 27 | socket_server_(clock, endpoints_container), |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 28 | network_thread_(&socket_server_), |
| 29 | sent_first_update_(false), |
| 30 | start_count_(0) { |
| 31 | network_thread_.SetName("net_thread", nullptr); |
| 32 | network_thread_.Start(); |
| 33 | } |
| 34 | |
| 35 | void EmulatedNetworkManager::EnableEndpoint(EmulatedEndpoint* endpoint) { |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 36 | RTC_CHECK(endpoints_container_->HasEndpoint(endpoint)) |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 37 | << "No such interface: " << endpoint->GetPeerLocalAddress().ToString(); |
| 38 | network_thread_.PostTask(RTC_FROM_HERE, [this, endpoint]() { |
| 39 | endpoint->Enable(); |
| 40 | UpdateNetworksOnce(); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | void EmulatedNetworkManager::DisableEndpoint(EmulatedEndpoint* endpoint) { |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 45 | RTC_CHECK(endpoints_container_->HasEndpoint(endpoint)) |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 46 | << "No such interface: " << endpoint->GetPeerLocalAddress().ToString(); |
| 47 | network_thread_.PostTask(RTC_FROM_HERE, [this, endpoint]() { |
| 48 | endpoint->Disable(); |
| 49 | UpdateNetworksOnce(); |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | // Network manager interface. All these methods are supposed to be called from |
| 54 | // the same thread. |
| 55 | void EmulatedNetworkManager::StartUpdating() { |
| 56 | RTC_DCHECK_RUN_ON(&network_thread_); |
| 57 | |
| 58 | if (start_count_) { |
| 59 | // If network interfaces are already discovered and signal is sent, |
| 60 | // we should trigger network signal immediately for the new clients |
| 61 | // to start allocating ports. |
| 62 | if (sent_first_update_) |
| 63 | network_thread_.PostTask(RTC_FROM_HERE, |
| 64 | [this]() { MaybeSignalNetworksChanged(); }); |
| 65 | } else { |
| 66 | network_thread_.PostTask(RTC_FROM_HERE, [this]() { UpdateNetworksOnce(); }); |
| 67 | } |
| 68 | ++start_count_; |
| 69 | } |
| 70 | |
| 71 | void EmulatedNetworkManager::StopUpdating() { |
| 72 | RTC_DCHECK_RUN_ON(&network_thread_); |
| 73 | if (!start_count_) |
| 74 | return; |
| 75 | |
| 76 | --start_count_; |
| 77 | if (!start_count_) { |
| 78 | sent_first_update_ = false; |
| 79 | } |
| 80 | } |
| 81 | |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 82 | void EmulatedNetworkManager::GetStats( |
| 83 | std::function<void(EmulatedNetworkStats)> stats_callback) const { |
| 84 | task_queue_->PostTask([stats_callback, this]() { |
| 85 | stats_callback(endpoints_container_->GetStats()); |
| 86 | }); |
| 87 | } |
| 88 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 89 | void EmulatedNetworkManager::UpdateNetworksOnce() { |
| 90 | RTC_DCHECK_RUN_ON(&network_thread_); |
| 91 | |
| 92 | std::vector<rtc::Network*> networks; |
| 93 | for (std::unique_ptr<rtc::Network>& net : |
Artem Titov | bdecb3c | 2019-03-29 15:20:23 +0100 | [diff] [blame] | 94 | endpoints_container_->GetEnabledNetworks()) { |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 95 | net->set_default_local_address_provider(this); |
| 96 | networks.push_back(net.release()); |
| 97 | } |
| 98 | |
| 99 | bool changed; |
| 100 | MergeNetworkList(networks, &changed); |
| 101 | if (changed || !sent_first_update_) { |
| 102 | MaybeSignalNetworksChanged(); |
| 103 | sent_first_update_ = true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void EmulatedNetworkManager::MaybeSignalNetworksChanged() { |
| 108 | RTC_DCHECK_RUN_ON(&network_thread_); |
| 109 | // If manager is stopped we don't need to signal anything. |
| 110 | if (start_count_ == 0) { |
| 111 | return; |
| 112 | } |
| 113 | SignalNetworksChanged(); |
| 114 | } |
| 115 | |
| 116 | } // namespace test |
| 117 | } // namespace webrtc |