~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/portallocator.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2004--2005, 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_PORTALLOCATOR_H_
 
29
#define TALK_P2P_BASE_PORTALLOCATOR_H_
 
30
 
 
31
#include <string>
 
32
#include <vector>
 
33
 
 
34
#include "talk/base/sigslot.h"
 
35
#include "talk/p2p/base/port.h"
 
36
 
 
37
namespace cricket {
 
38
 
 
39
// PortAllocator is responsible for allocating Port types for a given
 
40
// P2PSocket. It also handles port freeing.
 
41
//
 
42
// Clients can override this class to control port allocation, including
 
43
// what kinds of ports are allocated.
 
44
 
 
45
const uint32 PORTALLOCATOR_DISABLE_UDP = 0x01;
 
46
const uint32 PORTALLOCATOR_DISABLE_STUN = 0x02;
 
47
const uint32 PORTALLOCATOR_DISABLE_RELAY = 0x04;
 
48
const uint32 PORTALLOCATOR_DISABLE_TCP = 0x08;
 
49
const uint32 PORTALLOCATOR_ENABLE_SHAKER = 0x10;
 
50
const uint32 PORTALLOCATOR_ENABLE_BUNDLE = 0x20;
 
51
 
 
52
const uint32 kDefaultPortAllocatorFlags = 0;
 
53
 
 
54
class PortAllocatorSessionMuxer;
 
55
 
 
56
class PortAllocatorSession : public sigslot::has_slots<> {
 
57
 public:
 
58
  // TODO Remove session_type argument (and other places), as
 
59
  // its not used.
 
60
  PortAllocatorSession(const std::string& name,
 
61
                       const std::string& session_type,
 
62
                       uint32 flags)
 
63
      : name_(name),
 
64
        session_type_(session_type),
 
65
        flags_(flags) {
 
66
  }
 
67
 
 
68
  // Subclasses should clean up any ports created.
 
69
  virtual ~PortAllocatorSession() {}
 
70
 
 
71
  uint32 flags() const { return flags_; }
 
72
  void set_flags(uint32 flags) { flags_ = flags; }
 
73
  const std::string& name() const { return name_; }
 
74
  const std::string& session_type() const { return session_type_; }
 
75
 
 
76
  // Prepares an initial set of ports to try.
 
77
  virtual void GetInitialPorts() = 0;
 
78
 
 
79
  // Starts and stops the flow of additional ports to try.
 
80
  virtual void StartGetAllPorts() = 0;
 
81
  virtual void StopGetAllPorts() = 0;
 
82
  virtual bool IsGettingAllPorts() = 0;
 
83
 
 
84
  sigslot::signal2<PortAllocatorSession*, Port*> SignalPortReady;
 
85
  sigslot::signal2<PortAllocatorSession*,
 
86
                   const std::vector<Candidate>&> SignalCandidatesReady;
 
87
  sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
 
88
 
 
89
  uint32 generation() { return generation_; }
 
90
  void set_generation(uint32 generation) { generation_ = generation; }
 
91
  sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
 
92
 
 
93
 protected:
 
94
  std::string name_;
 
95
  std::string session_type_;
 
96
 
 
97
 private:
 
98
  uint32 flags_;
 
99
  uint32 generation_;
 
100
};
 
101
 
 
102
class PortAllocator : public sigslot::has_slots<> {
 
103
 public:
 
104
  PortAllocator() :
 
105
      flags_(kDefaultPortAllocatorFlags),
 
106
      min_port_(0),
 
107
      max_port_(0) {
 
108
  }
 
109
  virtual ~PortAllocator();
 
110
 
 
111
  PortAllocatorSession* CreateSession(
 
112
      const std::string& sid,
 
113
      const std::string& name,
 
114
      const std::string& session_type);
 
115
 
 
116
  PortAllocatorSessionMuxer* GetSessionMuxer(const std::string& sid) const;
 
117
  void OnSessionMuxerDestroyed(PortAllocatorSessionMuxer* session);
 
118
 
 
119
  uint32 flags() const { return flags_; }
 
120
  void set_flags(uint32 flags) { flags_ = flags; }
 
121
 
 
122
  const std::string& user_agent() const { return agent_; }
 
123
  const talk_base::ProxyInfo& proxy() const { return proxy_; }
 
124
  void set_proxy(const std::string& agent, const talk_base::ProxyInfo& proxy) {
 
125
    agent_ = agent;
 
126
    proxy_ = proxy;
 
127
  }
 
128
 
 
129
  // Gets/Sets the port range to use when choosing client ports.
 
130
  int min_port() const { return min_port_; }
 
131
  int max_port() const { return max_port_; }
 
132
  bool SetPortRange(int min_port, int max_port) {
 
133
    if (min_port > max_port) {
 
134
      return false;
 
135
    }
 
136
 
 
137
    min_port_ = min_port;
 
138
    max_port_ = max_port;
 
139
    return true;
 
140
  }
 
141
 
 
142
 protected:
 
143
  // TODO - Change this version of CreateSession name to avoid method hiding
 
144
  // when called by the derived classes.
 
145
  virtual PortAllocatorSession* CreateSession(const std::string &name,
 
146
      const std::string &session_type) = 0;
 
147
 
 
148
  typedef std::map<std::string, PortAllocatorSessionMuxer*> SessionMuxerMap;
 
149
 
 
150
  uint32 flags_;
 
151
  std::string agent_;
 
152
  talk_base::ProxyInfo proxy_;
 
153
  int min_port_;
 
154
  int max_port_;
 
155
  SessionMuxerMap muxers_;
 
156
};
 
157
 
 
158
}  // namespace cricket
 
159
 
 
160
#endif  // TALK_P2P_BASE_PORTALLOCATOR_H_