1/*
2 * libjingle
3 * Copyright 2010, 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#ifndef TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
29#define TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
30
31#include <string>
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"
36
37namespace rtc {
38class SocketFactory;
39class Thread;
40}
41
42namespace cricket {
43
44class FakePortAllocatorSession : public PortAllocatorSession {
45 public:
46  FakePortAllocatorSession(rtc::Thread* worker_thread,
47                           rtc::PacketSocketFactory* factory,
48                           const std::string& content_name,
49                           int component,
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),
55        factory_(factory),
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));
61  }
62
63  virtual void StartGettingPorts() {
64    if (!port_) {
65      port_.reset(cricket::UDPPort::Create(worker_thread_,
66                                           factory_,
67                                           &network_,
68#ifdef USE_WEBRTC_DEV_BRANCH
69                                           network_.GetBestIP(),
70#else  // USE_WEBRTC_DEV_BRANCH
71                                           network_.ip(),
72#endif  // USE_WEBRTC_DEV_BRANCH
73                                           0,
74                                           0,
75                                           username(),
76                                           password()));
77      AddPort(port_.get());
78    }
79    ++port_config_count_;
80    running_ = true;
81  }
82
83  virtual void StopGettingPorts() { running_ = false; }
84  virtual bool IsGettingPorts() { return running_; }
85  int port_config_count() { return port_config_count_; }
86
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);
94  }
95  void OnPortComplete(cricket::Port* port) {
96    SignalCandidatesReady(this, port->Candidates());
97    SignalCandidatesAllocationDone(this);
98  }
99
100 private:
101  rtc::Thread* worker_thread_;
102  rtc::PacketSocketFactory* factory_;
103  rtc::Network network_;
104  rtc::scoped_ptr<cricket::Port> port_;
105  bool running_;
106  int port_config_count_;
107};
108
109class FakePortAllocator : public cricket::PortAllocator {
110 public:
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(
116          worker_thread_));
117      factory_ = owned_factory_.get();
118    }
119  }
120
121  virtual cricket::PortAllocatorSession* CreateSessionInternal(
122      const std::string& content_name,
123      int component,
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);
128  }
129
130 private:
131  rtc::Thread* worker_thread_;
132  rtc::PacketSocketFactory* factory_;
133  rtc::scoped_ptr<rtc::BasicPacketSocketFactory> owned_factory_;
134};
135
136}  // namespace cricket
137
138#endif  // TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
139