1/*
2 * libjingle
3 * Copyright 2004--2011, 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_BASE_PORTALLOCATORSESSIONPROXY_H_
29#define TALK_P2P_BASE_PORTALLOCATORSESSIONPROXY_H_
30
31#include <string>
32
33#include "talk/p2p/base/candidate.h"
34#include "talk/p2p/base/portallocator.h"
35
36namespace cricket {
37class PortAllocator;
38class PortAllocatorSessionProxy;
39class PortProxy;
40
41// This class maintains the list of cricket::Port* objects. Ports will be
42// deleted upon receiving SignalDestroyed signal. This class is used when
43// PORTALLOCATOR_ENABLE_BUNDLE flag is set.
44
45class PortAllocatorSessionMuxer : public rtc::MessageHandler,
46                                  public sigslot::has_slots<> {
47 public:
48  explicit PortAllocatorSessionMuxer(PortAllocatorSession* session);
49  virtual ~PortAllocatorSessionMuxer();
50
51  void RegisterSessionProxy(PortAllocatorSessionProxy* session_proxy);
52
53  void OnPortReady(PortAllocatorSession* session, PortInterface* port);
54  void OnPortDestroyed(PortInterface* port);
55  void OnCandidatesAllocationDone(PortAllocatorSession* session);
56
57  const std::vector<PortInterface*>& ports() { return ports_; }
58
59  sigslot::signal1<PortAllocatorSessionMuxer*> SignalDestroyed;
60
61 private:
62  virtual void OnMessage(rtc::Message *pmsg);
63  void OnSessionProxyDestroyed(PortAllocatorSession* proxy);
64  void SendAllocationDone_w(PortAllocatorSessionProxy* proxy);
65  void SendAllocatedPorts_w(PortAllocatorSessionProxy* proxy);
66
67  // Port will be deleted when SignalDestroyed received, otherwise delete
68  // happens when PortAllocatorSession dtor is called.
69  rtc::Thread* worker_thread_;
70  std::vector<PortInterface*> ports_;
71  rtc::scoped_ptr<PortAllocatorSession> session_;
72  std::vector<PortAllocatorSessionProxy*> session_proxies_;
73  bool candidate_done_signal_received_;
74};
75
76class PortAllocatorSessionProxy : public PortAllocatorSession {
77 public:
78  PortAllocatorSessionProxy(const std::string& content_name,
79                            int component,
80                            uint32 flags)
81        // Use empty string as the ufrag and pwd because the proxy always uses
82        // the ufrag and pwd from the underlying implementation.
83      : PortAllocatorSession(content_name, component, "", "", flags),
84        impl_(NULL) {
85  }
86
87  virtual ~PortAllocatorSessionProxy();
88
89  PortAllocatorSession* impl() { return impl_; }
90  void set_impl(PortAllocatorSession* session);
91
92  // Forwards call to the actual PortAllocatorSession.
93  virtual void StartGettingPorts();
94  virtual void StopGettingPorts();
95  virtual bool IsGettingPorts();
96
97  virtual void set_generation(uint32 generation) {
98    ASSERT(impl_ != NULL);
99    impl_->set_generation(generation);
100  }
101
102  virtual uint32 generation() {
103    ASSERT(impl_ != NULL);
104    return impl_->generation();
105  }
106
107 private:
108  void OnPortReady(PortAllocatorSession* session, PortInterface* port);
109  void OnCandidatesReady(PortAllocatorSession* session,
110                         const std::vector<Candidate>& candidates);
111  void OnPortDestroyed(PortInterface* port);
112  void OnCandidatesAllocationDone(PortAllocatorSession* session);
113
114  // This is the actual PortAllocatorSession, owned by PortAllocator.
115  PortAllocatorSession* impl_;
116  std::map<PortInterface*, PortProxy*> proxy_ports_;
117
118  friend class PortAllocatorSessionMuxer;
119};
120
121}  // namespace cricket
122
123#endif  // TALK_P2P_BASE_PORTALLOCATORSESSIONPROXY_H_
124