summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/metrics/metrics_service.cc10
-rw-r--r--chrome/browser/metrics/metrics_service.h9
-rw-r--r--chrome/browser/net/network_stats.cc430
-rw-r--r--chrome/browser/net/network_stats.h221
-rw-r--r--chrome/browser/net/network_stats_unittest.cc99
-rw-r--r--chrome/chrome_browser.gypi2
-rw-r--r--chrome/chrome_tests.gypi1
-rw-r--r--chrome/installer/util/browser_distribution.cc4
-rw-r--r--chrome/installer/util/browser_distribution.h2
-rw-r--r--chrome/installer/util/chrome_frame_distribution.cc5
-rw-r--r--chrome/installer/util/chrome_frame_distribution.h2
-rw-r--r--chrome/installer/util/google_chrome_distribution.cc5
-rw-r--r--chrome/installer/util/google_chrome_distribution.h2
-rw-r--r--chrome/installer/util/google_chrome_distribution_dummy.cc5
-rw-r--r--net/base/test_data_stream.cc68
-rw-r--r--net/base/test_data_stream.h48
-rw-r--r--net/curvecp/test_client.h2
-rw-r--r--net/curvecp/test_data_stream.h88
-rw-r--r--net/curvecp/test_server.h2
-rw-r--r--net/net.gyp2
-rw-r--r--net/test/test_server.cc8
-rw-r--r--net/test/test_server.h2
-rwxr-xr-xnet/tools/testserver/testserver.py85
23 files changed, 1011 insertions, 91 deletions
diff --git a/chrome/browser/metrics/metrics_service.cc b/chrome/browser/metrics/metrics_service.cc
index 674f966..9243951 100644
--- a/chrome/browser/metrics/metrics_service.cc
+++ b/chrome/browser/metrics/metrics_service.cc
@@ -174,6 +174,7 @@
#include "chrome/browser/metrics/histogram_synchronizer.h"
#include "chrome/browser/metrics/metrics_log.h"
#include "chrome/browser/metrics/metrics_reporting_scheduler.h"
+#include "chrome/browser/net/network_stats.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
@@ -438,6 +439,7 @@ MetricsService::MetricsService()
reporting_active_(false),
state_(INITIALIZED),
current_fetch_(NULL),
+ io_thread_(NULL),
idle_since_last_transmission_(false),
next_window_id_(0),
ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)),
@@ -694,9 +696,12 @@ void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
void MetricsService::InitializeMetricsState() {
#if defined(OS_POSIX)
server_url_ = L"https://clients4.google.com/firefox/metrics/collect";
+ // TODO(rtenneti): Return the network stats server name.
+ network_stats_server_ = "";
#else
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
server_url_ = dist->GetStatsServerURL();
+ network_stats_server_ = dist->GetNetworkStatsServer();
#endif
PrefService* pref = g_browser_process->local_state();
@@ -792,6 +797,7 @@ void MetricsService::OnInitTaskComplete(
DCHECK(state_ == INIT_TASK_SCHEDULED);
hardware_class_ = hardware_class;
plugins_ = plugins;
+ io_thread_ = g_browser_process->io_thread();
if (state_ == INIT_TASK_SCHEDULED)
state_ = INIT_TASK_DONE;
}
@@ -1365,6 +1371,10 @@ void MetricsService::OnURLFetchComplete(const URLFetcher* source,
bool server_is_healthy = upload_succeeded || response_code == 400;
scheduler_->UploadFinished(server_is_healthy, unsent_logs());
+
+ // Collect network stats if UMA upload succeeded.
+ if (server_is_healthy && io_thread_)
+ chrome_browser_net::CollectNetworkStats(network_stats_server_, io_thread_);
}
void MetricsService::LogBadResponseCode() {
diff --git a/chrome/browser/metrics/metrics_service.h b/chrome/browser/metrics/metrics_service.h
index cb61682..5a031ed 100644
--- a/chrome/browser/metrics/metrics_service.h
+++ b/chrome/browser/metrics/metrics_service.h
@@ -16,6 +16,7 @@
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
+#include "chrome/browser/io_thread.h"
#include "chrome/common/metrics_helpers.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
@@ -360,6 +361,14 @@ class MetricsService : public NotificationObserver,
// The URL for the metrics server.
std::wstring server_url_;
+ // The TCP/UDP echo server to collect network connectivity stats.
+ std::string network_stats_server_;
+
+ // The IOThread for accessing global HostResolver to resolve
+ // network_stats_server_ host. |io_thread_| is accessed on IO thread and it is
+ // safe to access it on IO thread.
+ IOThread* io_thread_;
+
// The identifier that's sent to the server with the log reports.
std::string client_id_;
diff --git a/chrome/browser/net/network_stats.cc b/chrome/browser/net/network_stats.cc
new file mode 100644
index 0000000..b728351
--- /dev/null
+++ b/chrome/browser/net/network_stats.cc
@@ -0,0 +1,430 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/net/network_stats.h"
+
+#include "base/callback_old.h"
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/metrics/field_trial.h"
+#include "base/metrics/histogram.h"
+#include "base/task.h"
+#include "base/threading/platform_thread.h"
+#include "base/time.h"
+#include "base/tuple.h"
+#include "content/browser/browser_thread.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_util.h"
+#include "net/base/network_change_notifier.h"
+#include "net/base/sys_addrinfo.h"
+#include "net/base/test_completion_callback.h"
+#include "net/socket/tcp_client_socket.h"
+#include "net/udp/udp_client_socket.h"
+#include "net/udp/udp_server_socket.h"
+
+namespace chrome_browser_net {
+
+// This specifies the number of bytes to be sent to the TCP/UDP servers as part
+// of small packet size test.
+static const int kSmallTestBytesToSend = 100;
+
+// This specifies the number of bytes to be sent to the TCP/UDP servers as part
+// of large packet size test.
+static const int kLargeTestBytesToSend = 1200;
+
+// NetworkStats methods and members.
+NetworkStats::NetworkStats()
+ : bytes_to_read_(0),
+ bytes_to_send_(0),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ read_callback_(this, &NetworkStats::OnReadComplete)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ write_callback_(this, &NetworkStats::OnWriteComplete)),
+ finished_callback_(NULL),
+ start_time_(base::TimeTicks::Now()) {
+}
+
+NetworkStats::~NetworkStats() {
+ socket_.reset();
+}
+
+void NetworkStats::Initialize(int bytes_to_send,
+ net::CompletionCallback* finished_callback) {
+ DCHECK(bytes_to_send); // We should have data to send.
+
+ load_size_ = bytes_to_send;
+ bytes_to_send_ = bytes_to_send;
+ bytes_to_read_ = bytes_to_send;
+ finished_callback_ = finished_callback;
+}
+
+bool NetworkStats::DoStart(int result) {
+ if (result < 0) {
+ Finish(CONNECT_FAILED, result);
+ return false;
+ }
+
+ DCHECK(bytes_to_send_); // We should have data to send.
+
+ start_time_ = base::TimeTicks::Now();
+
+ int rv = SendData();
+ if (rv < 0) {
+ if (rv != net::ERR_IO_PENDING) {
+ Finish(WRITE_FAILED, rv);
+ return false;
+ }
+ }
+
+ stream_.Reset();
+ ReadData();
+
+ return true;
+}
+
+void NetworkStats::DoFinishCallback(int result) {
+ if (finished_callback_ != NULL) {
+ net::CompletionCallback* callback = finished_callback_;
+ finished_callback_ = NULL;
+ callback->Run(result);
+ }
+}
+
+void NetworkStats::set_socket(net::Socket* socket) {
+ DCHECK(socket);
+ DCHECK(!socket_.get());
+ socket_.reset(socket);
+}
+
+bool NetworkStats::ReadComplete(int result) {
+ DCHECK(socket_.get());
+ DCHECK_NE(net::ERR_IO_PENDING, result);
+ if (result < 0) {
+ Finish(READ_FAILED, result);
+ return true;
+ }
+
+ if (!stream_.VerifyBytes(read_buffer_->data(), result)) {
+ Finish(READ_VERIFY_FAILED, net::ERR_INVALID_RESPONSE);
+ return true;
+ }
+
+ read_buffer_ = NULL;
+ bytes_to_read_ -= result;
+
+ // No more data to read.
+ if (!bytes_to_read_) {
+ Finish(SUCCESS, net::OK);
+ return true;
+ }
+ ReadData();
+ return false;
+}
+
+void NetworkStats::OnReadComplete(int result) {
+ ReadComplete(result);
+}
+
+void NetworkStats::OnWriteComplete(int result) {
+ DCHECK(socket_.get());
+ DCHECK_NE(net::ERR_IO_PENDING, result);
+ if (result < 0) {
+ Finish(WRITE_FAILED, result);
+ return;
+ }
+
+ write_buffer_->DidConsume(result);
+ bytes_to_send_ -= result;
+ if (!write_buffer_->BytesRemaining())
+ write_buffer_ = NULL;
+
+ if (bytes_to_send_) {
+ int rv = SendData();
+ if (rv < 0) {
+ if (rv != net::ERR_IO_PENDING) {
+ Finish(WRITE_FAILED, rv);
+ return;
+ }
+ }
+ }
+}
+
+void NetworkStats::ReadData() {
+ DCHECK(!read_buffer_.get());
+ int kMaxMessage = 2048;
+
+ // We release the read_buffer_ in the destructor if there is an error.
+ read_buffer_ = new net::IOBuffer(kMaxMessage);
+
+ int rv;
+ do {
+ DCHECK(socket_.get());
+ rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_);
+ if (rv == net::ERR_IO_PENDING)
+ return;
+ if (ReadComplete(rv)) // Complete the read manually.
+ return;
+ } while (rv > 0);
+}
+
+int NetworkStats::SendData() {
+ DCHECK(bytes_to_send_); // We should have data to send.
+ do {
+ if (!write_buffer_.get()) {
+ scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(bytes_to_send_));
+ stream_.GetBytes(buffer->data(), bytes_to_send_);
+ write_buffer_ = new net::DrainableIOBuffer(buffer, bytes_to_send_);
+ }
+
+ DCHECK(socket_.get());
+ int rv = socket_->Write(write_buffer_,
+ write_buffer_->BytesRemaining(),
+ &write_callback_);
+ if (rv < 0)
+ return rv;
+ write_buffer_->DidConsume(rv);
+ bytes_to_send_ -= rv;
+ if (!write_buffer_->BytesRemaining())
+ write_buffer_ = NULL;
+ } while (bytes_to_send_);
+ return net::OK;
+}
+
+// UDPStatsClient methods and members.
+UDPStatsClient::UDPStatsClient()
+ : NetworkStats() {
+}
+
+UDPStatsClient::~UDPStatsClient() {
+}
+
+bool UDPStatsClient::Start(const std::string& ip_str,
+ int port,
+ int bytes_to_send,
+ net::CompletionCallback* finished_callback) {
+ DCHECK(port);
+ DCHECK(bytes_to_send); // We should have data to send.
+
+ Initialize(bytes_to_send, finished_callback);
+
+ net::IPAddressNumber ip_number;
+ if (!net::ParseIPLiteralToNumber(ip_str, &ip_number)) {
+ Finish(IP_STRING_PARSE_FAILED, net::ERR_INVALID_ARGUMENT);
+ return false;
+ }
+ net::IPEndPoint server_address = net::IPEndPoint(ip_number, port);
+
+ net::UDPClientSocket* udp_socket =
+ new net::UDPClientSocket(NULL, net::NetLog::Source());
+ DCHECK(udp_socket);
+ set_socket(udp_socket);
+
+ int rv = udp_socket->Connect(server_address);
+ return DoStart(rv);
+}
+
+void UDPStatsClient::Finish(Status status, int result) {
+ base::TimeDelta duration = base::TimeTicks::Now() - start_time();
+ if (load_size() == kSmallTestBytesToSend) {
+ if (result == net::OK)
+ UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Success.100B.RTT", duration);
+ else
+ UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Fail.100B.RTT", duration);
+
+ UMA_HISTOGRAM_ENUMERATION(
+ "NetConnectivity.UDP.Status.100B", status, STATUS_MAX);
+ } else {
+ if (result == net::OK)
+ UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Success.1K.RTT", duration);
+ else
+ UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Fail.1K.RTT", duration);
+
+ UMA_HISTOGRAM_ENUMERATION(
+ "NetConnectivity.UDP.Status.1K", status, STATUS_MAX);
+ }
+
+ DoFinishCallback(result);
+
+ // Close the socket so that there are no more IO operations.
+ net::UDPClientSocket* udp_socket =
+ static_cast<net::UDPClientSocket*>(socket());
+ if (udp_socket)
+ udp_socket->Close();
+
+ delete this;
+}
+
+// TCPStatsClient methods and members.
+TCPStatsClient::TCPStatsClient()
+ : NetworkStats(),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ resolve_callback_(this, &TCPStatsClient::OnResolveComplete)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ connect_callback_(this, &TCPStatsClient::OnConnectComplete)) {
+}
+
+TCPStatsClient::~TCPStatsClient() {
+}
+
+bool TCPStatsClient::Start(net::HostResolver* host_resolver,
+ const net::HostPortPair& server_host_port_pair,
+ int bytes_to_send,
+ net::CompletionCallback* finished_callback) {
+ DCHECK(bytes_to_send); // We should have data to send.
+
+ Initialize(bytes_to_send, finished_callback);
+
+ net::HostResolver::RequestInfo request(server_host_port_pair);
+ int rv = host_resolver->Resolve(request,
+ &addresses_,
+ &resolve_callback_,
+ NULL,
+ net::BoundNetLog());
+ if (rv == net::ERR_IO_PENDING)
+ return true;
+ return DoConnect(rv);
+}
+
+void TCPStatsClient::OnResolveComplete(int result) {
+ DoConnect(result);
+}
+
+bool TCPStatsClient::DoConnect(int result) {
+ if (result != net::OK) {
+ Finish(RESOLVE_FAILED, result);
+ return false;
+ }
+
+ net::TCPClientSocket* tcp_socket =
+ new net::TCPClientSocket(addresses_, NULL, net::NetLog::Source());
+ DCHECK(tcp_socket);
+ set_socket(tcp_socket);
+
+ int rv = tcp_socket->Connect(&connect_callback_);
+ if (rv == net::ERR_IO_PENDING)
+ return true;
+
+ return DoStart(rv);
+}
+
+void TCPStatsClient::OnConnectComplete(int result) {
+ DoStart(result);
+}
+
+void TCPStatsClient::Finish(Status status, int result) {
+ base::TimeDelta duration = base::TimeTicks::Now() - start_time();
+ if (load_size() == kSmallTestBytesToSend) {
+ if (result == net::OK)
+ UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Success.100B.RTT", duration);
+ else
+ UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Fail.100B.RTT", duration);
+
+ UMA_HISTOGRAM_ENUMERATION(
+ "NetConnectivity.TCP.Status.100B", status, STATUS_MAX);
+ } else {
+ if (result == net::OK)
+ UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Success.1K.RTT", duration);
+ else
+ UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Fail.1K.RTT", duration);
+
+ UMA_HISTOGRAM_ENUMERATION(
+ "NetConnectivity.TCP.Status.1K", status, STATUS_MAX);
+ }
+
+ DoFinishCallback(result);
+
+ // Disconnect the socket so that there are no more IO operations.
+ net::TCPClientSocket* tcp_socket =
+ static_cast<net::TCPClientSocket*>(socket());
+ if (tcp_socket)
+ tcp_socket->Disconnect();
+
+ delete this;
+}
+
+// static
+void CollectNetworkStats(const std::string& network_stats_server,
+ IOThread* io_thread) {
+ if (network_stats_server.empty())
+ return;
+
+ // If we are not on IO Thread, then post a task to call CollectNetworkStats on
+ // IO Thread.
+ if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ NewRunnableFunction(
+ &CollectNetworkStats, network_stats_server, io_thread));
+ return;
+ }
+
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ // Check that there is a network connection. We get called only if UMA upload
+ // to the server has succeeded.
+ DCHECK(!net::NetworkChangeNotifier::IsOffline());
+
+ static scoped_refptr<base::FieldTrial> trial = NULL;
+ static bool collect_stats = false;
+
+ if (!trial.get()) {
+ // Set up a field trial to collect network stats for UDP and TCP.
+ base::FieldTrial::Probability kDivisor = 1000;
+
+ // Enable the connectivity testing for 0.5% of the users.
+ base::FieldTrial::Probability kProbabilityPerGroup = 5;
+
+ // After October 30, 2011 builds, it will always be in default group
+ // (disable_network_stats).
+ trial = new base::FieldTrial("NetworkConnectivity", kDivisor,
+ "disable_network_stats", 2011, 10, 30);
+
+ // Add option to collect_stats for NetworkConnectivity.
+ int collect_stats_group = trial->AppendGroup("collect_stats",
+ kProbabilityPerGroup);
+ if (trial->group() == collect_stats_group)
+ collect_stats = true;
+ }
+
+ if (!collect_stats)
+ return;
+
+ // Run test kMaxNumberOfTests times.
+ const size_t kMaxNumberOfTests = INT_MAX;
+ static size_t number_of_tests_done = 0;
+ if (number_of_tests_done > kMaxNumberOfTests)
+ return;
+
+ ++number_of_tests_done;
+
+ // Use SPDY's UDP port per http://www.iana.org/assignments/port-numbers.
+ // |network_stats_server| echo TCP and UDP servers listen on the following
+ // ports.
+ uint32 kTCPTestingPort = 80;
+ uint32 kUDPTestingPort = 6121;
+
+ UDPStatsClient* small_udp_stats = new UDPStatsClient();
+ small_udp_stats->Start(
+ network_stats_server, kUDPTestingPort, kSmallTestBytesToSend, NULL);
+
+ UDPStatsClient* large_udp_stats = new UDPStatsClient();
+ large_udp_stats->Start(
+ network_stats_server, kUDPTestingPort, kLargeTestBytesToSend, NULL);
+
+ net::HostResolver* host_resolver = io_thread->globals()->host_resolver.get();
+ DCHECK(host_resolver);
+
+ net::HostPortPair server_address(network_stats_server, kTCPTestingPort);
+
+ TCPStatsClient* small_tcp_client = new TCPStatsClient();
+ small_tcp_client->Start(host_resolver, server_address, kSmallTestBytesToSend,
+ NULL);
+
+ TCPStatsClient* large_tcp_client = new TCPStatsClient();
+ large_tcp_client->Start(host_resolver, server_address, kLargeTestBytesToSend,
+ NULL);
+}
+
+} // namespace chrome_browser_net
diff --git a/chrome/browser/net/network_stats.h b/chrome/browser/net/network_stats.h
new file mode 100644
index 0000000..af95b39
--- /dev/null
+++ b/chrome/browser/net/network_stats.h
@@ -0,0 +1,221 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_
+#define CHROME_BROWSER_NET_NETWORK_STATS_H_
+#pragma once
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/memory/ref_counted.h"
+#include "base/scoped_ptr.h"
+#include "base/time.h"
+#include "chrome/browser/io_thread.h"
+#include "net/base/address_list.h"
+#include "net/base/completion_callback.h"
+#include "net/base/host_port_pair.h"
+#include "net/base/host_resolver.h"
+#include "net/base/io_buffer.h"
+#include "net/base/ip_endpoint.h"
+#include "net/base/test_data_stream.h"
+#include "net/socket/socket.h"
+
+namespace chrome_browser_net {
+
+// This class is used for live experiment of network connectivity (either TCP or
+// UDP) metrics. A small percentage of users participate in this experiment. All
+// users (who are in the experiment) must have enabled "UMA upload".
+//
+// This class collects the following stats from users who have opted in.
+// a) What percentage of users can get a message end-to-end to a UDP server?
+// b) What percentage of users can get a message end-to-end to a TCP server?
+// c) What is the latency for UDP and TCP.
+// d) If connectivity failed, at what stage (Connect or Write or Read) did it
+// fail?
+
+class NetworkStats {
+ public:
+ enum Status { // Used in HISTOGRAM_ENUMERATION.
+ SUCCESS, // Successfully received bytes from the server.
+ IP_STRING_PARSE_FAILED, // Parsing of IP string failed.
+ RESOLVE_FAILED, // Host resolution failed.
+ CONNECT_FAILED, // Connection to the server failed.
+ WRITE_FAILED, // Sending an echo message to the server failed.
+ READ_FAILED, // Reading the reply from the server failed.
+ READ_VERIFY_FAILED, // Verification of data failed.
+ STATUS_MAX, // Bounding value.
+ };
+
+ protected:
+ // Constructs an NetworkStats object that collects metrics for network
+ // connectivity (either TCP or UDP).
+ NetworkStats();
+ virtual ~NetworkStats();
+
+ // Initializes |finished_callback_| and the number of bytes to send to the
+ // server. |finished_callback| is called when we are done with the test.
+ // |finished_callback| is mainly useful for unittests.
+ void Initialize(int bytes_to_send,
+ net::CompletionCallback* finished_callback);
+
+ // This method is called after socket connection is completed. It will send
+ // |bytes_to_send| bytes to |server| by calling SendData(). After successfully
+ // sending data to the |server|, it calls ReadData() to read/verify the data
+ // from the |server|. Returns true if successful.
+ bool DoStart(int result);
+
+ // Collects network connectivity stats. This is called when all the data from
+ // server is read or when there is a failure during connect/read/write.
+ virtual void Finish(Status status, int result) {}
+
+ // This method is called from Finish() and calls |finished_callback_| callback
+ // to indicate that the test has finished.
+ void DoFinishCallback(int result);
+
+ // Returns the number of bytes to be sent to the |server|.
+ int load_size() const { return load_size_; }
+
+ // Helper methods to get and set |socket_|.
+ net::Socket* socket() { return socket_.get(); }
+ void set_socket(net::Socket* socket);
+
+ // Returns |start_time_| (used by histograms).
+ base::TimeTicks start_time() const { return start_time_; }
+
+ private:
+ // Verifies the data and calls Finish() if there is an error or if all bytes
+ // are read. Returns true if Finish() is called otherwise returns false.
+ bool ReadComplete(int result);
+
+ // Callbacks when an internal IO is completed.
+ void OnReadComplete(int result);
+ void OnWriteComplete(int result);
+
+ // Reads data from server until an error occurs.
+ void ReadData();
+
+ // Sends data to server until an error occurs.
+ int SendData();
+
+ // The socket handle for this session.
+ scoped_ptr<net::Socket> socket_;
+
+ // The read buffer used to read data from the socket.
+ scoped_refptr<net::IOBuffer> read_buffer_;
+
+ // The write buffer used to write data to the socket.
+ scoped_refptr<net::DrainableIOBuffer> write_buffer_;
+
+ // Some counters for the session.
+ int load_size_;
+ int bytes_to_read_;
+ int bytes_to_send_;
+
+ // |stream_| is used to generate data to be sent to the server and it is also
+ // used to verify the data received from the server.
+ net::TestDataStream stream_;
+
+ // Callback to call when data is read from the server.
+ net::CompletionCallbackImpl<NetworkStats> read_callback_;
+
+ // Callback to call when data is sent to the server.
+ net::CompletionCallbackImpl<NetworkStats> write_callback_;
+
+ // Callback to call when echo protocol is successefully finished or whenever
+ // there is an error (this allows unittests to wait until echo protocol's
+ // round trip is finished).
+ net::CompletionCallback* finished_callback_;
+
+ // The time when the session was started.
+ base::TimeTicks start_time_;
+};
+
+class UDPStatsClient : public NetworkStats {
+ public:
+ // Constructs an UDPStatsClient object that collects metrics for UDP
+ // connectivity.
+ UDPStatsClient();
+ virtual ~UDPStatsClient();
+
+ // Starts the client, connecting to |server|.
+ // Client will send |bytes_to_send| bytes to |server|.
+ // When client has received all echoed bytes from the server, or
+ // when an error occurs causing the client to stop, |Finish| will be
+ // called with a net status code.
+ // |Finish| will collect histogram stats.
+ // Returns true if successful in starting the client.
+ bool Start(const std::string& ip_str,
+ int port,
+ int bytes_to_send,
+ net::CompletionCallback* callback);
+
+ protected:
+ // Allow tests to access our innards for testing purposes.
+ friend class NetworkStatsTestUDP;
+
+ // Collects stats for UDP connectivity. This is called when all the data from
+ // server is read or when there is a failure during connect/read/write.
+ virtual void Finish(Status status, int result);
+};
+
+class TCPStatsClient : public NetworkStats {
+ public:
+ // Constructs a TCPStatsClient object that collects metrics for TCP
+ // connectivity.
+ TCPStatsClient();
+ virtual ~TCPStatsClient();
+
+ // Starts the client, connecting to |server|.
+ // Client will send |bytes_to_send| bytes.
+ // When the client has received all echoed bytes from the server, or
+ // when an error occurs causing the client to stop, |Finish| will be
+ // called with a net status code.
+ // |Finish| will collect histogram stats.
+ // Returns true if successful in starting the client.
+ bool Start(net::HostResolver* host_resolver,
+ const net::HostPortPair& server,
+ int bytes_to_send,
+ net::CompletionCallback* callback);
+
+ protected:
+ // Allow tests to access our innards for testing purposes.
+ friend class NetworkStatsTestTCP;
+
+ // Collects stats for TCP connectivity. This is called when all the data from
+ // server is read or when there is a failure during connect/read/write.
+ virtual void Finish(Status status, int result);
+
+ private:
+ // Callback that is called when host resolution is completed.
+ void OnResolveComplete(int result);
+
+ // Called after host is resolved. Creates TCPClientSocket and connects to the
+ // server.
+ bool DoConnect(int result);
+
+ // Callback that is called when connect is completed and calls DoStart() to
+ // start the echo protocl.
+ void OnConnectComplete(int result);
+
+ // Callback to call when host resolution is completed.
+ net::CompletionCallbackImpl<TCPStatsClient> resolve_callback_;
+
+ // Callback to call when connect is completed.
+ net::CompletionCallbackImpl<TCPStatsClient> connect_callback_;
+
+ // HostResolver fills out the |addresses_| after host resolution is completed.
+ net::AddressList addresses_;
+};
+
+// This collects the network connectivity stats for UDP and TCP for small
+// percentage of users who are participating in the experiment. All users must
+// have enabled "UMA upload". This method gets called only if UMA upload to the
+// server has succeeded.
+void CollectNetworkStats(const std::string& network_stats_server_url,
+ IOThread* io_thread);
+
+} // namespace chrome_browser_net
+
+#endif // CHROME_BROWSER_NET_NETWORK_STATS_H_
diff --git a/chrome/browser/net/network_stats_unittest.cc b/chrome/browser/net/network_stats_unittest.cc
new file mode 100644
index 0000000..9251d46
--- /dev/null
+++ b/chrome/browser/net/network_stats_unittest.cc
@@ -0,0 +1,99 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/basictypes.h"
+#include "base/message_loop.h"
+#include "chrome/browser/net/network_stats.h"
+#include "net/base/host_resolver.h"
+#include "net/base/mock_host_resolver.h"
+#include "net/base/test_completion_callback.h"
+#include "net/test/test_server.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace chrome_browser_net {
+
+class NetworkStatsTest : public PlatformTest {
+ public:
+ NetworkStatsTest() {}
+ protected:
+ virtual void TearDown() {
+ // Flush the message loop to make Purify happy.
+ message_loop_.RunAllPending();
+ }
+ MessageLoopForIO message_loop_;
+};
+
+class NetworkStatsTestUDP : public NetworkStatsTest {
+ public:
+ NetworkStatsTestUDP()
+ : test_server_(net::TestServer::TYPE_UDP_ECHO,
+ FilePath(FILE_PATH_LITERAL("net/data"))) {
+ }
+
+ protected:
+ void RunUDPEchoTest(int bytes) {
+ TestCompletionCallback cb;
+
+ UDPStatsClient* udp_stats_client = new UDPStatsClient();
+ EXPECT_TRUE(udp_stats_client->Start(test_server_.host_port_pair().host(),
+ test_server_.host_port_pair().port(),
+ bytes,
+ &cb));
+ int rv = cb.WaitForResult();
+ // Check there were no errors during connect/write/read to echo UDP server.
+ EXPECT_EQ(0, rv);
+ }
+
+ net::TestServer test_server_;
+};
+
+class NetworkStatsTestTCP : public NetworkStatsTest {
+ public:
+ NetworkStatsTestTCP()
+ : test_server_(net::TestServer::TYPE_TCP_ECHO,
+ FilePath(FILE_PATH_LITERAL("net/data"))) {
+ }
+
+ protected:
+ void RunTCPEchoTest(int bytes) {
+ TestCompletionCallback cb;
+
+ scoped_ptr<net::MockHostResolver> host_resolver(
+ new net::MockHostResolver());
+
+ TCPStatsClient* tcp_stats_client = new TCPStatsClient();
+ EXPECT_TRUE(tcp_stats_client->Start(host_resolver.get(),
+ test_server_.host_port_pair(),
+ bytes,
+ &cb));
+ int rv = cb.WaitForResult();
+ // Check there were no errors during connect/write/read to echo TCP server.
+ EXPECT_EQ(0, rv);
+ }
+
+ net::TestServer test_server_;
+};
+
+TEST_F(NetworkStatsTestUDP, UDPEcho_50B_Of_Data) {
+ ASSERT_TRUE(test_server_.Start());
+ RunUDPEchoTest(50);
+}
+
+TEST_F(NetworkStatsTestUDP, UDPEcho_1K_Of_Data) {
+ ASSERT_TRUE(test_server_.Start());
+ RunUDPEchoTest(1024);
+}
+
+TEST_F(NetworkStatsTestTCP, TCPEcho_50B_Of_Data) {
+ ASSERT_TRUE(test_server_.Start());
+ RunTCPEchoTest(50);
+}
+
+TEST_F(NetworkStatsTestTCP, TCPEcho_1K_Of_Data) {
+ ASSERT_TRUE(test_server_.Start());
+ RunTCPEchoTest(1024);
+}
+
+} // namespace chrome_browser_net
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index 69bbcdc..bf4c2ce 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -1391,6 +1391,8 @@
'browser/net/net_log_logger.h',
'browser/net/net_pref_observer.cc',
'browser/net/net_pref_observer.h',
+ 'browser/net/network_stats.cc',
+ 'browser/net/network_stats.h',
'browser/net/passive_log_collector.cc',
'browser/net/passive_log_collector.h',
'browser/net/preconnect.cc',
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index 361e5cc..80249ba 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -1465,6 +1465,7 @@
'browser/net/gaia/token_service_unittest.cc',
'browser/net/gaia/token_service_unittest.h',
'browser/net/load_timing_observer_unittest.cc',
+ 'browser/net/network_stats_unittest.cc',
'browser/net/passive_log_collector_unittest.cc',
'browser/net/predictor_unittest.cc',
'browser/net/pref_proxy_config_service_unittest.cc',
diff --git a/chrome/installer/util/browser_distribution.cc b/chrome/installer/util/browser_distribution.cc
index 18dba7e..ce4d1c6 100644
--- a/chrome/installer/util/browser_distribution.cc
+++ b/chrome/installer/util/browser_distribution.cc
@@ -179,6 +179,10 @@ std::wstring BrowserDistribution::GetStatsServerURL() {
return L"";
}
+std::string BrowserDistribution::GetNetworkStatsServer() const {
+ return "";
+}
+
std::wstring BrowserDistribution::GetDistributionData(HKEY root_key) {
return L"";
}
diff --git a/chrome/installer/util/browser_distribution.h b/chrome/installer/util/browser_distribution.h
index c766a8a..fa0c9d6 100644
--- a/chrome/installer/util/browser_distribution.h
+++ b/chrome/installer/util/browser_distribution.h
@@ -88,6 +88,8 @@ class BrowserDistribution {
virtual std::wstring GetStatsServerURL();
+ virtual std::string GetNetworkStatsServer() const;
+
#if defined(OS_WIN)
virtual std::wstring GetDistributionData(HKEY root_key);
#endif
diff --git a/chrome/installer/util/chrome_frame_distribution.cc b/chrome/installer/util/chrome_frame_distribution.cc
index 6dc84ea..7305fdc 100644
--- a/chrome/installer/util/chrome_frame_distribution.cc
+++ b/chrome/installer/util/chrome_frame_distribution.cc
@@ -85,6 +85,11 @@ std::wstring ChromeFrameDistribution::GetStatsServerURL() {
return L"https://clients4.google.com/firefox/metrics/collect";
}
+std::string ChromeFrameDistribution::GetNetworkStatsServer() const {
+ // TODO(rtenneti): Return the network stats server name.
+ return "";
+}
+
std::wstring ChromeFrameDistribution::GetUninstallLinkName() {
return L"Uninstall Chrome Frame";
}
diff --git a/chrome/installer/util/chrome_frame_distribution.h b/chrome/installer/util/chrome_frame_distribution.h
index dca7086..f8078d4 100644
--- a/chrome/installer/util/chrome_frame_distribution.h
+++ b/chrome/installer/util/chrome_frame_distribution.h
@@ -43,6 +43,8 @@ class ChromeFrameDistribution : public BrowserDistribution {
virtual std::wstring GetStatsServerURL() OVERRIDE;
+ virtual std::string GetNetworkStatsServer() const OVERRIDE;
+
virtual std::wstring GetUninstallLinkName() OVERRIDE;
virtual std::wstring GetUninstallRegPath() OVERRIDE;
diff --git a/chrome/installer/util/google_chrome_distribution.cc b/chrome/installer/util/google_chrome_distribution.cc
index 89606d1..bb56cab 100644
--- a/chrome/installer/util/google_chrome_distribution.cc
+++ b/chrome/installer/util/google_chrome_distribution.cc
@@ -422,6 +422,11 @@ std::wstring GoogleChromeDistribution::GetStatsServerURL() {
return L"https://clients4.google.com/firefox/metrics/collect";
}
+std::string GoogleChromeDistribution::GetNetworkStatsServer() const {
+ // TODO(rtenneti): Return the network stats server name.
+ return "";
+}
+
std::wstring GoogleChromeDistribution::GetDistributionData(HKEY root_key) {
std::wstring sub_key(google_update::kRegPathClientState);
sub_key.append(L"\\");
diff --git a/chrome/installer/util/google_chrome_distribution.h b/chrome/installer/util/google_chrome_distribution.h
index 5586896..637b639 100644
--- a/chrome/installer/util/google_chrome_distribution.h
+++ b/chrome/installer/util/google_chrome_distribution.h
@@ -56,6 +56,8 @@ class GoogleChromeDistribution : public BrowserDistribution {
virtual std::wstring GetStatsServerURL() OVERRIDE;
+ virtual std::string GetNetworkStatsServer() const OVERRIDE;
+
// This method reads data from the Google Update ClientState key for
// potential use in the uninstall survey. It must be called before the
// key returned by GetVersionKey() is deleted.
diff --git a/chrome/installer/util/google_chrome_distribution_dummy.cc b/chrome/installer/util/google_chrome_distribution_dummy.cc
index 496d264..19e2800 100644
--- a/chrome/installer/util/google_chrome_distribution_dummy.cc
+++ b/chrome/installer/util/google_chrome_distribution_dummy.cc
@@ -80,6 +80,11 @@ std::wstring GoogleChromeDistribution::GetStatsServerURL() {
return std::wstring();
}
+std::string GoogleChromeDistribution::GetNetworkStatsServer() const {
+ NOTREACHED();
+ return std::string();
+}
+
std::wstring GoogleChromeDistribution::GetDistributionData(HKEY root_key) {
NOTREACHED();
return std::wstring();
diff --git a/net/base/test_data_stream.cc b/net/base/test_data_stream.cc
new file mode 100644
index 0000000..6672804
--- /dev/null
+++ b/net/base/test_data_stream.cc
@@ -0,0 +1,68 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/test_data_stream.h"
+
+namespace net {
+
+TestDataStream::TestDataStream() {
+ Reset();
+}
+
+// Fill |buffer| with |length| bytes of data from the stream.
+void TestDataStream::GetBytes(char* buffer, int length) {
+ while (length) {
+ AdvanceIndex();
+ int bytes_to_copy = std::min(length, bytes_remaining_);
+ memcpy(buffer, buffer_ptr_, bytes_to_copy);
+ buffer += bytes_to_copy;
+ Consume(bytes_to_copy);
+ length -= bytes_to_copy;
+ }
+}
+
+bool TestDataStream::VerifyBytes(const char *buffer, int length) {
+ while (length) {
+ AdvanceIndex();
+ int bytes_to_compare = std::min(length, bytes_remaining_);
+ if (memcmp(buffer, buffer_ptr_, bytes_to_compare))
+ return false;
+ Consume(bytes_to_compare);
+ length -= bytes_to_compare;
+ buffer += bytes_to_compare;
+ }
+ return true;
+}
+
+void TestDataStream::Reset() {
+ index_ = 0;
+ bytes_remaining_ = 0;
+ buffer_ptr_ = buffer_;
+}
+
+// If there is no data spilled over from the previous index, advance the
+// index and fill the buffer.
+void TestDataStream::AdvanceIndex() {
+ if (bytes_remaining_ == 0) {
+ // Convert it to ascii, but don't bother to reverse it.
+ // (e.g. 12345 becomes "54321")
+ int val = index_++;
+ do {
+ buffer_[bytes_remaining_++] = (val % 10) + '0';
+ } while ((val /= 10) > 0);
+ buffer_[bytes_remaining_++] = '.';
+ }
+}
+
+// Consume data from the spill buffer.
+void TestDataStream::Consume(int bytes) {
+ bytes_remaining_ -= bytes;
+ if (bytes_remaining_)
+ buffer_ptr_ += bytes;
+ else
+ buffer_ptr_ = buffer_;
+}
+
+} // namespace net
+
diff --git a/net/base/test_data_stream.h b/net/base/test_data_stream.h
new file mode 100644
index 0000000..b92a9f3
--- /dev/null
+++ b/net/base/test_data_stream.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_TEST_DATA_STREAM_H_
+#define NET_BASE_TEST_DATA_STREAM_H_
+#pragma once
+
+#include <string.h> // for memcpy().
+#include <algorithm>
+#include "net/base/net_api.h"
+
+// This is a class for generating an infinite stream of data which can be
+// verified independently to be the correct stream of data.
+
+namespace net {
+
+class NET_API TestDataStream {
+ public:
+ TestDataStream();
+
+ // Fill |buffer| with |length| bytes of data from the stream.
+ void GetBytes(char* buffer, int length);
+
+ // Verify that |buffer| contains the expected next |length| bytes from the
+ // stream. Returns true if correct, false otherwise.
+ bool VerifyBytes(const char *buffer, int length);
+
+ // Resets all the data.
+ void Reset();
+
+ private:
+ // If there is no data spilled over from the previous index, advance the
+ // index and fill the buffer.
+ void AdvanceIndex();
+
+ // Consume data from the spill buffer.
+ void Consume(int bytes);
+
+ int index_;
+ int bytes_remaining_;
+ char buffer_[16];
+ char* buffer_ptr_;
+};
+
+} // namespace net
+
+#endif // NET_BASE_TEST_DATA_STREAM_H_
diff --git a/net/curvecp/test_client.h b/net/curvecp/test_client.h
index 08f496f..e992ce8 100644
--- a/net/curvecp/test_client.h
+++ b/net/curvecp/test_client.h
@@ -10,7 +10,7 @@
#include "net/base/completion_callback.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
-#include "net/curvecp/test_data_stream.h"
+#include "net/base/test_data_stream.h"
namespace net {
diff --git a/net/curvecp/test_data_stream.h b/net/curvecp/test_data_stream.h
deleted file mode 100644
index d2bbc87..0000000
--- a/net/curvecp/test_data_stream.h
+++ /dev/null
@@ -1,88 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef NET_CURVECP_TEST_DATA_STREAM_H_
-#define NET_CURVECP_TEST_DATA_STREAM_H_
-#pragma once
-
-#include <string.h> // for memcpy()
-#include <algorithm>
-
-// This is a test class for generating an infinite stream of data which can
-// be verified independently to be the correct stream of data.
-
-namespace net {
-
-class TestDataStream {
- public:
- TestDataStream() {
- Reset();
- }
-
- // Fill |buffer| with |length| bytes of data from the stream.
- void GetBytes(char* buffer, int length) {
- while (length) {
- AdvanceIndex();
- int bytes_to_copy = std::min(length, bytes_remaining_);
- memcpy(buffer, buffer_ptr_, bytes_to_copy);
- buffer += bytes_to_copy;
- Consume(bytes_to_copy);
- length -= bytes_to_copy;
- }
- }
-
- // Verify that |buffer| contains the expected next |length| bytes from the
- // stream. Returns true if correct, false otherwise.
- bool VerifyBytes(const char *buffer, int length) {
- while (length) {
- AdvanceIndex();
- int bytes_to_compare = std::min(length, bytes_remaining_);
- if (memcmp(buffer, buffer_ptr_, bytes_to_compare))
- return false;
- Consume(bytes_to_compare);
- length -= bytes_to_compare;
- buffer += bytes_to_compare;
- }
- return true;
- }
-
- void Reset() {
- index_ = 0;
- bytes_remaining_ = 0;
- buffer_ptr_ = buffer_;
- }
-
- private:
- // If there is no data spilled over from the previous index, advance the
- // index and fill the buffer.
- void AdvanceIndex() {
- if (bytes_remaining_ == 0) {
- // Convert it to ascii, but don't bother to reverse it.
- // (e.g. 12345 becomes "54321")
- int val = index_++;
- do {
- buffer_[bytes_remaining_++] = (val % 10) + '0';
- } while ((val /= 10) > 0);
- buffer_[bytes_remaining_++] = '.';
- }
- }
-
- // Consume data from the spill buffer.
- void Consume(int bytes) {
- bytes_remaining_ -= bytes;
- if (bytes_remaining_)
- buffer_ptr_ += bytes;
- else
- buffer_ptr_ = buffer_;
- }
-
- int index_;
- int bytes_remaining_;
- char buffer_[16];
- char* buffer_ptr_;
-};
-
-} // namespace net
-
-#endif // NET_CURVECP_TEST_DATA_STREAM_H_
diff --git a/net/curvecp/test_server.h b/net/curvecp/test_server.h
index 22c44e4..a941401 100644
--- a/net/curvecp/test_server.h
+++ b/net/curvecp/test_server.h
@@ -8,8 +8,8 @@
#include "base/task.h"
#include "net/base/completion_callback.h"
+#include "net/base/test_data_stream.h"
#include "net/curvecp/curvecp_server_socket.h"
-#include "net/curvecp/test_data_stream.h"
namespace net {
diff --git a/net/net.gyp b/net/net.gyp
index dd23a99..63045517 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -198,6 +198,8 @@
'base/ssl_info.h',
'base/static_cookie_policy.cc',
'base/static_cookie_policy.h',
+ 'base/test_data_stream.cc',
+ 'base/test_data_stream.h',
'base/test_root_certs.cc',
'base/test_root_certs.h',
'base/test_root_certs_mac.cc',
diff --git a/net/test/test_server.cc b/net/test/test_server.cc
index b6d3286..b3821bb 100644
--- a/net/test/test_server.cc
+++ b/net/test/test_server.cc
@@ -182,6 +182,10 @@ std::string TestServer::GetScheme() const {
return "http";
case TYPE_HTTPS:
return "https";
+ case TYPE_TCP_ECHO:
+ NOTREACHED();
+ case TYPE_UDP_ECHO:
+ NOTREACHED();
default:
NOTREACHED();
}
@@ -357,6 +361,10 @@ bool TestServer::AddCommandLineArguments(CommandLine* command_line) const {
command_line->AppendArg("-f");
} else if (type_ == TYPE_SYNC) {
command_line->AppendArg("--sync");
+ } else if (type_ == TYPE_TCP_ECHO) {
+ command_line->AppendArg("--tcp-echo");
+ } else if (type_ == TYPE_UDP_ECHO) {
+ command_line->AppendArg("--udp-echo");
} else if (type_ == TYPE_HTTPS) {
FilePath certificate_path(certificates_dir_);
certificate_path = certificate_path.Append(
diff --git a/net/test/test_server.h b/net/test/test_server.h
index 03a4dfc..81b0ad3 100644
--- a/net/test/test_server.h
+++ b/net/test/test_server.h
@@ -42,6 +42,8 @@ class TestServer {
TYPE_HTTP,
TYPE_HTTPS,
TYPE_SYNC,
+ TYPE_TCP_ECHO,
+ TYPE_UDP_ECHO,
};
// Container for various options to control how the HTTPS server is
diff --git a/net/tools/testserver/testserver.py b/net/tools/testserver/testserver.py
index c8fab2b..693b50b 100755
--- a/net/tools/testserver/testserver.py
+++ b/net/tools/testserver/testserver.py
@@ -3,7 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-"""This is a simple HTTP server used for testing Chrome.
+"""This is a simple HTTP/FTP/SYNC/TCP ECHO/UDP ECHO/ server used for testing
+Chrome.
It supports several test URLs, as specified by the handlers in TestPageHandler.
By default, it listens on an ephemeral port and sends the port number back to
@@ -52,6 +53,8 @@ if sys.platform == 'win32':
SERVER_HTTP = 0
SERVER_FTP = 1
SERVER_SYNC = 2
+SERVER_TCP_ECHO = 3
+SERVER_UDP_ECHO = 4
# Using debug() seems to cause hangs on XP: see http://crbug.com/64515 .
debug_output = sys.stderr
@@ -209,6 +212,42 @@ class SyncHTTPServer(StoppableHTTPServer):
asyncore.dispatcher.handle_expt_event)
+class TCPEchoServer(SocketServer.TCPServer):
+ """A TCP echo server that echoes back what it has received."""
+
+ def server_bind(self):
+ """Override server_bind to store the server name."""
+ SocketServer.TCPServer.server_bind(self)
+ host, port = self.socket.getsockname()[:2]
+ self.server_name = socket.getfqdn(host)
+ self.server_port = port
+
+ def serve_forever(self):
+ self.stop = False
+ self.nonce_time = None
+ while not self.stop:
+ self.handle_request()
+ self.socket.close()
+
+
+class UDPEchoServer(SocketServer.UDPServer):
+ """A UDP echo server that echoes back what it has received."""
+
+ def server_bind(self):
+ """Override server_bind to store the server name."""
+ SocketServer.UDPServer.server_bind(self)
+ host, port = self.socket.getsockname()[:2]
+ self.server_name = socket.getfqdn(host)
+ self.server_port = port
+
+ def serve_forever(self):
+ self.stop = False
+ self.nonce_time = None
+ while not self.stop:
+ self.handle_request()
+ self.socket.close()
+
+
class BasePageHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, request, client_address, socket_server,
@@ -1446,6 +1485,34 @@ def MakeDataDir():
return my_data_dir
+
+class TCPEchoHandler(SocketServer.BaseRequestHandler):
+ """The RequestHandler class for TCP echo server.
+
+ It is instantiated once per connection to the server, and overrides the
+ handle() method to implement communication to the client.
+ """
+
+ def handle(self):
+ data = self.request.recv(65536)
+ if not data:
+ return
+ self.request.send(data)
+
+
+class UDPEchoHandler(SocketServer.BaseRequestHandler):
+ """The RequestHandler class for UDP echo server.
+
+ It is instantiated once per connection to the server, and overrides the
+ handle() method to implement communication to the client.
+ """
+
+ def handle(self):
+ data = self.request[0].strip()
+ socket = self.request[1]
+ socket.sendto(data, self.client_address)
+
+
class FileMultiplexer:
def __init__(self, fd1, fd2) :
self.__fd1 = fd1
@@ -1509,6 +1576,14 @@ def main(options, args):
print 'Sync XMPP server started on port %d...' % server.xmpp_port
server_data['port'] = server.server_port
server_data['xmpp_port'] = server.xmpp_port
+ elif options.server_type == SERVER_TCP_ECHO:
+ server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler)
+ print 'Echo TCP server started on port %d...' % server.server_port
+ server_data['port'] = server.server_port
+ elif options.server_type == SERVER_UDP_ECHO:
+ server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler)
+ print 'Echo UDP server started on port %d...' % server.server_port
+ server_data['port'] = server.server_port
# means FTP Server
else:
my_data_dir = MakeDataDir()
@@ -1571,6 +1646,14 @@ if __name__ == '__main__':
const=SERVER_SYNC, default=SERVER_HTTP,
dest='server_type',
help='start up a sync server.')
+ option_parser.add_option('', '--tcp-echo', action='store_const',
+ const=SERVER_TCP_ECHO, default=SERVER_HTTP,
+ dest='server_type',
+ help='start up a tcp echo server.')
+ option_parser.add_option('', '--udp-echo', action='store_const',
+ const=SERVER_UDP_ECHO, default=SERVER_HTTP,
+ dest='server_type',
+ help='start up a udp echo server.')
option_parser.add_option('', '--log-to-console', action='store_const',
const=True, default=False,
dest='log_to_console',