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
3 // found in the LICENSE file.
5 #include "remoting/host/heartbeat_sender.h"
10 #include "base/rand_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringize_macros.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "remoting/base/constants.h"
16 #include "remoting/base/logging.h"
17 #include "remoting/host/server_log_entry_host.h"
18 #include "remoting/signaling/iq_sender.h"
19 #include "remoting/signaling/server_log_entry.h"
20 #include "remoting/signaling/signal_strategy.h"
21 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
22 #include "third_party/webrtc/libjingle/xmpp/constants.h"
25 using buzz::XmlElement
;
31 const char kHeartbeatQueryTag
[] = "heartbeat";
32 const char kHostIdAttr
[] = "hostid";
33 const char kHostVersionTag
[] = "host-version";
34 const char kHeartbeatSignatureTag
[] = "signature";
35 const char kSequenceIdAttr
[] = "sequence-id";
36 const char kHostOfflineReasonAttr
[] = "host-offline-reason";
38 const char kErrorTag
[] = "error";
39 const char kNotFoundTag
[] = "item-not-found";
41 const char kHeartbeatResultTag
[] = "heartbeat-result";
42 const char kSetIntervalTag
[] = "set-interval";
43 const char kExpectedSequenceIdTag
[] = "expected-sequence-id";
45 const int64 kDefaultHeartbeatIntervalMs
= 5 * 60 * 1000; // 5 minutes.
46 const int64 kResendDelayMs
= 10 * 1000; // 10 seconds.
47 const int64 kResendDelayOnHostNotFoundMs
= 10 * 1000; // 10 seconds.
48 const int kMaxResendOnHostNotFoundCount
= 12; // 2 minutes (12 x 10 seconds).
52 HeartbeatSender::HeartbeatSender(
53 const base::Closure
& on_heartbeat_successful_callback
,
54 const base::Closure
& on_unknown_host_id_error
,
55 const std::string
& host_id
,
56 SignalStrategy
* signal_strategy
,
57 const scoped_refptr
<const RsaKeyPair
>& host_key_pair
,
58 const std::string
& directory_bot_jid
)
59 : on_heartbeat_successful_callback_(on_heartbeat_successful_callback
),
60 on_unknown_host_id_error_(on_unknown_host_id_error
),
62 signal_strategy_(signal_strategy
),
63 host_key_pair_(host_key_pair
),
64 directory_bot_jid_(directory_bot_jid
),
65 interval_ms_(kDefaultHeartbeatIntervalMs
),
67 sequence_id_was_set_(false),
68 sequence_id_recent_set_num_(0),
69 heartbeat_succeeded_(false),
70 failed_startup_heartbeat_count_(0) {
71 DCHECK(signal_strategy_
);
72 DCHECK(host_key_pair_
.get());
73 DCHECK(thread_checker_
.CalledOnValidThread());
75 signal_strategy_
->AddListener(this);
77 // Start heartbeats if the |signal_strategy_| is already connected.
78 OnSignalStrategyStateChange(signal_strategy_
->GetState());
81 HeartbeatSender::~HeartbeatSender() {
82 signal_strategy_
->RemoveListener(this);
85 void HeartbeatSender::OnSignalStrategyStateChange(SignalStrategy::State state
) {
86 DCHECK(thread_checker_
.CalledOnValidThread());
87 if (state
== SignalStrategy::CONNECTED
) {
88 iq_sender_
.reset(new IqSender(signal_strategy_
));
90 timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(interval_ms_
),
91 this, &HeartbeatSender::SendStanza
);
92 } else if (state
== SignalStrategy::DISCONNECTED
) {
100 bool HeartbeatSender::OnSignalStrategyIncomingStanza(
101 const buzz::XmlElement
* stanza
) {
105 void HeartbeatSender::OnHostOfflineReasonTimeout() {
106 DCHECK(!host_offline_reason_ack_callback_
.is_null());
108 base::Callback
<void(bool)> local_callback
= host_offline_reason_ack_callback_
;
109 host_offline_reason_ack_callback_
.Reset();
110 local_callback
.Run(false);
113 void HeartbeatSender::OnHostOfflineReasonAck() {
114 if (host_offline_reason_ack_callback_
.is_null()) {
115 DCHECK(!host_offline_reason_timeout_timer_
.IsRunning());
119 DCHECK(host_offline_reason_timeout_timer_
.IsRunning());
120 host_offline_reason_timeout_timer_
.Stop();
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);
127 host_offline_reason_ack_callback_
.Reset();
128 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
129 fully_bound_ack_callback
);
132 void HeartbeatSender::SetHostOfflineReason(
133 const std::string
& host_offline_reason
,
134 const base::TimeDelta
& timeout
,
135 const base::Callback
<void(bool success
)>& ack_callback
) {
136 DCHECK(thread_checker_
.CalledOnValidThread());
137 DCHECK(host_offline_reason_ack_callback_
.is_null());
138 host_offline_reason_
= host_offline_reason
;
139 host_offline_reason_ack_callback_
= ack_callback
;
140 host_offline_reason_timeout_timer_
.Start(
141 FROM_HERE
, timeout
, this, &HeartbeatSender::OnHostOfflineReasonTimeout
);
142 if (signal_strategy_
->GetState() == SignalStrategy::CONNECTED
) {
147 void HeartbeatSender::SendStanza() {
148 // Make sure we don't send another heartbeat before the heartbeat interval
150 timer_resend_
.Stop();
154 void HeartbeatSender::ResendStanza() {
155 // Make sure we don't send another heartbeat before the heartbeat interval
161 void HeartbeatSender::DoSendStanza() {
162 DCHECK(thread_checker_
.CalledOnValidThread());
163 DCHECK(signal_strategy_
->GetState() == SignalStrategy::CONNECTED
);
164 VLOG(1) << "Sending heartbeat stanza to " << directory_bot_jid_
;
166 request_
= iq_sender_
->SendIq(
167 buzz::STR_SET
, directory_bot_jid_
, CreateHeartbeatMessage(),
168 base::Bind(&HeartbeatSender::ProcessResponse
,
169 base::Unretained(this),
170 !host_offline_reason_
.empty()));
174 void HeartbeatSender::ProcessResponse(
175 bool is_offline_heartbeat_response
,
177 const XmlElement
* response
) {
178 DCHECK(thread_checker_
.CalledOnValidThread());
180 std::string type
= response
->Attr(buzz::QN_TYPE
);
181 if (type
== buzz::STR_ERROR
) {
182 const XmlElement
* error_element
=
183 response
->FirstNamed(QName(buzz::NS_CLIENT
, kErrorTag
));
185 if (error_element
->FirstNamed(QName(buzz::NS_STANZA
, kNotFoundTag
))) {
186 LOG(ERROR
) << "Received error: Host ID not found";
187 // If the host was registered immediately before it sends a heartbeat,
188 // then server-side latency may prevent the server recognizing the
189 // host ID in the heartbeat. So even if all of the first few heartbeats
190 // get a "host ID not found" error, that's not a good enough reason to
192 failed_startup_heartbeat_count_
++;
193 if (!heartbeat_succeeded_
&& (failed_startup_heartbeat_count_
<=
194 kMaxResendOnHostNotFoundCount
)) {
195 timer_resend_
.Start(FROM_HERE
,
196 base::TimeDelta::FromMilliseconds(
197 kResendDelayOnHostNotFoundMs
),
199 &HeartbeatSender::ResendStanza
);
202 on_unknown_host_id_error_
.Run();
207 LOG(ERROR
) << "Received error in response to heartbeat: "
212 // This method must only be called for error or result stanzas.
213 DCHECK_EQ(std::string(buzz::STR_RESULT
), type
);
215 const XmlElement
* result_element
=
216 response
->FirstNamed(QName(kChromotingXmlNamespace
, kHeartbeatResultTag
));
217 if (result_element
) {
218 const XmlElement
* set_interval_element
=
219 result_element
->FirstNamed(QName(kChromotingXmlNamespace
,
221 if (set_interval_element
) {
222 const std::string
& interval_str
= set_interval_element
->BodyText();
224 if (!base::StringToInt(interval_str
, &interval
) || interval
<= 0) {
225 LOG(ERROR
) << "Received invalid set-interval: "
226 << set_interval_element
->Str();
228 SetInterval(interval
* base::Time::kMillisecondsPerSecond
);
232 bool did_set_sequence_id
= false;
233 const XmlElement
* expected_sequence_id_element
=
234 result_element
->FirstNamed(QName(kChromotingXmlNamespace
,
235 kExpectedSequenceIdTag
));
236 if (expected_sequence_id_element
) {
237 // The sequence ID sent in the previous heartbeat was not what the server
238 // expected, so send another heartbeat with the expected sequence ID.
239 const std::string
& expected_sequence_id_str
=
240 expected_sequence_id_element
->BodyText();
241 int expected_sequence_id
;
242 if (!base::StringToInt(expected_sequence_id_str
, &expected_sequence_id
)) {
243 LOG(ERROR
) << "Received invalid " << kExpectedSequenceIdTag
<< ": " <<
244 expected_sequence_id_element
->Str();
246 SetSequenceId(expected_sequence_id
);
247 sequence_id_recent_set_num_
++;
248 did_set_sequence_id
= true;
251 if (!did_set_sequence_id
) {
252 // It seems the bot accepted our signature and our message.
253 sequence_id_recent_set_num_
= 0;
255 // Notify listener of the first successful heartbeat.
256 if (!heartbeat_succeeded_
) {
257 on_heartbeat_successful_callback_
.Run();
259 heartbeat_succeeded_
= true;
261 // Notify caller of SetHostOfflineReason that we got an ack.
262 if (is_offline_heartbeat_response
) {
263 OnHostOfflineReasonAck();
269 void HeartbeatSender::SetInterval(int interval
) {
270 if (interval
!= interval_ms_
) {
271 interval_ms_
= interval
;
273 // Restart the timer with the new interval.
274 if (timer_
.IsRunning()) {
276 timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(interval_ms_
),
277 this, &HeartbeatSender::SendStanza
);
282 void HeartbeatSender::SetSequenceId(int sequence_id
) {
283 sequence_id_
= sequence_id
;
284 // Setting the sequence ID may be a symptom of a temporary server-side
285 // problem, which would affect many hosts, so don't send a new heartbeat
286 // immediately, as many hosts doing so may overload the server.
287 // But the server will usually set the sequence ID when it receives the first
288 // heartbeat from a host. In that case, we can send a new heartbeat
289 // immediately, as that only happens once per host instance.
290 if (!sequence_id_was_set_
) {
293 HOST_LOG
<< "The heartbeat sequence ID has been set more than once: "
294 << "the new value is " << sequence_id
;
295 double delay
= pow(2.0, sequence_id_recent_set_num_
) *
296 (1 + base::RandDouble()) * kResendDelayMs
;
297 if (delay
<= interval_ms_
) {
298 timer_resend_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(delay
),
299 this, &HeartbeatSender::ResendStanza
);
302 sequence_id_was_set_
= true;
305 scoped_ptr
<XmlElement
> HeartbeatSender::CreateHeartbeatMessage() {
306 // Create heartbeat stanza.
307 scoped_ptr
<XmlElement
> heartbeat(new XmlElement(
308 QName(kChromotingXmlNamespace
, kHeartbeatQueryTag
)));
309 heartbeat
->AddAttr(QName(kChromotingXmlNamespace
, kHostIdAttr
), host_id_
);
310 heartbeat
->AddAttr(QName(kChromotingXmlNamespace
, kSequenceIdAttr
),
311 base::IntToString(sequence_id_
));
312 if (!host_offline_reason_
.empty()) {
314 QName(kChromotingXmlNamespace
, kHostOfflineReasonAttr
),
315 host_offline_reason_
);
317 heartbeat
->AddElement(CreateSignature().release());
318 // Append host version.
319 scoped_ptr
<XmlElement
> version_tag(new XmlElement(
320 QName(kChromotingXmlNamespace
, kHostVersionTag
)));
321 version_tag
->AddText(STRINGIZE(VERSION
));
322 heartbeat
->AddElement(version_tag
.release());
323 // Append log message (which isn't signed).
324 scoped_ptr
<XmlElement
> log(ServerLogEntry::MakeStanza());
325 scoped_ptr
<ServerLogEntry
> log_entry(MakeLogEntryForHeartbeat());
326 AddHostFieldsToLogEntry(log_entry
.get());
327 log
->AddElement(log_entry
->ToStanza().release());
328 heartbeat
->AddElement(log
.release());
329 return heartbeat
.Pass();
332 scoped_ptr
<XmlElement
> HeartbeatSender::CreateSignature() {
333 scoped_ptr
<XmlElement
> signature_tag(new XmlElement(
334 QName(kChromotingXmlNamespace
, kHeartbeatSignatureTag
)));
336 std::string message
= signal_strategy_
->GetLocalJid() + ' ' +
337 base::IntToString(sequence_id_
);
338 std::string
signature(host_key_pair_
->SignMessage(message
));
339 signature_tag
->AddText(signature
);
341 return signature_tag
.Pass();
344 } // namespace remoting