blob: dcdf1ed4770c9f9f7a4dca8bd9a9f74b3c0da176 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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/base/sigslot.h"
37#include "talk/base/thread.h"
38#include "talk/p2p/base/portallocator.h"
39#include "talk/p2p/base/transportdescriptionfactory.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 talk_base::Thread *worker_thread = NULL);
57 virtual ~SessionManager();
58
59 PortAllocator *port_allocator() const { return allocator_; }
60 talk_base::Thread *worker_thread() const { return worker_thread_; }
61 talk_base::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(talk_base::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 // Destroys the given session.
96 void DestroySession(Session *session);
97
98 // Returns the session with the given ID or NULL if none exists.
99 Session *GetSession(const std::string& sid);
100
101 // Terminates all of the sessions created by this manager.
102 void TerminateAll();
103
104 // These are signaled whenever the set of existing sessions changes.
105 sigslot::signal2<Session *, bool> SignalSessionCreate;
106 sigslot::signal1<Session *> SignalSessionDestroy;
107
108 // Determines whether the given stanza is intended for some session.
109 bool IsSessionMessage(const buzz::XmlElement* stanza);
110
111 // Given a sid, initiator, and remote_name, this finds the matching Session
112 Session* FindSession(const std::string& sid,
113 const std::string& remote_name);
114
115 // Called when we receive a stanza for which IsSessionMessage is true.
116 void OnIncomingMessage(const buzz::XmlElement* stanza);
117
118 // Called when we get a response to a message that we sent.
119 void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
120 const buzz::XmlElement* response_stanza);
121
122 // Called if an attempted to send times out or an error is returned. In the
123 // timeout case error_stanza will be NULL
124 void OnFailedSend(const buzz::XmlElement* orig_stanza,
125 const buzz::XmlElement* error_stanza);
126
127 // Signalled each time a session generates a signaling message to send.
128 // Also signalled on errors, but with a NULL session.
129 sigslot::signal2<SessionManager*,
130 const buzz::XmlElement*> SignalOutgoingMessage;
131
132 // Signaled before sessions try to send certain signaling messages. The
133 // client should call OnSignalingReady once it is safe to send them. These
134 // steps are taken so that we don't send signaling messages trying to
135 // re-establish the connectivity of a session when the client cannot send
136 // the messages (and would probably just drop them on the floor).
137 //
138 // Note: you can connect this directly to OnSignalingReady(), if a signalling
139 // check is not supported.
140 sigslot::signal0<> SignalRequestSignaling;
141 void OnSignalingReady();
142
143 // Signaled when this SessionManager is deleted.
144 sigslot::signal0<> SignalDestroyed;
145
146 private:
147 typedef std::map<std::string, Session*> SessionMap;
148 typedef std::map<std::string, SessionClient*> ClientMap;
149
150 // Helper function for CreateSession. This is also invoked when we receive
151 // a message attempting to initiate a session with this client.
152 Session *CreateSession(const std::string& local_name,
153 const std::string& initiator,
154 const std::string& sid,
155 const std::string& content_type,
156 bool received_initiate);
157
158 // Attempts to find a registered session type whose description appears as
159 // a child of the session element. Such a child should be present indicating
160 // the application they hope to initiate.
161 std::string FindClient(const buzz::XmlElement* session);
162
163 // Sends a message back to the other client indicating that we found an error
164 // in the stanza they sent. name identifies the error, type is one of the
165 // standard XMPP types (cancel, continue, modify, auth, wait), and text is a
166 // description for debugging purposes.
167 void SendErrorMessage(const buzz::XmlElement* stanza,
168 const buzz::QName& name,
169 const std::string& type,
170 const std::string& text,
171 const buzz::XmlElement* extra_info);
172
173 // Creates and returns an error message from the given components. The
174 // caller is responsible for deleting this.
175 buzz::XmlElement* CreateErrorMessage(
176 const buzz::XmlElement* stanza,
177 const buzz::QName& name,
178 const std::string& type,
179 const std::string& text,
180 const buzz::XmlElement* extra_info);
181
182 // Called each time a session requests signaling.
183 void OnRequestSignaling(Session* session);
184
185 // Called each time a session has an outgoing message.
186 void OnOutgoingMessage(Session* session, const buzz::XmlElement* stanza);
187
188 // Called each time a session has an error to send.
189 void OnErrorMessage(BaseSession* session,
190 const buzz::XmlElement* stanza,
191 const buzz::QName& name,
192 const std::string& type,
193 const std::string& text,
194 const buzz::XmlElement* extra_info);
195
196 PortAllocator *allocator_;
197 talk_base::Thread *signaling_thread_;
198 talk_base::Thread *worker_thread_;
199 int timeout_;
200 TransportDescriptionFactory transport_desc_factory_;
201 SessionMap session_map_;
202 ClientMap client_map_;
203};
204
205} // namespace cricket
206
207#endif // TALK_P2P_BASE_SESSIONMANAGER_H_