blob: ecd35cad4ea4a3a3bc86b4dbe31e54b907dd675e [file] [log] [blame]
[email protected]564e40d02011-06-21 19:00:361// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]cb3b1f9312010-06-07 19:58:232// 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]41ed1312011-08-26 00:03:477#include "base/bind.h"
[email protected]cb3b1f9312010-06-07 19:58:238#include "base/logging.h"
[email protected]60fc96002011-08-12 23:07:059#include "base/message_loop_proxy.h"
[email protected]cdf8c572010-08-04 23:04:0510#include "base/string_number_conversions.h"
11#include "base/time.h"
[email protected]cb3b1f9312010-06-07 19:58:2312#include "remoting/base/constants.h"
[email protected]34f09f1a2010-06-15 23:00:2613#include "remoting/host/host_config.h"
[email protected]cb3b1f9312010-06-07 19:58:2314#include "remoting/jingle_glue/iq_request.h"
[email protected]cb3b1f9312010-06-07 19:58:2315#include "remoting/jingle_glue/jingle_thread.h"
[email protected]b6e2d6a62011-06-29 22:31:0416#include "remoting/jingle_glue/signal_strategy.h"
[email protected]cdf8c572010-08-04 23:04:0517#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
18#include "third_party/libjingle/source/talk/xmpp/constants.h"
[email protected]cb3b1f9312010-06-07 19:58:2319
[email protected]0e097f902010-12-14 03:05:4020using buzz::QName;
21using buzz::XmlElement;
22
[email protected]cb3b1f9312010-06-07 19:58:2323namespace remoting {
24
25namespace {
[email protected]cdf8c572010-08-04 23:04:0526const char kHeartbeatQueryTag[] = "heartbeat";
27const char kHostIdAttr[] = "hostid";
28const char kHeartbeatSignatureTag[] = "signature";
29const char kSignatureTimeAttr[] = "time";
[email protected]cb3b1f9312010-06-07 19:58:2330
[email protected]0e097f902010-12-14 03:05:4031const char kHeartbeatResultTag[] = "heartbeat-result";
32const char kSetIntervalTag[] = "set-interval";
33
34const int64 kDefaultHeartbeatIntervalMs = 5 * 60 * 1000; // 5 minutes.
[email protected]cb3b1f9312010-06-07 19:58:2335}
36
[email protected]60fc96002011-08-12 23:07:0537HeartbeatSender::HeartbeatSender(base::MessageLoopProxy* message_loop,
[email protected]e05eb1d2011-03-03 15:56:5738 MutableHostConfig* config)
39
[email protected]0e097f902010-12-14 03:05:4040 : state_(CREATED),
[email protected]e05eb1d2011-03-03 15:56:5741 message_loop_(message_loop),
[email protected]e05eb1d2011-03-03 15:56:5742 config_(config),
[email protected]0e097f902010-12-14 03:05:4043 interval_ms_(kDefaultHeartbeatIntervalMs) {
[email protected]e05eb1d2011-03-03 15:56:5744 DCHECK(config_);
[email protected]cb3b1f9312010-06-07 19:58:2345}
46
[email protected]cdf8c572010-08-04 23:04:0547HeartbeatSender::~HeartbeatSender() {
48 DCHECK(state_ == CREATED || state_ == INITIALIZED || state_ == STOPPED);
49}
[email protected]cb3b1f9312010-06-07 19:58:2350
[email protected]e05eb1d2011-03-03 15:56:5751bool HeartbeatSender::Init() {
[email protected]cdf8c572010-08-04 23:04:0552 DCHECK(state_ == CREATED);
[email protected]cb3b1f9312010-06-07 19:58:2353
[email protected]76207352010-06-17 23:43:0054 if (!config_->GetString(kHostIdConfigPath, &host_id_)) {
55 LOG(ERROR) << "host_id is not defined in the config.";
[email protected]cdf8c572010-08-04 23:04:0556 return false;
57 }
58
[email protected]e05eb1d2011-03-03 15:56:5759 if (!key_pair_.Load(config_)) {
[email protected]cdf8c572010-08-04 23:04:0560 return false;
61 }
62
63 state_ = INITIALIZED;
64
65 return true;
66}
67
[email protected]f6bca1f2011-05-03 20:07:4068void HeartbeatSender::OnSignallingConnected(SignalStrategy* signal_strategy,
69 const std::string& full_jid) {
[email protected]60fc96002011-08-12 23:07:0570 DCHECK(message_loop_->BelongsToCurrentThread());
[email protected]f6bca1f2011-05-03 20:07:4071 DCHECK(state_ == INITIALIZED || state_ == STOPPED);
[email protected]cdf8c572010-08-04 23:04:0572 state_ = STARTED;
[email protected]cb3b1f9312010-06-07 19:58:2373
[email protected]f6bca1f2011-05-03 20:07:4074 full_jid_ = full_jid;
75 request_.reset(signal_strategy->CreateIqRequest());
[email protected]41ed1312011-08-26 00:03:4776 request_->set_callback(base::Bind(&HeartbeatSender::ProcessResponse,
77 base::Unretained(this)));
[email protected]cb3b1f9312010-06-07 19:58:2378
[email protected]f6bca1f2011-05-03 20:07:4079 DoSendStanza();
[email protected]d323a172011-09-02 18:23:0280 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(interval_ms_), this,
[email protected]f6bca1f2011-05-03 20:07:4081 &HeartbeatSender::DoSendStanza);
[email protected]cb3b1f9312010-06-07 19:58:2382}
83
[email protected]f6bca1f2011-05-03 20:07:4084void HeartbeatSender::OnSignallingDisconnected() {
[email protected]60fc96002011-08-12 23:07:0585 DCHECK(message_loop_->BelongsToCurrentThread());
[email protected]cdf8c572010-08-04 23:04:0586 state_ = STOPPED;
87 request_.reset(NULL);
88}
89
[email protected]273d0de7c2011-06-20 21:18:1090// Ignore any notifications other than signalling
91// connected/disconnected events.
92void HeartbeatSender::OnAccessDenied() { }
[email protected]f19d9bd2011-09-13 05:21:1193void HeartbeatSender::OnClientAuthenticated(const std::string& jid) { }
94void HeartbeatSender::OnClientDisconnected(const std::string& jid) { }
[email protected]273d0de7c2011-06-20 21:18:1095void HeartbeatSender::OnShutdown() { }
[email protected]f6bca1f2011-05-03 20:07:4096
[email protected]cb3b1f9312010-06-07 19:58:2397void HeartbeatSender::DoSendStanza() {
[email protected]60fc96002011-08-12 23:07:0598 DCHECK(message_loop_->BelongsToCurrentThread());
[email protected]f6bca1f2011-05-03 20:07:4099 DCHECK_EQ(state_, STARTED);
[email protected]cb3b1f9312010-06-07 19:58:23100
[email protected]f6bca1f2011-05-03 20:07:40101 VLOG(1) << "Sending heartbeat stanza to " << kChromotingBotJid;
[email protected]19d963f72011-09-06 22:50:13102 request_->SendIq(IqRequest::MakeIqStanza(
103 buzz::STR_SET, kChromotingBotJid, CreateHeartbeatMessage()));
[email protected]cb3b1f9312010-06-07 19:58:23104}
105
[email protected]0e097f902010-12-14 03:05:40106void HeartbeatSender::ProcessResponse(const XmlElement* response) {
[email protected]60fc96002011-08-12 23:07:05107 DCHECK(message_loop_->BelongsToCurrentThread());
[email protected]f6bca1f2011-05-03 20:07:40108
[email protected]0e097f902010-12-14 03:05:40109 std::string type = response->Attr(buzz::QN_TYPE);
110 if (type == buzz::STR_ERROR) {
[email protected]95def6d2010-06-08 01:50:41111 LOG(ERROR) << "Received error in response to heartbeat: "
112 << response->Str();
[email protected]0e097f902010-12-14 03:05:40113 return;
114 }
115
116 // This method must only be called for error or result stanzas.
[email protected]0116cf52011-11-01 02:44:32117 DCHECK_EQ(std::string(buzz::STR_RESULT), type);
[email protected]0e097f902010-12-14 03:05:40118
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]f6bca1f2011-05-03 20:07:40132 SetInterval(interval * base::Time::kMillisecondsPerSecond);
[email protected]0e097f902010-12-14 03:05:40133 }
134 }
[email protected]95def6d2010-06-08 01:50:41135 }
136}
137
[email protected]f6bca1f2011-05-03 20:07:40138void 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]d323a172011-09-02 18:23:02145 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(interval_ms_),
146 this, &HeartbeatSender::DoSendStanza);
[email protected]f6bca1f2011-05-03 20:07:40147 }
148 }
149}
150
[email protected]0e097f902010-12-14 03:05:40151XmlElement* HeartbeatSender::CreateHeartbeatMessage() {
152 XmlElement* query = new XmlElement(
153 QName(kChromotingXmlNamespace, kHeartbeatQueryTag));
154 query->AddAttr(QName(kChromotingXmlNamespace, kHostIdAttr), host_id_);
[email protected]cdf8c572010-08-04 23:04:05155 query->AddElement(CreateSignature());
156 return query;
157}
158
[email protected]0e097f902010-12-14 03:05:40159XmlElement* HeartbeatSender::CreateSignature() {
160 XmlElement* signature_tag = new XmlElement(
161 QName(kChromotingXmlNamespace, kHeartbeatSignatureTag));
[email protected]cdf8c572010-08-04 23:04:05162
163 int64 time = static_cast<int64>(base::Time::Now().ToDoubleT());
164 std::string time_str(base::Int64ToString(time));
165 signature_tag->AddAttr(
[email protected]0e097f902010-12-14 03:05:40166 QName(kChromotingXmlNamespace, kSignatureTimeAttr), time_str);
[email protected]cdf8c572010-08-04 23:04:05167
[email protected]f6bca1f2011-05-03 20:07:40168 std::string message = full_jid_ + ' ' + time_str;
[email protected]cdf8c572010-08-04 23:04:05169 std::string signature(key_pair_.GetSignature(message));
170 signature_tag->AddText(signature);
171
172 return signature_tag;
173}
174
[email protected]cb3b1f9312010-06-07 19:58:23175} // namespace remoting