2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef WEBRTC_P2P_BASE_SESSIONMANAGER_H_
12 #define WEBRTC_P2P_BASE_SESSIONMANAGER_H_
19 #include "webrtc/p2p/base/portallocator.h"
20 #include "webrtc/p2p/base/transportdescriptionfactory.h"
21 #include "webrtc/base/sigslot.h"
22 #include "webrtc/base/thread.h"
35 // SessionManager manages session instances.
36 class SessionManager : public sigslot::has_slots<> {
38 SessionManager(PortAllocator *allocator,
39 rtc::Thread *worker_thread = NULL);
40 virtual ~SessionManager();
42 PortAllocator *port_allocator() const { return allocator_; }
43 rtc::Thread *worker_thread() const { return worker_thread_; }
44 rtc::Thread *signaling_thread() const { return signaling_thread_; }
46 int session_timeout() const { return timeout_; }
47 void set_session_timeout(int timeout) { timeout_ = timeout; }
49 // Set what transport protocol we want to default to.
50 void set_transport_protocol(TransportProtocol proto) {
51 transport_desc_factory_.set_protocol(proto);
54 // Control use of DTLS. An identity must be supplied if DTLS is enabled.
55 void set_secure(SecurePolicy policy) {
56 transport_desc_factory_.set_secure(policy);
58 void set_identity(rtc::SSLIdentity* identity) {
59 transport_desc_factory_.set_identity(identity);
61 const TransportDescriptionFactory* transport_desc_factory() const {
62 return &transport_desc_factory_;
65 // Registers support for the given client. If we receive an initiate
66 // describing a session of the given type, we will automatically create a
67 // Session object and notify this client. The client may then accept or
68 // reject the session.
69 void AddClient(const std::string& content_type, SessionClient* client);
70 void RemoveClient(const std::string& content_type);
71 SessionClient* GetClient(const std::string& content_type);
73 // Creates a new session. The given name is the JID of the client on whose
74 // behalf we initiate the session.
75 Session *CreateSession(const std::string& local_name,
76 const std::string& content_type);
78 Session *CreateSession(const std::string& id,
79 const std::string& local_name,
80 const std::string& content_type);
82 // Destroys the given session.
83 void DestroySession(Session *session);
85 // Returns the session with the given ID or NULL if none exists.
86 Session *GetSession(const std::string& sid);
88 // Terminates all of the sessions created by this manager.
91 // These are signaled whenever the set of existing sessions changes.
92 sigslot::signal2<Session *, bool> SignalSessionCreate;
93 sigslot::signal1<Session *> SignalSessionDestroy;
95 // Determines whether the given stanza is intended for some session.
96 bool IsSessionMessage(const buzz::XmlElement* stanza);
98 // Given a sid, initiator, and remote_name, this finds the matching Session
99 Session* FindSession(const std::string& sid,
100 const std::string& remote_name);
102 // Called when we receive a stanza for which IsSessionMessage is true.
103 void OnIncomingMessage(const buzz::XmlElement* stanza);
105 // Called when we get a response to a message that we sent.
106 void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
107 const buzz::XmlElement* response_stanza);
109 // Called if an attempted to send times out or an error is returned. In the
110 // timeout case error_stanza will be NULL
111 void OnFailedSend(const buzz::XmlElement* orig_stanza,
112 const buzz::XmlElement* error_stanza);
114 // Signalled each time a session generates a signaling message to send.
115 // Also signalled on errors, but with a NULL session.
116 sigslot::signal2<SessionManager*,
117 const buzz::XmlElement*> SignalOutgoingMessage;
119 // Signaled before sessions try to send certain signaling messages. The
120 // client should call OnSignalingReady once it is safe to send them. These
121 // steps are taken so that we don't send signaling messages trying to
122 // re-establish the connectivity of a session when the client cannot send
123 // the messages (and would probably just drop them on the floor).
125 // Note: you can connect this directly to OnSignalingReady(), if a signalling
126 // check is not supported.
127 sigslot::signal0<> SignalRequestSignaling;
128 void OnSignalingReady();
130 // Signaled when this SessionManager is deleted.
131 sigslot::signal0<> SignalDestroyed;
134 typedef std::map<std::string, Session*> SessionMap;
135 typedef std::map<std::string, SessionClient*> ClientMap;
137 // Helper function for CreateSession. This is also invoked when we receive
138 // a message attempting to initiate a session with this client.
139 Session *CreateSession(const std::string& local_name,
140 const std::string& initiator,
141 const std::string& sid,
142 const std::string& content_type,
143 bool received_initiate);
145 // Attempts to find a registered session type whose description appears as
146 // a child of the session element. Such a child should be present indicating
147 // the application they hope to initiate.
148 std::string FindClient(const buzz::XmlElement* session);
150 // Sends a message back to the other client indicating that we found an error
151 // in the stanza they sent. name identifies the error, type is one of the
152 // standard XMPP types (cancel, continue, modify, auth, wait), and text is a
153 // description for debugging purposes.
154 void SendErrorMessage(const buzz::XmlElement* stanza,
155 const buzz::QName& name,
156 const std::string& type,
157 const std::string& text,
158 const buzz::XmlElement* extra_info);
160 // Creates and returns an error message from the given components. The
161 // caller is responsible for deleting this.
162 buzz::XmlElement* CreateErrorMessage(
163 const buzz::XmlElement* stanza,
164 const buzz::QName& name,
165 const std::string& type,
166 const std::string& text,
167 const buzz::XmlElement* extra_info);
169 // Called each time a session requests signaling.
170 void OnRequestSignaling(Session* session);
172 // Called each time a session has an outgoing message.
173 void OnOutgoingMessage(Session* session, const buzz::XmlElement* stanza);
175 // Called each time a session has an error to send.
176 void OnErrorMessage(BaseSession* session,
177 const buzz::XmlElement* stanza,
178 const buzz::QName& name,
179 const std::string& type,
180 const std::string& text,
181 const buzz::XmlElement* extra_info);
183 PortAllocator *allocator_;
184 rtc::Thread *signaling_thread_;
185 rtc::Thread *worker_thread_;
187 TransportDescriptionFactory transport_desc_factory_;
188 SessionMap session_map_;
189 ClientMap client_map_;
192 } // namespace cricket
194 #endif // WEBRTC_P2P_BASE_SESSIONMANAGER_H_