Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / p2p / client / fakeportallocator.h
1 /*
2  *  Copyright 2010 The WebRTC Project Authors. All rights reserved.
3  *
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.
9  */
10
11 #ifndef WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_
12 #define WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_
13
14 #include <string>
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"
19
20 namespace rtc {
21 class SocketFactory;
22 class Thread;
23 }
24
25 namespace cricket {
26
27 class FakePortAllocatorSession : public PortAllocatorSession {
28  public:
29   FakePortAllocatorSession(rtc::Thread* worker_thread,
30                            rtc::PacketSocketFactory* factory,
31                            const std::string& content_name,
32                            int component,
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),
38         factory_(factory),
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));
44   }
45
46   virtual void StartGettingPorts() {
47     if (!port_) {
48       port_.reset(cricket::UDPPort::Create(worker_thread_,
49                                            factory_,
50                                            &network_,
51 #ifdef USE_WEBRTC_DEV_BRANCH
52                                            network_.GetBestIP(),
53 #else  // USE_WEBRTC_DEV_BRANCH
54                                            network_.ip(),
55 #endif  // USE_WEBRTC_DEV_BRANCH
56                                            0,
57                                            0,
58                                            username(),
59                                            password()));
60       AddPort(port_.get());
61     }
62     ++port_config_count_;
63     running_ = true;
64   }
65
66   virtual void StopGettingPorts() { running_ = false; }
67   virtual bool IsGettingPorts() { return running_; }
68   int port_config_count() { return port_config_count_; }
69
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);
77   }
78   void OnPortComplete(cricket::Port* port) {
79     SignalCandidatesReady(this, port->Candidates());
80     SignalCandidatesAllocationDone(this);
81   }
82
83  private:
84   rtc::Thread* worker_thread_;
85   rtc::PacketSocketFactory* factory_;
86   rtc::Network network_;
87   rtc::scoped_ptr<cricket::Port> port_;
88   bool running_;
89   int port_config_count_;
90 };
91
92 class FakePortAllocator : public cricket::PortAllocator {
93  public:
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(
99           worker_thread_));
100       factory_ = owned_factory_.get();
101     }
102   }
103
104   virtual cricket::PortAllocatorSession* CreateSessionInternal(
105       const std::string& content_name,
106       int component,
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);
111   }
112
113  private:
114   rtc::Thread* worker_thread_;
115   rtc::PacketSocketFactory* factory_;
116   rtc::scoped_ptr<rtc::BasicPacketSocketFactory> owned_factory_;
117 };
118
119 }  // namespace cricket
120
121 #endif  // WEBRTC_P2P_CLIENT_FAKEPORTALLOCATOR_H_