xref: /aosp_15_r20/external/webrtc/test/network/emulated_network_manager.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_
12 #define TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_
13 
14 #include <functional>
15 #include <memory>
16 #include <vector>
17 
18 #include "api/sequence_checker.h"
19 #include "api/test/network_emulation_manager.h"
20 #include "api/test/time_controller.h"
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"
25 #include "test/network/network_emulation.h"
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:
35   EmulatedNetworkManager(TimeController* time_controller,
36                          TaskQueueForTest* task_queue,
37                          EndpointsContainer* endpoints_container);
38 
39   void EnableEndpoint(EmulatedEndpointImpl* endpoint);
40   void DisableEndpoint(EmulatedEndpointImpl* endpoint);
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 
47   // We don't support any address interfaces in the network emulation framework.
GetAnyAddressNetworks()48   std::vector<const rtc::Network*> GetAnyAddressNetworks() override {
49     return {};
50   }
51 
52   // EmulatedNetworkManagerInterface API
network_thread()53   rtc::Thread* network_thread() override { return network_thread_.get(); }
network_manager()54   rtc::NetworkManager* network_manager() override { return this; }
packet_socket_factory()55   rtc::PacketSocketFactory* packet_socket_factory() override {
56     return packet_socket_factory_.get();
57   }
endpoints()58   std::vector<EmulatedEndpoint*> endpoints() const override {
59     return endpoints_container_->GetEndpoints();
60   }
61   void GetStats(
62       std::function<void(EmulatedNetworkStats)> stats_callback) const override;
63 
64  private:
65   void UpdateNetworksOnce();
66   void MaybeSignalNetworksChanged();
67 
68   TaskQueueForTest* const task_queue_;
69   const EndpointsContainer* const endpoints_container_;
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<...>.
74   std::unique_ptr<rtc::Thread> network_thread_;
75   std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
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 
83 #endif  // TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_
84