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 "chrome/browser/sync/engine/net/server_connection_manager.h" | 5 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <ostream> | 9 #include <ostream> |
10 #include <string> | 10 #include <string> |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 const string& server, | 173 const string& server, |
174 int port, | 174 int port, |
175 bool use_ssl, | 175 bool use_ssl, |
176 const string& user_agent) | 176 const string& user_agent) |
177 : sync_server_(server), | 177 : sync_server_(server), |
178 sync_server_port_(port), | 178 sync_server_port_(port), |
179 user_agent_(user_agent), | 179 user_agent_(user_agent), |
180 use_ssl_(use_ssl), | 180 use_ssl_(use_ssl), |
181 proto_sync_path_(kSyncServerSyncPath), | 181 proto_sync_path_(kSyncServerSyncPath), |
182 get_time_path_(kSyncServerGetTimePath), | 182 get_time_path_(kSyncServerGetTimePath), |
183 error_count_(0), | |
184 server_status_(HttpResponse::NONE), | 183 server_status_(HttpResponse::NONE), |
185 server_reachable_(false), | 184 server_reachable_(false), |
186 terminated_(false), | 185 terminated_(false), |
187 active_connection_(NULL) { | 186 active_connection_(NULL) { |
188 } | 187 } |
189 | 188 |
190 ServerConnectionManager::~ServerConnectionManager() { | 189 ServerConnectionManager::~ServerConnectionManager() { |
191 } | 190 } |
192 | 191 |
193 ServerConnectionManager::Connection* | 192 ServerConnectionManager::Connection* |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 if (!post.get()) { | 244 if (!post.get()) { |
246 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; | 245 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; |
247 return false; | 246 return false; |
248 } | 247 } |
249 | 248 |
250 // Note that |post| may be aborted by now, which will just cause Init to fail | 249 // Note that |post| may be aborted by now, which will just cause Init to fail |
251 // with CONNECTION_UNAVAILABLE. | 250 // with CONNECTION_UNAVAILABLE. |
252 bool ok = post.get()->Init( | 251 bool ok = post.get()->Init( |
253 path.c_str(), auth_token, params->buffer_in, ¶ms->response); | 252 path.c_str(), auth_token, params->buffer_in, ¶ms->response); |
254 | 253 |
255 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR) { | 254 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR) |
256 InvalidateAndClearAuthToken(); | 255 InvalidateAndClearAuthToken(); |
257 } | |
258 | 256 |
259 if (!ok || RC_REQUEST_OK != params->response.response_code) { | 257 if (!ok || RC_REQUEST_OK != params->response.response_code) |
260 IncrementErrorCount(); | |
261 return false; | 258 return false; |
262 } | |
263 | 259 |
264 if (post.get()->ReadBufferResponse( | 260 if (post.get()->ReadBufferResponse( |
265 ¶ms->buffer_out, ¶ms->response, true)) { | 261 ¶ms->buffer_out, ¶ms->response, true)) { |
266 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; | 262 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; |
267 server_reachable_ = true; | 263 server_reachable_ = true; |
268 return true; | 264 return true; |
269 } | 265 } |
270 return false; | 266 return false; |
271 } | 267 } |
272 | 268 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 if (!ok || string::npos != | 300 if (!ok || string::npos != |
305 time_response.find_first_not_of("0123456789")) { | 301 time_response.find_first_not_of("0123456789")) { |
306 LOG(ERROR) << "unable to read a non-numeric response from get_time:" | 302 LOG(ERROR) << "unable to read a non-numeric response from get_time:" |
307 << time_response; | 303 << time_response; |
308 continue; | 304 continue; |
309 } | 305 } |
310 *out_time = atoi(time_response.c_str()); | 306 *out_time = atoi(time_response.c_str()); |
311 VLOG(1) << "Server was reachable."; | 307 VLOG(1) << "Server was reachable."; |
312 return true; | 308 return true; |
313 } | 309 } |
314 IncrementErrorCount(); | |
315 return false; | 310 return false; |
316 } | 311 } |
317 | 312 |
318 bool ServerConnectionManager::IsServerReachable() { | 313 bool ServerConnectionManager::IsServerReachable() { |
319 DCHECK(thread_checker_.CalledOnValidThread()); | 314 DCHECK(thread_checker_.CalledOnValidThread()); |
320 int32 time; | 315 int32 time; |
321 return CheckTime(&time); | 316 return CheckTime(&time); |
322 } | 317 } |
323 | 318 |
324 bool ServerConnectionManager::IsUserAuthenticated() { | 319 bool ServerConnectionManager::IsUserAuthenticated() { |
325 DCHECK(thread_checker_.CalledOnValidThread()); | 320 DCHECK(thread_checker_.CalledOnValidThread()); |
326 return IsGoodReplyFromServer(server_status_); | 321 return IsGoodReplyFromServer(server_status_); |
327 } | 322 } |
328 | 323 |
329 bool ServerConnectionManager::CheckServerReachable() { | 324 bool ServerConnectionManager::CheckServerReachable() { |
330 DCHECK(thread_checker_.CalledOnValidThread()); | 325 DCHECK(thread_checker_.CalledOnValidThread()); |
331 const bool server_is_reachable = IsServerReachable(); | 326 const bool server_is_reachable = IsServerReachable(); |
332 if (server_reachable_ != server_is_reachable) { | 327 if (server_reachable_ != server_is_reachable) { |
333 server_reachable_ = server_is_reachable; | 328 server_reachable_ = server_is_reachable; |
334 NotifyStatusChanged(); | 329 NotifyStatusChanged(); |
335 } | 330 } |
336 return server_is_reachable; | 331 return server_is_reachable; |
337 } | 332 } |
338 | 333 |
339 bool ServerConnectionManager::IncrementErrorCount() { | |
340 DCHECK(thread_checker_.CalledOnValidThread()); | |
341 error_count_++; | |
342 | |
343 if (error_count_ > kMaxConnectionErrorsBeforeReset) { | |
344 error_count_ = 0; | |
345 | |
346 if (!IsServerReachable()) { | |
347 LOG(WARNING) << "Too many connection failures, server is not reachable. " | |
348 << "Resetting connections."; | |
349 } else { | |
350 LOG(WARNING) << "Multiple connection failures while server is reachable."; | |
351 } | |
352 return false; | |
353 } | |
354 | |
355 return true; | |
356 } | |
357 | |
358 void ServerConnectionManager::SetServerParameters(const string& server_url, | 334 void ServerConnectionManager::SetServerParameters(const string& server_url, |
359 int port, | 335 int port, |
360 bool use_ssl) { | 336 bool use_ssl) { |
361 DCHECK(thread_checker_.CalledOnValidThread()); | 337 DCHECK(thread_checker_.CalledOnValidThread()); |
362 sync_server_ = server_url; | 338 sync_server_ = server_url; |
363 sync_server_port_ = port; | 339 sync_server_port_ = port; |
364 use_ssl_ = use_ssl; | 340 use_ssl_ = use_ssl; |
365 } | 341 } |
366 | 342 |
367 // Returns the current server parameters in server_url and port. | 343 // Returns the current server parameters in server_url and port. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 } | 411 } |
436 | 412 |
437 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { | 413 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { |
438 s << " Response Code (bogus on error): " << hr.response_code; | 414 s << " Response Code (bogus on error): " << hr.response_code; |
439 s << " Content-Length (bogus on error): " << hr.content_length; | 415 s << " Content-Length (bogus on error): " << hr.content_length; |
440 s << " Server Status: " << hr.server_status; | 416 s << " Server Status: " << hr.server_status; |
441 return s; | 417 return s; |
442 } | 418 } |
443 | 419 |
444 } // namespace browser_sync | 420 } // namespace browser_sync |
OLD | NEW |