| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/test/spawned_test_server/spawner_communicator.h" | 5 #include "net/test/spawned_test_server/spawner_communicator.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 // is still applicable. | 221 // is still applicable. |
| 222 if (!cur_request_.get()) | 222 if (!cur_request_.get()) |
| 223 return; | 223 return; |
| 224 SpawnerRequestData* data = | 224 SpawnerRequestData* data = |
| 225 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 225 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 226 DCHECK(data); | 226 DCHECK(data); |
| 227 | 227 |
| 228 if (!data->DoesRequestIdMatch(id)) | 228 if (!data->DoesRequestIdMatch(id)) |
| 229 return; | 229 return; |
| 230 // Set the result code and cancel the timed-out task. | 230 // Set the result code and cancel the timed-out task. |
| 231 data->SetResultCode(ERR_TIMED_OUT); | 231 int result = cur_request_->CancelWithError(ERR_TIMED_OUT); |
| 232 cur_request_->Cancel(); | 232 OnSpawnerCommandCompleted(cur_request_.get(), result); |
| 233 OnSpawnerCommandCompleted(cur_request_.get()); | |
| 234 } | 233 } |
| 235 | 234 |
| 236 void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request) { | 235 void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request, |
| 236 int net_error) { |
| 237 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 238 |
| 237 if (!cur_request_.get()) | 239 if (!cur_request_.get()) |
| 238 return; | 240 return; |
| 239 DCHECK_EQ(request, cur_request_.get()); | 241 DCHECK_EQ(request, cur_request_.get()); |
| 240 SpawnerRequestData* data = | 242 SpawnerRequestData* data = |
| 241 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 243 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 242 DCHECK(data); | 244 DCHECK(data); |
| 243 | 245 |
| 244 // If request is faild,return the error code. | 246 // If request is faild,return the error code. |
| 245 if (!cur_request_->status().is_success()) | 247 if (net_error != OK) |
| 246 data->SetResultCode(cur_request_->status().error()); | 248 data->SetResultCode(net_error); |
| 247 | 249 |
| 248 if (!data->IsResultOK()) { | 250 if (!data->IsResultOK()) { |
| 249 LOG(ERROR) << "request failed, status: " | 251 LOG(ERROR) << "request failed, error: " << net_error; |
| 250 << static_cast<int>(request->status().status()) | |
| 251 << ", error: " << request->status().error(); | |
| 252 // Clear the buffer of received data if any net error happened. | 252 // Clear the buffer of received data if any net error happened. |
| 253 data->ClearReceivedData(); | 253 data->ClearReceivedData(); |
| 254 } else { | 254 } else { |
| 255 DCHECK_EQ(1, data->response_started_count()); | 255 DCHECK_EQ(1, data->response_started_count()); |
| 256 } | 256 } |
| 257 | 257 |
| 258 // Clear current request to indicate the completion of sending a command | 258 // Clear current request to indicate the completion of sending a command |
| 259 // to spawner server and getting the result. | 259 // to spawner server and getting the result. |
| 260 cur_request_.reset(); | 260 cur_request_.reset(); |
| 261 context_.reset(); | 261 context_.reset(); |
| 262 // Invalidate the weak pointers on the IO thread. | 262 // Invalidate the weak pointers on the IO thread. |
| 263 weak_factory_.InvalidateWeakPtrs(); | 263 weak_factory_.InvalidateWeakPtrs(); |
| 264 | 264 |
| 265 // Wakeup the caller in user thread. | 265 // Wakeup the caller in user thread. |
| 266 event_.Signal(); | 266 event_.Signal(); |
| 267 } | 267 } |
| 268 | 268 |
| 269 void SpawnerCommunicator::ReadResult(URLRequest* request) { | 269 void SpawnerCommunicator::ReadResult(URLRequest* request) { |
| 270 DCHECK_EQ(request, cur_request_.get()); | 270 DCHECK_EQ(request, cur_request_.get()); |
| 271 SpawnerRequestData* data = | 271 SpawnerRequestData* data = |
| 272 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 272 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 273 DCHECK(data); | 273 DCHECK(data); |
| 274 | 274 |
| 275 IOBuffer* buf = data->buf(); | 275 IOBuffer* buf = data->buf(); |
| 276 // Read as many bytes as are available synchronously. | 276 // Read as many bytes as are available synchronously. |
| 277 while (true) { | 277 while (true) { |
| 278 int num_bytes; | 278 int rv = request->Read(buf, kBufferSize); |
| 279 if (!request->Read(buf, kBufferSize, &num_bytes)) { | 279 if (rv == ERR_IO_PENDING) |
| 280 // Check whether the read failed synchronously. | 280 return; |
| 281 if (!request->status().is_io_pending()) | 281 |
| 282 OnSpawnerCommandCompleted(request); | 282 if (rv < 0) { |
| 283 OnSpawnerCommandCompleted(request, rv); |
| 283 return; | 284 return; |
| 284 } | 285 } |
| 285 if (!data->ConsumeBytesRead(num_bytes)) { | 286 |
| 286 OnSpawnerCommandCompleted(request); | 287 if (!data->ConsumeBytesRead(rv)) { |
| 288 OnSpawnerCommandCompleted(request, rv); |
| 287 return; | 289 return; |
| 288 } | 290 } |
| 289 } | 291 } |
| 290 } | 292 } |
| 291 | 293 |
| 292 void SpawnerCommunicator::OnResponseStarted(URLRequest* request) { | 294 void SpawnerCommunicator::OnResponseStarted(URLRequest* request, |
| 295 int net_error) { |
| 293 DCHECK_EQ(request, cur_request_.get()); | 296 DCHECK_EQ(request, cur_request_.get()); |
| 297 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 298 |
| 294 SpawnerRequestData* data = | 299 SpawnerRequestData* data = |
| 295 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 300 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 296 DCHECK(data); | 301 DCHECK(data); |
| 297 | 302 |
| 298 data->IncreaseResponseStartedCount(); | 303 data->IncreaseResponseStartedCount(); |
| 299 | 304 |
| 300 if (!request->status().is_success()) { | 305 if (net_error != OK) { |
| 301 OnSpawnerCommandCompleted(request); | 306 OnSpawnerCommandCompleted(request, net_error); |
| 302 return; | 307 return; |
| 303 } | 308 } |
| 304 | 309 |
| 305 // Require HTTP responses to have a success status code. | 310 // Require HTTP responses to have a success status code. |
| 306 if (request->GetResponseCode() != 200) { | 311 if (request->GetResponseCode() != 200) { |
| 307 LOG(ERROR) << "Spawner server returned bad status: " | 312 LOG(ERROR) << "Spawner server returned bad status: " |
| 308 << request->response_headers()->GetStatusLine(); | 313 << request->response_headers()->GetStatusLine(); |
| 309 data->SetResultCode(ERR_FAILED); | 314 data->SetResultCode(ERR_FAILED); |
| 310 request->Cancel(); | 315 request->Cancel(); |
| 311 OnSpawnerCommandCompleted(request); | 316 OnSpawnerCommandCompleted(request, ERR_ABORTED); |
| 312 return; | 317 return; |
| 313 } | 318 } |
| 314 | 319 |
| 315 ReadResult(request); | 320 ReadResult(request); |
| 316 } | 321 } |
| 317 | 322 |
| 318 void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) { | 323 void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) { |
| 324 DCHECK_NE(ERR_IO_PENDING, num_bytes); |
| 325 |
| 319 if (!cur_request_.get()) | 326 if (!cur_request_.get()) |
| 320 return; | 327 return; |
| 321 DCHECK_EQ(request, cur_request_.get()); | 328 DCHECK_EQ(request, cur_request_.get()); |
| 322 SpawnerRequestData* data = | 329 SpawnerRequestData* data = |
| 323 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 330 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 324 DCHECK(data); | 331 DCHECK(data); |
| 325 | 332 |
| 326 if (data->ConsumeBytesRead(num_bytes)) { | 333 if (data->ConsumeBytesRead(num_bytes)) { |
| 327 // Keep reading. | 334 // Keep reading. |
| 328 ReadResult(request); | 335 ReadResult(request); |
| 329 } else { | 336 } else { |
| 330 OnSpawnerCommandCompleted(request); | 337 // |bytes_read| < 0 |
| 338 int net_error = num_bytes; |
| 339 OnSpawnerCommandCompleted(request, net_error); |
| 331 } | 340 } |
| 332 } | 341 } |
| 333 | 342 |
| 334 bool SpawnerCommunicator::StartServer(const std::string& arguments, | 343 bool SpawnerCommunicator::StartServer(const std::string& arguments, |
| 335 uint16_t* port) { | 344 uint16_t* port) { |
| 336 *port = 0; | 345 *port = 0; |
| 337 // Send the start command to spawner server to start the Python test server | 346 // Send the start command to spawner server to start the Python test server |
| 338 // on remote machine. | 347 // on remote machine. |
| 339 std::string server_return_data; | 348 std::string server_return_data; |
| 340 int result_code; | 349 int result_code; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 std::string server_return_data; | 389 std::string server_return_data; |
| 381 int result_code; | 390 int result_code; |
| 382 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); | 391 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); |
| 383 Shutdown(); | 392 Shutdown(); |
| 384 if (OK != result_code || server_return_data != "killed") | 393 if (OK != result_code || server_return_data != "killed") |
| 385 return false; | 394 return false; |
| 386 return true; | 395 return true; |
| 387 } | 396 } |
| 388 | 397 |
| 389 } // namespace net | 398 } // namespace net |
| OLD | NEW |