10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2004--2011, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_P2P_BASE_PORTALLOCATORSESSIONPROXY_H_
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_P2P_BASE_PORTALLOCATORSESSIONPROXY_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/candidate.h"
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/portallocator.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass PortAllocator;
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass PortAllocatorSessionProxy;
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass PortProxy;
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This class maintains the list of cricket::Port* objects. Ports will be
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// deleted upon receiving SignalDestroyed signal. This class is used when
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// PORTALLOCATOR_ENABLE_BUNDLE flag is set.
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
452a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgclass PortAllocatorSessionMuxer : public rtc::MessageHandler,
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                  public sigslot::has_slots<> {
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  explicit PortAllocatorSessionMuxer(PortAllocatorSession* session);
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~PortAllocatorSessionMuxer();
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void RegisterSessionProxy(PortAllocatorSessionProxy* session_proxy);
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnPortReady(PortAllocatorSession* session, PortInterface* port);
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnPortDestroyed(PortInterface* port);
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnCandidatesAllocationDone(PortAllocatorSession* session);
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::vector<PortInterface*>& ports() { return ports_; }
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal1<PortAllocatorSessionMuxer*> SignalDestroyed;
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
622a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual void OnMessage(rtc::Message *pmsg);
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnSessionProxyDestroyed(PortAllocatorSession* proxy);
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SendAllocationDone_w(PortAllocatorSessionProxy* proxy);
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SendAllocatedPorts_w(PortAllocatorSessionProxy* proxy);
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Port will be deleted when SignalDestroyed received, otherwise delete
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // happens when PortAllocatorSession dtor is called.
692a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* worker_thread_;
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::vector<PortInterface*> ports_;
712a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<PortAllocatorSession> session_;
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::vector<PortAllocatorSessionProxy*> session_proxies_;
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool candidate_done_signal_received_;
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass PortAllocatorSessionProxy : public PortAllocatorSession {
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PortAllocatorSessionProxy(const std::string& content_name,
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                            int component,
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                            uint32 flags)
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        // Use empty string as the ufrag and pwd because the proxy always uses
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        // the ufrag and pwd from the underlying implementation.
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      : PortAllocatorSession(content_name, component, "", "", flags),
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        impl_(NULL) {
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~PortAllocatorSessionProxy();
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PortAllocatorSession* impl() { return impl_; }
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_impl(PortAllocatorSession* session);
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Forwards call to the actual PortAllocatorSession.
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void StartGettingPorts();
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void StopGettingPorts();
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsGettingPorts();
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void set_generation(uint32 generation) {
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ASSERT(impl_ != NULL);
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    impl_->set_generation(generation);
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual uint32 generation() {
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ASSERT(impl_ != NULL);
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return impl_->generation();
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnPortReady(PortAllocatorSession* session, PortInterface* port);
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnCandidatesReady(PortAllocatorSession* session,
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                         const std::vector<Candidate>& candidates);
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnPortDestroyed(PortInterface* port);
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnCandidatesAllocationDone(PortAllocatorSession* session);
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This is the actual PortAllocatorSession, owned by PortAllocator.
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PortAllocatorSession* impl_;
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::map<PortInterface*, PortProxy*> proxy_ports_;
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  friend class PortAllocatorSessionMuxer;
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_P2P_BASE_PORTALLOCATORSESSIONPROXY_H_
124