| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" |
| 8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 10 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 11 #include "base/metrics/stats_counters.h" | 13 #include "base/metrics/stats_counters.h" |
| 12 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 14 #include "net/base/cert_verifier.h" | 16 #include "net/base/cert_verifier.h" |
| 15 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
| 16 #include "net/base/host_resolver.h" | 18 #include "net/base/host_resolver.h" |
| 17 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 int clients_; | 51 int clients_; |
| 50 }; | 52 }; |
| 51 | 53 |
| 52 static base::LazyInstance<Driver> g_driver = LAZY_INSTANCE_INITIALIZER; | 54 static base::LazyInstance<Driver> g_driver = LAZY_INSTANCE_INITIALIZER; |
| 53 | 55 |
| 54 // A network client | 56 // A network client |
| 55 class Client { | 57 class Client { |
| 56 public: | 58 public: |
| 57 Client(net::HttpTransactionFactory* factory, const std::string& url) : | 59 Client(net::HttpTransactionFactory* factory, const std::string& url) : |
| 58 url_(url), | 60 url_(url), |
| 59 buffer_(new net::IOBuffer(kBufferSize)), | 61 buffer_(new net::IOBuffer(kBufferSize)) { |
| 60 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 61 connect_callback_(this, &Client::OnConnectComplete)), | |
| 62 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 63 read_callback_(this, &Client::OnReadComplete)) { | |
| 64 int rv = factory->CreateTransaction(&transaction_); | 62 int rv = factory->CreateTransaction(&transaction_); |
| 65 DCHECK_EQ(net::OK, rv); | 63 DCHECK_EQ(net::OK, rv); |
| 66 buffer_->AddRef(); | 64 buffer_->AddRef(); |
| 67 g_driver.Get().ClientStarted(); | 65 g_driver.Get().ClientStarted(); |
| 68 request_info_.url = url_; | 66 request_info_.url = url_; |
| 69 request_info_.method = "GET"; | 67 request_info_.method = "GET"; |
| 70 int state = transaction_->Start( | 68 int state = transaction_->Start( |
| 71 &request_info_, &connect_callback_, net::BoundNetLog()); | 69 &request_info_, |
| 70 base::Bind(&Client::OnConnectComplete, base::Unretained(this)), |
| 71 net::BoundNetLog()); |
| 72 DCHECK(state == net::ERR_IO_PENDING); | 72 DCHECK(state == net::ERR_IO_PENDING); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 void OnConnectComplete(int result) { | 76 void OnConnectComplete(int result) { |
| 77 // Do work here. | 77 // Do work here. |
| 78 int state = transaction_->Read(buffer_.get(), kBufferSize, &read_callback_); | 78 int state = transaction_->Read( |
| 79 buffer_.get(), kBufferSize, |
| 80 base::Bind(&Client::OnReadComplete, base::Unretained(this))); |
| 79 if (state == net::ERR_IO_PENDING) | 81 if (state == net::ERR_IO_PENDING) |
| 80 return; // IO has started. | 82 return; // IO has started. |
| 81 if (state < 0) | 83 if (state < 0) |
| 82 return; // ERROR! | 84 return; // ERROR! |
| 83 OnReadComplete(state); | 85 OnReadComplete(state); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void OnReadComplete(int result) { | 88 void OnReadComplete(int result) { |
| 87 if (result == 0) { | 89 if (result == 0) { |
| 88 OnRequestComplete(result); | 90 OnRequestComplete(result); |
| 89 return; | 91 return; |
| 90 } | 92 } |
| 91 | 93 |
| 92 // Deal with received data here. | 94 // Deal with received data here. |
| 93 base::StatsCounter bytes_read("FetchClient.bytes_read"); | 95 base::StatsCounter bytes_read("FetchClient.bytes_read"); |
| 94 bytes_read.Add(result); | 96 bytes_read.Add(result); |
| 95 | 97 |
| 96 // Issue a read for more data. | 98 // Issue a read for more data. |
| 97 int state = transaction_->Read(buffer_.get(), kBufferSize, &read_callback_); | 99 int state = transaction_->Read( |
| 100 buffer_.get(), kBufferSize, |
| 101 base::Bind(&Client::OnReadComplete, base::Unretained(this))); |
| 98 if (state == net::ERR_IO_PENDING) | 102 if (state == net::ERR_IO_PENDING) |
| 99 return; // IO has started. | 103 return; // IO has started. |
| 100 if (state < 0) | 104 if (state < 0) |
| 101 return; // ERROR! | 105 return; // ERROR! |
| 102 OnReadComplete(state); | 106 OnReadComplete(state); |
| 103 } | 107 } |
| 104 | 108 |
| 105 void OnRequestComplete(int result) { | 109 void OnRequestComplete(int result) { |
| 106 base::StatsCounter requests("FetchClient.requests"); | 110 base::StatsCounter requests("FetchClient.requests"); |
| 107 requests.Increment(); | 111 requests.Increment(); |
| 108 g_driver.Get().ClientStopped(); | 112 g_driver.Get().ClientStopped(); |
| 109 printf("."); | 113 printf("."); |
| 110 } | 114 } |
| 111 | 115 |
| 112 static const int kBufferSize = (16 * 1024); | 116 static const int kBufferSize = (16 * 1024); |
| 113 GURL url_; | 117 GURL url_; |
| 114 net::HttpRequestInfo request_info_; | 118 net::HttpRequestInfo request_info_; |
| 115 scoped_ptr<net::HttpTransaction> transaction_; | 119 scoped_ptr<net::HttpTransaction> transaction_; |
| 116 scoped_refptr<net::IOBuffer> buffer_; | 120 scoped_refptr<net::IOBuffer> buffer_; |
| 117 net::OldCompletionCallbackImpl<Client> connect_callback_; | |
| 118 net::OldCompletionCallbackImpl<Client> read_callback_; | |
| 119 }; | 121 }; |
| 120 | 122 |
| 121 int main(int argc, char** argv) { | 123 int main(int argc, char** argv) { |
| 122 base::AtExitManager exit; | 124 base::AtExitManager exit; |
| 123 base::StatsTable table("fetchclient", 50, 1000); | 125 base::StatsTable table("fetchclient", 50, 1000); |
| 124 table.set_current(&table); | 126 table.set_current(&table); |
| 125 | 127 |
| 126 CommandLine::Init(argc, argv); | 128 CommandLine::Init(argc, argv); |
| 127 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | 129 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 128 std::string url = parsed_command_line.GetSwitchValueASCII("url"); | 130 std::string url = parsed_command_line.GetSwitchValueASCII("url"); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 std::string name(table.GetRowName(index)); | 215 std::string name(table.GetRowName(index)); |
| 214 if (name.length() > 0) { | 216 if (name.length() > 0) { |
| 215 int value = table.GetRowValue(index); | 217 int value = table.GetRowValue(index); |
| 216 printf("%s:\t%d\n", name.c_str(), value); | 218 printf("%s:\t%d\n", name.c_str(), value); |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 printf("</stats>\n"); | 221 printf("</stats>\n"); |
| 220 } | 222 } |
| 221 return 0; | 223 return 0; |
| 222 } | 224 } |
| OLD | NEW |