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.

207 lines
8.3 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_BASE_SESSIONMANAGER_H_
  28. #define TALK_P2P_BASE_SESSIONMANAGER_H_
  29. #include <map>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. #include "talk/base/sigslot.h"
  34. #include "talk/base/thread.h"
  35. #include "talk/p2p/base/portallocator.h"
  36. #include "talk/p2p/base/transportdescriptionfactory.h"
  37. namespace buzz {
  38. class QName;
  39. class XmlElement;
  40. }
  41. namespace cricket {
  42. class Session;
  43. class BaseSession;
  44. class SessionClient;
  45. // SessionManager manages session instances.
  46. class SessionManager : public sigslot::has_slots<> {
  47. public:
  48. SessionManager(PortAllocator *allocator,
  49. talk_base::Thread *worker_thread = NULL);
  50. virtual ~SessionManager();
  51. PortAllocator *port_allocator() const { return allocator_; }
  52. talk_base::Thread *worker_thread() const { return worker_thread_; }
  53. talk_base::Thread *signaling_thread() const { return signaling_thread_; }
  54. int session_timeout() const { return timeout_; }
  55. void set_session_timeout(int timeout) { timeout_ = timeout; }
  56. // Set what transport protocol we want to default to.
  57. void set_transport_protocol(TransportProtocol proto) {
  58. transport_desc_factory_.set_protocol(proto);
  59. }
  60. // Control use of DTLS. An identity must be supplied if DTLS is enabled.
  61. void set_secure(SecurePolicy policy) {
  62. transport_desc_factory_.set_secure(policy);
  63. }
  64. void set_identity(talk_base::SSLIdentity* identity) {
  65. transport_desc_factory_.set_identity(identity);
  66. }
  67. const TransportDescriptionFactory* transport_desc_factory() const {
  68. return &transport_desc_factory_;
  69. }
  70. // Registers support for the given client. If we receive an initiate
  71. // describing a session of the given type, we will automatically create a
  72. // Session object and notify this client. The client may then accept or
  73. // reject the session.
  74. void AddClient(const std::string& content_type, SessionClient* client);
  75. void RemoveClient(const std::string& content_type);
  76. SessionClient* GetClient(const std::string& content_type);
  77. // Creates a new session. The given name is the JID of the client on whose
  78. // behalf we initiate the session.
  79. Session *CreateSession(const std::string& local_name,
  80. const std::string& content_type);
  81. // Destroys the given session.
  82. void DestroySession(Session *session);
  83. // Returns the session with the given ID or NULL if none exists.
  84. Session *GetSession(const std::string& sid);
  85. // Terminates all of the sessions created by this manager.
  86. void TerminateAll();
  87. // These are signaled whenever the set of existing sessions changes.
  88. sigslot::signal2<Session *, bool> SignalSessionCreate;
  89. sigslot::signal1<Session *> SignalSessionDestroy;
  90. // Determines whether the given stanza is intended for some session.
  91. bool IsSessionMessage(const buzz::XmlElement* stanza);
  92. // Given a sid, initiator, and remote_name, this finds the matching Session
  93. Session* FindSession(const std::string& sid,
  94. const std::string& remote_name);
  95. // Called when we receive a stanza for which IsSessionMessage is true.
  96. void OnIncomingMessage(const buzz::XmlElement* stanza);
  97. // Called when we get a response to a message that we sent.
  98. void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
  99. const buzz::XmlElement* response_stanza);
  100. // Called if an attempted to send times out or an error is returned. In the
  101. // timeout case error_stanza will be NULL
  102. void OnFailedSend(const buzz::XmlElement* orig_stanza,
  103. const buzz::XmlElement* error_stanza);
  104. // Signalled each time a session generates a signaling message to send.
  105. // Also signalled on errors, but with a NULL session.
  106. sigslot::signal2<SessionManager*,
  107. const buzz::XmlElement*> SignalOutgoingMessage;
  108. // Signaled before sessions try to send certain signaling messages. The
  109. // client should call OnSignalingReady once it is safe to send them. These
  110. // steps are taken so that we don't send signaling messages trying to
  111. // re-establish the connectivity of a session when the client cannot send
  112. // the messages (and would probably just drop them on the floor).
  113. //
  114. // Note: you can connect this directly to OnSignalingReady(), if a signalling
  115. // check is not supported.
  116. sigslot::signal0<> SignalRequestSignaling;
  117. void OnSignalingReady();
  118. // Signaled when this SessionManager is deleted.
  119. sigslot::signal0<> SignalDestroyed;
  120. private:
  121. typedef std::map<std::string, Session*> SessionMap;
  122. typedef std::map<std::string, SessionClient*> ClientMap;
  123. // Helper function for CreateSession. This is also invoked when we receive
  124. // a message attempting to initiate a session with this client.
  125. Session *CreateSession(const std::string& local_name,
  126. const std::string& initiator,
  127. const std::string& sid,
  128. const std::string& content_type,
  129. bool received_initiate);
  130. // Attempts to find a registered session type whose description appears as
  131. // a child of the session element. Such a child should be present indicating
  132. // the application they hope to initiate.
  133. std::string FindClient(const buzz::XmlElement* session);
  134. // Sends a message back to the other client indicating that we found an error
  135. // in the stanza they sent. name identifies the error, type is one of the
  136. // standard XMPP types (cancel, continue, modify, auth, wait), and text is a
  137. // description for debugging purposes.
  138. void SendErrorMessage(const buzz::XmlElement* stanza,
  139. const buzz::QName& name,
  140. const std::string& type,
  141. const std::string& text,
  142. const buzz::XmlElement* extra_info);
  143. // Creates and returns an error message from the given components. The
  144. // caller is responsible for deleting this.
  145. buzz::XmlElement* CreateErrorMessage(
  146. const buzz::XmlElement* stanza,
  147. const buzz::QName& name,
  148. const std::string& type,
  149. const std::string& text,
  150. const buzz::XmlElement* extra_info);
  151. // Called each time a session requests signaling.
  152. void OnRequestSignaling(Session* session);
  153. // Called each time a session has an outgoing message.
  154. void OnOutgoingMessage(Session* session, const buzz::XmlElement* stanza);
  155. // Called each time a session has an error to send.
  156. void OnErrorMessage(BaseSession* session,
  157. const buzz::XmlElement* stanza,
  158. const buzz::QName& name,
  159. const std::string& type,
  160. const std::string& text,
  161. const buzz::XmlElement* extra_info);
  162. PortAllocator *allocator_;
  163. talk_base::Thread *signaling_thread_;
  164. talk_base::Thread *worker_thread_;
  165. int timeout_;
  166. TransportDescriptionFactory transport_desc_factory_;
  167. SessionMap session_map_;
  168. ClientMap client_map_;
  169. };
  170. } // namespace cricket
  171. #endif // TALK_P2P_BASE_SESSIONMANAGER_H_