blob: 9c0d3245336366951934adb737aac392ef0ff4af [file] [log] [blame]
minyue939df962017-04-19 01:58:38 -07001/*
2 * Copyright 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/network_tester/test_controller.h"
minyue939df962017-04-19 01:58:38 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <limits>
14
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010015#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/ip_address.h"
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010018#include "rtc_base/thread.h"
19
minyue939df962017-04-19 01:58:38 -070020namespace webrtc {
21
22TestController::TestController(int min_port,
23 int max_port,
michaeltfcea39d2017-04-20 05:39:30 -070024 const std::string& config_file_path,
25 const std::string& log_file_path)
Niels Möller9def9942021-09-07 09:16:49 +020026 // TODO(bugs.webrtc.org/13145): Add a SocketFactory argument.
27 : socket_factory_(
28 rtc::ThreadManager::Instance()->WrapCurrentThread()->socketserver()),
michaelt2fe9ac32017-04-20 06:56:27 -070029 config_file_path_(config_file_path),
michaeltfcea39d2017-04-20 05:39:30 -070030 packet_logger_(log_file_path),
minyue939df962017-04-19 01:58:38 -070031 local_test_done_(false),
32 remote_test_done_(false) {
33 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
minyue939df962017-04-19 01:58:38 -070034 packet_sender_checker_.Detach();
tschumimd4aebb02017-05-22 00:04:30 -070035 send_data_.fill(42);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010036 udp_socket_ =
minyue939df962017-04-19 01:58:38 -070037 std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket(
38 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port));
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010039 udp_socket_->SignalReadPacket.connect(this, &TestController::OnReadPacket);
minyue939df962017-04-19 01:58:38 -070040}
41
42void TestController::SendConnectTo(const std::string& hostname, int port) {
43 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010044 remote_address_ = rtc::SocketAddress(hostname, port);
minyue939df962017-04-19 01:58:38 -070045 NetworkTesterPacket packet;
46 packet.set_type(NetworkTesterPacket::HAND_SHAKING);
Danil Chapovalov431abd92018-06-18 12:54:17 +020047 SendData(packet, absl::nullopt);
Markus Handell85585f42020-07-08 23:04:37 +020048 MutexLock scoped_lock(&local_test_done_lock_);
minyue939df962017-04-19 01:58:38 -070049 local_test_done_ = false;
50 remote_test_done_ = false;
51}
52
53void TestController::Run() {
54 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
55 rtc::Thread::Current()->ProcessMessages(0);
56}
57
58void TestController::SendData(const NetworkTesterPacket& packet,
Danil Chapovalov431abd92018-06-18 12:54:17 +020059 absl::optional<size_t> data_size) {
minyue939df962017-04-19 01:58:38 -070060 // Can be call from packet_sender or from test_controller thread.
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010061 size_t packet_size = packet.ByteSizeLong();
minyue939df962017-04-19 01:58:38 -070062 send_data_[0] = packet_size;
tschumimd4aebb02017-05-22 00:04:30 -070063 packet_size++;
minyue939df962017-04-19 01:58:38 -070064 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
65 if (data_size && *data_size > packet_size)
66 packet_size = *data_size;
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010067 udp_socket_->SendTo((const void*)send_data_.data(), packet_size,
68 remote_address_, rtc::PacketOptions());
minyue939df962017-04-19 01:58:38 -070069}
70
71void TestController::OnTestDone() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020072 RTC_DCHECK_RUN_ON(&packet_sender_checker_);
minyue939df962017-04-19 01:58:38 -070073 NetworkTesterPacket packet;
74 packet.set_type(NetworkTesterPacket::TEST_DONE);
Danil Chapovalov431abd92018-06-18 12:54:17 +020075 SendData(packet, absl::nullopt);
Markus Handell85585f42020-07-08 23:04:37 +020076 MutexLock scoped_lock(&local_test_done_lock_);
minyue939df962017-04-19 01:58:38 -070077 local_test_done_ = true;
78}
79
80bool TestController::IsTestDone() {
81 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
Markus Handell85585f42020-07-08 23:04:37 +020082 MutexLock scoped_lock(&local_test_done_lock_);
minyue939df962017-04-19 01:58:38 -070083 return local_test_done_ && remote_test_done_;
84}
85
86void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
87 const char* data,
88 size_t len,
89 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010090 const int64_t& packet_time_us) {
minyue939df962017-04-19 01:58:38 -070091 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
92 size_t packet_size = data[0];
93 std::string receive_data(&data[1], packet_size);
94 NetworkTesterPacket packet;
95 packet.ParseFromString(receive_data);
96 RTC_CHECK(packet.has_type());
97 switch (packet.type()) {
98 case NetworkTesterPacket::HAND_SHAKING: {
99 NetworkTesterPacket packet;
100 packet.set_type(NetworkTesterPacket::TEST_START);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +0100101 remote_address_ = remote_addr;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200102 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -0700103 packet_sender_.reset(new PacketSender(this, config_file_path_));
104 packet_sender_->StartSending();
Markus Handell85585f42020-07-08 23:04:37 +0200105 MutexLock scoped_lock(&local_test_done_lock_);
minyue939df962017-04-19 01:58:38 -0700106 local_test_done_ = false;
107 remote_test_done_ = false;
108 break;
109 }
110 case NetworkTesterPacket::TEST_START: {
111 packet_sender_.reset(new PacketSender(this, config_file_path_));
112 packet_sender_->StartSending();
Markus Handell85585f42020-07-08 23:04:37 +0200113 MutexLock scoped_lock(&local_test_done_lock_);
michaelt2fe9ac32017-04-20 06:56:27 -0700114 local_test_done_ = false;
115 remote_test_done_ = false;
minyue939df962017-04-19 01:58:38 -0700116 break;
117 }
118 case NetworkTesterPacket::TEST_DATA: {
Niels Möllere6933812018-11-05 13:01:41 +0100119 packet.set_arrival_timestamp(packet_time_us);
minyue939df962017-04-19 01:58:38 -0700120 packet.set_packet_size(len);
michaeltfcea39d2017-04-20 05:39:30 -0700121 packet_logger_.LogPacket(packet);
minyue939df962017-04-19 01:58:38 -0700122 break;
123 }
124 case NetworkTesterPacket::TEST_DONE: {
125 remote_test_done_ = true;
126 break;
127 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200128 default: {
129 RTC_NOTREACHED();
130 }
minyue939df962017-04-19 01:58:38 -0700131 }
132}
133
134} // namespace webrtc