15e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org/*
25e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * libjingle
35e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * Copyright 2010, Google Inc.
45e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *
55e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * Redistribution and use in source and binary forms, with or without
65e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * modification, are permitted provided that the following conditions are met:
75e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *
85e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *     this list of conditions and the following disclaimer.
105e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *     and/or other materials provided with the distribution.
135e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *     derived from this software without specific prior written permission.
155e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org *
165e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/basicpacketsocketfactory.h"
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/portallocator.h"
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/udpport.h"
35cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/scoped_ptr.h"
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgnamespace rtc {
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass SocketFactory;
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass Thread;
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakePortAllocatorSession : public PortAllocatorSession {
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
462a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  FakePortAllocatorSession(rtc::Thread* worker_thread,
472a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                           rtc::PacketSocketFactory* factory,
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                           const std::string& content_name,
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                           int component,
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                           const std::string& ice_ufrag,
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                           const std::string& ice_pwd)
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      : PortAllocatorSession(content_name, component, ice_ufrag, ice_pwd,
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                             cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG),
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        worker_thread_(worker_thread),
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        factory_(factory),
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        network_("network", "unittest",
572a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                 rtc::IPAddress(INADDR_LOOPBACK), 8),
585c9dd59107e049112f2e9a62d08a02ef4448a957wu@webrtc.org        port_(), running_(false),
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        port_config_count_(0) {
602a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK));
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void StartGettingPorts() {
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (!port_) {
655e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org      port_.reset(cricket::UDPPort::Create(worker_thread_,
665e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           factory_,
675e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           &network_,
684318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#ifdef USE_WEBRTC_DEV_BRANCH
695e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           network_.GetBestIP(),
704318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#else  // USE_WEBRTC_DEV_BRANCH
714318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org                                           network_.ip(),
724318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#endif  // USE_WEBRTC_DEV_BRANCH
735e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           0,
745e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           0,
755e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           username(),
765e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org                                           password()));
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      AddPort(port_.get());
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ++port_config_count_;
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    running_ = true;
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void StopGettingPorts() { running_ = false; }
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsGettingPorts() { return running_; }
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int port_config_count() { return port_config_count_; }
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void AddPort(cricket::Port* port) {
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    port->set_component(component_);
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    port->set_generation(0);
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    port->SignalPortComplete.connect(
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        this, &FakePortAllocatorSession::OnPortComplete);
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    port->PrepareAddress();
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    SignalPortReady(this, port);
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnPortComplete(cricket::Port* port) {
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    SignalCandidatesReady(this, port->Candidates());
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    SignalCandidatesAllocationDone(this);
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
1012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* worker_thread_;
1022a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::PacketSocketFactory* factory_;
1032a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Network network_;
1042a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<cricket::Port> port_;
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool running_;
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int port_config_count_;
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakePortAllocator : public cricket::PortAllocator {
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
1112a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  FakePortAllocator(rtc::Thread* worker_thread,
1122a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                    rtc::PacketSocketFactory* factory)
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      : worker_thread_(worker_thread), factory_(factory) {
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (factory_ == NULL) {
1152a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      owned_factory_.reset(new rtc::BasicPacketSocketFactory(
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          worker_thread_));
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      factory_ = owned_factory_.get();
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual cricket::PortAllocatorSession* CreateSessionInternal(
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& content_name,
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int component,
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& ice_ufrag,
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& ice_pwd) {
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return new FakePortAllocatorSession(
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        worker_thread_, factory_, content_name, component, ice_ufrag, ice_pwd);
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
1312a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* worker_thread_;
1322a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::PacketSocketFactory* factory_;
1332a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<rtc::BasicPacketSocketFactory> owned_factory_;
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_P2P_CLIENT_FAKEPORTALLOCATOR_H_
139