| 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 int result = cur_request_->CancelWithError(ERR_TIMED_OUT); | 231 data->SetResultCode(ERR_TIMED_OUT); |
| 232 OnSpawnerCommandCompleted(cur_request_.get(), result); | 232 cur_request_->Cancel(); |
| 233 OnSpawnerCommandCompleted(cur_request_.get()); |
| 233 } | 234 } |
| 234 | 235 |
| 235 void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request, | 236 void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request) { |
| 236 int net_error) { | |
| 237 DCHECK_NE(ERR_IO_PENDING, net_error); | |
| 238 | |
| 239 if (!cur_request_.get()) | 237 if (!cur_request_.get()) |
| 240 return; | 238 return; |
| 241 DCHECK_EQ(request, cur_request_.get()); | 239 DCHECK_EQ(request, cur_request_.get()); |
| 242 SpawnerRequestData* data = | 240 SpawnerRequestData* data = |
| 243 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 241 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 244 DCHECK(data); | 242 DCHECK(data); |
| 245 | 243 |
| 246 // If request is faild,return the error code. | 244 // If request is faild,return the error code. |
| 247 if (net_error != OK) | 245 if (!cur_request_->status().is_success()) |
| 248 data->SetResultCode(net_error); | 246 data->SetResultCode(cur_request_->status().error()); |
| 249 | 247 |
| 250 if (!data->IsResultOK()) { | 248 if (!data->IsResultOK()) { |
| 251 LOG(ERROR) << "request failed, error: " << net_error; | 249 LOG(ERROR) << "request failed, status: " |
| 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 rv = request->Read(buf, kBufferSize); | 278 int num_bytes; |
| 279 if (rv == ERR_IO_PENDING) | 279 if (!request->Read(buf, kBufferSize, &num_bytes)) { |
| 280 return; | 280 // Check whether the read failed synchronously. |
| 281 | 281 if (!request->status().is_io_pending()) |
| 282 if (rv < 0) { | 282 OnSpawnerCommandCompleted(request); |
| 283 OnSpawnerCommandCompleted(request, rv); | |
| 284 return; | 283 return; |
| 285 } | 284 } |
| 286 | 285 if (!data->ConsumeBytesRead(num_bytes)) { |
| 287 if (!data->ConsumeBytesRead(rv)) { | 286 OnSpawnerCommandCompleted(request); |
| 288 OnSpawnerCommandCompleted(request, rv); | |
| 289 return; | 287 return; |
| 290 } | 288 } |
| 291 } | 289 } |
| 292 } | 290 } |
| 293 | 291 |
| 294 void SpawnerCommunicator::OnResponseStarted(URLRequest* request, | 292 void SpawnerCommunicator::OnResponseStarted(URLRequest* request) { |
| 295 int net_error) { | |
| 296 DCHECK_EQ(request, cur_request_.get()); | 293 DCHECK_EQ(request, cur_request_.get()); |
| 297 DCHECK_NE(ERR_IO_PENDING, net_error); | |
| 298 | |
| 299 SpawnerRequestData* data = | 294 SpawnerRequestData* data = |
| 300 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 295 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 301 DCHECK(data); | 296 DCHECK(data); |
| 302 | 297 |
| 303 data->IncreaseResponseStartedCount(); | 298 data->IncreaseResponseStartedCount(); |
| 304 | 299 |
| 305 if (net_error != OK) { | 300 if (!request->status().is_success()) { |
| 306 OnSpawnerCommandCompleted(request, net_error); | 301 OnSpawnerCommandCompleted(request); |
| 307 return; | 302 return; |
| 308 } | 303 } |
| 309 | 304 |
| 310 // Require HTTP responses to have a success status code. | 305 // Require HTTP responses to have a success status code. |
| 311 if (request->GetResponseCode() != 200) { | 306 if (request->GetResponseCode() != 200) { |
| 312 LOG(ERROR) << "Spawner server returned bad status: " | 307 LOG(ERROR) << "Spawner server returned bad status: " |
| 313 << request->response_headers()->GetStatusLine(); | 308 << request->response_headers()->GetStatusLine(); |
| 314 data->SetResultCode(ERR_FAILED); | 309 data->SetResultCode(ERR_FAILED); |
| 315 request->Cancel(); | 310 request->Cancel(); |
| 316 OnSpawnerCommandCompleted(request, ERR_ABORTED); | 311 OnSpawnerCommandCompleted(request); |
| 317 return; | 312 return; |
| 318 } | 313 } |
| 319 | 314 |
| 320 ReadResult(request); | 315 ReadResult(request); |
| 321 } | 316 } |
| 322 | 317 |
| 323 void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) { | 318 void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) { |
| 324 DCHECK_NE(ERR_IO_PENDING, num_bytes); | |
| 325 | |
| 326 if (!cur_request_.get()) | 319 if (!cur_request_.get()) |
| 327 return; | 320 return; |
| 328 DCHECK_EQ(request, cur_request_.get()); | 321 DCHECK_EQ(request, cur_request_.get()); |
| 329 SpawnerRequestData* data = | 322 SpawnerRequestData* data = |
| 330 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 323 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 331 DCHECK(data); | 324 DCHECK(data); |
| 332 | 325 |
| 333 if (data->ConsumeBytesRead(num_bytes)) { | 326 if (data->ConsumeBytesRead(num_bytes)) { |
| 334 // Keep reading. | 327 // Keep reading. |
| 335 ReadResult(request); | 328 ReadResult(request); |
| 336 } else { | 329 } else { |
| 337 // |bytes_read| < 0 | 330 OnSpawnerCommandCompleted(request); |
| 338 int net_error = num_bytes; | |
| 339 OnSpawnerCommandCompleted(request, net_error); | |
| 340 } | 331 } |
| 341 } | 332 } |
| 342 | 333 |
| 343 bool SpawnerCommunicator::StartServer(const std::string& arguments, | 334 bool SpawnerCommunicator::StartServer(const std::string& arguments, |
| 344 uint16_t* port) { | 335 uint16_t* port) { |
| 345 *port = 0; | 336 *port = 0; |
| 346 // Send the start command to spawner server to start the Python test server | 337 // Send the start command to spawner server to start the Python test server |
| 347 // on remote machine. | 338 // on remote machine. |
| 348 std::string server_return_data; | 339 std::string server_return_data; |
| 349 int result_code; | 340 int result_code; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 std::string server_return_data; | 380 std::string server_return_data; |
| 390 int result_code; | 381 int result_code; |
| 391 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); | 382 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); |
| 392 Shutdown(); | 383 Shutdown(); |
| 393 if (OK != result_code || server_return_data != "killed") | 384 if (OK != result_code || server_return_data != "killed") |
| 394 return false; | 385 return false; |
| 395 return true; | 386 return true; |
| 396 } | 387 } |
| 397 | 388 |
| 398 } // namespace net | 389 } // namespace net |
| OLD | NEW |