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 | #ifndef TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_ |
| 12 | #define TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_ |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 13 | |
Artem Titov | 3e0b65d | 2020-07-23 02:19:02 +0200 | [diff] [blame] | 14 | #include <functional> |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 15 | #include <memory> |
| 16 | #include <vector> |
| 17 | |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 18 | #include "api/sequence_checker.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 19 | #include "api/test/network_emulation_manager.h" |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 20 | #include "api/test/time_controller.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 21 | #include "rtc_base/ip_address.h" |
| 22 | #include "rtc_base/network.h" |
| 23 | #include "rtc_base/socket_server.h" |
| 24 | #include "rtc_base/thread.h" |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 25 | #include "test/network/network_emulation.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | |
| 30 | // Framework assumes that rtc::NetworkManager is called from network thread. |
| 31 | class EmulatedNetworkManager : public rtc::NetworkManagerBase, |
| 32 | public sigslot::has_slots<>, |
| 33 | public EmulatedNetworkManagerInterface { |
| 34 | public: |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 35 | EmulatedNetworkManager(TimeController* time_controller, |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 36 | TaskQueueForTest* task_queue, |
| 37 | EndpointsContainer* endpoints_container); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 38 | |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 39 | void EnableEndpoint(EmulatedEndpointImpl* endpoint); |
| 40 | void DisableEndpoint(EmulatedEndpointImpl* endpoint); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 41 | |
| 42 | // NetworkManager interface. All these methods are supposed to be called from |
| 43 | // the same thread. |
| 44 | void StartUpdating() override; |
| 45 | void StopUpdating() override; |
| 46 | |
Sebastian Jansson | f4cf4c7 | 2019-12-17 10:27:33 +0100 | [diff] [blame] | 47 | // We don't support any address interfaces in the network emulation framework. |
Niels Möller | 2221144 | 2022-04-07 11:43:28 +0200 | [diff] [blame] | 48 | std::vector<const rtc::Network*> GetAnyAddressNetworks() override { |
| 49 | return {}; |
| 50 | } |
Sebastian Jansson | f4cf4c7 | 2019-12-17 10:27:33 +0100 | [diff] [blame] | 51 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 52 | // EmulatedNetworkManagerInterface API |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 53 | rtc::Thread* network_thread() override { return network_thread_.get(); } |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 54 | rtc::NetworkManager* network_manager() override { return this; } |
Niels Möller | 5e7a3ae | 2021-09-09 15:54:42 +0200 | [diff] [blame] | 55 | rtc::PacketSocketFactory* packet_socket_factory() override { |
| 56 | return packet_socket_factory_.get(); |
| 57 | } |
Artem Titov | cf78128 | 2020-07-28 13:45:16 +0200 | [diff] [blame] | 58 | std::vector<EmulatedEndpoint*> endpoints() const override { |
Artem Titov | 5501cef | 2020-08-04 11:49:19 +0200 | [diff] [blame] | 59 | return endpoints_container_->GetEndpoints(); |
Artem Titov | cf78128 | 2020-07-28 13:45:16 +0200 | [diff] [blame] | 60 | } |
Artem Titov | 3e0b65d | 2020-07-23 02:19:02 +0200 | [diff] [blame] | 61 | void GetStats(std::function<void(std::unique_ptr<EmulatedNetworkStats>)> |
| 62 | stats_callback) const override; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | void UpdateNetworksOnce(); |
| 66 | void MaybeSignalNetworksChanged(); |
| 67 | |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 68 | TaskQueueForTest* const task_queue_; |
Artem Titov | 5501cef | 2020-08-04 11:49:19 +0200 | [diff] [blame] | 69 | const EndpointsContainer* const endpoints_container_; |
Niels Möller | 5e7a3ae | 2021-09-09 15:54:42 +0200 | [diff] [blame] | 70 | // The `network_thread_` must outlive `packet_socket_factory_`, because they |
| 71 | // both refer to a socket server that is owned by `network_thread_`. Both |
| 72 | // pointers are assigned only in the constructor, but the way they are |
| 73 | // initialized unfortunately doesn't work with const std::unique_ptr<...>. |
Sebastian Jansson | 2a92d2b | 2020-01-13 16:49:32 +0100 | [diff] [blame] | 74 | std::unique_ptr<rtc::Thread> network_thread_; |
Niels Möller | 5e7a3ae | 2021-09-09 15:54:42 +0200 | [diff] [blame] | 75 | std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 76 | bool sent_first_update_ RTC_GUARDED_BY(network_thread_); |
| 77 | int start_count_ RTC_GUARDED_BY(network_thread_); |
| 78 | }; |
| 79 | |
| 80 | } // namespace test |
| 81 | } // namespace webrtc |
| 82 | |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 83 | #endif // TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_ |