~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/p2p/base/portallocatorsessionproxy.cc

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2004--2011, 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/portallocatorsessionproxy.h"
 
29
 
 
30
#include "talk/p2p/base/portallocator.h"
 
31
#include "talk/p2p/base/portproxy.h"
 
32
 
 
33
namespace cricket {
 
34
 
 
35
PortAllocatorSessionMuxer::PortAllocatorSessionMuxer(
 
36
    PortAllocatorSession* session)
 
37
    : session_(session) {
 
38
  session_->SignalPortReady.connect(
 
39
      this, &PortAllocatorSessionMuxer::OnPortReady);
 
40
}
 
41
 
 
42
PortAllocatorSessionMuxer::~PortAllocatorSessionMuxer() {
 
43
  for (size_t i = 0; i < session_proxies_.size(); ++i)
 
44
    delete session_proxies_[i];
 
45
 
 
46
  SignalDestroyed(this);
 
47
}
 
48
 
 
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());
 
55
}
 
56
 
 
57
void PortAllocatorSessionMuxer::OnPortReady(PortAllocatorSession* session,
 
58
                                            Port* port) {
 
59
  ASSERT(session == session_.get());
 
60
  ports_.push_back(port);
 
61
  port->SignalDestroyed.connect(
 
62
      this, &PortAllocatorSessionMuxer::OnPortDestroyed);
 
63
}
 
64
 
 
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())
 
69
    ports_.erase(it);
 
70
}
 
71
 
 
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);
 
78
 
 
79
  if (session_proxies_.empty()) {
 
80
    // Destroy PortAllocatorSession and its associated muxer object if all
 
81
    // proxies belonging to this session are already destroyed.
 
82
    delete this;
 
83
  }
 
84
}
 
85
 
 
86
PortAllocatorSessionProxy::~PortAllocatorSessionProxy() {
 
87
  std::map<Port*, PortProxy*>::iterator it;
 
88
  for (it = proxy_ports_.begin(); it != proxy_ports_.end(); it++)
 
89
    delete it->second;
 
90
 
 
91
  SignalDestroyed(this);
 
92
}
 
93
 
 
94
void PortAllocatorSessionProxy::set_impl(
 
95
    PortAllocatorSession* session) {
 
96
  impl_ = session;
 
97
 
 
98
  impl_->SignalCandidatesReady.connect(
 
99
      this, &PortAllocatorSessionProxy::OnCandidatesReady);
 
100
  impl_->SignalPortReady.connect(
 
101
      this, &PortAllocatorSessionProxy::OnPortReady);
 
102
}
 
103
 
 
104
void PortAllocatorSessionProxy::GetInitialPorts() {
 
105
  ASSERT(impl_ != NULL);
 
106
  impl_->GetInitialPorts();
 
107
}
 
108
 
 
109
void PortAllocatorSessionProxy::StartGetAllPorts() {
 
110
  ASSERT(impl_ != NULL);
 
111
  impl_->StartGetAllPorts();
 
112
}
 
113
 
 
114
void PortAllocatorSessionProxy::StopGetAllPorts() {
 
115
  ASSERT(impl_ != NULL);
 
116
  impl_->StartGetAllPorts();
 
117
}
 
118
 
 
119
bool PortAllocatorSessionProxy::IsGettingAllPorts() {
 
120
  ASSERT(impl_ != NULL);
 
121
  return impl_->IsGettingAllPorts();
 
122
}
 
123
 
 
124
void PortAllocatorSessionProxy::OnPortReady(PortAllocatorSession* session,
 
125
                                            Port* port) {
 
126
  ASSERT(session == impl_);
 
127
 
 
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);
 
134
}
 
135
 
 
136
void PortAllocatorSessionProxy::OnCandidatesReady(
 
137
    PortAllocatorSession* session,
 
138
    const std::vector<Candidate>& candidates) {
 
139
  ASSERT(session == impl_);
 
140
 
 
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);
 
149
  }
 
150
 
 
151
  SignalCandidatesReady(this, our_candidates);
 
152
}
 
153
 
 
154
}  // namespace cricket