minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_tools/network_tester/test_controller.h" |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 12 | |
Jonas Olsson | cfe3b6a | 2018-11-12 10:12:47 +0100 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
| 14 | #include "rtc_base/thread.h" |
| 15 | |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | |
| 18 | TestController::TestController(int min_port, |
| 19 | int max_port, |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 20 | const std::string& config_file_path, |
| 21 | const std::string& log_file_path) |
michaelt | 2fe9ac3 | 2017-04-20 06:56:27 -0700 | [diff] [blame] | 22 | : socket_factory_(rtc::ThreadManager::Instance()->WrapCurrentThread()), |
| 23 | config_file_path_(config_file_path), |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 24 | packet_logger_(log_file_path), |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 25 | local_test_done_(false), |
| 26 | remote_test_done_(false) { |
| 27 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 28 | packet_sender_checker_.Detach(); |
tschumim | d4aebb0 | 2017-05-22 00:04:30 -0700 | [diff] [blame] | 29 | send_data_.fill(42); |
Jonas Olsson | cfe3b6a | 2018-11-12 10:12:47 +0100 | [diff] [blame] | 30 | udp_socket_ = |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 31 | std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket( |
| 32 | rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port)); |
Jonas Olsson | cfe3b6a | 2018-11-12 10:12:47 +0100 | [diff] [blame] | 33 | udp_socket_->SignalReadPacket.connect(this, &TestController::OnReadPacket); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void TestController::SendConnectTo(const std::string& hostname, int port) { |
| 37 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
Jonas Olsson | cfe3b6a | 2018-11-12 10:12:47 +0100 | [diff] [blame] | 38 | remote_address_ = rtc::SocketAddress(hostname, port); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 39 | NetworkTesterPacket packet; |
| 40 | packet.set_type(NetworkTesterPacket::HAND_SHAKING); |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 41 | SendData(packet, absl::nullopt); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 42 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 43 | local_test_done_ = false; |
| 44 | remote_test_done_ = false; |
| 45 | } |
| 46 | |
| 47 | void TestController::Run() { |
| 48 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 49 | rtc::Thread::Current()->ProcessMessages(0); |
| 50 | } |
| 51 | |
| 52 | void TestController::SendData(const NetworkTesterPacket& packet, |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 53 | absl::optional<size_t> data_size) { |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 54 | // Can be call from packet_sender or from test_controller thread. |
Mirko Bonadei | 5b86f0a | 2017-11-29 15:20:26 +0100 | [diff] [blame] | 55 | size_t packet_size = packet.ByteSizeLong(); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 56 | send_data_[0] = packet_size; |
tschumim | d4aebb0 | 2017-05-22 00:04:30 -0700 | [diff] [blame] | 57 | packet_size++; |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 58 | packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max()); |
| 59 | if (data_size && *data_size > packet_size) |
| 60 | packet_size = *data_size; |
Jonas Olsson | cfe3b6a | 2018-11-12 10:12:47 +0100 | [diff] [blame] | 61 | udp_socket_->SendTo((const void*)send_data_.data(), packet_size, |
| 62 | remote_address_, rtc::PacketOptions()); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void TestController::OnTestDone() { |
| 66 | RTC_DCHECK_CALLED_SEQUENTIALLY(&packet_sender_checker_); |
| 67 | NetworkTesterPacket packet; |
| 68 | packet.set_type(NetworkTesterPacket::TEST_DONE); |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 69 | SendData(packet, absl::nullopt); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 70 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 71 | local_test_done_ = true; |
| 72 | } |
| 73 | |
| 74 | bool 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 | |
| 80 | void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 81 | const char* data, |
| 82 | size_t len, |
| 83 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 84 | const int64_t& packet_time_us) { |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 85 | 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 Olsson | cfe3b6a | 2018-11-12 10:12:47 +0100 | [diff] [blame] | 95 | remote_address_ = remote_addr; |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 96 | SendData(packet, absl::nullopt); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 97 | 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(); |
michaelt | 2fe9ac3 | 2017-04-20 06:56:27 -0700 | [diff] [blame] | 107 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 108 | local_test_done_ = false; |
| 109 | remote_test_done_ = false; |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | case NetworkTesterPacket::TEST_DATA: { |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 113 | packet.set_arrival_timestamp(packet_time_us); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 114 | packet.set_packet_size(len); |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 115 | packet_logger_.LogPacket(packet); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 116 | 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 |