2 * Copyright 2010 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_
12 #define WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_
15 #include "webrtc/p2p/base/basicpacketsocketfactory.h"
16 #include "webrtc/p2p/base/portallocator.h"
17 #include "webrtc/p2p/base/udpport.h"
18 #include "webrtc/base/scoped_ptr.h"
27 class FakePortAllocatorSession : public PortAllocatorSession {
29 FakePortAllocatorSession(rtc::Thread* worker_thread,
30 rtc::PacketSocketFactory* factory,
31 const std::string& content_name,
33 const std::string& ice_ufrag,
34 const std::string& ice_pwd)
35 : PortAllocatorSession(content_name, component, ice_ufrag, ice_pwd,
36 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG),
37 worker_thread_(worker_thread),
39 network_("network", "unittest",
40 rtc::IPAddress(INADDR_LOOPBACK), 8),
41 port_(), running_(false),
42 port_config_count_(0) {
43 network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK));
46 virtual void StartGettingPorts() {
48 port_.reset(cricket::UDPPort::Create(worker_thread_,
51 #ifdef USE_WEBRTC_DEV_BRANCH
53 #else // USE_WEBRTC_DEV_BRANCH
55 #endif // USE_WEBRTC_DEV_BRANCH
66 virtual void StopGettingPorts() { running_ = false; }
67 virtual bool IsGettingPorts() { return running_; }
68 int port_config_count() { return port_config_count_; }
70 void AddPort(cricket::Port* port) {
71 port->set_component(component_);
72 port->set_generation(0);
73 port->SignalPortComplete.connect(
74 this, &FakePortAllocatorSession::OnPortComplete);
75 port->PrepareAddress();
76 SignalPortReady(this, port);
78 void OnPortComplete(cricket::Port* port) {
79 SignalCandidatesReady(this, port->Candidates());
80 SignalCandidatesAllocationDone(this);
84 rtc::Thread* worker_thread_;
85 rtc::PacketSocketFactory* factory_;
86 rtc::Network network_;
87 rtc::scoped_ptr<cricket::Port> port_;
89 int port_config_count_;
92 class FakePortAllocator : public cricket::PortAllocator {
94 FakePortAllocator(rtc::Thread* worker_thread,
95 rtc::PacketSocketFactory* factory)
96 : worker_thread_(worker_thread), factory_(factory) {
97 if (factory_ == NULL) {
98 owned_factory_.reset(new rtc::BasicPacketSocketFactory(
100 factory_ = owned_factory_.get();
104 virtual cricket::PortAllocatorSession* CreateSessionInternal(
105 const std::string& content_name,
107 const std::string& ice_ufrag,
108 const std::string& ice_pwd) {
109 return new FakePortAllocatorSession(
110 worker_thread_, factory_, content_name, component, ice_ufrag, ice_pwd);
114 rtc::Thread* worker_thread_;
115 rtc::PacketSocketFactory* factory_;
116 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> owned_factory_;
119 } // namespace cricket
121 #endif // WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_