blob: ec179b0ea65fbdd6cd07442bc5f51802914d17cb [file] [log] [blame]
Artem Titov40f51152019-01-04 15:45:01 +01001/*
2 * Copyright (c) 2018 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_SCENARIO_NETWORK_NETWORK_EMULATION_H_
12#define TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_
13
14#include <cstdint>
Artem Titov37d18482019-01-08 15:41:45 +010015#include <map>
Artem Titov40f51152019-01-04 15:45:01 +010016#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "absl/types/optional.h"
Artem Titov37d18482019-01-08 15:41:45 +010022#include "api/test/simulated_network.h"
Artem Titov40f51152019-01-04 15:45:01 +010023#include "api/units/timestamp.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/async_socket.h"
25#include "rtc_base/copy_on_write_buffer.h"
26#include "rtc_base/socket_address.h"
Artem Titov40f51152019-01-04 15:45:01 +010027#include "rtc_base/thread.h"
28
29namespace webrtc {
Artem Titov37d18482019-01-08 15:41:45 +010030namespace test {
Artem Titov40f51152019-01-04 15:45:01 +010031
32struct EmulatedIpPacket {
33 public:
34 EmulatedIpPacket(const rtc::SocketAddress& from,
35 const rtc::SocketAddress& to,
36 uint64_t dest_endpoint_id,
37 rtc::CopyOnWriteBuffer data,
38 Timestamp arrival_time);
39
40 ~EmulatedIpPacket();
41 // This object is not copyable or assignable.
42 EmulatedIpPacket(const EmulatedIpPacket&) = delete;
43 EmulatedIpPacket& operator=(const EmulatedIpPacket&) = delete;
44 // This object is only moveable.
45 EmulatedIpPacket(EmulatedIpPacket&&);
46 EmulatedIpPacket& operator=(EmulatedIpPacket&&);
47
48 size_t size() const { return data.size(); }
49 const uint8_t* cdata() const { return data.cdata(); }
50
51 rtc::SocketAddress from;
52 rtc::SocketAddress to;
53 uint64_t dest_endpoint_id;
54 rtc::CopyOnWriteBuffer data;
55 Timestamp arrival_time;
56};
57
58class EmulatedNetworkReceiverInterface {
59 public:
60 virtual ~EmulatedNetworkReceiverInterface() = default;
61
62 virtual void OnPacketReceived(EmulatedIpPacket packet) = 0;
63};
64
Artem Titov37d18482019-01-08 15:41:45 +010065// Represents node in the emulated network. Nodes can be connected with each
66// other to form different networks with different behavior. The behavior of
67// the node itself is determined by a concrete implementation of
68// NetworkBehaviorInterface that is provided on construction.
69class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface {
70 public:
71 // Creates node based on |network_behavior|. The specified |packet_overhead|
72 // is added to the size of each packet in the information provided to
73 // |network_behavior|.
74 EmulatedNetworkNode(
75 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
76 size_t packet_overhead = 0);
77 ~EmulatedNetworkNode() override;
78 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
79
80 void OnPacketReceived(EmulatedIpPacket packet) override;
81 void Process(Timestamp at_time);
82 void SetReceiver(uint64_t dest_endpoint_id,
83 EmulatedNetworkReceiverInterface* receiver);
84 void RemoveReceiver(uint64_t dest_endpoint_id);
85
86 // Creates a route for the given receiver_id over all the given nodes to the
87 // given receiver.
88 static void CreateRoute(uint64_t receiver_id,
89 std::vector<EmulatedNetworkNode*> nodes,
90 EmulatedNetworkReceiverInterface* receiver);
91 static void ClearRoute(uint64_t receiver_id,
92 std::vector<EmulatedNetworkNode*> nodes);
93
94 private:
95 struct StoredPacket {
96 uint64_t id;
97 EmulatedIpPacket packet;
98 bool removed;
99 };
100
101 rtc::CriticalSection lock_;
102 std::map<uint64_t, EmulatedNetworkReceiverInterface*> routing_
103 RTC_GUARDED_BY(lock_);
104 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
105 RTC_GUARDED_BY(lock_);
106 const size_t packet_overhead_ RTC_GUARDED_BY(lock_);
107 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(lock_);
108
109 uint64_t next_packet_id_ RTC_GUARDED_BY(lock_) = 1;
110};
111
112} // namespace test
Artem Titov40f51152019-01-04 15:45:01 +0100113} // namespace webrtc
114
115#endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_