emulated_network_manager.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_
  11. #define TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_
  12. #include <functional>
  13. #include <memory>
  14. #include <vector>
  15. #include "api/test/network_emulation_manager.h"
  16. #include "api/test/time_controller.h"
  17. #include "rtc_base/ip_address.h"
  18. #include "rtc_base/network.h"
  19. #include "rtc_base/socket_server.h"
  20. #include "rtc_base/thread.h"
  21. #include "rtc_base/thread_checker.h"
  22. #include "test/network/network_emulation.h"
  23. namespace webrtc {
  24. namespace test {
  25. // Framework assumes that rtc::NetworkManager is called from network thread.
  26. class EmulatedNetworkManager : public rtc::NetworkManagerBase,
  27. public sigslot::has_slots<>,
  28. public EmulatedNetworkManagerInterface {
  29. public:
  30. EmulatedNetworkManager(TimeController* time_controller,
  31. TaskQueueForTest* task_queue,
  32. EndpointsContainer* endpoints_container);
  33. void EnableEndpoint(EmulatedEndpointImpl* endpoint);
  34. void DisableEndpoint(EmulatedEndpointImpl* endpoint);
  35. // NetworkManager interface. All these methods are supposed to be called from
  36. // the same thread.
  37. void StartUpdating() override;
  38. void StopUpdating() override;
  39. // We don't support any address interfaces in the network emulation framework.
  40. void GetAnyAddressNetworks(NetworkList* networks) override {}
  41. // EmulatedNetworkManagerInterface API
  42. rtc::Thread* network_thread() override { return network_thread_.get(); }
  43. rtc::NetworkManager* network_manager() override { return this; }
  44. std::vector<EmulatedEndpoint*> endpoints() const override {
  45. return endpoints_container_->GetEndpoints();
  46. }
  47. void GetStats(std::function<void(std::unique_ptr<EmulatedNetworkStats>)>
  48. stats_callback) const override;
  49. private:
  50. void UpdateNetworksOnce();
  51. void MaybeSignalNetworksChanged();
  52. TaskQueueForTest* const task_queue_;
  53. const EndpointsContainer* const endpoints_container_;
  54. std::unique_ptr<rtc::Thread> network_thread_;
  55. bool sent_first_update_ RTC_GUARDED_BY(network_thread_);
  56. int start_count_ RTC_GUARDED_BY(network_thread_);
  57. };
  58. } // namespace test
  59. } // namespace webrtc
  60. #endif // TEST_NETWORK_EMULATED_NETWORK_MANAGER_H_