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