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"
35
PortAllocatorSessionMuxer::PortAllocatorSessionMuxer(
36
PortAllocatorSession* session)
38
session_->SignalPortReady.connect(
39
this, &PortAllocatorSessionMuxer::OnPortReady);
42
PortAllocatorSessionMuxer::~PortAllocatorSessionMuxer() {
43
for (size_t i = 0; i < session_proxies_.size(); ++i)
44
delete session_proxies_[i];
46
SignalDestroyed(this);
49
void PortAllocatorSessionMuxer::RegisterSessionProxy(
50
PortAllocatorSessionProxy* session_proxy) {
51
session_proxies_.push_back(session_proxy);
52
session_proxy->SignalDestroyed.connect(
53
this, &PortAllocatorSessionMuxer::OnSessionProxyDestroyed);
54
session_proxy->set_impl(session_.get());
57
void PortAllocatorSessionMuxer::OnPortReady(PortAllocatorSession* session,
59
ASSERT(session == session_.get());
60
ports_.push_back(port);
61
port->SignalDestroyed.connect(
62
this, &PortAllocatorSessionMuxer::OnPortDestroyed);
65
void PortAllocatorSessionMuxer::OnPortDestroyed(Port* port) {
66
std::vector<Port*>::iterator it =
67
std::find(ports_.begin(), ports_.end(), port);
68
if (it != ports_.end())
72
void PortAllocatorSessionMuxer::OnSessionProxyDestroyed(
73
PortAllocatorSession* proxy) {
74
std::vector<PortAllocatorSessionProxy*>::iterator it =
75
std::find(session_proxies_.begin(), session_proxies_.end(), proxy);
76
if (it != session_proxies_.end())
77
session_proxies_.erase(it);
79
if (session_proxies_.empty()) {
80
// Destroy PortAllocatorSession and its associated muxer object if all
81
// proxies belonging to this session are already destroyed.
86
PortAllocatorSessionProxy::~PortAllocatorSessionProxy() {
87
std::map<Port*, PortProxy*>::iterator it;
88
for (it = proxy_ports_.begin(); it != proxy_ports_.end(); it++)
91
SignalDestroyed(this);
94
void PortAllocatorSessionProxy::set_impl(
95
PortAllocatorSession* session) {
98
impl_->SignalCandidatesReady.connect(
99
this, &PortAllocatorSessionProxy::OnCandidatesReady);
100
impl_->SignalPortReady.connect(
101
this, &PortAllocatorSessionProxy::OnPortReady);
104
void PortAllocatorSessionProxy::GetInitialPorts() {
105
ASSERT(impl_ != NULL);
106
impl_->GetInitialPorts();
109
void PortAllocatorSessionProxy::StartGetAllPorts() {
110
ASSERT(impl_ != NULL);
111
impl_->StartGetAllPorts();
114
void PortAllocatorSessionProxy::StopGetAllPorts() {
115
ASSERT(impl_ != NULL);
116
impl_->StartGetAllPorts();
119
bool PortAllocatorSessionProxy::IsGettingAllPorts() {
120
ASSERT(impl_ != NULL);
121
return impl_->IsGettingAllPorts();
124
void PortAllocatorSessionProxy::OnPortReady(PortAllocatorSession* session,
126
ASSERT(session == impl_);
128
PortProxy* proxy_port = new PortProxy(
129
port->thread(), port->type(), port->socket_factory(), port->network(),
130
port->ip(), port->min_port(), port->max_port());
131
proxy_port->set_impl(port);
132
proxy_ports_[port] = proxy_port;
133
SignalPortReady(this, proxy_port);
136
void PortAllocatorSessionProxy::OnCandidatesReady(
137
PortAllocatorSession* session,
138
const std::vector<Candidate>& candidates) {
139
ASSERT(session == impl_);
141
// Since all proxy sessions share a common PortAllocatorSession,
142
// all Candidates will have name associated with the common PAS.
143
// Change Candidate name with the PortAllocatorSessionProxy name.
144
std::vector<Candidate> our_candidates;
145
for (size_t i = 0; i < candidates.size(); ++i) {
146
Candidate new_local_candidate = candidates[i];
147
new_local_candidate.set_name(name_);
148
our_candidates.push_back(new_local_candidate);
151
SignalCandidatesReady(this, our_candidates);
154
} // namespace cricket