minyue | cb067fa | 2017-04-13 01:24:03 -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 | |
| 11 | #include "webrtc/tools/network_tester/test_controller.h" |
| 12 | |
| 13 | namespace webrtc { |
| 14 | |
| 15 | TestController::TestController(int min_port, |
| 16 | int max_port, |
| 17 | const std::string& config_file_path) |
| 18 | : config_file_path_(config_file_path), |
| 19 | local_test_done_(false), |
| 20 | remote_test_done_(false) { |
| 21 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 22 | send_data_.fill(42); |
| 23 | packet_sender_checker_.Detach(); |
| 24 | auto socket = |
| 25 | std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket( |
| 26 | rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port)); |
| 27 | socket->SignalReadPacket.connect(this, &TestController::OnReadPacket); |
| 28 | udp_transport_.reset( |
| 29 | new cricket::UdpTransport("network tester transport", std::move(socket))); |
| 30 | } |
| 31 | |
| 32 | void TestController::SendConnectTo(const std::string& hostname, int port) { |
| 33 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 34 | udp_transport_->SetRemoteAddress(rtc::SocketAddress(hostname, port)); |
| 35 | NetworkTesterPacket packet; |
| 36 | packet.set_type(NetworkTesterPacket::HAND_SHAKING); |
| 37 | SendData(packet, rtc::Optional<size_t>()); |
| 38 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 39 | local_test_done_ = false; |
| 40 | remote_test_done_ = false; |
| 41 | } |
| 42 | |
| 43 | void TestController::Run() { |
| 44 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 45 | rtc::Thread::Current()->ProcessMessages(0); |
| 46 | } |
| 47 | |
| 48 | void TestController::SendData(const NetworkTesterPacket& packet, |
| 49 | rtc::Optional<size_t> data_size) { |
| 50 | // Can be call from packet_sender or from test_controller thread. |
| 51 | size_t packet_size = packet.ByteSize(); |
| 52 | send_data_[0] = packet_size; |
| 53 | packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max()); |
| 54 | if (data_size && *data_size > packet_size) |
| 55 | packet_size = *data_size; |
| 56 | udp_transport_->SendPacket(send_data_.data(), packet_size + 1, |
| 57 | rtc::PacketOptions(), 0); |
| 58 | } |
| 59 | |
| 60 | void TestController::OnTestDone() { |
| 61 | RTC_DCHECK_CALLED_SEQUENTIALLY(&packet_sender_checker_); |
| 62 | NetworkTesterPacket packet; |
| 63 | packet.set_type(NetworkTesterPacket::TEST_DONE); |
| 64 | SendData(packet, rtc::Optional<size_t>()); |
| 65 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 66 | local_test_done_ = true; |
| 67 | } |
| 68 | |
| 69 | bool TestController::IsTestDone() { |
| 70 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 71 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 72 | return local_test_done_ && remote_test_done_; |
| 73 | } |
| 74 | |
| 75 | void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 76 | const char* data, |
| 77 | size_t len, |
| 78 | const rtc::SocketAddress& remote_addr, |
| 79 | const rtc::PacketTime& packet_time) { |
| 80 | RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); |
| 81 | size_t packet_size = data[0]; |
| 82 | std::string receive_data(&data[1], packet_size); |
| 83 | NetworkTesterPacket packet; |
| 84 | packet.ParseFromString(receive_data); |
| 85 | RTC_CHECK(packet.has_type()); |
| 86 | switch (packet.type()) { |
| 87 | case NetworkTesterPacket::HAND_SHAKING: { |
| 88 | NetworkTesterPacket packet; |
| 89 | packet.set_type(NetworkTesterPacket::TEST_START); |
| 90 | udp_transport_->SetRemoteAddress(remote_addr); |
| 91 | SendData(packet, rtc::Optional<size_t>()); |
| 92 | packet_sender_.reset(new PacketSender(this, config_file_path_)); |
| 93 | packet_sender_->StartSending(); |
| 94 | rtc::CritScope scoped_lock(&local_test_done_lock_); |
| 95 | local_test_done_ = false; |
| 96 | remote_test_done_ = false; |
| 97 | break; |
| 98 | } |
| 99 | case NetworkTesterPacket::TEST_START: { |
| 100 | packet_sender_.reset(new PacketSender(this, config_file_path_)); |
| 101 | packet_sender_->StartSending(); |
| 102 | break; |
| 103 | } |
| 104 | case NetworkTesterPacket::TEST_DATA: { |
| 105 | packet.set_arrival_timestamp(packet_time.timestamp); |
| 106 | packet.set_packet_size(len); |
| 107 | // log packet |
| 108 | break; |
| 109 | } |
| 110 | case NetworkTesterPacket::TEST_DONE: { |
| 111 | remote_test_done_ = true; |
| 112 | break; |
| 113 | } |
| 114 | default: { RTC_NOTREACHED(); } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } // namespace webrtc |