3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#ifndef TALK_P2P_BASE_SESSIONMANAGER_H_
29
#define TALK_P2P_BASE_SESSIONMANAGER_H_
36
#include "talk/base/sigslot.h"
37
#include "talk/base/thread.h"
38
#include "talk/p2p/base/portallocator.h"
51
// SessionManager manages session instances
53
class SessionManager : public sigslot::has_slots<> {
55
SessionManager(PortAllocator *allocator,
56
talk_base::Thread *worker_thread = NULL);
57
virtual ~SessionManager();
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_; }
63
int session_timeout() const { return timeout_; }
64
void set_session_timeout(int timeout) { timeout_ = timeout; }
66
// Registers support for the given client. If we receive an initiate
67
// describing a session of the given type, we will automatically create a
68
// Session object and notify this client. The client may then accept or
69
// reject the session.
70
void AddClient(const std::string& content_type, SessionClient* client);
71
void RemoveClient(const std::string& content_type);
72
SessionClient* GetClient(const std::string& content_type);
74
// Creates a new session. The given name is the JID of the client on whose
75
// behalf we initiate the session.
76
Session *CreateSession(const std::string& local_name,
77
const std::string& content_type);
79
// Destroys the given session.
80
void DestroySession(Session *session);
82
// Returns the session with the given ID or NULL if none exists.
83
Session *GetSession(const std::string& sid);
85
// Terminates all of the sessions created by this manager.
88
// These are signaled whenever the set of existing sessions changes.
89
sigslot::signal2<Session *, bool> SignalSessionCreate;
90
sigslot::signal1<Session *> SignalSessionDestroy;
92
// Determines whether the given stanza is intended for some session.
93
bool IsSessionMessage(const buzz::XmlElement* stanza);
95
// Given a sid, initiator, and remote_name, this finds the matching Session
96
Session* FindSession(const std::string& sid,
97
const std::string& remote_name);
99
// Called when we receive a stanza for which IsSessionMessage is true.
100
void OnIncomingMessage(const buzz::XmlElement* stanza);
102
// Called when we get a response to a message that we sent.
103
void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
104
const buzz::XmlElement* response_stanza);
106
// Called if an attempted to send times out or an error is returned. In the
107
// timeout case error_stanza will be NULL
108
void OnFailedSend(const buzz::XmlElement* orig_stanza,
109
const buzz::XmlElement* error_stanza);
111
// Signalled each time a session generates a signaling message to send.
112
// Also signalled on errors, but with a NULL session.
113
sigslot::signal2<SessionManager*,
114
const buzz::XmlElement*> SignalOutgoingMessage;
116
// Signaled before sessions try to send certain signaling messages. The
117
// client should call OnSignalingReady once it is safe to send them. These
118
// steps are taken so that we don't send signaling messages trying to
119
// re-establish the connectivity of a session when the client cannot send
120
// the messages (and would probably just drop them on the floor).
122
// Note: you can connect this directly to OnSignalingReady(), if a signalling
123
// check is not supported.
124
sigslot::signal0<> SignalRequestSignaling;
125
void OnSignalingReady();
127
// Signaled when this SessionManager is deleted.
128
sigslot::signal0<> SignalDestroyed;
131
typedef std::map<std::string, Session*> SessionMap;
132
typedef std::map<std::string, SessionClient*> ClientMap;
134
PortAllocator *allocator_;
135
talk_base::Thread *signaling_thread_;
136
talk_base::Thread *worker_thread_;
138
SessionMap session_map_;
139
ClientMap client_map_;
141
// Helper function for CreateSession. This is also invoked when we receive
142
// a message attempting to initiate a session with this client.
143
Session *CreateSession(const std::string& local_name,
144
const std::string& initiator,
145
const std::string& sid,
146
const std::string& content_type,
147
bool received_initiate);
149
// Attempts to find a registered session type whose description appears as
150
// a child of the session element. Such a child should be present indicating
151
// the application they hope to initiate.
152
std::string FindClient(const buzz::XmlElement* session);
154
// Sends a message back to the other client indicating that we found an error
155
// in the stanza they sent. name identifies the error, type is one of the
156
// standard XMPP types (cancel, continue, modify, auth, wait), and text is a
157
// description for debugging purposes.
158
void SendErrorMessage(const buzz::XmlElement* stanza,
159
const buzz::QName& name,
160
const std::string& type,
161
const std::string& text,
162
const buzz::XmlElement* extra_info);
164
// Creates and returns an error message from the given components. The
165
// caller is responsible for deleting this.
166
buzz::XmlElement* CreateErrorMessage(
167
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);
173
// Called each time a session requests signaling.
174
void OnRequestSignaling(Session* session);
176
// Called each time a session has an outgoing message.
177
void OnOutgoingMessage(Session* session, const buzz::XmlElement* stanza);
179
// Called each time a session has an error to send.
180
void OnErrorMessage(BaseSession* session,
181
const buzz::XmlElement* stanza,
182
const buzz::QName& name,
183
const std::string& type,
184
const std::string& text,
185
const buzz::XmlElement* extra_info);
188
} // namespace cricket
190
#endif // TALK_P2P_BASE_SESSIONMANAGER_H_