3 * Copyright 2010, 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 #ifndef TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
29 #define TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
32 #include "talk/p2p/base/basicpacketsocketfactory.h"
33 #include "talk/p2p/base/portallocator.h"
34 #include "talk/p2p/base/udpport.h"
35 #include "webrtc/base/scoped_ptr.h"
44 class FakePortAllocatorSession : public PortAllocatorSession {
46 FakePortAllocatorSession(rtc::Thread* worker_thread,
47 rtc::PacketSocketFactory* factory,
48 const std::string& content_name,
50 const std::string& ice_ufrag,
51 const std::string& ice_pwd)
52 : PortAllocatorSession(content_name, component, ice_ufrag, ice_pwd,
53 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG),
54 worker_thread_(worker_thread),
56 network_("network", "unittest",
57 rtc::IPAddress(INADDR_LOOPBACK), 8),
58 port_(), running_(false),
59 port_config_count_(0) {
60 network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK));
63 virtual void StartGettingPorts() {
65 port_.reset(cricket::UDPPort::Create(worker_thread_,
68 #ifdef USE_WEBRTC_DEV_BRANCH
70 #else // USE_WEBRTC_DEV_BRANCH
72 #endif // USE_WEBRTC_DEV_BRANCH
83 virtual void StopGettingPorts() { running_ = false; }
84 virtual bool IsGettingPorts() { return running_; }
85 int port_config_count() { return port_config_count_; }
87 void AddPort(cricket::Port* port) {
88 port->set_component(component_);
89 port->set_generation(0);
90 port->SignalPortComplete.connect(
91 this, &FakePortAllocatorSession::OnPortComplete);
92 port->PrepareAddress();
93 SignalPortReady(this, port);
95 void OnPortComplete(cricket::Port* port) {
96 SignalCandidatesReady(this, port->Candidates());
97 SignalCandidatesAllocationDone(this);
101 rtc::Thread* worker_thread_;
102 rtc::PacketSocketFactory* factory_;
103 rtc::Network network_;
104 rtc::scoped_ptr<cricket::Port> port_;
106 int port_config_count_;
109 class FakePortAllocator : public cricket::PortAllocator {
111 FakePortAllocator(rtc::Thread* worker_thread,
112 rtc::PacketSocketFactory* factory)
113 : worker_thread_(worker_thread), factory_(factory) {
114 if (factory_ == NULL) {
115 owned_factory_.reset(new rtc::BasicPacketSocketFactory(
117 factory_ = owned_factory_.get();
121 virtual cricket::PortAllocatorSession* CreateSessionInternal(
122 const std::string& content_name,
124 const std::string& ice_ufrag,
125 const std::string& ice_pwd) {
126 return new FakePortAllocatorSession(
127 worker_thread_, factory_, content_name, component, ice_ufrag, ice_pwd);
131 rtc::Thread* worker_thread_;
132 rtc::PacketSocketFactory* factory_;
133 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> owned_factory_;
136 } // namespace cricket
138 #endif // TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_