You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.1 KiB

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