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