blob: 3cb329ca4992b94afd42ad61a50ce6e10b79a1e8 [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;
49};
50
51// Provides an API for creating and configuring emulated network layer.
52// All objects returned by this API are owned by NetworkEmulationManager itself
53// and will be deleted when manager will be deleted.
54class NetworkEmulationManager {
55 public:
56 virtual ~NetworkEmulationManager() = default;
57
58 // Creates an emulated network node, which represents single network in
59 // the emulated network layer.
60 virtual EmulatedNetworkNode* CreateEmulatedNode(
61 std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;
62
63 // Creates an emulated endpoint, which represents single network interface on
64 // the peer's device.
65 virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0;
66
67 // Creates a route between endpoints going through specified network nodes.
68 // This route is single direction only and describe how traffic that was
69 // sent by network interface |from| have to be delivered to the network
70 // interface |to|. Return object can be used to remove created route.
71 //
72 // Assume there are endpoints E1, E2 and E3 and network nodes A, B, C and D.
73 // Also assume, that there is a route constructed via A, B and C like this:
74 // E1 -> A -> B -> C -> E2. In such case:
75 // * Caller mustn't use A, B and C in any route, that is leading to E2.
76 // * If caller will then create a new route E1 -> D -> E3, then first
77 // route will be corrupted, so if caller want to do this, first route
78 // should be deleted by ClearRoute(...) and then a new one should be
79 // created.
80 // * Caller can use A, B or C for any other routes.
81 // * Caller can create other routes leading to E2.
82 virtual EmulatedRoute* CreateRoute(
83 EmulatedEndpoint* from,
84 const std::vector<EmulatedNetworkNode*>& via_nodes,
85 EmulatedEndpoint* to) = 0;
86 // Removes route previously created by CreateRoute(...).
87 // Caller mustn't call this function with route, that have been already
88 // removed earlier.
89 virtual void ClearRoute(EmulatedRoute* route) = 0;
90
91 // Creates rtc::Thread that should be used as network thread for peer
92 // connection. Created thread contains special rtc::SocketServer inside it
93 // to enable correct integration between peer connection and emulated network
94 // layer.
95 virtual rtc::Thread* CreateNetworkThread(
96 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
97 // Creates rtc::NetworkManager that should be used inside
98 // cricket::PortAllocator for peer connection to provide correct list of
99 // network interfaces, that exists in emulated network later.
100 virtual rtc::NetworkManager* CreateNetworkManager(
101 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
102};
103
104} // namespace webrtc
105
106#endif // API_TEST_NETWORK_EMULATION_MANAGER_H_