blob: 00ba4d8ce2b310744f534ff544c3fed272e58b56 [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#include "talk/p2p/base/portallocator.h"
29
30#include "talk/p2p/base/portallocatorsessionproxy.h"
31
32namespace cricket {
33
34PortAllocatorSession::PortAllocatorSession(const std::string& content_name,
35 int component,
36 const std::string& ice_ufrag,
37 const std::string& ice_pwd,
38 uint32 flags)
39 : content_name_(content_name),
40 component_(component),
41 flags_(flags),
42 // If PORTALLOCATOR_ENABLE_SHARED_UFRAG flag is not enabled, ignore the
43 // incoming ufrag and pwd, which will cause each Port to generate one
44 // by itself.
45 username_(flags_ & PORTALLOCATOR_ENABLE_SHARED_UFRAG ? ice_ufrag : ""),
46 password_(flags_ & PORTALLOCATOR_ENABLE_SHARED_UFRAG ? ice_pwd : "") {
47}
48
49PortAllocator::~PortAllocator() {
50 for (SessionMuxerMap::iterator iter = muxers_.begin();
51 iter != muxers_.end(); ++iter) {
52 delete iter->second;
53 }
54}
55
56PortAllocatorSession* PortAllocator::CreateSession(
57 const std::string& sid,
58 const std::string& content_name,
59 int component,
60 const std::string& ice_ufrag,
61 const std::string& ice_pwd) {
62 if (flags_ & PORTALLOCATOR_ENABLE_BUNDLE) {
63 // If we just use |sid| as key in identifying PortAllocatorSessionMuxer,
64 // ICE restart will not result in different candidates, as |sid| will
65 // be same. To yield different candiates we are using combination of
66 // |ice_ufrag| and |ice_pwd|.
67 // Ideally |ice_ufrag| and |ice_pwd| should change together, but
68 // there can be instances where only ice_pwd will be changed.
69 std::string key_str = ice_ufrag + ":" + ice_pwd;
70 PortAllocatorSessionMuxer* muxer = GetSessionMuxer(key_str);
71 if (!muxer) {
72 PortAllocatorSession* session_impl = CreateSessionInternal(
73 content_name, component, ice_ufrag, ice_pwd);
74 // Create PortAllocatorSessionMuxer object for |session_impl|.
75 muxer = new PortAllocatorSessionMuxer(session_impl);
76 muxer->SignalDestroyed.connect(
77 this, &PortAllocator::OnSessionMuxerDestroyed);
78 // Add PortAllocatorSession to the map.
79 muxers_[key_str] = muxer;
80 }
81 PortAllocatorSessionProxy* proxy =
82 new PortAllocatorSessionProxy(content_name, component, flags_);
83 muxer->RegisterSessionProxy(proxy);
84 return proxy;
85 }
86 return CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd);
87}
88
89PortAllocatorSessionMuxer* PortAllocator::GetSessionMuxer(
90 const std::string& key) const {
91 SessionMuxerMap::const_iterator iter = muxers_.find(key);
92 if (iter != muxers_.end())
93 return iter->second;
94 return NULL;
95}
96
97void PortAllocator::OnSessionMuxerDestroyed(
98 PortAllocatorSessionMuxer* session) {
99 SessionMuxerMap::iterator iter;
100 for (iter = muxers_.begin(); iter != muxers_.end(); ++iter) {
101 if (iter->second == session)
102 break;
103 }
104 if (iter != muxers_.end())
105 muxers_.erase(iter);
106}
107
108} // namespace cricket