OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/jingle_glue/iq_sender.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "remoting/jingle_glue/signal_strategy.h" |
| 10 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 11 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
| 12 |
| 13 namespace remoting { |
| 14 |
| 15 // static |
| 16 buzz::XmlElement* IqSender::MakeIqStanza(const std::string& type, |
| 17 const std::string& addressee, |
| 18 buzz::XmlElement* iq_body) { |
| 19 buzz::XmlElement* stanza = new buzz::XmlElement(buzz::QN_IQ); |
| 20 stanza->AddAttr(buzz::QN_TYPE, type); |
| 21 if (!addressee.empty()) |
| 22 stanza->AddAttr(buzz::QN_TO, addressee); |
| 23 stanza->AddElement(iq_body); |
| 24 return stanza; |
| 25 } |
| 26 |
| 27 IqSender::IqSender(SignalStrategy* signal_strategy) |
| 28 : signal_strategy_(signal_strategy) { |
| 29 signal_strategy_->AddListener(this); |
| 30 } |
| 31 |
| 32 IqSender::~IqSender() { |
| 33 signal_strategy_->RemoveListener(this); |
| 34 } |
| 35 |
| 36 IqRequest* IqSender::SendIq(buzz::XmlElement* stanza, |
| 37 const ReplyCallback& callback) { |
| 38 std::string id = signal_strategy_->GetNextId(); |
| 39 stanza->AddAttr(buzz::QN_ID, id); |
| 40 if (!signal_strategy_->SendStanza(stanza)) { |
| 41 return NULL; |
| 42 } |
| 43 DCHECK(requests_.find(id) == requests_.end()); |
| 44 IqRequest* request = new IqRequest(this, callback); |
| 45 if (!callback.is_null()) |
| 46 requests_[id] = request; |
| 47 return request; |
| 48 } |
| 49 |
| 50 IqRequest* IqSender::SendIq(const std::string& type, |
| 51 const std::string& addressee, |
| 52 buzz::XmlElement* iq_body, |
| 53 const ReplyCallback& callback) { |
| 54 return SendIq(MakeIqStanza(type, addressee, iq_body), callback); |
| 55 } |
| 56 |
| 57 void IqSender::RemoveRequest(IqRequest* request) { |
| 58 IqRequestMap::iterator it = requests_.begin(); |
| 59 while (it != requests_.end()) { |
| 60 IqRequestMap::iterator cur = it; |
| 61 ++it; |
| 62 if (cur->second == request) { |
| 63 requests_.erase(cur); |
| 64 break; |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 bool IqSender::OnIncomingStanza(const buzz::XmlElement* stanza) { |
| 70 if (stanza->Name() != buzz::QN_IQ) { |
| 71 LOG(WARNING) << "Received unexpected non-IQ packet" << stanza->Str(); |
| 72 return false; |
| 73 } |
| 74 |
| 75 const std::string& id = stanza->Attr(buzz::QN_ID); |
| 76 if (id.empty()) { |
| 77 LOG(WARNING) << "IQ packet missing id" << stanza->Str(); |
| 78 return false; |
| 79 } |
| 80 |
| 81 IqRequestMap::iterator it = requests_.find(id); |
| 82 if (it == requests_.end()) { |
| 83 return false; |
| 84 } |
| 85 |
| 86 IqRequest* request = it->second; |
| 87 requests_.erase(it); |
| 88 |
| 89 request->OnResponse(stanza); |
| 90 return true; |
| 91 } |
| 92 |
| 93 IqRequest::IqRequest(IqSender* sender, const IqSender::ReplyCallback& callback) |
| 94 : sender_(sender), |
| 95 callback_(callback) { |
| 96 } |
| 97 |
| 98 IqRequest::~IqRequest() { |
| 99 sender_->RemoveRequest(this); |
| 100 } |
| 101 |
| 102 void IqRequest::OnResponse(const buzz::XmlElement* stanza) { |
| 103 DCHECK(!callback_.is_null()); |
| 104 IqSender::ReplyCallback callback(callback_); |
| 105 callback_.Reset(); |
| 106 callback.Run(stanza); |
| 107 } |
| 108 |
| 109 } // namespace remoting |
OLD | NEW |