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 // The StaticCallHostOfflineReasonAckCallback is just like | |
106 // Callback<void(AckOrTimeout)::Run method except that the first (i.e. |this|) | |
107 // argument is taken by value rather than by pointer. This is needed to make it | |
108 // work with base::Bind inside CallHostOfflineReasonAckCallback below. | |
Wez
2014/12/18 00:46:20
This description is not very helpful; I think all
Łukasz Anforowicz
2014/12/18 19:02:32
N/A - this function goes away in patchset #18 (it
| |
109 static void StaticCallHostOfflineReasonAckCallback( | |
Lambros
2014/12/16 02:14:05
nit: Maybe just call it RunAckCallback().
nit: Mov
Łukasz Anforowicz
2014/12/17 18:02:04
Done (except I renamed to RunHostOfflineReasonAckC
Wez
2014/12/18 00:46:20
CallHostOfflineReasonAckCallback
Łukasz Anforowicz
2014/12/18 19:02:33
N/A as of patchset #18
| |
110 base::Callback<void(HeartbeatSender::AckOrTimeout)> ack_callback, | |
111 HeartbeatSender::AckOrTimeout ack_or_timeout) { | |
112 ack_callback.Run(ack_or_timeout); | |
113 } | |
114 | |
115 void HeartbeatSender::CallHostOfflineReasonAckCallback( | |
Wez
2014/12/18 00:46:20
PostHostOfflineReasonAckCallback
Łukasz Anforowicz
2014/12/18 19:02:33
Good point - thanks.
| |
116 AckOrTimeout ack_or_timeout) { | |
117 // We go through PostTask (rather than directly calling Run), | |
Lambros
2014/12/16 02:14:05
I don't understand why you have to make this async
Łukasz Anforowicz
2014/12/17 18:02:04
RE: why
Requiring the callers (and callers of the
| |
118 // to unwind frames with |this| pointer from the stack. | |
119 // While Run invocation can be the last statement in this | |
120 // particular method, we don't want to force maintainers | |
121 // of our callers to ensure that they call us last. | |
122 base::Closure fully_bound_ack_callback = | |
123 base::Bind(StaticCallHostOfflineReasonAckCallback, | |
Lambros
2014/12/16 02:14:04
Are you sure you really need this? It seems very s
Łukasz Anforowicz
2014/12/17 18:02:04
I tried passing the callback object by value - thi
| |
124 host_offline_reason_ack_callback_, ack_or_timeout); | |
125 base::MessageLoop::current()->PostTask(FROM_HERE, fully_bound_ack_callback); | |
126 | |
127 host_offline_reason_ack_callback_.Reset(); | |
128 } | |
129 | |
105 void HeartbeatSender::SetHostOfflineReason( | 130 void HeartbeatSender::SetHostOfflineReason( |
106 const std::string& host_offline_reason, | 131 const std::string& host_offline_reason, |
107 const base::Closure& ack_callback) { | 132 const base::TimeDelta& timeout, |
133 const base::Callback<void(AckOrTimeout)>& ack_callback) { | |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
109 DCHECK(host_offline_reason_ack_callback_.is_null()); | 135 DCHECK(host_offline_reason_ack_callback_.is_null()); |
110 host_offline_reason_ = host_offline_reason; | 136 host_offline_reason_ = host_offline_reason; |
111 host_offline_reason_ack_callback_ = ack_callback; | 137 host_offline_reason_ack_callback_ = ack_callback; |
138 host_offline_reason_timeout_timer_.Start( | |
139 FROM_HERE, timeout, | |
140 base::Bind(&HeartbeatSender::CallHostOfflineReasonAckCallback, | |
141 base::Unretained(this), AckOrTimeout::Timeout)); | |
112 if (signal_strategy_->GetState() == SignalStrategy::CONNECTED) { | 142 if (signal_strategy_->GetState() == SignalStrategy::CONNECTED) { |
113 DoSendStanza(); | 143 DoSendStanza(); |
114 } | 144 } |
115 } | 145 } |
116 | 146 |
117 void HeartbeatSender::SendStanza() { | 147 void HeartbeatSender::SendStanza() { |
118 // Make sure we don't send another heartbeat before the heartbeat interval | 148 // Make sure we don't send another heartbeat before the heartbeat interval |
119 // has expired. | 149 // has expired. |
120 timer_resend_.Stop(); | 150 timer_resend_.Stop(); |
121 DoSendStanza(); | 151 DoSendStanza(); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 | 254 |
225 // Notify listener of the first successful heartbeat. | 255 // Notify listener of the first successful heartbeat. |
226 if (!heartbeat_succeeded_) { | 256 if (!heartbeat_succeeded_) { |
227 on_heartbeat_successful_callback_.Run(); | 257 on_heartbeat_successful_callback_.Run(); |
228 } | 258 } |
229 heartbeat_succeeded_ = true; | 259 heartbeat_succeeded_ = true; |
230 | 260 |
231 // Notify caller of SetHostOfflineReason that we got an ack. | 261 // Notify caller of SetHostOfflineReason that we got an ack. |
232 if (is_offline_heartbeat_response) { | 262 if (is_offline_heartbeat_response) { |
233 if (!host_offline_reason_ack_callback_.is_null()) { | 263 if (!host_offline_reason_ack_callback_.is_null()) { |
234 base::MessageLoop::current()->PostTask( | 264 CallHostOfflineReasonAckCallback(AckOrTimeout::Ack); |
235 FROM_HERE, | 265 host_offline_reason_timeout_timer_.Stop(); |
Lambros
2014/12/16 02:14:04
Maybe stop the timer first?
Łukasz Anforowicz
2014/12/17 18:02:04
Done.
| |
236 host_offline_reason_ack_callback_); | |
237 host_offline_reason_ack_callback_.Reset(); | |
238 } | 266 } |
239 } | 267 } |
240 } | 268 } |
241 } | 269 } |
242 } | 270 } |
243 | 271 |
244 void HeartbeatSender::SetInterval(int interval) { | 272 void HeartbeatSender::SetInterval(int interval) { |
245 if (interval != interval_ms_) { | 273 if (interval != interval_ms_) { |
246 interval_ms_ = interval; | 274 interval_ms_ = interval; |
247 | 275 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
310 | 338 |
311 std::string message = signal_strategy_->GetLocalJid() + ' ' + | 339 std::string message = signal_strategy_->GetLocalJid() + ' ' + |
312 base::IntToString(sequence_id_); | 340 base::IntToString(sequence_id_); |
313 std::string signature(key_pair_->SignMessage(message)); | 341 std::string signature(key_pair_->SignMessage(message)); |
314 signature_tag->AddText(signature); | 342 signature_tag->AddText(signature); |
315 | 343 |
316 return signature_tag.Pass(); | 344 return signature_tag.Pass(); |
317 } | 345 } |
318 | 346 |
319 } // namespace remoting | 347 } // namespace remoting |
OLD | NEW |