blob: e5bd92efa93c6db1550548ab628875f259b5b081 [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
13namespace webrtc {
14
15TestController::TestController(int min_port,
16 int max_port,
michaeltfcea39d2017-04-20 05:39:30 -070017 const std::string& config_file_path,
18 const std::string& log_file_path)
michaelt2fe9ac32017-04-20 06:56:27 -070019 : socket_factory_(rtc::ThreadManager::Instance()->WrapCurrentThread()),
20 config_file_path_(config_file_path),
michaeltfcea39d2017-04-20 05:39:30 -070021 packet_logger_(log_file_path),
minyue939df962017-04-19 01:58:38 -070022 local_test_done_(false),
23 remote_test_done_(false) {
24 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
minyue939df962017-04-19 01:58:38 -070025 packet_sender_checker_.Detach();
tschumimd4aebb02017-05-22 00:04:30 -070026 send_data_.fill(42);
minyue939df962017-04-19 01:58:38 -070027 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
35void 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 Chapovalov431abd92018-06-18 12:54:17 +020040 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070041 rtc::CritScope scoped_lock(&local_test_done_lock_);
42 local_test_done_ = false;
43 remote_test_done_ = false;
44}
45
46void TestController::Run() {
47 RTC_DCHECK_RUN_ON(&test_controller_thread_checker_);
48 rtc::Thread::Current()->ProcessMessages(0);
49}
50
51void TestController::SendData(const NetworkTesterPacket& packet,
Danil Chapovalov431abd92018-06-18 12:54:17 +020052 absl::optional<size_t> data_size) {
minyue939df962017-04-19 01:58:38 -070053 // Can be call from packet_sender or from test_controller thread.
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010054 size_t packet_size = packet.ByteSizeLong();
minyue939df962017-04-19 01:58:38 -070055 send_data_[0] = packet_size;
tschumimd4aebb02017-05-22 00:04:30 -070056 packet_size++;
minyue939df962017-04-19 01:58:38 -070057 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
58 if (data_size && *data_size > packet_size)
59 packet_size = *data_size;
tschumimd4aebb02017-05-22 00:04:30 -070060 udp_transport_->SendPacket(send_data_.data(), packet_size,
minyue939df962017-04-19 01:58:38 -070061 rtc::PacketOptions(), 0);
62}
63
64void TestController::OnTestDone() {
65 RTC_DCHECK_CALLED_SEQUENTIALLY(&packet_sender_checker_);
66 NetworkTesterPacket packet;
67 packet.set_type(NetworkTesterPacket::TEST_DONE);
Danil Chapovalov431abd92018-06-18 12:54:17 +020068 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070069 rtc::CritScope scoped_lock(&local_test_done_lock_);
70 local_test_done_ = true;
71}
72
73bool 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
79void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
80 const char* data,
81 size_t len,
82 const rtc::SocketAddress& remote_addr,
83 const rtc::PacketTime& packet_time) {
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 Chapovalov431abd92018-06-18 12:54:17 +020095 SendData(packet, absl::nullopt);
minyue939df962017-04-19 01:58:38 -070096 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();
michaelt2fe9ac32017-04-20 06:56:27 -0700106 rtc::CritScope scoped_lock(&local_test_done_lock_);
107 local_test_done_ = false;
108 remote_test_done_ = false;
minyue939df962017-04-19 01:58:38 -0700109 break;
110 }
111 case NetworkTesterPacket::TEST_DATA: {
112 packet.set_arrival_timestamp(packet_time.timestamp);
113 packet.set_packet_size(len);
michaeltfcea39d2017-04-20 05:39:30 -0700114 packet_logger_.LogPacket(packet);
minyue939df962017-04-19 01:58:38 -0700115 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