From fcea39d7ce3f1601d07fb34f04635be7f98eae25 Mon Sep 17 00:00:00 2001 From: michaelt Date: Thu, 20 Apr 2017 05:39:30 -0700 Subject: [PATCH] Add packet logger and server BUG=webrtc:7426 Review-Url: https://codereview.webrtc.org/2790513002 Cr-Commit-Position: refs/heads/master@{#17788} --- webrtc/tools/network_tester/BUILD.gn | 18 ++++++++ .../network_tester/network_tester_unittest.cc | 6 ++- webrtc/tools/network_tester/packet_logger.cc | 43 +++++++++++++++++ webrtc/tools/network_tester/packet_logger.h | 46 +++++++++++++++++++ webrtc/tools/network_tester/packet_sender.h | 1 + webrtc/tools/network_tester/server.cc | 19 ++++++++ .../tools/network_tester/test_controller.cc | 6 ++- webrtc/tools/network_tester/test_controller.h | 8 +++- 8 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 webrtc/tools/network_tester/packet_logger.cc create mode 100644 webrtc/tools/network_tester/packet_logger.h create mode 100644 webrtc/tools/network_tester/server.cc diff --git a/webrtc/tools/network_tester/BUILD.gn b/webrtc/tools/network_tester/BUILD.gn index 8b1fa85b65..1ba87ac957 100644 --- a/webrtc/tools/network_tester/BUILD.gn +++ b/webrtc/tools/network_tester/BUILD.gn @@ -28,6 +28,8 @@ if (rtc_enable_protobuf) { sources = [ "config_reader.cc", "config_reader.h", + "packet_logger.cc", + "packet_logger.h", "packet_sender.cc", "packet_sender.h", "test_controller.cc", @@ -39,6 +41,7 @@ if (rtc_enable_protobuf) { deps = [ ":network_tester_config_proto", ":network_tester_packet_proto", + "../../base:protobuf_utils", "../../base:rtc_task_queue", "../../p2p", ] @@ -93,4 +96,19 @@ if (rtc_enable_protobuf) { suppressed_configs += [ "//build/config/clang:find_bad_constructs" ] } } + + rtc_executable("network_tester_server") { + sources = [ + "server.cc", + ] + + deps = [ + ":network_tester", + ] + + if (!build_with_chromium && is_clang) { + # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163). + suppressed_configs += [ "//build/config/clang:find_bad_constructs" ] + } + } } diff --git a/webrtc/tools/network_tester/network_tester_unittest.cc b/webrtc/tools/network_tester/network_tester_unittest.cc index eccf7c19bd..fbb1bdb555 100644 --- a/webrtc/tools/network_tester/network_tester_unittest.cc +++ b/webrtc/tools/network_tester/network_tester_unittest.cc @@ -21,10 +21,12 @@ namespace webrtc { TEST(NetworkTesterTest, ServerClient) { TestController client( - 0, 0, webrtc::test::ResourcePath("network_tester/client_config", "dat")); + 0, 0, webrtc::test::ResourcePath("network_tester/client_config", "dat"), + webrtc::test::OutputPath() + "client_packet_log.dat"); TestController server( 9090, 9090, - webrtc::test::ResourcePath("network_tester/server_config", "dat")); + webrtc::test::ResourcePath("network_tester/server_config", "dat"), + webrtc::test::OutputPath() + "server_packet_log.dat"); client.SendConnectTo("127.0.0.1", 9090); EXPECT_TRUE_WAIT(server.IsTestDone() && client.IsTestDone(), 2000); } diff --git a/webrtc/tools/network_tester/packet_logger.cc b/webrtc/tools/network_tester/packet_logger.cc new file mode 100644 index 0000000000..d51c1b130a --- /dev/null +++ b/webrtc/tools/network_tester/packet_logger.cc @@ -0,0 +1,43 @@ +/* + * Copyright 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#include "webrtc/tools/network_tester/packet_logger.h" + +#include "webrtc/base/checks.h" +#include "webrtc/base/protobuf_utils.h" + +namespace webrtc { + +PacketLogger::PacketLogger(const std::string& log_file_path) + : packet_logger_stream_(log_file_path, + std::ios_base::out | std::ios_base::binary) { + RTC_DCHECK(packet_logger_stream_.is_open()); + RTC_DCHECK(packet_logger_stream_.good()); +} + +PacketLogger::~PacketLogger() = default; + +void PacketLogger::LogPacket(const NetworkTesterPacket& packet) { + // The protobuffer message will be saved in the following format to the file: + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | 1 byte | X byte | 1 byte | ... | X byte | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | Size of the next | proto | Size of the next | ... | proto | + // | proto message | message | proto message | | message | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + ProtoString packet_data; + packet.SerializeToString(&packet_data); + RTC_DCHECK_LE(packet_data.length(), 255); + RTC_DCHECK_GE(packet_data.length(), 0); + char proto_size = packet_data.length(); + packet_logger_stream_.write(&proto_size, sizeof(proto_size)); + packet_logger_stream_.write(packet_data.data(), packet_data.length()); +} + +} // namespace webrtc diff --git a/webrtc/tools/network_tester/packet_logger.h b/webrtc/tools/network_tester/packet_logger.h new file mode 100644 index 0000000000..35d6721903 --- /dev/null +++ b/webrtc/tools/network_tester/packet_logger.h @@ -0,0 +1,46 @@ +/* + * Copyright 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_TOOLS_NETWORK_TESTER_PACKET_LOGGER_H_ +#define WEBRTC_TOOLS_NETWORK_TESTER_PACKET_LOGGER_H_ + +#include +#include + +#include "webrtc/base/constructormagic.h" +#include "webrtc/base/ignore_wundef.h" + +#ifdef WEBRTC_NETWORK_TESTER_PROTO +RTC_PUSH_IGNORING_WUNDEF() +#include "webrtc/tools/network_tester/network_tester_packet.pb.h" +RTC_POP_IGNORING_WUNDEF() +using webrtc::network_tester::packet::NetworkTesterPacket; +#else +class NetworkTesterPacket; +#endif // WEBRTC_NETWORK_TESTER_PROTO + +namespace webrtc { + +class PacketLogger { + public: + explicit PacketLogger(const std::string& log_file_path); + ~PacketLogger(); + + void LogPacket(const NetworkTesterPacket& packet); + + private: + std::ofstream packet_logger_stream_; + + RTC_DISALLOW_COPY_AND_ASSIGN(PacketLogger); +}; + +} // namespace webrtc + +#endif // WEBRTC_TOOLS_NETWORK_TESTER_PACKET_LOGGER_H_ diff --git a/webrtc/tools/network_tester/packet_sender.h b/webrtc/tools/network_tester/packet_sender.h index bec9911f6f..0f1ae26161 100644 --- a/webrtc/tools/network_tester/packet_sender.h +++ b/webrtc/tools/network_tester/packet_sender.h @@ -14,6 +14,7 @@ #include #include +#include "webrtc/base/constructormagic.h" #include "webrtc/base/ignore_wundef.h" #include "webrtc/base/sequenced_task_checker.h" #include "webrtc/base/task_queue.h" diff --git a/webrtc/tools/network_tester/server.cc b/webrtc/tools/network_tester/server.cc new file mode 100644 index 0000000000..0983f1e169 --- /dev/null +++ b/webrtc/tools/network_tester/server.cc @@ -0,0 +1,19 @@ +/* + * Copyright 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/tools/network_tester/test_controller.h" + +int main() { + webrtc::TestController server(9090, 9090, "server_config.dat", + "server_packet_log.dat"); + while (!server.IsTestDone()) { + server.Run(); + } +} diff --git a/webrtc/tools/network_tester/test_controller.cc b/webrtc/tools/network_tester/test_controller.cc index 713a974995..44a16148b2 100644 --- a/webrtc/tools/network_tester/test_controller.cc +++ b/webrtc/tools/network_tester/test_controller.cc @@ -14,8 +14,10 @@ namespace webrtc { TestController::TestController(int min_port, int max_port, - const std::string& config_file_path) + const std::string& config_file_path, + const std::string& log_file_path) : config_file_path_(config_file_path), + packet_logger_(log_file_path), local_test_done_(false), remote_test_done_(false) { RTC_DCHECK_RUN_ON(&test_controller_thread_checker_); @@ -104,7 +106,7 @@ void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket, case NetworkTesterPacket::TEST_DATA: { packet.set_arrival_timestamp(packet_time.timestamp); packet.set_packet_size(len); - // log packet + packet_logger_.LogPacket(packet); break; } case NetworkTesterPacket::TEST_DONE: { diff --git a/webrtc/tools/network_tester/test_controller.h b/webrtc/tools/network_tester/test_controller.h index 5dae3eaf8b..d8f3463766 100644 --- a/webrtc/tools/network_tester/test_controller.h +++ b/webrtc/tools/network_tester/test_controller.h @@ -17,9 +17,11 @@ #include #include +#include "webrtc/base/constructormagic.h" #include "webrtc/base/ignore_wundef.h" #include "webrtc/p2p/base/basicpacketsocketfactory.h" #include "webrtc/p2p/base/udptransport.h" +#include "webrtc/tools/network_tester/packet_logger.h" #include "webrtc/tools/network_tester/packet_sender.h" #ifdef WEBRTC_NETWORK_TESTER_PROTO @@ -39,7 +41,8 @@ class TestController : public sigslot::has_slots<> { public: TestController(int min_port, int max_port, - const std::string& config_file_path); + const std::string& config_file_path, + const std::string& log_file_path); void Run(); @@ -62,12 +65,15 @@ class TestController : public sigslot::has_slots<> { rtc::SequencedTaskChecker packet_sender_checker_; rtc::BasicPacketSocketFactory socket_factory_; const std::string config_file_path_; + PacketLogger packet_logger_; rtc::CriticalSection local_test_done_lock_; bool local_test_done_ GUARDED_BY(local_test_done_lock_); bool remote_test_done_; std::array send_data_; std::unique_ptr udp_transport_; std::unique_ptr packet_sender_; + + RTC_DISALLOW_COPY_AND_ASSIGN(TestController); }; } // namespace webrtc