1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/heartbeat_sender.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_number_conversions.h"
#include "base/time.h"
#include "remoting/base/constants.h"
#include "remoting/host/host_config.h"
#include "remoting/jingle_glue/iq_request.h"
#include "remoting/jingle_glue/jingle_client.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
#include "third_party/libjingle/source/talk/xmpp/constants.h"
namespace remoting {
namespace {
const char kHeartbeatQueryTag[] = "heartbeat";
const char kHostIdAttr[] = "hostid";
const char kHeartbeatSignatureTag[] = "signature";
const char kSignatureTimeAttr[] = "time";
// TODO(sergeyu): Make this configurable by the cloud.
const int64 kHeartbeatPeriodMs = 5 * 60 * 1000; // 5 minutes.
}
HeartbeatSender::HeartbeatSender()
: state_(CREATED) {
}
HeartbeatSender::~HeartbeatSender() {
DCHECK(state_ == CREATED || state_ == INITIALIZED || state_ == STOPPED);
}
bool HeartbeatSender::Init(MutableHostConfig* config,
JingleClient* jingle_client) {
DCHECK(jingle_client);
DCHECK(state_ == CREATED);
jingle_client_ = jingle_client;
config_ = config;
if (!config_->GetString(kHostIdConfigPath, &host_id_)) {
LOG(ERROR) << "host_id is not defined in the config.";
return false;
}
if (!key_pair_.Load(config)) {
return false;
}
state_ = INITIALIZED;
return true;
}
void HeartbeatSender::Start() {
if (MessageLoop::current() != jingle_client_->message_loop()) {
jingle_client_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::Start));
return;
}
DCHECK_EQ(INITIALIZED, state_);
state_ = STARTED;
request_.reset(jingle_client_->CreateIqRequest());
request_->set_callback(NewCallback(this, &HeartbeatSender::ProcessResponse));
jingle_client_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza));
}
void HeartbeatSender::Stop() {
if (MessageLoop::current() != jingle_client_->message_loop()) {
jingle_client_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::Stop));
return;
}
// We may call Stop() even if we have not started.
if (state_ != STARTED)
return;
state_ = STOPPED;
request_.reset(NULL);
}
void HeartbeatSender::DoSendStanza() {
if (state_ == STARTED) {
// |jingle_client_| may be already destroyed if |state_| is set to
// |STOPPED|, so don't touch it here unless we are in |STARTED| state.
DCHECK(MessageLoop::current() == jingle_client_->message_loop());
LOG(INFO) << "Sending heartbeat stanza to " << kChromotingBotJid;
request_->SendIq(buzz::STR_SET, kChromotingBotJid,
CreateHeartbeatMessage());
// Schedule next heartbeat.
jingle_client_->message_loop()->PostDelayedTask(
FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza),
kHeartbeatPeriodMs);
}
}
void HeartbeatSender::ProcessResponse(const buzz::XmlElement* response) {
if (response->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) {
LOG(ERROR) << "Received error in response to heartbeat: "
<< response->Str();
}
}
buzz::XmlElement* HeartbeatSender::CreateHeartbeatMessage() {
buzz::XmlElement* query = new buzz::XmlElement(
buzz::QName(kChromotingXmlNamespace, kHeartbeatQueryTag));
query->AddAttr(buzz::QName(kChromotingXmlNamespace, kHostIdAttr), host_id_);
query->AddElement(CreateSignature());
return query;
}
buzz::XmlElement* HeartbeatSender::CreateSignature() {
buzz::XmlElement* signature_tag = new buzz::XmlElement(
buzz::QName(kChromotingXmlNamespace, kHeartbeatSignatureTag));
int64 time = static_cast<int64>(base::Time::Now().ToDoubleT());
std::string time_str(base::Int64ToString(time));
signature_tag->AddAttr(
buzz::QName(kChromotingXmlNamespace, kSignatureTimeAttr), time_str);
std::string message = jingle_client_->GetFullJid() + ' ' + time_str;
std::string signature(key_pair_.GetSignature(message));
signature_tag->AddText(signature);
return signature_tag;
}
} // namespace remoting
|