blob: 0049eb6a8301629fe5258d54eacc4abed376d0eb [file] [log] [blame]
Artem Titov7bf8c7f2019-03-15 15:00:37 +01001/*
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 API_TEST_NETWORK_EMULATION_MANAGER_H_
12#define API_TEST_NETWORK_EMULATION_MANAGER_H_
13
14#include <memory>
15#include <vector>
16
17#include "api/test/simulated_network.h"
18#include "rtc_base/network.h"
19#include "rtc_base/thread.h"
20
21namespace webrtc {
22
23// This API is still in development and can be changed without prior notice.
24
25// These classes are forward declared here, because they used as handles, to
26// make it possible for client code to operate with these abstractions and build
27// required network configuration. With forward declaration here implementation
28// is more readable, than with interfaces approach and cause user needn't any
29// API methods on these abstractions it is acceptable here.
30
31// EmulatedNetworkNode is an abstraction for some network in the real world,
32// like 3G network between peers, or Wi-Fi for one peer and LTE for another.
33// Multiple networks can be joined into chain emulating a network path from
34// one peer to another.
35class EmulatedNetworkNode;
36// EmulatedEndpoint is and abstraction for network interface on device.
37class EmulatedEndpoint;
38// EmulatedRoute is handle for single route from one network interface on one
39// peer device to another network interface on another peer device.
40class EmulatedRoute;
41
42struct EmulatedEndpointConfig {
43 enum class IpAddressFamily { kIpv4, kIpv6 };
44
45 IpAddressFamily generated_ip_family = IpAddressFamily::kIpv4;
46 // If specified will be used as IP address for endpoint node. Must be unique
47 // among all created nodes.
48 absl::optional<rtc::IPAddress> ip;
Artem Titove5cc85b2019-03-28 12:11:09 +010049 // Should endpoint be enabled or not, when it will be created.
50 // Enabled endpoints will be available for webrtc to send packets.
51 bool start_as_enabled = true;
52};
53
54// Provide interface to obtain all required objects to inject network emulation
55// layer into PeerConnection.
56class EmulatedNetworkManagerInterface {
57 public:
58 virtual ~EmulatedNetworkManagerInterface() = default;
59
60 virtual rtc::Thread* network_thread() = 0;
61 virtual rtc::NetworkManager* network_manager() = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +010062};
63
64// Provides an API for creating and configuring emulated network layer.
65// All objects returned by this API are owned by NetworkEmulationManager itself
66// and will be deleted when manager will be deleted.
67class NetworkEmulationManager {
68 public:
69 virtual ~NetworkEmulationManager() = default;
70
71 // Creates an emulated network node, which represents single network in
72 // the emulated network layer.
73 virtual EmulatedNetworkNode* CreateEmulatedNode(
74 std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;
75
76 // Creates an emulated endpoint, which represents single network interface on
77 // the peer's device.
78 virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0;
Artem Titove5cc85b2019-03-28 12:11:09 +010079 // Enable emulated endpoint to make it available for webrtc.
80 // Caller mustn't enable currently enabled endpoint.
81 virtual void EnableEndpoint(EmulatedEndpoint* endpoint) = 0;
82 // Disable emulated endpoint to make it unavailable for webrtc.
83 // Caller mustn't disable currently disabled endpoint.
84 virtual void DisableEndpoint(EmulatedEndpoint* endpoint) = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +010085
86 // Creates a route between endpoints going through specified network nodes.
87 // This route is single direction only and describe how traffic that was
88 // sent by network interface |from| have to be delivered to the network
89 // interface |to|. Return object can be used to remove created route.
90 //
91 // Assume there are endpoints E1, E2 and E3 and network nodes A, B, C and D.
92 // Also assume, that there is a route constructed via A, B and C like this:
93 // E1 -> A -> B -> C -> E2. In such case:
94 // * Caller mustn't use A, B and C in any route, that is leading to E2.
95 // * If caller will then create a new route E1 -> D -> E3, then first
96 // route will be corrupted, so if caller want to do this, first route
97 // should be deleted by ClearRoute(...) and then a new one should be
98 // created.
99 // * Caller can use A, B or C for any other routes.
100 // * Caller can create other routes leading to E2.
101 virtual EmulatedRoute* CreateRoute(
102 EmulatedEndpoint* from,
103 const std::vector<EmulatedNetworkNode*>& via_nodes,
104 EmulatedEndpoint* to) = 0;
105 // Removes route previously created by CreateRoute(...).
106 // Caller mustn't call this function with route, that have been already
107 // removed earlier.
108 virtual void ClearRoute(EmulatedRoute* route) = 0;
109
Artem Titove5cc85b2019-03-28 12:11:09 +0100110 // Creates EmulatedNetworkManagerInterface which can be used then to inject
111 // network emulation layer into PeerConnection. |endpoints| - are available
112 // network interfaces for PeerConnection. If endpoint is enabled, it will be
113 // immediately available for PeerConnection, otherwise user will be able to
114 // enable endpoint later to make it available for PeerConnection.
115 virtual EmulatedNetworkManagerInterface*
116 CreateEmulatedNetworkManagerInterface(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100117 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
118};
119
120} // namespace webrtc
121
122#endif // API_TEST_NETWORK_EMULATION_MANAGER_H_