You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_P2P_CLIENT_SESSIONSENDTASK_H_
  28. #define TALK_P2P_CLIENT_SESSIONSENDTASK_H_
  29. #include "talk/base/common.h"
  30. #include "talk/xmpp/constants.h"
  31. #include "talk/xmpp/xmppclient.h"
  32. #include "talk/xmpp/xmppengine.h"
  33. #include "talk/xmpp/xmpptask.h"
  34. #include "talk/p2p/base/sessionmanager.h"
  35. namespace cricket {
  36. // The job of this task is to send an IQ stanza out (after stamping it with
  37. // an ID attribute) and then wait for a response. If not response happens
  38. // within 5 seconds, it will signal failure on a SessionManager. If an error
  39. // happens it will also signal failure. If, however, the send succeeds this
  40. // task will quietly go away.
  41. class SessionSendTask : public buzz::XmppTask {
  42. public:
  43. SessionSendTask(buzz::XmppTaskParentInterface* parent,
  44. SessionManager* session_manager)
  45. : buzz::XmppTask(parent, buzz::XmppEngine::HL_SINGLE),
  46. session_manager_(session_manager) {
  47. set_timeout_seconds(15);
  48. session_manager_->SignalDestroyed.connect(
  49. this, &SessionSendTask::OnSessionManagerDestroyed);
  50. }
  51. virtual ~SessionSendTask() {
  52. SignalDone(this);
  53. }
  54. void Send(const buzz::XmlElement* stanza) {
  55. ASSERT(stanza_.get() == NULL);
  56. // This should be an IQ of type set, result, or error. In the first case,
  57. // we supply an ID. In the others, it should be present.
  58. ASSERT(stanza->Name() == buzz::QN_IQ);
  59. ASSERT(stanza->HasAttr(buzz::QN_TYPE));
  60. if (stanza->Attr(buzz::QN_TYPE) == "set") {
  61. ASSERT(!stanza->HasAttr(buzz::QN_ID));
  62. } else {
  63. ASSERT((stanza->Attr(buzz::QN_TYPE) == "result") ||
  64. (stanza->Attr(buzz::QN_TYPE) == "error"));
  65. ASSERT(stanza->HasAttr(buzz::QN_ID));
  66. }
  67. stanza_.reset(new buzz::XmlElement(*stanza));
  68. if (stanza_->HasAttr(buzz::QN_ID)) {
  69. set_task_id(stanza_->Attr(buzz::QN_ID));
  70. } else {
  71. stanza_->SetAttr(buzz::QN_ID, task_id());
  72. }
  73. }
  74. void OnSessionManagerDestroyed() {
  75. // If the session manager doesn't exist anymore, we should still try to
  76. // send the message, but avoid calling back into the SessionManager.
  77. session_manager_ = NULL;
  78. }
  79. sigslot::signal1<SessionSendTask *> SignalDone;
  80. protected:
  81. virtual int OnTimeout() {
  82. if (session_manager_ != NULL) {
  83. session_manager_->OnFailedSend(stanza_.get(), NULL);
  84. }
  85. return XmppTask::OnTimeout();
  86. }
  87. virtual int ProcessStart() {
  88. SendStanza(stanza_.get());
  89. if (stanza_->Attr(buzz::QN_TYPE) == buzz::STR_SET) {
  90. return STATE_RESPONSE;
  91. } else {
  92. return STATE_DONE;
  93. }
  94. }
  95. virtual int ProcessResponse() {
  96. const buzz::XmlElement* next = NextStanza();
  97. if (next == NULL)
  98. return STATE_BLOCKED;
  99. if (session_manager_ != NULL) {
  100. if (next->Attr(buzz::QN_TYPE) == buzz::STR_RESULT) {
  101. session_manager_->OnIncomingResponse(stanza_.get(), next);
  102. } else {
  103. session_manager_->OnFailedSend(stanza_.get(), next);
  104. }
  105. }
  106. return STATE_DONE;
  107. }
  108. virtual bool HandleStanza(const buzz::XmlElement *stanza) {
  109. if (!MatchResponseIq(stanza,
  110. buzz::Jid(stanza_->Attr(buzz::QN_TO)), task_id()))
  111. return false;
  112. if (stanza->Attr(buzz::QN_TYPE) == buzz::STR_RESULT ||
  113. stanza->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) {
  114. QueueStanza(stanza);
  115. return true;
  116. }
  117. return false;
  118. }
  119. private:
  120. SessionManager *session_manager_;
  121. talk_base::scoped_ptr<buzz::XmlElement> stanza_;
  122. };
  123. }
  124. #endif // TALK_P2P_CLIENT_SESSIONSENDTASK_H_