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