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 | |
| 13 | namespace webrtc { |
| 14 | |
| 15 | TestController::TestController(int min_port, |
| 16 | int max_port, |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 17 | const std::string& config_file_path, |
| 18 | const std::string& log_file_path) |
michaelt | 2fe9ac3 | 2017-04-20 06:56:27 -0700 | [diff] [blame] | 19 | : socket_factory_(rtc::ThreadManager::Instance()->WrapCurrentThread()), |
| 20 | config_file_path_(config_file_path), |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 21 | packet_logger_(log_file_path), |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 22 | local_test_done_(false), |
| 23 | remote_test_done_(false) { |
| 24 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 25 | packet_sender_checker_.Detach(); |
tschumim | d4aebb0 | 2017-05-22 00:04:30 -0700 | [diff] [blame] | 26 | send_data_.fill(42); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 27 | auto socket = |
| 28 | std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket( |
| 29 | rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port)); |
| 30 | socket->SignalReadPacket.connect(this, &TestController::OnReadPacket); |
| 31 | udp_transport_.reset( |
| 32 | new cricket::UdpTransport("network tester transport", std::move(socket))); |
| 33 | } |
| 34 | |
| 35 | void TestController::SendConnectTo(const std::string& hostname, int port) { |
| 36 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 37 | udp_transport_->SetRemoteAddress(rtc::SocketAddress(hostname, port)); |
| 38 | NetworkTesterPacket packet; |
| 39 | packet.set_type(NetworkTesterPacket::HAND_SHAKING); |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 40 | SendData(packet, absl::nullopt); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 41 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 42 | local_test_done_ = false; |
| 43 | remote_test_done_ = false; |
| 44 | } |
| 45 | |
| 46 | void TestController::Run() { |
| 47 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 48 | rtc::Thread::Current()->ProcessMessages(0); |
| 49 | } |
| 50 | |
| 51 | void TestController::SendData(const NetworkTesterPacket& packet, |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 52 | absl::optional<size_t> data_size) { |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 53 | // Can be call from packet_sender or from test_controller thread. |
Mirko Bonadei | 5b86f0a | 2017-11-29 15:20:26 +0100 | [diff] [blame] | 54 | size_t packet_size = packet.ByteSizeLong(); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 55 | send_data_[0] = packet_size; |
tschumim | d4aebb0 | 2017-05-22 00:04:30 -0700 | [diff] [blame] | 56 | packet_size++; |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 57 | packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max()); |
| 58 | if (data_size && *data_size > packet_size) |
| 59 | packet_size = *data_size; |
tschumim | d4aebb0 | 2017-05-22 00:04:30 -0700 | [diff] [blame] | 60 | udp_transport_->SendPacket(send_data_.data(), packet_size, |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 61 | rtc::PacketOptions(), 0); |
| 62 | } |
| 63 | |
| 64 | void TestController::OnTestDone() { |
| 65 | RTC_DCHECK_CALLED_SEQUENTIALLY(&packet_sender_checker_); |
| 66 | NetworkTesterPacket packet; |
| 67 | packet.set_type(NetworkTesterPacket::TEST_DONE); |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 68 | SendData(packet, absl::nullopt); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 69 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 70 | local_test_done_ = true; |
| 71 | } |
| 72 | |
| 73 | bool TestController::IsTestDone() { |
| 74 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 75 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 76 | return local_test_done_ && remote_test_done_; |
| 77 | } |
| 78 | |
| 79 | void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 80 | const char* data, |
| 81 | size_t len, |
| 82 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame^] | 83 | const int64_t& packet_time_us) { |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 84 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 85 | size_t packet_size = data[0]; |
| 86 | std::string receive_data(&data[1], packet_size); |
| 87 | NetworkTesterPacket packet; |
| 88 | packet.ParseFromString(receive_data); |
| 89 | RTC_CHECK(packet.has_type()); |
| 90 | switch (packet.type()) { |
| 91 | case NetworkTesterPacket::HAND_SHAKING: { |
| 92 | NetworkTesterPacket packet; |
| 93 | packet.set_type(NetworkTesterPacket::TEST_START); |
| 94 | udp_transport_->SetRemoteAddress(remote_addr); |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 95 | SendData(packet, absl::nullopt); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 96 | packet_sender_.reset(new PacketSender(this, config_file_path_)); |
| 97 | packet_sender_->StartSending(); |
| 98 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 99 | local_test_done_ = false; |
| 100 | remote_test_done_ = false; |
| 101 | break; |
| 102 | } |
| 103 | case NetworkTesterPacket::TEST_START: { |
| 104 | packet_sender_.reset(new PacketSender(this, config_file_path_)); |
| 105 | packet_sender_->StartSending(); |
michaelt | 2fe9ac3 | 2017-04-20 06:56:27 -0700 | [diff] [blame] | 106 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 107 | local_test_done_ = false; |
| 108 | remote_test_done_ = false; |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | case NetworkTesterPacket::TEST_DATA: { |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame^] | 112 | packet.set_arrival_timestamp(packet_time_us); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 113 | packet.set_packet_size(len); |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 114 | packet_logger_.LogPacket(packet); |
minyue | 939df96 | 2017-04-19 01:58:38 -0700 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | case NetworkTesterPacket::TEST_DONE: { |
| 118 | remote_test_done_ = true; |
| 119 | break; |
| 120 | } |
| 121 | default: { RTC_NOTREACHED(); } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } // namespace webrtc |