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" | |
11 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/stringize_macros.h" | 12 #include "base/strings/stringize_macros.h" |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "remoting/base/constants.h" | 15 #include "remoting/base/constants.h" |
16 #include "remoting/base/logging.h" | 16 #include "remoting/base/logging.h" |
17 #include "remoting/host/server_log_entry_host.h" | 17 #include "remoting/host/server_log_entry_host.h" |
18 #include "remoting/signaling/iq_sender.h" | 18 #include "remoting/signaling/iq_sender.h" |
19 #include "remoting/signaling/server_log_entry.h" | 19 #include "remoting/signaling/server_log_entry.h" |
20 #include "remoting/signaling/signal_strategy.h" | 20 #include "remoting/signaling/signal_strategy.h" |
21 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 21 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
22 #include "third_party/webrtc/libjingle/xmpp/constants.h" | 22 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
23 | 23 |
(...skipping 23 matching lines...) Expand all 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>& host_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 host_key_pair_(host_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), |
68 sequence_id_recent_set_num_(0), | 68 sequence_id_recent_set_num_(0), |
69 heartbeat_succeeded_(false), | 69 heartbeat_succeeded_(false), |
70 failed_startup_heartbeat_count_(0) { | 70 failed_startup_heartbeat_count_(0) { |
71 DCHECK(signal_strategy_); | 71 DCHECK(signal_strategy_); |
72 DCHECK(key_pair_.get()); | 72 DCHECK(host_key_pair_.get()); |
73 DCHECK(thread_checker_.CalledOnValidThread()); | 73 DCHECK(thread_checker_.CalledOnValidThread()); |
74 | 74 |
75 signal_strategy_->AddListener(this); | 75 signal_strategy_->AddListener(this); |
76 | 76 |
77 // Start heartbeats if the |signal_strategy_| is already connected. | 77 // Start heartbeats if the |signal_strategy_| is already connected. |
78 OnSignalStrategyStateChange(signal_strategy_->GetState()); | 78 OnSignalStrategyStateChange(signal_strategy_->GetState()); |
79 } | 79 } |
80 | 80 |
81 HeartbeatSender::~HeartbeatSender() { | 81 HeartbeatSender::~HeartbeatSender() { |
82 signal_strategy_->RemoveListener(this); | 82 signal_strategy_->RemoveListener(this); |
(...skipping 12 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::OnHostOfflineReasonTimeout() { | |
106 DCHECK(!host_offline_reason_ack_callback_.is_null()); | |
107 | |
108 base::Callback<void(bool)> local_callback = host_offline_reason_ack_callback_; | |
109 host_offline_reason_ack_callback_.Reset(); | |
110 local_callback.Run(false /* success */); | |
Wez
2015/01/08 01:49:50
s/success/failure :)
Łukasz Anforowicz
2015/01/08 23:50:31
Ok, I can see the ambiguity now. What I meant was
Wez
2015/01/09 02:55:45
Actually what I meant was that you comment /* succ
Łukasz Anforowicz
2015/01/09 19:00:13
Yes - avoiding this kind of confusion was why I wa
| |
111 } | |
112 | |
113 void HeartbeatSender::OnHostOfflineReasonAck() { | |
114 if (host_offline_reason_ack_callback_.is_null()) { | |
115 DCHECK(!host_offline_reason_timeout_timer_.IsRunning()); | |
116 return; | |
117 } | |
118 | |
119 DCHECK(host_offline_reason_timeout_timer_.IsRunning()); | |
120 host_offline_reason_timeout_timer_.Stop(); | |
121 | |
122 // Run the ACK callback under a clean stack via PostTask() (because the | |
123 // callback can end up deleting |this| HeartbeatSender [i.e. when used from | |
124 // HostSignalingManager]). | |
125 base::Closure fully_bound_ack_callback = | |
126 base::Bind(host_offline_reason_ack_callback_, true /* success */); | |
127 host_offline_reason_ack_callback_.Reset(); | |
128 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
129 fully_bound_ack_callback); | |
130 } | |
131 | |
105 void HeartbeatSender::SetHostOfflineReason( | 132 void HeartbeatSender::SetHostOfflineReason( |
106 const std::string& host_offline_reason, | 133 const std::string& host_offline_reason, |
107 const base::Closure& ack_callback) { | 134 const base::TimeDelta& timeout, |
135 const base::Callback<void(bool success)>& ack_callback) { | |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
109 DCHECK(host_offline_reason_ack_callback_.is_null()); | 137 DCHECK(host_offline_reason_ack_callback_.is_null()); |
110 host_offline_reason_ = host_offline_reason; | 138 host_offline_reason_ = host_offline_reason; |
111 host_offline_reason_ack_callback_ = ack_callback; | 139 host_offline_reason_ack_callback_ = ack_callback; |
140 host_offline_reason_timeout_timer_.Start( | |
141 FROM_HERE, timeout, this, &HeartbeatSender::OnHostOfflineReasonTimeout); | |
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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 sequence_id_recent_set_num_ = 0; | 253 sequence_id_recent_set_num_ = 0; |
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 OnHostOfflineReasonAck(); |
234 base::MessageLoop::current()->PostTask( | |
235 FROM_HERE, | |
236 host_offline_reason_ack_callback_); | |
237 host_offline_reason_ack_callback_.Reset(); | |
238 } | |
239 } | 264 } |
240 } | 265 } |
241 } | 266 } |
242 } | 267 } |
243 | 268 |
244 void HeartbeatSender::SetInterval(int interval) { | 269 void HeartbeatSender::SetInterval(int interval) { |
245 if (interval != interval_ms_) { | 270 if (interval != interval_ms_) { |
246 interval_ms_ = interval; | 271 interval_ms_ = interval; |
247 | 272 |
248 // Restart the timer with the new interval. | 273 // Restart the timer with the new interval. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 heartbeat->AddElement(log.release()); | 328 heartbeat->AddElement(log.release()); |
304 return heartbeat.Pass(); | 329 return heartbeat.Pass(); |
305 } | 330 } |
306 | 331 |
307 scoped_ptr<XmlElement> HeartbeatSender::CreateSignature() { | 332 scoped_ptr<XmlElement> HeartbeatSender::CreateSignature() { |
308 scoped_ptr<XmlElement> signature_tag(new XmlElement( | 333 scoped_ptr<XmlElement> signature_tag(new XmlElement( |
309 QName(kChromotingXmlNamespace, kHeartbeatSignatureTag))); | 334 QName(kChromotingXmlNamespace, kHeartbeatSignatureTag))); |
310 | 335 |
311 std::string message = signal_strategy_->GetLocalJid() + ' ' + | 336 std::string message = signal_strategy_->GetLocalJid() + ' ' + |
312 base::IntToString(sequence_id_); | 337 base::IntToString(sequence_id_); |
313 std::string signature(key_pair_->SignMessage(message)); | 338 std::string signature(host_key_pair_->SignMessage(message)); |
314 signature_tag->AddText(signature); | 339 signature_tag->AddText(signature); |
315 | 340 |
316 return signature_tag.Pass(); | 341 return signature_tag.Pass(); |
317 } | 342 } |
318 | 343 |
319 } // namespace remoting | 344 } // namespace remoting |
OLD | NEW |