blob: 9bfdfa7732c820e86019fd913180834c1884c0e5 [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
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010013#include "absl/types/optional.h"
14#include "rtc_base/thread.h"
15
minyue939df962017-04-19 01:58:38 -070016namespace webrtc {
17
18TestController::TestController(int min_port,
19 int max_port,
michaeltfcea39d2017-04-20 05:39:30 -070020 const std::string& config_file_path,
21 const std::string& log_file_path)
michaelt2fe9ac32017-04-20 06:56:27 -070022 : socket_factory_(rtc::ThreadManager::Instance()->WrapCurrentThread()),
23 config_file_path_(config_file_path),
michaeltfcea39d2017-04-20 05:39:30 -070024 packet_logger_(log_file_path),
minyue939df962017-04-19 01:58:38 -070025 local_test_done_(false),
26 remote_test_done_(false) {
27 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
minyue939df962017-04-19 01:58:38 -070028 packet_sender_checker_.Detach();
tschumimd4aebb02017-05-22 00:04:30 -070029 send_data_.fill(42);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010030 udp_socket_ =
minyue939df962017-04-19 01:58:38 -070031 std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket(
32 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port));
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010033 udp_socket_->SignalReadPacket.connect(this, &TestController::OnReadPacket);
minyue939df962017-04-19 01:58:38 -070034}
35
36void TestController::SendConnectTo(const std::string& hostname, int port) {
37 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010038 remote_address_ = rtc::SocketAddress(hostname, port);
minyue939df962017-04-19 01:58:38 -070039 NetworkTesterPacket packet;
40 packet.set_type(NetworkTesterPacket::HAND_SHAKING);
Danil Chapovalov431abd92018-06-18 12:54:17 +020041 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070042 rtc::CritScope scoped_lock(&local_test_done_lock_);
43 local_test_done_ = false;
44 remote_test_done_ = false;
45}
46
47void TestController::Run() {
48 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
49 rtc::Thread::Current()->ProcessMessages(0);
50}
51
52void TestController::SendData(const NetworkTesterPacket& packet,
Danil Chapovalov431abd92018-06-18 12:54:17 +020053 absl::optional<size_t> data_size) {
minyue939df962017-04-19 01:58:38 -070054 // Can be call from packet_sender or from test_controller thread.
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010055 size_t packet_size = packet.ByteSizeLong();
minyue939df962017-04-19 01:58:38 -070056 send_data_[0] = packet_size;
tschumimd4aebb02017-05-22 00:04:30 -070057 packet_size++;
minyue939df962017-04-19 01:58:38 -070058 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
59 if (data_size && *data_size > packet_size)
60 packet_size = *data_size;
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010061 udp_socket_->SendTo((const void*)send_data_.data(), packet_size,
62 remote_address_, rtc::PacketOptions());
minyue939df962017-04-19 01:58:38 -070063}
64
65void TestController::OnTestDone() {
66 RTC_DCHECK_CALLED_SEQUENTIALLY(&packet_sender_checker_);
67 NetworkTesterPacket packet;
68 packet.set_type(NetworkTesterPacket::TEST_DONE);
Danil Chapovalov431abd92018-06-18 12:54:17 +020069 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070070 rtc::CritScope scoped_lock(&local_test_done_lock_);
71 local_test_done_ = true;
72}
73
74bool TestController::IsTestDone() {
75 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
76 rtc::CritScope scoped_lock(&local_test_done_lock_);
77 return local_test_done_ && remote_test_done_;
78}
79
80void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
81 const char* data,
82 size_t len,
83 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010084 const int64_t& packet_time_us) {
minyue939df962017-04-19 01:58:38 -070085 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
86 size_t packet_size = data[0];
87 std::string receive_data(&data[1], packet_size);
88 NetworkTesterPacket packet;
89 packet.ParseFromString(receive_data);
90 RTC_CHECK(packet.has_type());
91 switch (packet.type()) {
92 case NetworkTesterPacket::HAND_SHAKING: {
93 NetworkTesterPacket packet;
94 packet.set_type(NetworkTesterPacket::TEST_START);
Jonas Olssoncfe3b6a2018-11-12 10:12:47 +010095 remote_address_ = remote_addr;
Danil Chapovalov431abd92018-06-18 12:54:17 +020096 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070097 packet_sender_.reset(new PacketSender(this, config_file_path_));
98 packet_sender_->StartSending();
99 rtc::CritScope scoped_lock(&local_test_done_lock_);
100 local_test_done_ = false;
101 remote_test_done_ = false;
102 break;
103 }
104 case NetworkTesterPacket::TEST_START: {
105 packet_sender_.reset(new PacketSender(this, config_file_path_));
106 packet_sender_->StartSending();
michaelt2fe9ac32017-04-20 06:56:27 -0700107 rtc::CritScope scoped_lock(&local_test_done_lock_);
108 local_test_done_ = false;
109 remote_test_done_ = false;
minyue939df962017-04-19 01:58:38 -0700110 break;
111 }
112 case NetworkTesterPacket::TEST_DATA: {
Niels Möllere6933812018-11-05 13:01:41 +0100113 packet.set_arrival_timestamp(packet_time_us);
minyue939df962017-04-19 01:58:38 -0700114 packet.set_packet_size(len);
michaeltfcea39d2017-04-20 05:39:30 -0700115 packet_logger_.LogPacket(packet);
minyue939df962017-04-19 01:58:38 -0700116 break;
117 }
118 case NetworkTesterPacket::TEST_DONE: {
119 remote_test_done_ = true;
120 break;
121 }
122 default: { RTC_NOTREACHED(); }
123 }
124}
125
126} // namespace webrtc