[email protected] | 564e40d0 | 2011-06-21 19:00:36 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
| 7 | #include "base/logging.h" |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 8 | #include "base/message_loop_proxy.h" |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 9 | #include "base/string_number_conversions.h" |
| 10 | #include "base/time.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 11 | #include "remoting/base/constants.h" |
[email protected] | 34f09f1a | 2010-06-15 23:00:26 | [diff] [blame] | 12 | #include "remoting/host/host_config.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 13 | #include "remoting/jingle_glue/iq_request.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 14 | #include "remoting/jingle_glue/jingle_thread.h" |
[email protected] | b6e2d6a6 | 2011-06-29 22:31:04 | [diff] [blame] | 15 | #include "remoting/jingle_glue/signal_strategy.h" |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 16 | #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 17 | #include "third_party/libjingle/source/talk/xmpp/constants.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 18 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 19 | using buzz::QName; |
| 20 | using buzz::XmlElement; |
| 21 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 22 | namespace remoting { |
| 23 | |
| 24 | namespace { |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 25 | const char kHeartbeatQueryTag[] = "heartbeat"; |
| 26 | const char kHostIdAttr[] = "hostid"; |
| 27 | const char kHeartbeatSignatureTag[] = "signature"; |
| 28 | const char kSignatureTimeAttr[] = "time"; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 29 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 30 | const char kHeartbeatResultTag[] = "heartbeat-result"; |
| 31 | const char kSetIntervalTag[] = "set-interval"; |
| 32 | |
| 33 | const int64 kDefaultHeartbeatIntervalMs = 5 * 60 * 1000; // 5 minutes. |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 36 | HeartbeatSender::HeartbeatSender(base::MessageLoopProxy* message_loop, |
[email protected] | e05eb1d | 2011-03-03 15:56:57 | [diff] [blame] | 37 | MutableHostConfig* config) |
| 38 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 39 | : state_(CREATED), |
[email protected] | e05eb1d | 2011-03-03 15:56:57 | [diff] [blame] | 40 | message_loop_(message_loop), |
[email protected] | e05eb1d | 2011-03-03 15:56:57 | [diff] [blame] | 41 | config_(config), |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 42 | interval_ms_(kDefaultHeartbeatIntervalMs) { |
[email protected] | e05eb1d | 2011-03-03 15:56:57 | [diff] [blame] | 43 | DCHECK(config_); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 46 | HeartbeatSender::~HeartbeatSender() { |
| 47 | DCHECK(state_ == CREATED || state_ == INITIALIZED || state_ == STOPPED); |
| 48 | } |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 49 | |
[email protected] | e05eb1d | 2011-03-03 15:56:57 | [diff] [blame] | 50 | bool HeartbeatSender::Init() { |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 51 | DCHECK(state_ == CREATED); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 52 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 53 | if (!config_->GetString(kHostIdConfigPath, &host_id_)) { |
| 54 | LOG(ERROR) << "host_id is not defined in the config."; |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
[email protected] | e05eb1d | 2011-03-03 15:56:57 | [diff] [blame] | 58 | if (!key_pair_.Load(config_)) { |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
| 62 | state_ = INITIALIZED; |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 67 | void HeartbeatSender::OnSignallingConnected(SignalStrategy* signal_strategy, |
| 68 | const std::string& full_jid) { |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 69 | DCHECK(message_loop_->BelongsToCurrentThread()); |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 70 | DCHECK(state_ == INITIALIZED || state_ == STOPPED); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 71 | state_ = STARTED; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 72 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 73 | full_jid_ = full_jid; |
| 74 | request_.reset(signal_strategy->CreateIqRequest()); |
[email protected] | 95def6d | 2010-06-08 01:50:41 | [diff] [blame] | 75 | request_->set_callback(NewCallback(this, &HeartbeatSender::ProcessResponse)); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 76 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 77 | DoSendStanza(); |
| 78 | timer_.Start(base::TimeDelta::FromMilliseconds(interval_ms_), this, |
| 79 | &HeartbeatSender::DoSendStanza); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 82 | void HeartbeatSender::OnSignallingDisconnected() { |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 83 | DCHECK(message_loop_->BelongsToCurrentThread()); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 84 | state_ = STOPPED; |
| 85 | request_.reset(NULL); |
| 86 | } |
| 87 | |
[email protected] | 273d0de7c | 2011-06-20 21:18:10 | [diff] [blame] | 88 | // Ignore any notifications other than signalling |
| 89 | // connected/disconnected events. |
| 90 | void HeartbeatSender::OnAccessDenied() { } |
[email protected] | e0080400 | 2011-08-03 00:10:38 | [diff] [blame] | 91 | void HeartbeatSender::OnClientAuthenticated( |
| 92 | remoting::protocol::ConnectionToClient* client) { } |
| 93 | void HeartbeatSender::OnClientDisconnected( |
| 94 | remoting::protocol::ConnectionToClient* client) { } |
[email protected] | 273d0de7c | 2011-06-20 21:18:10 | [diff] [blame] | 95 | void HeartbeatSender::OnShutdown() { } |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 96 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 97 | void HeartbeatSender::DoSendStanza() { |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 98 | DCHECK(message_loop_->BelongsToCurrentThread()); |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 99 | DCHECK_EQ(state_, STARTED); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 100 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 101 | VLOG(1) << "Sending heartbeat stanza to " << kChromotingBotJid; |
| 102 | request_->SendIq(buzz::STR_SET, kChromotingBotJid, CreateHeartbeatMessage()); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 105 | void HeartbeatSender::ProcessResponse(const XmlElement* response) { |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 106 | DCHECK(message_loop_->BelongsToCurrentThread()); |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 107 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 108 | std::string type = response->Attr(buzz::QN_TYPE); |
| 109 | if (type == buzz::STR_ERROR) { |
[email protected] | 95def6d | 2010-06-08 01:50:41 | [diff] [blame] | 110 | LOG(ERROR) << "Received error in response to heartbeat: " |
| 111 | << response->Str(); |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | // This method must only be called for error or result stanzas. |
| 116 | DCHECK_EQ(buzz::STR_RESULT, type); |
| 117 | |
| 118 | const XmlElement* result_element = |
| 119 | response->FirstNamed(QName(kChromotingXmlNamespace, kHeartbeatResultTag)); |
| 120 | if (result_element) { |
| 121 | const XmlElement* set_interval_element = |
| 122 | result_element->FirstNamed(QName(kChromotingXmlNamespace, |
| 123 | kSetIntervalTag)); |
| 124 | if (set_interval_element) { |
| 125 | const std::string& interval_str = set_interval_element->BodyText(); |
| 126 | int interval; |
| 127 | if (!base::StringToInt(interval_str, &interval) || interval <= 0) { |
| 128 | LOG(ERROR) << "Received invalid set-interval: " |
| 129 | << set_interval_element->Str(); |
| 130 | } else { |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 131 | SetInterval(interval * base::Time::kMillisecondsPerSecond); |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 132 | } |
| 133 | } |
[email protected] | 95def6d | 2010-06-08 01:50:41 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 137 | void HeartbeatSender::SetInterval(int interval) { |
| 138 | if (interval != interval_ms_) { |
| 139 | interval_ms_ = interval; |
| 140 | |
| 141 | // Restart the timer with the new interval. |
| 142 | if (state_ == STARTED) { |
| 143 | timer_.Stop(); |
| 144 | timer_.Start(base::TimeDelta::FromMilliseconds(interval_ms_), this, |
| 145 | &HeartbeatSender::DoSendStanza); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 150 | XmlElement* HeartbeatSender::CreateHeartbeatMessage() { |
| 151 | XmlElement* query = new XmlElement( |
| 152 | QName(kChromotingXmlNamespace, kHeartbeatQueryTag)); |
| 153 | query->AddAttr(QName(kChromotingXmlNamespace, kHostIdAttr), host_id_); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 154 | query->AddElement(CreateSignature()); |
| 155 | return query; |
| 156 | } |
| 157 | |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 158 | XmlElement* HeartbeatSender::CreateSignature() { |
| 159 | XmlElement* signature_tag = new XmlElement( |
| 160 | QName(kChromotingXmlNamespace, kHeartbeatSignatureTag)); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 161 | |
| 162 | int64 time = static_cast<int64>(base::Time::Now().ToDoubleT()); |
| 163 | std::string time_str(base::Int64ToString(time)); |
| 164 | signature_tag->AddAttr( |
[email protected] | 0e097f90 | 2010-12-14 03:05:40 | [diff] [blame] | 165 | QName(kChromotingXmlNamespace, kSignatureTimeAttr), time_str); |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 166 | |
[email protected] | f6bca1f | 2011-05-03 20:07:40 | [diff] [blame] | 167 | std::string message = full_jid_ + ' ' + time_str; |
[email protected] | cdf8c57 | 2010-08-04 23:04:05 | [diff] [blame] | 168 | std::string signature(key_pair_.GetSignature(message)); |
| 169 | signature_tag->AddText(signature); |
| 170 | |
| 171 | return signature_tag; |
| 172 | } |
| 173 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 174 | } // namespace remoting |