[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/host/heartbeat_sender.h" |
| 6 | |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 7 | #include <math.h> |
| 8 | |
[email protected] | 41ed131 | 2011-08-26 00:03:47 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame] | 11 | #include "base/message_loop_proxy.h" |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 12 | #include "base/rand_util.h" |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 13 | #include "base/string_number_conversions.h" |
| 14 | #include "base/time.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 15 | #include "remoting/base/constants.h" |
[email protected] | bd7b911d | 2012-05-22 16:44:12 | [diff] [blame] | 16 | #include "remoting/host/server_log_entry.h" |
[email protected] | b39e182 | 2011-11-04 01:00:49 | [diff] [blame] | 17 | #include "remoting/jingle_glue/iq_sender.h" |
[email protected] | b6e2d6a6 | 2011-06-29 22:31:04 | [diff] [blame] | 18 | #include "remoting/jingle_glue/signal_strategy.h" |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 19 | #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 20 | #include "third_party/libjingle/source/talk/xmpp/constants.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 21 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 22 | using buzz::QName; |
| 23 | using buzz::XmlElement; |
| 24 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 25 | namespace remoting { |
| 26 | |
| 27 | namespace { |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 28 | |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 29 | const char kHeartbeatQueryTag[] = "heartbeat"; |
| 30 | const char kHostIdAttr[] = "hostid"; |
| 31 | const char kHeartbeatSignatureTag[] = "signature"; |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 32 | const char kSequenceIdAttr[] = "sequence-id"; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 33 | |
[email protected] | f43970a | 2012-02-01 04:22:43 | [diff] [blame] | 34 | const char kErrorTag[] = "error"; |
| 35 | const char kNotFoundTag[] = "item-not-found"; |
| 36 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 37 | const char kHeartbeatResultTag[] = "heartbeat-result"; |
| 38 | const char kSetIntervalTag[] = "set-interval"; |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 39 | const char kExpectedSequenceIdTag[] = "expected-sequence-id"; |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 40 | |
| 41 | const int64 kDefaultHeartbeatIntervalMs = 5 * 60 * 1000; // 5 minutes. |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 42 | const int64 kResendDelayMs = 10 * 1000; // 10 seconds. |
[email protected] | 67d948d0 | 2012-04-12 03:56:39 | [diff] [blame] | 43 | const int64 kResendDelayOnHostNotFoundMs = 10 * 1000; // 10 seconds. |
| 44 | const int kMaxResendOnHostNotFoundCount = 12; // 2 minutes (12 x 10 seconds). |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 45 | |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 46 | } // namespace |
| 47 | |
| 48 | HeartbeatSender::HeartbeatSender( |
[email protected] | ac31b78 | 2012-04-17 22:30:54 | [diff] [blame] | 49 | Listener* listener, |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 50 | const std::string& host_id, |
| 51 | SignalStrategy* signal_strategy, |
| 52 | HostKeyPair* key_pair) |
[email protected] | ac31b78 | 2012-04-17 22:30:54 | [diff] [blame] | 53 | : listener_(listener), |
| 54 | host_id_(host_id), |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 55 | signal_strategy_(signal_strategy), |
| 56 | key_pair_(key_pair), |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 57 | interval_ms_(kDefaultHeartbeatIntervalMs), |
| 58 | sequence_id_(0), |
| 59 | sequence_id_was_set_(false), |
[email protected] | 67d948d0 | 2012-04-12 03:56:39 | [diff] [blame] | 60 | sequence_id_recent_set_num_(0), |
| 61 | heartbeat_succeeded_(false), |
| 62 | failed_startup_heartbeat_count_(0) { |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 63 | DCHECK(signal_strategy_); |
| 64 | DCHECK(key_pair_); |
| 65 | |
| 66 | signal_strategy_->AddListener(this); |
| 67 | |
| 68 | // Start heartbeats if the |signal_strategy_| is already connected. |
| 69 | OnSignalStrategyStateChange(signal_strategy_->GetState()); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 72 | HeartbeatSender::~HeartbeatSender() { |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 73 | signal_strategy_->RemoveListener(this); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 76 | void HeartbeatSender::OnSignalStrategyStateChange(SignalStrategy::State state) { |
| 77 | if (state == SignalStrategy::CONNECTED) { |
[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 78 | iq_sender_.reset(new IqSender(signal_strategy_)); |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 79 | SendStanza(); |
[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 80 | timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(interval_ms_), |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 81 | this, &HeartbeatSender::SendStanza); |
[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 82 | } else if (state == SignalStrategy::DISCONNECTED) { |
[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 83 | request_.reset(); |
| 84 | iq_sender_.reset(); |
| 85 | timer_.Stop(); |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 86 | timer_resend_.Stop(); |
[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 87 | } |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | c172d697 | 2012-08-09 22:18:21 | [diff] [blame] | 90 | bool HeartbeatSender::OnSignalStrategyIncomingStanza( |
| 91 | const buzz::XmlElement* stanza) { |
| 92 | return false; |
| 93 | } |
| 94 | |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 95 | void HeartbeatSender::SendStanza() { |
| 96 | DoSendStanza(); |
| 97 | // Make sure we don't send another heartbeat before the heartbeat interval |
| 98 | // has expired. |
| 99 | timer_resend_.Stop(); |
| 100 | } |
| 101 | |
| 102 | void HeartbeatSender::ResendStanza() { |
| 103 | DoSendStanza(); |
| 104 | // Make sure we don't send another heartbeat before the heartbeat interval |
| 105 | // has expired. |
| 106 | timer_.Reset(); |
| 107 | } |
| 108 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 109 | void HeartbeatSender::DoSendStanza() { |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 110 | VLOG(1) << "Sending heartbeat stanza to " << kChromotingBotJid; |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 111 | request_ = iq_sender_->SendIq( |
[email protected] | b39e182 | 2011-11-04 01:00:49 | [diff] [blame] | 112 | buzz::STR_SET, kChromotingBotJid, CreateHeartbeatMessage(), |
| 113 | base::Bind(&HeartbeatSender::ProcessResponse, |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 114 | base::Unretained(this))); |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 115 | ++sequence_id_; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 137e7cd | 2012-02-26 00:28:07 | [diff] [blame] | 118 | void HeartbeatSender::ProcessResponse(IqRequest* request, |
| 119 | const XmlElement* response) { |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 120 | std::string type = response->Attr(buzz::QN_TYPE); |
| 121 | if (type == buzz::STR_ERROR) { |
[email protected] | f43970a | 2012-02-01 04:22:43 | [diff] [blame] | 122 | const XmlElement* error_element = |
| 123 | response->FirstNamed(QName(buzz::NS_CLIENT, kErrorTag)); |
| 124 | if (error_element) { |
| 125 | if (error_element->FirstNamed(QName(buzz::NS_STANZA, kNotFoundTag))) { |
[email protected] | 67d948d0 | 2012-04-12 03:56:39 | [diff] [blame] | 126 | LOG(ERROR) << "Received error: Host ID not found"; |
| 127 | // If the host was registered immediately before it sends a heartbeat, |
| 128 | // then server-side latency may prevent the server recognizing the |
| 129 | // host ID in the heartbeat. So even if all of the first few heartbeats |
| 130 | // get a "host ID not found" error, that's not a good enough reason to |
| 131 | // exit. |
| 132 | failed_startup_heartbeat_count_++; |
| 133 | if (!heartbeat_succeeded_ && (failed_startup_heartbeat_count_ <= |
| 134 | kMaxResendOnHostNotFoundCount)) { |
| 135 | timer_resend_.Start(FROM_HERE, |
| 136 | base::TimeDelta::FromMilliseconds( |
| 137 | kResendDelayOnHostNotFoundMs), |
| 138 | this, |
| 139 | &HeartbeatSender::ResendStanza); |
| 140 | return; |
| 141 | } |
[email protected] | ac31b78 | 2012-04-17 22:30:54 | [diff] [blame] | 142 | listener_->OnUnknownHostIdError(); |
| 143 | return; |
[email protected] | f43970a | 2012-02-01 04:22:43 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
[email protected] | 95def6d | 2010-06-08 01:50:41 | [diff] [blame] | 147 | LOG(ERROR) << "Received error in response to heartbeat: " |
| 148 | << response->Str(); |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
[email protected] | 67d948d0 | 2012-04-12 03:56:39 | [diff] [blame] | 152 | heartbeat_succeeded_ = true; |
| 153 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 154 | // This method must only be called for error or result stanzas. |
[email protected] | 0116cf5 | 2011-11-01 02:44:32 | [diff] [blame] | 155 | DCHECK_EQ(std::string(buzz::STR_RESULT), type); |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 156 | |
| 157 | const XmlElement* result_element = |
| 158 | response->FirstNamed(QName(kChromotingXmlNamespace, kHeartbeatResultTag)); |
| 159 | if (result_element) { |
| 160 | const XmlElement* set_interval_element = |
| 161 | result_element->FirstNamed(QName(kChromotingXmlNamespace, |
| 162 | kSetIntervalTag)); |
| 163 | if (set_interval_element) { |
| 164 | const std::string& interval_str = set_interval_element->BodyText(); |
| 165 | int interval; |
| 166 | if (!base::StringToInt(interval_str, &interval) || interval <= 0) { |
| 167 | LOG(ERROR) << "Received invalid set-interval: " |
| 168 | << set_interval_element->Str(); |
| 169 | } else { |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 170 | SetInterval(interval * base::Time::kMillisecondsPerSecond); |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 171 | } |
| 172 | } |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 173 | |
| 174 | bool did_set_sequence_id = false; |
| 175 | const XmlElement* expected_sequence_id_element = |
| 176 | result_element->FirstNamed(QName(kChromotingXmlNamespace, |
| 177 | kExpectedSequenceIdTag)); |
| 178 | if (expected_sequence_id_element) { |
| 179 | // The sequence ID sent in the previous heartbeat was not what the server |
| 180 | // expected, so send another heartbeat with the expected sequence ID. |
| 181 | const std::string& expected_sequence_id_str = |
| 182 | expected_sequence_id_element->BodyText(); |
| 183 | int expected_sequence_id; |
| 184 | if (!base::StringToInt(expected_sequence_id_str, &expected_sequence_id)) { |
| 185 | LOG(ERROR) << "Received invalid " << kExpectedSequenceIdTag << ": " << |
| 186 | expected_sequence_id_element->Str(); |
| 187 | } else { |
| 188 | SetSequenceId(expected_sequence_id); |
| 189 | sequence_id_recent_set_num_++; |
| 190 | did_set_sequence_id = true; |
| 191 | } |
| 192 | } |
| 193 | if (!did_set_sequence_id) { |
| 194 | sequence_id_recent_set_num_ = 0; |
| 195 | } |
[email protected] | 95def6d | 2010-06-08 01:50:41 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 199 | void HeartbeatSender::SetInterval(int interval) { |
| 200 | if (interval != interval_ms_) { |
| 201 | interval_ms_ = interval; |
| 202 | |
| 203 | // Restart the timer with the new interval. |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 204 | if (timer_.IsRunning()) { |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 205 | timer_.Stop(); |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 206 | timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(interval_ms_), |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 207 | this, &HeartbeatSender::SendStanza); |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 212 | void HeartbeatSender::SetSequenceId(int sequence_id) { |
| 213 | sequence_id_ = sequence_id; |
| 214 | // Setting the sequence ID may be a symptom of a temporary server-side |
| 215 | // problem, which would affect many hosts, so don't send a new heartbeat |
| 216 | // immediately, as many hosts doing so may overload the server. |
| 217 | // But the server will usually set the sequence ID when it receives the first |
| 218 | // heartbeat from a host. In that case, we can send a new heartbeat |
| 219 | // immediately, as that only happens once per host instance. |
| 220 | if (!sequence_id_was_set_) { |
| 221 | ResendStanza(); |
| 222 | } else { |
| 223 | LOG(INFO) << "The heartbeat sequence ID has been set more than once: " |
| 224 | << "the new value is " << sequence_id; |
| 225 | double delay = pow(2.0, sequence_id_recent_set_num_) * |
| 226 | (1 + base::RandDouble()) * kResendDelayMs; |
| 227 | if (delay <= interval_ms_) { |
| 228 | timer_resend_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(delay), |
| 229 | this, &HeartbeatSender::ResendStanza); |
| 230 | } |
| 231 | } |
| 232 | sequence_id_was_set_ = true; |
| 233 | } |
| 234 | |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 235 | scoped_ptr<XmlElement> HeartbeatSender::CreateHeartbeatMessage() { |
[email protected] | bd7b911d | 2012-05-22 16:44:12 | [diff] [blame] | 236 | // Create heartbeat stanza. |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 237 | scoped_ptr<XmlElement> query(new XmlElement( |
| 238 | QName(kChromotingXmlNamespace, kHeartbeatQueryTag))); |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 239 | query->AddAttr(QName(kChromotingXmlNamespace, kHostIdAttr), host_id_); |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 240 | query->AddAttr(QName(kChromotingXmlNamespace, kSequenceIdAttr), |
| 241 | base::IntToString(sequence_id_)); |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 242 | query->AddElement(CreateSignature().release()); |
[email protected] | bd7b911d | 2012-05-22 16:44:12 | [diff] [blame] | 243 | // Append log message (which isn't signed). |
| 244 | scoped_ptr<XmlElement> log(ServerLogEntry::MakeStanza()); |
| 245 | scoped_ptr<ServerLogEntry> log_entry(ServerLogEntry::MakeForHeartbeat()); |
| 246 | log_entry->AddHostFields(); |
| 247 | log->AddElement(log_entry->ToStanza().release()); |
| 248 | query->AddElement(log.release()); |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 249 | return query.Pass(); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 252 | scoped_ptr<XmlElement> HeartbeatSender::CreateSignature() { |
| 253 | scoped_ptr<XmlElement> signature_tag(new XmlElement( |
| 254 | QName(kChromotingXmlNamespace, kHeartbeatSignatureTag))); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 255 | |
[email protected] | da32a46 | 2012-02-08 01:43:26 | [diff] [blame] | 256 | std::string message = signal_strategy_->GetLocalJid() + ' ' + |
| 257 | base::IntToString(sequence_id_); |
[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 258 | std::string signature(key_pair_->GetSignature(message)); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 259 | signature_tag->AddText(signature); |
| 260 | |
[email protected] | cff2764 | 2012-02-23 12:06:19 | [diff] [blame] | 261 | return signature_tag.Pass(); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 264 | } // namespace remoting |