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.

189 lines
6.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2006, 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_XMPP_XMPPTASK_H_
  28. #define TALK_XMPP_XMPPTASK_H_
  29. #include <string>
  30. #include <deque>
  31. #include "talk/base/sigslot.h"
  32. #include "talk/base/task.h"
  33. #include "talk/base/taskparent.h"
  34. #include "talk/xmpp/xmppengine.h"
  35. namespace buzz {
  36. /////////////////////////////////////////////////////////////////////
  37. //
  38. // XMPPTASK
  39. //
  40. /////////////////////////////////////////////////////////////////////
  41. //
  42. // See Task and XmppClient first.
  43. //
  44. // XmppTask is a task that is designed to go underneath XmppClient and be
  45. // useful there. It has a way of finding its XmppClient parent so you
  46. // can have it nested arbitrarily deep under an XmppClient and it can
  47. // still find the XMPP services.
  48. //
  49. // Tasks register themselves to listen to particular kinds of stanzas
  50. // that are sent out by the client. Rather than processing stanzas
  51. // right away, they should decide if they own the sent stanza,
  52. // and if so, queue it and Wake() the task, or if a stanza does not belong
  53. // to you, return false right away so the next XmppTask can take a crack.
  54. // This technique (synchronous recognize, but asynchronous processing)
  55. // allows you to have arbitrary logic for recognizing stanzas yet still,
  56. // for example, disconnect a client while processing a stanza -
  57. // without reentrancy problems.
  58. //
  59. /////////////////////////////////////////////////////////////////////
  60. class XmppTask;
  61. // XmppClientInterface is an abstract interface for sending and
  62. // handling stanzas. It can be implemented for unit tests or
  63. // different network environments. It will usually be implemented by
  64. // XmppClient.
  65. class XmppClientInterface {
  66. public:
  67. XmppClientInterface();
  68. virtual ~XmppClientInterface();
  69. virtual XmppEngine::State GetState() const = 0;
  70. virtual const Jid& jid() const = 0;
  71. virtual std::string NextId() = 0;
  72. virtual XmppReturnStatus SendStanza(const XmlElement* stanza) = 0;
  73. virtual XmppReturnStatus SendStanzaError(const XmlElement* original_stanza,
  74. XmppStanzaError error_code,
  75. const std::string& message) = 0;
  76. virtual void AddXmppTask(XmppTask* task, XmppEngine::HandlerLevel level) = 0;
  77. virtual void RemoveXmppTask(XmppTask* task) = 0;
  78. sigslot::signal0<> SignalDisconnected;
  79. DISALLOW_EVIL_CONSTRUCTORS(XmppClientInterface);
  80. };
  81. // XmppTaskParentInterface is the interface require for any parent of
  82. // an XmppTask. It needs, for example, a way to get an
  83. // XmppClientInterface.
  84. // We really ought to inherit from a TaskParentInterface, but we tried
  85. // that and it's way too complicated to change
  86. // Task/TaskParent/TaskRunner. For now, this works.
  87. class XmppTaskParentInterface : public talk_base::Task {
  88. public:
  89. explicit XmppTaskParentInterface(talk_base::TaskParent* parent)
  90. : Task(parent) {
  91. }
  92. virtual ~XmppTaskParentInterface() {}
  93. virtual XmppClientInterface* GetClient() = 0;
  94. DISALLOW_EVIL_CONSTRUCTORS(XmppTaskParentInterface);
  95. };
  96. class XmppTaskBase : public XmppTaskParentInterface {
  97. public:
  98. explicit XmppTaskBase(XmppTaskParentInterface* parent)
  99. : XmppTaskParentInterface(parent),
  100. parent_(parent) {
  101. }
  102. virtual ~XmppTaskBase() {}
  103. virtual XmppClientInterface* GetClient() {
  104. return parent_->GetClient();
  105. }
  106. protected:
  107. XmppTaskParentInterface* parent_;
  108. DISALLOW_EVIL_CONSTRUCTORS(XmppTaskBase);
  109. };
  110. class XmppTask : public XmppTaskBase,
  111. public XmppStanzaHandler,
  112. public sigslot::has_slots<>
  113. {
  114. public:
  115. XmppTask(XmppTaskParentInterface* parent,
  116. XmppEngine::HandlerLevel level = XmppEngine::HL_NONE);
  117. virtual ~XmppTask();
  118. std::string task_id() const { return id_; }
  119. void set_task_id(std::string id) { id_ = id; }
  120. #ifdef _DEBUG
  121. void set_debug_force_timeout(const bool f) { debug_force_timeout_ = f; }
  122. #endif
  123. virtual bool HandleStanza(const XmlElement* stanza) { return false; }
  124. protected:
  125. XmppReturnStatus SendStanza(const XmlElement* stanza);
  126. XmppReturnStatus SetResult(const std::string& code);
  127. XmppReturnStatus SendStanzaError(const XmlElement* element_original,
  128. XmppStanzaError code,
  129. const std::string& text);
  130. virtual void Stop();
  131. virtual void OnDisconnect();
  132. virtual void QueueStanza(const XmlElement* stanza);
  133. const XmlElement* NextStanza();
  134. bool MatchStanzaFrom(const XmlElement* stanza, const Jid& match_jid);
  135. bool MatchResponseIq(const XmlElement* stanza, const Jid& to,
  136. const std::string& task_id);
  137. static bool MatchRequestIq(const XmlElement* stanza, const std::string& type,
  138. const QName& qn);
  139. static XmlElement *MakeIqResult(const XmlElement* query);
  140. static XmlElement *MakeIq(const std::string& type,
  141. const Jid& to, const std::string& task_id);
  142. // Returns true if the task is under the specified rate limit and updates the
  143. // rate limit accordingly
  144. bool VerifyTaskRateLimit(const std::string task_name, int max_count,
  145. int per_x_seconds);
  146. private:
  147. void StopImpl();
  148. bool stopped_;
  149. std::deque<XmlElement*> stanza_queue_;
  150. talk_base::scoped_ptr<XmlElement> next_stanza_;
  151. std::string id_;
  152. #ifdef _DEBUG
  153. bool debug_force_timeout_;
  154. #endif
  155. };
  156. } // namespace buzz
  157. #endif // TALK_XMPP_XMPPTASK_H_