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.

313 lines
11 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. #include "talk/p2p/base/sessionmanager.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/helpers.h"
  30. #include "talk/base/logging.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/stringencode.h"
  33. #include "talk/p2p/base/constants.h"
  34. #include "talk/p2p/base/session.h"
  35. #include "talk/p2p/base/sessionmessages.h"
  36. #include "talk/xmpp/constants.h"
  37. #include "talk/xmpp/jid.h"
  38. namespace cricket {
  39. SessionManager::SessionManager(PortAllocator *allocator,
  40. talk_base::Thread *worker) {
  41. allocator_ = allocator;
  42. signaling_thread_ = talk_base::Thread::Current();
  43. if (worker == NULL) {
  44. worker_thread_ = talk_base::Thread::Current();
  45. } else {
  46. worker_thread_ = worker;
  47. }
  48. timeout_ = 50;
  49. }
  50. SessionManager::~SessionManager() {
  51. // Note: Session::Terminate occurs asynchronously, so it's too late to
  52. // delete them now. They better be all gone.
  53. ASSERT(session_map_.empty());
  54. // TerminateAll();
  55. SignalDestroyed();
  56. }
  57. void SessionManager::AddClient(const std::string& content_type,
  58. SessionClient* client) {
  59. ASSERT(client_map_.find(content_type) == client_map_.end());
  60. client_map_[content_type] = client;
  61. }
  62. void SessionManager::RemoveClient(const std::string& content_type) {
  63. ClientMap::iterator iter = client_map_.find(content_type);
  64. ASSERT(iter != client_map_.end());
  65. client_map_.erase(iter);
  66. }
  67. SessionClient* SessionManager::GetClient(const std::string& content_type) {
  68. ClientMap::iterator iter = client_map_.find(content_type);
  69. return (iter != client_map_.end()) ? iter->second : NULL;
  70. }
  71. Session* SessionManager::CreateSession(const std::string& local_name,
  72. const std::string& content_type) {
  73. return CreateSession(local_name, local_name,
  74. talk_base::ToString(talk_base::CreateRandomId()),
  75. content_type, false);
  76. }
  77. Session* SessionManager::CreateSession(
  78. const std::string& local_name, const std::string& initiator_name,
  79. const std::string& sid, const std::string& content_type,
  80. bool received_initiate) {
  81. SessionClient* client = GetClient(content_type);
  82. ASSERT(client != NULL);
  83. Session* session = new Session(this, local_name, initiator_name,
  84. sid, content_type, client);
  85. session->set_identity(transport_desc_factory_.identity());
  86. session_map_[session->id()] = session;
  87. session->SignalRequestSignaling.connect(
  88. this, &SessionManager::OnRequestSignaling);
  89. session->SignalOutgoingMessage.connect(
  90. this, &SessionManager::OnOutgoingMessage);
  91. session->SignalErrorMessage.connect(this, &SessionManager::OnErrorMessage);
  92. SignalSessionCreate(session, received_initiate);
  93. session->client()->OnSessionCreate(session, received_initiate);
  94. return session;
  95. }
  96. void SessionManager::DestroySession(Session* session) {
  97. if (session != NULL) {
  98. SessionMap::iterator it = session_map_.find(session->id());
  99. if (it != session_map_.end()) {
  100. SignalSessionDestroy(session);
  101. session->client()->OnSessionDestroy(session);
  102. session_map_.erase(it);
  103. delete session;
  104. }
  105. }
  106. }
  107. Session* SessionManager::GetSession(const std::string& sid) {
  108. SessionMap::iterator it = session_map_.find(sid);
  109. if (it != session_map_.end())
  110. return it->second;
  111. return NULL;
  112. }
  113. void SessionManager::TerminateAll() {
  114. while (session_map_.begin() != session_map_.end()) {
  115. Session* session = session_map_.begin()->second;
  116. session->Terminate();
  117. }
  118. }
  119. bool SessionManager::IsSessionMessage(const buzz::XmlElement* stanza) {
  120. return cricket::IsSessionMessage(stanza);
  121. }
  122. Session* SessionManager::FindSession(const std::string& sid,
  123. const std::string& remote_name) {
  124. SessionMap::iterator iter = session_map_.find(sid);
  125. if (iter == session_map_.end())
  126. return NULL;
  127. Session* session = iter->second;
  128. if (buzz::Jid(remote_name) != buzz::Jid(session->remote_name()))
  129. return NULL;
  130. return session;
  131. }
  132. void SessionManager::OnIncomingMessage(const buzz::XmlElement* stanza) {
  133. SessionMessage msg;
  134. ParseError error;
  135. if (!ParseSessionMessage(stanza, &msg, &error)) {
  136. SendErrorMessage(stanza, buzz::QN_STANZA_BAD_REQUEST, "modify",
  137. error.text, NULL);
  138. return;
  139. }
  140. Session* session = FindSession(msg.sid, msg.from);
  141. if (session) {
  142. session->OnIncomingMessage(msg);
  143. return;
  144. }
  145. if (msg.type != ACTION_SESSION_INITIATE) {
  146. SendErrorMessage(stanza, buzz::QN_STANZA_BAD_REQUEST, "modify",
  147. "unknown session", NULL);
  148. return;
  149. }
  150. std::string content_type;
  151. if (!ParseContentType(msg.protocol, msg.action_elem,
  152. &content_type, &error)) {
  153. SendErrorMessage(stanza, buzz::QN_STANZA_BAD_REQUEST, "modify",
  154. error.text, NULL);
  155. return;
  156. }
  157. if (!GetClient(content_type)) {
  158. SendErrorMessage(stanza, buzz::QN_STANZA_BAD_REQUEST, "modify",
  159. "unknown content type: " + content_type, NULL);
  160. return;
  161. }
  162. session = CreateSession(msg.to, msg.initiator, msg.sid,
  163. content_type, true);
  164. session->OnIncomingMessage(msg);
  165. }
  166. void SessionManager::OnIncomingResponse(const buzz::XmlElement* orig_stanza,
  167. const buzz::XmlElement* response_stanza) {
  168. if (orig_stanza == NULL || response_stanza == NULL) {
  169. return;
  170. }
  171. SessionMessage msg;
  172. ParseError error;
  173. if (!ParseSessionMessage(orig_stanza, &msg, &error)) {
  174. LOG(LS_WARNING) << "Error parsing incoming response: " << error.text
  175. << ":" << orig_stanza;
  176. return;
  177. }
  178. Session* session = FindSession(msg.sid, msg.to);
  179. if (session) {
  180. session->OnIncomingResponse(orig_stanza, response_stanza, msg);
  181. }
  182. }
  183. void SessionManager::OnFailedSend(const buzz::XmlElement* orig_stanza,
  184. const buzz::XmlElement* error_stanza) {
  185. SessionMessage msg;
  186. ParseError error;
  187. if (!ParseSessionMessage(orig_stanza, &msg, &error)) {
  188. return; // TODO: log somewhere?
  189. }
  190. Session* session = FindSession(msg.sid, msg.to);
  191. if (session) {
  192. talk_base::scoped_ptr<buzz::XmlElement> synthetic_error;
  193. if (!error_stanza) {
  194. // A failed send is semantically equivalent to an error response, so we
  195. // can just turn the former into the latter.
  196. synthetic_error.reset(
  197. CreateErrorMessage(orig_stanza, buzz::QN_STANZA_ITEM_NOT_FOUND,
  198. "cancel", "Recipient did not respond", NULL));
  199. error_stanza = synthetic_error.get();
  200. }
  201. session->OnFailedSend(orig_stanza, error_stanza);
  202. }
  203. }
  204. void SessionManager::SendErrorMessage(const buzz::XmlElement* stanza,
  205. const buzz::QName& name,
  206. const std::string& type,
  207. const std::string& text,
  208. const buzz::XmlElement* extra_info) {
  209. talk_base::scoped_ptr<buzz::XmlElement> msg(
  210. CreateErrorMessage(stanza, name, type, text, extra_info));
  211. SignalOutgoingMessage(this, msg.get());
  212. }
  213. buzz::XmlElement* SessionManager::CreateErrorMessage(
  214. const buzz::XmlElement* stanza,
  215. const buzz::QName& name,
  216. const std::string& type,
  217. const std::string& text,
  218. const buzz::XmlElement* extra_info) {
  219. buzz::XmlElement* iq = new buzz::XmlElement(buzz::QN_IQ);
  220. iq->SetAttr(buzz::QN_TO, stanza->Attr(buzz::QN_FROM));
  221. iq->SetAttr(buzz::QN_ID, stanza->Attr(buzz::QN_ID));
  222. iq->SetAttr(buzz::QN_TYPE, "error");
  223. CopyXmlChildren(stanza, iq);
  224. buzz::XmlElement* error = new buzz::XmlElement(buzz::QN_ERROR);
  225. error->SetAttr(buzz::QN_TYPE, type);
  226. iq->AddElement(error);
  227. // If the error name is not in the standard namespace, we have to first add
  228. // some error from that namespace.
  229. if (name.Namespace() != buzz::NS_STANZA) {
  230. error->AddElement(
  231. new buzz::XmlElement(buzz::QN_STANZA_UNDEFINED_CONDITION));
  232. }
  233. error->AddElement(new buzz::XmlElement(name));
  234. if (extra_info)
  235. error->AddElement(new buzz::XmlElement(*extra_info));
  236. if (text.size() > 0) {
  237. // It's okay to always use English here. This text is for debugging
  238. // purposes only.
  239. buzz::XmlElement* text_elem = new buzz::XmlElement(buzz::QN_STANZA_TEXT);
  240. text_elem->SetAttr(buzz::QN_XML_LANG, "en");
  241. text_elem->SetBodyText(text);
  242. error->AddElement(text_elem);
  243. }
  244. // TODO: Should we include error codes as well for SIP compatibility?
  245. return iq;
  246. }
  247. void SessionManager::OnOutgoingMessage(Session* session,
  248. const buzz::XmlElement* stanza) {
  249. SignalOutgoingMessage(this, stanza);
  250. }
  251. void SessionManager::OnErrorMessage(BaseSession* session,
  252. const buzz::XmlElement* stanza,
  253. const buzz::QName& name,
  254. const std::string& type,
  255. const std::string& text,
  256. const buzz::XmlElement* extra_info) {
  257. SendErrorMessage(stanza, name, type, text, extra_info);
  258. }
  259. void SessionManager::OnSignalingReady() {
  260. for (SessionMap::iterator it = session_map_.begin();
  261. it != session_map_.end();
  262. ++it) {
  263. it->second->OnSignalingReady();
  264. }
  265. }
  266. void SessionManager::OnRequestSignaling(Session* session) {
  267. SignalRequestSignaling();
  268. }
  269. } // namespace cricket