blob: 52007860fdaad9b254fc4afb782cf453ea505201 [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)
michaelt2fe9ac32017-04-20 06:56:27 -070026 : socket_factory_(rtc::ThreadManager::Instance()->WrapCurrentThread()),
27 config_file_path_(config_file_path),
michaeltfcea39d2017-04-20 05:39:30 -070028 packet_logger_(log_file_path),
minyue939df962017-04-19 01:58:38 -070029 local_test_done_(false),
30 remote_test_done_(false) {
31 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
minyue939df962017-04-19 01:58:38 -070032 packet_sender_checker_.Detach();
tschumimd4aebb02017-05-22 00:04:30 -070033 send_data_.fill(42);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010034 udp_socket_ =
minyue939df962017-04-19 01:58:38 -070035 std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket(
36 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port));
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010037 udp_socket_->SignalReadPacket.connect(this, &TestController::OnReadPacket);
minyue939df962017-04-19 01:58:38 -070038}
39
40void TestController::SendConnectTo(const std::string& hostname, int port) {
41 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010042 remote_address_ = rtc::SocketAddress(hostname, port);
minyue939df962017-04-19 01:58:38 -070043 NetworkTesterPacket packet;
44 packet.set_type(NetworkTesterPacket::HAND_SHAKING);
Danil Chapovalov431abd92018-06-18 12:54:17 +020045 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070046 rtc::CritScope scoped_lock(&local_test_done_lock_);
47 local_test_done_ = false;
48 remote_test_done_ = false;
49}
50
51void TestController::Run() {
52 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
53 rtc::Thread::Current()->ProcessMessages(0);
54}
55
56void TestController::SendData(const NetworkTesterPacket& packet,
Danil Chapovalov431abd92018-06-18 12:54:17 +020057 absl::optional<size_t> data_size) {
minyue939df962017-04-19 01:58:38 -070058 // Can be call from packet_sender or from test_controller thread.
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010059 size_t packet_size = packet.ByteSizeLong();
minyue939df962017-04-19 01:58:38 -070060 send_data_[0] = packet_size;
tschumimd4aebb02017-05-22 00:04:30 -070061 packet_size++;
minyue939df962017-04-19 01:58:38 -070062 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
63 if (data_size && *data_size > packet_size)
64 packet_size = *data_size;
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010065 udp_socket_->SendTo((const void*)send_data_.data(), packet_size,
66 remote_address_, rtc::PacketOptions());
minyue939df962017-04-19 01:58:38 -070067}
68
69void TestController::OnTestDone() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020070 RTC_DCHECK_RUN_ON(&packet_sender_checker_);
minyue939df962017-04-19 01:58:38 -070071 NetworkTesterPacket packet;
72 packet.set_type(NetworkTesterPacket::TEST_DONE);
Danil Chapovalov431abd92018-06-18 12:54:17 +020073 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070074 rtc::CritScope scoped_lock(&local_test_done_lock_);
75 local_test_done_ = true;
76}
77
78bool TestController::IsTestDone() {
79 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
80 rtc::CritScope scoped_lock(&local_test_done_lock_);
81 return local_test_done_ && remote_test_done_;
82}
83
84void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
85 const char* data,
86 size_t len,
87 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010088 const int64_t& packet_time_us) {
minyue939df962017-04-19 01:58:38 -070089 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
90 size_t packet_size = data[0];
91 std::string receive_data(&data[1], packet_size);
92 NetworkTesterPacket packet;
93 packet.ParseFromString(receive_data);
94 RTC_CHECK(packet.has_type());
95 switch (packet.type()) {
96 case NetworkTesterPacket::HAND_SHAKING: {
97 NetworkTesterPacket packet;
98 packet.set_type(NetworkTesterPacket::TEST_START);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010099 remote_address_ = remote_addr;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200100 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -0700101 packet_sender_.reset(new PacketSender(this, config_file_path_));
102 packet_sender_->StartSending();
103 rtc::CritScope scoped_lock(&local_test_done_lock_);
104 local_test_done_ = false;
105 remote_test_done_ = false;
106 break;
107 }
108 case NetworkTesterPacket::TEST_START: {
109 packet_sender_.reset(new PacketSender(this, config_file_path_));
110 packet_sender_->StartSending();
michaelt2fe9ac32017-04-20 06:56:27 -0700111 rtc::CritScope scoped_lock(&local_test_done_lock_);
112 local_test_done_ = false;
113 remote_test_done_ = false;
minyue939df962017-04-19 01:58:38 -0700114 break;
115 }
116 case NetworkTesterPacket::TEST_DATA: {
Niels Möllere6933812018-11-05 13:01:41 +0100117 packet.set_arrival_timestamp(packet_time_us);
minyue939df962017-04-19 01:58:38 -0700118 packet.set_packet_size(len);
michaeltfcea39d2017-04-20 05:39:30 -0700119 packet_logger_.LogPacket(packet);
minyue939df962017-04-19 01:58:38 -0700120 break;
121 }
122 case NetworkTesterPacket::TEST_DONE: {
123 remote_test_done_ = true;
124 break;
125 }
126 default: { RTC_NOTREACHED(); }
127 }
128}
129
130} // namespace webrtc