blob: b409bce6d59127e34abeece107e6249786b6c22f [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
Artem Titovff393122019-04-05 11:19:52 +020089 // interface |to|. Return object can be used to remove created route. The
90 // route must contains at least one network node inside it.
Artem Titov7bf8c7f2019-03-15 15:00:37 +010091 //
Artem Titovff393122019-04-05 11:19:52 +020092 // Assume that E{0-9} are endpoints and N{0-9} are network nodes, then
93 // creation of the route have to follow these rules:
94 // 1. A route consists of a source endpoint, an ordered list of one or
95 // more network nodes, and a destination endpoint.
96 // 2. If (E1, ..., E2) is a route, then E1 != E2.
97 // In other words, the source and the destination may not be the same.
98 // 3. Given two simultaneously existing routes (E1, ..., E2) and
99 // (E3, ..., E4), either E1 != E3 or E2 != E4.
100 // In other words, there may be at most one route from any given source
101 // endpoint to any given destination endpoint.
102 // 4. Given two simultaneously existing routes (E1, ..., N1, ..., E2)
103 // and (E3, ..., N2, ..., E4), either N1 != N2 or E2 != E4.
104 // In other words, a network node may not belong to two routes that lead
105 // to the same destination endpoint.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100106 virtual EmulatedRoute* CreateRoute(
107 EmulatedEndpoint* from,
108 const std::vector<EmulatedNetworkNode*>& via_nodes,
109 EmulatedEndpoint* to) = 0;
110 // Removes route previously created by CreateRoute(...).
111 // Caller mustn't call this function with route, that have been already
112 // removed earlier.
113 virtual void ClearRoute(EmulatedRoute* route) = 0;
114
Artem Titove5cc85b2019-03-28 12:11:09 +0100115 // Creates EmulatedNetworkManagerInterface which can be used then to inject
116 // network emulation layer into PeerConnection. |endpoints| - are available
117 // network interfaces for PeerConnection. If endpoint is enabled, it will be
118 // immediately available for PeerConnection, otherwise user will be able to
119 // enable endpoint later to make it available for PeerConnection.
120 virtual EmulatedNetworkManagerInterface*
121 CreateEmulatedNetworkManagerInterface(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100122 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
123};
124
125} // namespace webrtc
126
127#endif // API_TEST_NETWORK_EMULATION_MANAGER_H_