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 "remoting/host/heartbeat_sender.h" | 5 #include "remoting/host/heartbeat_sender.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 const int64 kResendDelayOnHostNotFoundMs = 10 * 1000; // 10 seconds. | 47 const int64 kResendDelayOnHostNotFoundMs = 10 * 1000; // 10 seconds. |
48 const int kMaxResendOnHostNotFoundCount = 12; // 2 minutes (12 x 10 seconds). | 48 const int kMaxResendOnHostNotFoundCount = 12; // 2 minutes (12 x 10 seconds). |
49 | 49 |
50 } // namespace | 50 } // namespace |
51 | 51 |
52 HeartbeatSender::HeartbeatSender( | 52 HeartbeatSender::HeartbeatSender( |
53 const base::Closure& on_heartbeat_successful_callback, | 53 const base::Closure& on_heartbeat_successful_callback, |
54 const base::Closure& on_unknown_host_id_error, | 54 const base::Closure& on_unknown_host_id_error, |
55 const std::string& host_id, | 55 const std::string& host_id, |
56 SignalStrategy* signal_strategy, | 56 SignalStrategy* signal_strategy, |
57 scoped_refptr<RsaKeyPair> key_pair, | 57 const scoped_refptr<const RsaKeyPair>& key_pair, |
58 const std::string& directory_bot_jid) | 58 const std::string& directory_bot_jid) |
59 : on_heartbeat_successful_callback_(on_heartbeat_successful_callback), | 59 : on_heartbeat_successful_callback_(on_heartbeat_successful_callback), |
60 on_unknown_host_id_error_(on_unknown_host_id_error), | 60 on_unknown_host_id_error_(on_unknown_host_id_error), |
61 host_id_(host_id), | 61 host_id_(host_id), |
62 signal_strategy_(signal_strategy), | 62 signal_strategy_(signal_strategy), |
63 key_pair_(key_pair), | 63 key_pair_(key_pair), |
64 directory_bot_jid_(directory_bot_jid), | 64 directory_bot_jid_(directory_bot_jid), |
65 interval_ms_(kDefaultHeartbeatIntervalMs), | 65 interval_ms_(kDefaultHeartbeatIntervalMs), |
66 sequence_id_(0), | 66 sequence_id_(0), |
67 sequence_id_was_set_(false), | 67 sequence_id_was_set_(false), |
(...skipping 27 matching lines...) Expand all Loading... | |
95 timer_.Stop(); | 95 timer_.Stop(); |
96 timer_resend_.Stop(); | 96 timer_resend_.Stop(); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 bool HeartbeatSender::OnSignalStrategyIncomingStanza( | 100 bool HeartbeatSender::OnSignalStrategyIncomingStanza( |
101 const buzz::XmlElement* stanza) { | 101 const buzz::XmlElement* stanza) { |
102 return false; | 102 return false; |
103 } | 103 } |
104 | 104 |
105 void HeartbeatSender::CallHostOfflineReasonAckCallback( | |
106 AckOrTimeout ack_or_timeout) { | |
107 // We go through PostTask (rather than directly calling Run), | |
108 // to unwind frames with |this| pointer from the stack. | |
109 // While Run invocation can be the last statement in this | |
110 // particular method, we don't want to force maintainers | |
111 // of our callers to ensure that they call us last. | |
Wez
2014/12/18 00:46:20
This is a common pattern, so doesn't need such a l
Łukasz Anforowicz
2014/12/18 19:02:33
I tried to shorten the comment following your sugg
Wez
2014/12/19 22:05:36
Acknowledged.
| |
112 base::Closure fully_bound_ack_callback = | |
Lambros
2014/12/17 23:44:34
nit: Don't need the temporary. We do
PostTask(FROM
Łukasz Anforowicz
2014/12/18 00:03:41
Acknowledged.
| |
113 base::Bind(host_offline_reason_ack_callback_, ack_or_timeout); | |
114 base::MessageLoop::current()->PostTask(FROM_HERE, fully_bound_ack_callback); | |
Wez
2014/12/18 00:46:20
nit: Use ThreadTaskRunnerHandle::Get() here.
Łukasz Anforowicz
2014/12/18 19:02:33
Done. Can you please give more details / help me
Wez
2014/12/19 22:05:36
In using the current MessageLoop's TaskRunner you'
| |
115 | |
116 host_offline_reason_ack_callback_.Reset(); | |
117 } | |
118 | |
105 void HeartbeatSender::SetHostOfflineReason( | 119 void HeartbeatSender::SetHostOfflineReason( |
106 const std::string& host_offline_reason, | 120 const std::string& host_offline_reason, |
107 const base::Closure& ack_callback) { | 121 const base::TimeDelta& timeout, |
122 const base::Callback<void(AckOrTimeout)>& ack_callback) { | |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 123 DCHECK(thread_checker_.CalledOnValidThread()); |
109 DCHECK(host_offline_reason_ack_callback_.is_null()); | 124 DCHECK(host_offline_reason_ack_callback_.is_null()); |
110 host_offline_reason_ = host_offline_reason; | 125 host_offline_reason_ = host_offline_reason; |
111 host_offline_reason_ack_callback_ = ack_callback; | 126 host_offline_reason_ack_callback_ = ack_callback; |
127 host_offline_reason_timeout_timer_.Start( | |
128 FROM_HERE, timeout, | |
129 base::Bind(&HeartbeatSender::CallHostOfflineReasonAckCallback, | |
130 base::Unretained(this), AckOrTimeout::Timeout)); | |
Wez
2014/12/18 00:46:20
Re-using CallHostOfflineReasonAckCallback here rea
Łukasz Anforowicz
2014/12/18 19:02:33
Good point. Done. This also helped keep the time
| |
112 if (signal_strategy_->GetState() == SignalStrategy::CONNECTED) { | 131 if (signal_strategy_->GetState() == SignalStrategy::CONNECTED) { |
113 DoSendStanza(); | 132 DoSendStanza(); |
114 } | 133 } |
115 } | 134 } |
116 | 135 |
117 void HeartbeatSender::SendStanza() { | 136 void HeartbeatSender::SendStanza() { |
118 // Make sure we don't send another heartbeat before the heartbeat interval | 137 // Make sure we don't send another heartbeat before the heartbeat interval |
119 // has expired. | 138 // has expired. |
120 timer_resend_.Stop(); | 139 timer_resend_.Stop(); |
121 DoSendStanza(); | 140 DoSendStanza(); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 | 243 |
225 // Notify listener of the first successful heartbeat. | 244 // Notify listener of the first successful heartbeat. |
226 if (!heartbeat_succeeded_) { | 245 if (!heartbeat_succeeded_) { |
227 on_heartbeat_successful_callback_.Run(); | 246 on_heartbeat_successful_callback_.Run(); |
228 } | 247 } |
229 heartbeat_succeeded_ = true; | 248 heartbeat_succeeded_ = true; |
230 | 249 |
231 // Notify caller of SetHostOfflineReason that we got an ack. | 250 // Notify caller of SetHostOfflineReason that we got an ack. |
232 if (is_offline_heartbeat_response) { | 251 if (is_offline_heartbeat_response) { |
233 if (!host_offline_reason_ack_callback_.is_null()) { | 252 if (!host_offline_reason_ack_callback_.is_null()) { |
234 base::MessageLoop::current()->PostTask( | 253 host_offline_reason_timeout_timer_.Stop(); |
Wez
2014/12/18 00:46:20
This seems wrong - surely you want to stop the tim
Łukasz Anforowicz
2014/12/18 19:02:33
In this case the timer is already stopped.
| |
235 FROM_HERE, | 254 CallHostOfflineReasonAckCallback(AckOrTimeout::Ack); |
236 host_offline_reason_ack_callback_); | |
237 host_offline_reason_ack_callback_.Reset(); | |
238 } | 255 } |
239 } | 256 } |
240 } | 257 } |
241 } | 258 } |
242 } | 259 } |
243 | 260 |
244 void HeartbeatSender::SetInterval(int interval) { | 261 void HeartbeatSender::SetInterval(int interval) { |
245 if (interval != interval_ms_) { | 262 if (interval != interval_ms_) { |
246 interval_ms_ = interval; | 263 interval_ms_ = interval; |
247 | 264 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
310 | 327 |
311 std::string message = signal_strategy_->GetLocalJid() + ' ' + | 328 std::string message = signal_strategy_->GetLocalJid() + ' ' + |
312 base::IntToString(sequence_id_); | 329 base::IntToString(sequence_id_); |
313 std::string signature(key_pair_->SignMessage(message)); | 330 std::string signature(key_pair_->SignMessage(message)); |
314 signature_tag->AddText(signature); | 331 signature_tag->AddText(signature); |
315 | 332 |
316 return signature_tag.Pass(); | 333 return signature_tag.Pass(); |
317 } | 334 } |
318 | 335 |
319 } // namespace remoting | 336 } // namespace remoting |
OLD | NEW |