Home | History | Annotate | Download | only in listener
      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 "jingle/notifier/listener/send_ping_task.h"
      6 
      7 #include <string>
      8 
      9 #include "base/logging.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "jingle/notifier/listener/xml_element_util.h"
     12 #include "talk/xmllite/qname.h"
     13 #include "talk/xmllite/xmlelement.h"
     14 #include "talk/xmpp/constants.h"
     15 #include "talk/xmpp/jid.h"
     16 #include "talk/xmpp/xmppclient.h"
     17 
     18 namespace notifier {
     19 
     20 SendPingTask::Delegate::~Delegate() {
     21 }
     22 
     23 SendPingTask::SendPingTask(buzz::XmppTaskParentInterface* parent,
     24                            Delegate* delegate)
     25     : XmppTask(parent, buzz::XmppEngine::HL_SINGLE), delegate_(delegate) {
     26 }
     27 
     28 SendPingTask::~SendPingTask() {
     29 }
     30 
     31 int SendPingTask::ProcessStart() {
     32   ping_task_id_ = task_id();
     33   scoped_ptr<buzz::XmlElement> stanza(MakePingStanza(ping_task_id_));
     34   DVLOG(1) << "Sending ping stanza " << XmlElementToString(*stanza);
     35   if (SendStanza(stanza.get()) != buzz::XMPP_RETURN_OK) {
     36     DLOG(WARNING) << "Could not send stanza " << XmlElementToString(*stanza);
     37     return STATE_ERROR;
     38   }
     39   return STATE_RESPONSE;
     40 }
     41 
     42 int SendPingTask::ProcessResponse() {
     43   const buzz::XmlElement* stanza = NextStanza();
     44   if (stanza == NULL) {
     45     return STATE_BLOCKED;
     46   }
     47 
     48   DVLOG(1) << "Received stanza " << XmlElementToString(*stanza);
     49 
     50   std::string type = stanza->Attr(buzz::QN_TYPE);
     51   if (type != buzz::STR_RESULT) {
     52     DLOG(WARNING) << "No type=\"result\" attribute found in stanza "
     53                   << XmlElementToString(*stanza);
     54     return STATE_ERROR;
     55   }
     56 
     57   delegate_->OnPingResponseReceived();
     58   return STATE_DONE;
     59 }
     60 
     61 bool SendPingTask::HandleStanza(const buzz::XmlElement* stanza) {
     62   // MatchResponseIq() matches the given Jid with the "from" field of the given
     63   // stanza, which in this case should be the empty string
     64   // (signifying the server).
     65   if (MatchResponseIq(stanza, buzz::Jid(buzz::STR_EMPTY), ping_task_id_)) {
     66     QueueStanza(stanza);
     67     return true;
     68   }
     69   return false;
     70 }
     71 
     72 buzz::XmlElement* SendPingTask::MakePingStanza(const std::string& task_id) {
     73   buzz::XmlElement* stanza = MakeIq(buzz::STR_GET,
     74                                     buzz::Jid(buzz::STR_EMPTY),
     75                                     task_id);
     76   stanza->AddElement(new buzz::XmlElement(buzz::QN_PING));
     77   return stanza;
     78 }
     79 
     80 }  // namespace notifier
     81