blob: a0ef3b722deff1d41ad064d605d6411bd09eca46 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2008, 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_HTTPPORTALLOCATOR_H_
29#define TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_
30
31#include <list>
32#include <string>
33#include <vector>
34
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/p2p/client/basicportallocator.h"
36
37class HttpPortAllocatorTest_TestSessionRequestUrl_Test;
38
39namespace talk_base {
40class AsyncHttpRequest;
41class SignalThread;
42}
43
44namespace cricket {
45
46class HttpPortAllocatorBase : public BasicPortAllocator {
47 public:
48 // The number of HTTP requests we should attempt before giving up.
49 static const int kNumRetries;
50
51 // Records the URL that we will GET in order to create a session.
52 static const char kCreateSessionURL[];
53
54 HttpPortAllocatorBase(talk_base::NetworkManager* network_manager,
55 const std::string& user_agent);
56 HttpPortAllocatorBase(talk_base::NetworkManager* network_manager,
57 talk_base::PacketSocketFactory* socket_factory,
58 const std::string& user_agent);
59 virtual ~HttpPortAllocatorBase();
60
61 // CreateSession is defined in BasicPortAllocator but is
62 // redefined here as pure virtual.
63 virtual PortAllocatorSession* CreateSessionInternal(
64 const std::string& content_name,
65 int component,
66 const std::string& ice_ufrag,
67 const std::string& ice_pwd) = 0;
68
69 void SetStunHosts(const std::vector<talk_base::SocketAddress>& hosts) {
70 if (!hosts.empty()) {
71 stun_hosts_ = hosts;
72 }
73 }
74 void SetRelayHosts(const std::vector<std::string>& hosts) {
75 if (!hosts.empty()) {
76 relay_hosts_ = hosts;
77 }
78 }
79 void SetRelayToken(const std::string& relay) { relay_token_ = relay; }
80
81 const std::vector<talk_base::SocketAddress>& stun_hosts() const {
82 return stun_hosts_;
83 }
84
85 const std::vector<std::string>& relay_hosts() const {
86 return relay_hosts_;
87 }
88
89 const std::string& relay_token() const {
90 return relay_token_;
91 }
92
93 const std::string& user_agent() const {
94 return agent_;
95 }
96
97 private:
98 std::vector<talk_base::SocketAddress> stun_hosts_;
99 std::vector<std::string> relay_hosts_;
100 std::string relay_token_;
101 std::string agent_;
102};
103
104class RequestData;
105
106class HttpPortAllocatorSessionBase : public BasicPortAllocatorSession {
107 public:
108 HttpPortAllocatorSessionBase(
109 HttpPortAllocatorBase* allocator,
110 const std::string& content_name,
111 int component,
112 const std::string& ice_ufrag,
113 const std::string& ice_pwd,
114 const std::vector<talk_base::SocketAddress>& stun_hosts,
115 const std::vector<std::string>& relay_hosts,
116 const std::string& relay,
117 const std::string& agent);
118 virtual ~HttpPortAllocatorSessionBase();
119
120 const std::string& relay_token() const {
121 return relay_token_;
122 }
123
124 const std::string& user_agent() const {
125 return agent_;
126 }
127
128 virtual void SendSessionRequest(const std::string& host, int port) = 0;
129 virtual void ReceiveSessionResponse(const std::string& response);
130
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000131 // Made public for testing. Should be protected.
132 std::string GetSessionRequestUrl();
133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 protected:
135 virtual void GetPortConfigurations();
136 void TryCreateRelaySession();
137 virtual HttpPortAllocatorBase* allocator() {
138 return static_cast<HttpPortAllocatorBase*>(
139 BasicPortAllocatorSession::allocator());
140 }
141
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 std::vector<std::string> relay_hosts_;
144 std::vector<talk_base::SocketAddress> stun_hosts_;
145 std::string relay_token_;
146 std::string agent_;
147 int attempts_;
148};
149
150class HttpPortAllocator : public HttpPortAllocatorBase {
151 public:
152 HttpPortAllocator(talk_base::NetworkManager* network_manager,
153 const std::string& user_agent);
154 HttpPortAllocator(talk_base::NetworkManager* network_manager,
155 talk_base::PacketSocketFactory* socket_factory,
156 const std::string& user_agent);
157 virtual ~HttpPortAllocator();
158 virtual PortAllocatorSession* CreateSessionInternal(
159 const std::string& content_name,
160 int component,
161 const std::string& ice_ufrag, const std::string& ice_pwd);
162};
163
164class HttpPortAllocatorSession : public HttpPortAllocatorSessionBase {
165 public:
166 HttpPortAllocatorSession(
167 HttpPortAllocator* allocator,
168 const std::string& content_name,
169 int component,
170 const std::string& ice_ufrag,
171 const std::string& ice_pwd,
172 const std::vector<talk_base::SocketAddress>& stun_hosts,
173 const std::vector<std::string>& relay_hosts,
174 const std::string& relay,
175 const std::string& agent);
176 virtual ~HttpPortAllocatorSession();
177
178 virtual void SendSessionRequest(const std::string& host, int port);
179
180 protected:
181 // Protected for diagnostics.
182 virtual void OnRequestDone(talk_base::SignalThread* request);
183
184 private:
185 std::list<talk_base::AsyncHttpRequest*> requests_;
186};
187
188} // namespace cricket
189
190#endif // TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_