3 * Copyright 2004--2011, 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 #include "talk/p2p/base/portallocatorsessionproxy.h"
30 #include "talk/p2p/base/portallocator.h"
31 #include "talk/p2p/base/portproxy.h"
32 #include "webrtc/base/thread.h"
37 MSG_SEND_ALLOCATION_DONE = 1,
38 MSG_SEND_ALLOCATED_PORTS,
41 typedef rtc::TypedMessageData<PortAllocatorSessionProxy*> ProxyObjData;
43 PortAllocatorSessionMuxer::PortAllocatorSessionMuxer(
44 PortAllocatorSession* session)
45 : worker_thread_(rtc::Thread::Current()),
47 candidate_done_signal_received_(false) {
48 session_->SignalPortReady.connect(
49 this, &PortAllocatorSessionMuxer::OnPortReady);
50 session_->SignalCandidatesAllocationDone.connect(
51 this, &PortAllocatorSessionMuxer::OnCandidatesAllocationDone);
54 PortAllocatorSessionMuxer::~PortAllocatorSessionMuxer() {
55 for (size_t i = 0; i < session_proxies_.size(); ++i)
56 delete session_proxies_[i];
58 SignalDestroyed(this);
61 void PortAllocatorSessionMuxer::RegisterSessionProxy(
62 PortAllocatorSessionProxy* session_proxy) {
63 session_proxies_.push_back(session_proxy);
64 session_proxy->SignalDestroyed.connect(
65 this, &PortAllocatorSessionMuxer::OnSessionProxyDestroyed);
66 session_proxy->set_impl(session_.get());
68 // Populate new proxy session with the information available in the actual
70 if (!ports_.empty()) {
72 this, MSG_SEND_ALLOCATED_PORTS, new ProxyObjData(session_proxy));
75 if (candidate_done_signal_received_) {
77 this, MSG_SEND_ALLOCATION_DONE, new ProxyObjData(session_proxy));
81 void PortAllocatorSessionMuxer::OnCandidatesAllocationDone(
82 PortAllocatorSession* session) {
83 candidate_done_signal_received_ = true;
86 void PortAllocatorSessionMuxer::OnPortReady(PortAllocatorSession* session,
87 PortInterface* port) {
88 ASSERT(session == session_.get());
89 ports_.push_back(port);
90 port->SignalDestroyed.connect(
91 this, &PortAllocatorSessionMuxer::OnPortDestroyed);
94 void PortAllocatorSessionMuxer::OnPortDestroyed(PortInterface* port) {
95 std::vector<PortInterface*>::iterator it =
96 std::find(ports_.begin(), ports_.end(), port);
97 if (it != ports_.end())
101 void PortAllocatorSessionMuxer::OnSessionProxyDestroyed(
102 PortAllocatorSession* proxy) {
104 std::vector<PortAllocatorSessionProxy*>::iterator it =
105 std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
106 if (it != session_proxies_.end()) {
107 session_proxies_.erase(it);
110 if (session_proxies_.empty()) {
111 // Destroy PortAllocatorSession and its associated muxer object if all
112 // proxies belonging to this session are already destroyed.
117 void PortAllocatorSessionMuxer::OnMessage(rtc::Message *pmsg) {
118 ProxyObjData* proxy = static_cast<ProxyObjData*>(pmsg->pdata);
119 switch (pmsg->message_id) {
120 case MSG_SEND_ALLOCATION_DONE:
121 SendAllocationDone_w(proxy->data());
124 case MSG_SEND_ALLOCATED_PORTS:
125 SendAllocatedPorts_w(proxy->data());
134 void PortAllocatorSessionMuxer::SendAllocationDone_w(
135 PortAllocatorSessionProxy* proxy) {
136 std::vector<PortAllocatorSessionProxy*>::iterator iter =
137 std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
138 if (iter != session_proxies_.end()) {
139 proxy->OnCandidatesAllocationDone(session_.get());
143 void PortAllocatorSessionMuxer::SendAllocatedPorts_w(
144 PortAllocatorSessionProxy* proxy) {
145 std::vector<PortAllocatorSessionProxy*>::iterator iter =
146 std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
147 if (iter != session_proxies_.end()) {
148 for (size_t i = 0; i < ports_.size(); ++i) {
149 PortInterface* port = ports_[i];
150 proxy->OnPortReady(session_.get(), port);
151 // If port already has candidates, send this to the clients of proxy
152 // session. This can happen if proxy is created later than the actual
154 if (!port->Candidates().empty()) {
155 proxy->OnCandidatesReady(session_.get(), port->Candidates());
161 PortAllocatorSessionProxy::~PortAllocatorSessionProxy() {
162 std::map<PortInterface*, PortProxy*>::iterator it;
163 for (it = proxy_ports_.begin(); it != proxy_ports_.end(); it++)
166 SignalDestroyed(this);
169 void PortAllocatorSessionProxy::set_impl(
170 PortAllocatorSession* session) {
173 impl_->SignalCandidatesReady.connect(
174 this, &PortAllocatorSessionProxy::OnCandidatesReady);
175 impl_->SignalPortReady.connect(
176 this, &PortAllocatorSessionProxy::OnPortReady);
177 impl_->SignalCandidatesAllocationDone.connect(
178 this, &PortAllocatorSessionProxy::OnCandidatesAllocationDone);
181 void PortAllocatorSessionProxy::StartGettingPorts() {
182 ASSERT(impl_ != NULL);
183 // Since all proxies share a common PortAllocatorSession, this check will
184 // prohibit sending multiple STUN ping messages to the stun server, which
185 // is a problem on Chrome. GetInitialPorts() and StartGetAllPorts() called
186 // from the worker thread and are called together from TransportChannel,
187 // checking for IsGettingAllPorts() for GetInitialPorts() will not be a
189 if (!impl_->IsGettingPorts()) {
190 impl_->StartGettingPorts();
194 void PortAllocatorSessionProxy::StopGettingPorts() {
195 ASSERT(impl_ != NULL);
196 if (impl_->IsGettingPorts()) {
197 impl_->StopGettingPorts();
201 bool PortAllocatorSessionProxy::IsGettingPorts() {
202 ASSERT(impl_ != NULL);
203 return impl_->IsGettingPorts();
206 void PortAllocatorSessionProxy::OnPortReady(PortAllocatorSession* session,
207 PortInterface* port) {
208 ASSERT(session == impl_);
210 PortProxy* proxy_port = new PortProxy();
211 proxy_port->set_impl(port);
212 proxy_ports_[port] = proxy_port;
213 SignalPortReady(this, proxy_port);
216 void PortAllocatorSessionProxy::OnCandidatesReady(
217 PortAllocatorSession* session,
218 const std::vector<Candidate>& candidates) {
219 ASSERT(session == impl_);
221 // Since all proxy sessions share a common PortAllocatorSession,
222 // all Candidates will have name associated with the common PAS.
223 // Change Candidate name with the PortAllocatorSessionProxy name.
224 std::vector<Candidate> our_candidates;
225 for (size_t i = 0; i < candidates.size(); ++i) {
226 Candidate new_local_candidate = candidates[i];
227 new_local_candidate.set_component(component_);
228 our_candidates.push_back(new_local_candidate);
230 SignalCandidatesReady(this, our_candidates);
233 void PortAllocatorSessionProxy::OnCandidatesAllocationDone(
234 PortAllocatorSession* session) {
235 ASSERT(session == impl_);
236 SignalCandidatesAllocationDone(this);
239 } // namespace cricket