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