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" |
13 #include "remoting/host/host_key_pair.h" | |
11 #include "remoting/jingle_glue/iq_request.h" | 14 #include "remoting/jingle_glue/iq_request.h" |
12 #include "remoting/jingle_glue/jingle_client.h" | 15 #include "remoting/jingle_glue/jingle_client.h" |
13 #include "remoting/jingle_glue/jingle_thread.h" | 16 #include "remoting/jingle_glue/jingle_thread.h" |
14 #include "talk/xmpp/constants.h" | 17 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
15 #include "talk/xmllite/xmlelement.h" | 18 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
16 | 19 |
17 namespace remoting { | 20 namespace remoting { |
18 | 21 |
19 namespace { | 22 namespace { |
20 const char * const kChromotingNamespace = "google:remoting"; | 23 const char kHeartbeatQueryTag[] = "heartbeat"; |
21 const buzz::QName kHeartbeatQuery(true, kChromotingNamespace, "heartbeat"); | 24 const char kHostIdAttr[] = "hostid"; |
22 const buzz::QName kHostIdAttr(true, kChromotingNamespace, "hostid"); | 25 const char kHeartbeatSignatureTag[] = "signature"; |
26 const char kSignatureTimeAttr[] = "time"; | |
23 | 27 |
24 // TODO(sergeyu): Make this configurable by the cloud. | 28 // TODO(sergeyu): Make this configurable by the cloud. |
25 const int64 kHeartbeatPeriodMs = 5 * 60 * 1000; // 5 minutes. | 29 const int64 kHeartbeatPeriodMs = 5 * 60 * 1000; // 5 minutes. |
26 } | 30 } |
27 | 31 |
28 HeartbeatSender::HeartbeatSender() | 32 HeartbeatSender::HeartbeatSender() |
29 : started_(false) { | 33 : state_(CREATED) { |
30 } | 34 } |
31 | 35 |
32 void HeartbeatSender::Start(MutableHostConfig* config, JingleClient* jingle_clie nt) { | 36 HeartbeatSender::~HeartbeatSender() { |
37 DCHECK(state_ != STARTED); | |
awong
2010/08/02 19:53:49
Should this be a positive filter? That is, specif
Sergey Ulanov
2010/08/03 02:10:39
Done.
| |
38 } | |
39 | |
40 bool HeartbeatSender::Init(MutableHostConfig* config, | |
41 JingleClient* jingle_client) { | |
33 DCHECK(jingle_client); | 42 DCHECK(jingle_client); |
34 DCHECK(!started_); | 43 DCHECK(state_ == CREATED); |
35 | |
36 started_ = true; | |
37 | 44 |
38 jingle_client_ = jingle_client; | 45 jingle_client_ = jingle_client; |
39 config_ = config; | 46 config_ = config; |
40 | 47 |
41 if (!config_->GetString(kHostIdConfigPath, &host_id_)) { | 48 if (!config_->GetString(kHostIdConfigPath, &host_id_)) { |
42 LOG(ERROR) << "host_id is not defined in the config."; | 49 LOG(ERROR) << "host_id is not defined in the config."; |
43 return; | 50 return false; |
44 } | 51 } |
45 | 52 |
53 key_pair_ = new HostKeyPair(); | |
54 if (!key_pair_->Load(config)) { | |
55 return false; | |
56 } | |
57 | |
58 state_ = INITIALIZED; | |
59 | |
60 return true; | |
61 } | |
62 | |
63 void HeartbeatSender::Start() { | |
64 DCHECK(state_ == INITIALIZED); | |
65 state_ = STARTED; | |
46 jingle_client_->message_loop()->PostTask( | 66 jingle_client_->message_loop()->PostTask( |
47 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoStart)); | 67 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoStart)); |
48 } | 68 } |
49 | 69 |
70 void HeartbeatSender::Stop() { | |
71 DCHECK(state_ == STARTED); | |
72 state_ = STOPPED; | |
73 jingle_client_->message_loop()->PostTask( | |
74 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoStop)); | |
75 } | |
76 | |
50 void HeartbeatSender::DoStart() { | 77 void HeartbeatSender::DoStart() { |
51 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); | 78 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
52 | 79 |
53 request_.reset(new IqRequest(jingle_client_)); | 80 request_.reset(jingle_client_->CreateIqRequest()); |
54 request_->set_callback(NewCallback(this, &HeartbeatSender::ProcessResponse)); | 81 request_->set_callback(NewCallback(this, &HeartbeatSender::ProcessResponse)); |
55 | 82 |
56 jingle_client_->message_loop()->PostTask( | 83 jingle_client_->message_loop()->PostTask( |
57 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza)); | 84 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza)); |
58 } | 85 } |
59 | 86 |
60 void HeartbeatSender::DoSendStanza() { | 87 void HeartbeatSender::DoStop() { |
61 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); | 88 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
62 | 89 |
63 LOG(INFO) << "Sending heartbeat stanza to " << kChromotingBotJid; | 90 request_.reset(NULL); |
91 } | |
64 | 92 |
65 buzz::XmlElement* stanza = new buzz::XmlElement(kHeartbeatQuery); | 93 void HeartbeatSender::DoSendStanza() { |
66 stanza->AddAttr(kHostIdAttr, host_id_); | 94 if (state_ == STARTED) { |
67 request_->SendIq(buzz::STR_SET, kChromotingBotJid, stanza); | 95 // |jingle_client_| may be already destroyed if |state_| is set to |
96 // |STOPPED|, so don't touch it here unless we are in |STARTED| state. | |
97 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); | |
68 | 98 |
69 // Schedule next heartbeat. | 99 LOG(INFO) << "Sending heartbeat stanza to " << kChromotingBotJid; |
70 jingle_client_->message_loop()->PostDelayedTask( | 100 |
71 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza), | 101 buzz::XmlElement* query = new buzz::XmlElement( |
72 kHeartbeatPeriodMs); | 102 buzz::QName(kChromotingXmlNamespace, kHeartbeatQueryTag)); |
103 query->AddAttr(buzz::QName(kChromotingXmlNamespace, kHostIdAttr), host_id_); | |
104 | |
105 query->AddElement(CreateSignature(jingle_client_->GetFullJid())); | |
106 | |
107 request_->SendIq(buzz::STR_SET, kChromotingBotJid, query); | |
108 | |
109 // Schedule next heartbeat. | |
110 jingle_client_->message_loop()->PostDelayedTask( | |
111 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza), | |
112 kHeartbeatPeriodMs); | |
113 } | |
73 } | 114 } |
74 | 115 |
75 void HeartbeatSender::ProcessResponse(const buzz::XmlElement* response) { | 116 void HeartbeatSender::ProcessResponse(const buzz::XmlElement* response) { |
76 if (response->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) { | 117 if (response->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) { |
77 LOG(ERROR) << "Received error in response to heartbeat: " | 118 LOG(ERROR) << "Received error in response to heartbeat: " |
78 << response->Str(); | 119 << response->Str(); |
79 } | 120 } |
80 } | 121 } |
81 | 122 |
123 buzz::XmlElement* HeartbeatSender::CreateSignature(const std::string& jid) { | |
awong
2010/08/02 19:53:49
Somewhere, probably at the top of the .h file for
Sergey Ulanov
2010/08/03 02:10:39
Added info to the .h file. BTW this info is alread
awong
2010/08/03 19:21:02
ah...forgot to look there. :) More reason to put
| |
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 = jid + ' ' + time_str; | |
awong
2010/08/02 19:53:49
So we only sign the jid + time_str?
Sergey Ulanov
2010/08/03 02:10:39
Yes. Do we need to sign anything else?
awong
2010/08/03 19:21:02
Don't think so, but I was expecting to see somethi
Sergey Ulanov
2010/08/04 01:41:12
Yeah, that was my firth thought too, but the probl
| |
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 |