OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
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/offline_status_sender.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/strings/stringize_macros.h" | |
10 #include "base/time.h" | |
11 #include "remoting/base/constants.h" | |
12 #include "remoting/jingle_glue/iq_sender.h" | |
13 #include "remoting/jingle_glue/signal_strategy.h" | |
14 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | |
15 #include "third_party/libjingle/source/talk/xmpp/constants.h" | |
16 | |
17 using buzz::QName; | |
18 using buzz::XmlElement; | |
19 | |
20 namespace remoting { | |
21 | |
22 namespace { | |
23 | |
24 const char kOfflineStatusTag[] = "offline-status"; | |
rmsousa
2013/07/02 02:09:24
Can you make the tag "host-status", and make "offl
| |
25 const char kHostIdAttr[] = "hostid"; | |
26 const char kHostVersionTag[] = "host-version"; | |
27 const char kHeartbeatSignatureTag[] = "signature"; | |
28 const char kStatusAttr[] = "status"; | |
rmsousa
2013/07/02 02:09:24
if we add the separate online/offline "status" att
| |
29 const char kErrorCodeAttr[] = "errorcode"; | |
30 | |
31 } // namespace | |
32 | |
33 OfflineStatusSender::OfflineStatusSender( | |
rmsousa
2013/07/02 02:09:24
Please rename to HostStatusSender (see above)
| |
34 const std::string& host_id, | |
35 SignalStrategy* signal_strategy, | |
36 scoped_refptr<RsaKeyPair> key_pair, | |
37 const std::string& directory_bot_jid) | |
38 : host_id_(host_id), | |
39 signal_strategy_(signal_strategy), | |
40 key_pair_(key_pair), | |
41 directory_bot_jid_(directory_bot_jid) { | |
42 DCHECK(signal_strategy_); | |
43 DCHECK(key_pair_.get()); | |
44 | |
45 VLOG(1) << "Creating OfflineStatusSender"; | |
46 | |
47 signal_strategy_->AddListener(this); | |
48 } | |
49 | |
50 OfflineStatusSender::~OfflineStatusSender() { | |
51 signal_strategy_->RemoveListener(this); | |
52 } | |
53 | |
54 void OfflineStatusSender::OnSignalStrategyStateChange( | |
55 SignalStrategy::State state) { | |
56 if (state == SignalStrategy::CONNECTED) | |
57 iq_sender_.reset(new IqSender(signal_strategy_)); | |
58 else if (state == SignalStrategy::DISCONNECTED) | |
59 iq_sender_.reset(); | |
60 } | |
61 | |
62 bool OfflineStatusSender::OnSignalStrategyIncomingStanza( | |
63 const XmlElement* stanza) { | |
64 return false; | |
65 } | |
66 | |
67 void OfflineStatusSender::SendOfflineStatus(int status, uint errorcode) { | |
68 SignalStrategy::State state = signal_strategy_->GetState(); | |
69 if (state == SignalStrategy::CONNECTED) { | |
70 LOG(INFO) << "Sending offline status '" | |
71 << status | |
72 << "' to " | |
73 << directory_bot_jid_; | |
74 | |
75 iq_sender_->SendIq(buzz::STR_SET, | |
76 directory_bot_jid_, | |
77 CreateOfflineStatusMessage(status, errorcode), | |
78 IqSender::ReplyCallback()); | |
79 } else { | |
80 LOG(INFO) << "Cannot send offline status to '" | |
81 << directory_bot_jid_ | |
82 << " ' because the state of the SignalStrategy is " | |
83 << state; | |
84 } | |
85 } | |
86 | |
87 scoped_ptr<XmlElement> OfflineStatusSender::CreateOfflineStatusMessage( | |
88 int status, uint errorcode) { | |
89 // Create offline status stanza. | |
90 scoped_ptr<XmlElement> offline_status(new XmlElement( | |
91 QName(kChromotingXmlNamespace, kOfflineStatusTag))); | |
92 offline_status->AddAttr( | |
93 QName(kChromotingXmlNamespace, kHostIdAttr), host_id_); | |
94 offline_status->AddAttr( | |
95 QName(kChromotingXmlNamespace, kStatusAttr), base::IntToString(status)); | |
96 // The error code is currently unused and always contains a value of 0. | |
rmsousa
2013/07/02 02:09:24
We should add this only when we know where it'll c
| |
97 // In the future we may use it for things like windows error code. | |
rmsousa
2013/07/02 02:09:24
Nit: Comments describing the arguments should be i
| |
98 offline_status->AddAttr( | |
99 QName(kChromotingXmlNamespace, kErrorCodeAttr), | |
100 base::IntToString(errorcode)); | |
101 offline_status->AddElement(CreateSignature().release()); | |
102 // Append host version. | |
103 scoped_ptr<XmlElement> version_tag(new XmlElement( | |
104 QName(kChromotingXmlNamespace, kHostVersionTag))); | |
105 version_tag->AddText(STRINGIZE(VERSION)); | |
106 offline_status->AddElement(version_tag.release()); | |
rmsousa
2013/07/02 02:09:24
please piggyback a log message, like heartbeat sen
| |
107 return offline_status.Pass(); | |
108 } | |
109 | |
110 scoped_ptr<XmlElement> OfflineStatusSender::CreateSignature() { | |
111 scoped_ptr<XmlElement> signature_tag(new XmlElement( | |
112 QName(kChromotingXmlNamespace, kHeartbeatSignatureTag))); | |
rmsousa
2013/07/02 02:09:24
s/Heartbeat/OfflineStatus
| |
113 | |
114 std::string message = signal_strategy_->GetLocalJid(); | |
rmsousa
2013/07/02 02:09:24
I'm not sure we need to sign this message, but if
| |
115 std::string signature(key_pair_->SignMessage(message)); | |
116 signature_tag->AddText(signature); | |
117 | |
118 return signature_tag.Pass(); | |
119 } | |
120 | |
121 } // namespace remoting | |
OLD | NEW |