OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "jingle/notifier/communicator/login.h" | 7 #include "jingle/notifier/communicator/login.h" |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "talk/xmpp/prexmppauth.h" | 23 #include "talk/xmpp/prexmppauth.h" |
24 #include "talk/xmpp/xmppclient.h" | 24 #include "talk/xmpp/xmppclient.h" |
25 #include "talk/xmpp/xmppclientsettings.h" | 25 #include "talk/xmpp/xmppclientsettings.h" |
26 #include "talk/xmpp/xmppengine.h" | 26 #include "talk/xmpp/xmppengine.h" |
27 | 27 |
28 namespace notifier { | 28 namespace notifier { |
29 | 29 |
30 // Redirect valid for 5 minutes. | 30 // Redirect valid for 5 minutes. |
31 static const int kRedirectTimeoutMinutes = 5; | 31 static const int kRedirectTimeoutMinutes = 5; |
32 | 32 |
33 Login::Login(const buzz::XmppClientSettings& user_settings, | 33 Login::Login(Delegate* delegate, |
| 34 const buzz::XmppClientSettings& user_settings, |
34 const ConnectionOptions& options, | 35 const ConnectionOptions& options, |
35 net::HostResolver* host_resolver, | 36 net::HostResolver* host_resolver, |
36 ServerInformation* server_list, | 37 ServerInformation* server_list, |
37 int server_count, | 38 int server_count, |
38 bool try_ssltcp_first) | 39 bool try_ssltcp_first) |
39 : login_settings_(new LoginSettings(user_settings, | 40 : delegate_(delegate), |
| 41 login_settings_(new LoginSettings(user_settings, |
40 options, | 42 options, |
41 host_resolver, | 43 host_resolver, |
42 server_list, | 44 server_list, |
43 server_count, | 45 server_count, |
44 try_ssltcp_first)), | 46 try_ssltcp_first)), |
45 redirect_port_(0) { | 47 redirect_port_(0) { |
46 net::NetworkChangeNotifier::AddObserver(this); | 48 net::NetworkChangeNotifier::AddObserver(this); |
47 ResetReconnectState(); | 49 ResetReconnectState(); |
48 } | 50 } |
49 | 51 |
50 Login::~Login() { | 52 Login::~Login() { |
51 net::NetworkChangeNotifier::RemoveObserver(this); | 53 net::NetworkChangeNotifier::RemoveObserver(this); |
52 } | 54 } |
53 | 55 |
54 void Login::StartConnection() { | 56 void Login::StartConnection() { |
55 // If there is a server redirect, use it. | 57 // If there is a server redirect, use it. |
56 if (base::Time::Now() < | 58 if (base::Time::Now() < |
57 (redirect_time_ + | 59 (redirect_time_ + |
58 base::TimeDelta::FromMinutes(kRedirectTimeoutMinutes))) { | 60 base::TimeDelta::FromMinutes(kRedirectTimeoutMinutes))) { |
59 // Override server/port with redirect values. | 61 // Override server/port with redirect values. |
60 DCHECK_NE(redirect_port_, 0); | 62 DCHECK_NE(redirect_port_, 0); |
61 net::HostPortPair server_override(redirect_server_, redirect_port_); | 63 net::HostPortPair server_override(redirect_server_, redirect_port_); |
62 login_settings_->set_server_override(server_override); | 64 login_settings_->set_server_override(server_override); |
63 } else { | 65 } else { |
64 login_settings_->clear_server_override(); | 66 login_settings_->clear_server_override(); |
65 } | 67 } |
66 | 68 |
67 VLOG(1) << "Starting connection..."; | 69 VLOG(1) << "Starting connection..."; |
68 | 70 |
69 single_attempt_.reset(new SingleLoginAttempt(login_settings_.get())); | 71 single_attempt_.reset(new SingleLoginAttempt(login_settings_.get(), this)); |
| 72 } |
70 | 73 |
71 // Do the signaling hook-ups. | 74 void Login::OnConnect(base::WeakPtr<talk_base::Task> base_task) { |
72 single_attempt_->SignalNeedAutoReconnect.connect( | 75 ResetReconnectState(); |
73 this, | 76 delegate_->OnConnect(base_task); |
74 &Login::TryReconnect); | 77 } |
75 single_attempt_->SignalRedirect.connect(this, &Login::OnRedirect); | 78 |
76 single_attempt_->SignalConnect.connect( | 79 void Login::OnNeedReconnect() { |
77 this, | 80 TryReconnect(); |
78 &Login::OnConnect); | |
79 } | 81 } |
80 | 82 |
81 void Login::OnRedirect(const std::string& redirect_server, int redirect_port) { | 83 void Login::OnRedirect(const std::string& redirect_server, int redirect_port) { |
82 DCHECK_NE(redirect_port_, 0); | 84 DCHECK_NE(redirect_port_, 0); |
83 | 85 |
84 redirect_time_ = base::Time::Now(); | 86 redirect_time_ = base::Time::Now(); |
85 redirect_server_ = redirect_server; | 87 redirect_server_ = redirect_server; |
86 redirect_port_ = redirect_port; | 88 redirect_port_ = redirect_port; |
87 | 89 |
88 // Drop the current connection, and start the login process again. | 90 // Drop the current connection, and start the login process again. |
89 StartConnection(); | 91 StartConnection(); |
90 } | 92 } |
91 | 93 |
92 void Login::OnConnect(base::WeakPtr<talk_base::Task> base_task) { | |
93 ResetReconnectState(); | |
94 SignalConnect(base_task); | |
95 } | |
96 | |
97 void Login::OnIPAddressChanged() { | 94 void Login::OnIPAddressChanged() { |
98 VLOG(1) << "Detected IP address change"; | 95 VLOG(1) << "Detected IP address change"; |
99 // Reconnect in 1 to 9 seconds (vary the time a little to try to | 96 // Reconnect in 1 to 9 seconds (vary the time a little to try to |
100 // avoid spikey behavior on network hiccups). | 97 // avoid spikey behavior on network hiccups). |
101 reconnect_interval_ = base::TimeDelta::FromSeconds(base::RandInt(1, 9)); | 98 reconnect_interval_ = base::TimeDelta::FromSeconds(base::RandInt(1, 9)); |
102 TryReconnect(); | 99 TryReconnect(); |
103 } | 100 } |
104 | 101 |
105 void Login::ResetReconnectState() { | 102 void Login::ResetReconnectState() { |
106 reconnect_interval_ = | 103 reconnect_interval_ = |
107 base::TimeDelta::FromSeconds(base::RandInt(5, 25)); | 104 base::TimeDelta::FromSeconds(base::RandInt(5, 25)); |
108 reconnect_timer_.Stop(); | 105 reconnect_timer_.Stop(); |
109 } | 106 } |
110 | 107 |
111 void Login::TryReconnect() { | 108 void Login::TryReconnect() { |
112 DCHECK_GT(reconnect_interval_.InSeconds(), 0); | 109 DCHECK_GT(reconnect_interval_.InSeconds(), 0); |
113 single_attempt_.reset(); | 110 single_attempt_.reset(); |
114 reconnect_timer_.Stop(); | 111 reconnect_timer_.Stop(); |
115 VLOG(1) << "Reconnecting in " | 112 VLOG(1) << "Reconnecting in " |
116 << reconnect_interval_.InSeconds() << " seconds"; | 113 << reconnect_interval_.InSeconds() << " seconds"; |
117 reconnect_timer_.Start( | 114 reconnect_timer_.Start( |
118 reconnect_interval_, this, &Login::DoReconnect); | 115 reconnect_interval_, this, &Login::DoReconnect); |
119 SignalDisconnect(); | 116 delegate_->OnDisconnect(); |
120 } | 117 } |
121 | 118 |
122 void Login::DoReconnect() { | 119 void Login::DoReconnect() { |
123 // Double reconnect time up to 30 minutes. | 120 // Double reconnect time up to 30 minutes. |
124 const base::TimeDelta kMaxReconnectInterval = | 121 const base::TimeDelta kMaxReconnectInterval = |
125 base::TimeDelta::FromMinutes(30); | 122 base::TimeDelta::FromMinutes(30); |
126 reconnect_interval_ *= 2; | 123 reconnect_interval_ *= 2; |
127 if (reconnect_interval_ > kMaxReconnectInterval) | 124 if (reconnect_interval_ > kMaxReconnectInterval) |
128 reconnect_interval_ = kMaxReconnectInterval; | 125 reconnect_interval_ = kMaxReconnectInterval; |
129 VLOG(1) << "Reconnecting..."; | 126 VLOG(1) << "Reconnecting..."; |
130 StartConnection(); | 127 StartConnection(); |
131 } | 128 } |
132 | 129 |
133 } // namespace notifier | 130 } // namespace notifier |
OLD | NEW |