Home | History | Annotate | Download | only in client
      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 
     28 #ifndef TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_
     29 #define TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 #include "talk/p2p/client/basicportallocator.h"
     34 
     35 namespace talk_base {
     36 class SignalThread;
     37 }
     38 
     39 namespace cricket {
     40 
     41 class HttpPortAllocator : public BasicPortAllocator {
     42  public:
     43   // Records the port on the hosts that will receive HTTP requests.
     44   static const int kHostPort;
     45 
     46   // The number of HTTP requests we should attempt before giving up.
     47   static const int kNumRetries;
     48 
     49   // Records the URL that we will GET in order to create a session.
     50   static const std::string kCreateSessionURL;
     51 
     52   HttpPortAllocator(talk_base::NetworkManager* network_manager,
     53                     talk_base::PacketSocketFactory* socket_factory,
     54                     const std::string& user_agent);
     55   virtual ~HttpPortAllocator();
     56 
     57   virtual PortAllocatorSession* CreateSession(const std::string& name,
     58                                               const std::string& session_type);
     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 
     71   const std::vector<talk_base::SocketAddress>& stun_hosts() const {
     72     return stun_hosts_;
     73   }
     74 
     75   const std::vector<std::string>& relay_hosts() const {
     76     return relay_hosts_;
     77   }
     78 
     79   const std::string& relay_token() const {
     80     return relay_token_;
     81   }
     82 
     83   const std::string& user_agent() const {
     84     return agent_;
     85   }
     86 
     87  private:
     88   std::vector<talk_base::SocketAddress> stun_hosts_;
     89   std::vector<std::string> relay_hosts_;
     90   std::string relay_token_;
     91   std::string agent_;
     92 };
     93 
     94 class RequestData;
     95 
     96 class HttpPortAllocatorSession : public BasicPortAllocatorSession {
     97  public:
     98   HttpPortAllocatorSession(
     99       HttpPortAllocator* allocator,
    100       const std::string& name,
    101       const std::string& session_type,
    102       const std::vector<talk_base::SocketAddress>& stun_hosts,
    103       const std::vector<std::string>& relay_hosts,
    104       const std::string& relay,
    105       const std::string& agent);
    106   virtual ~HttpPortAllocatorSession() {}
    107 
    108   const std::string& relay_token() const {
    109     return relay_token_;
    110   }
    111   virtual void SendSessionRequest(const std::string& host, int port);
    112   virtual void ReceiveSessionResponse(const std::string& response);
    113 
    114  protected:
    115   virtual void GetPortConfigurations();
    116   void TryCreateRelaySession();
    117 
    118  private:
    119   virtual HttpPortAllocator* allocator() {
    120     return static_cast<HttpPortAllocator*>(
    121         BasicPortAllocatorSession::allocator());
    122   }
    123 
    124   void OnRequestDone(talk_base::SignalThread* request);
    125 
    126   std::vector<std::string> relay_hosts_;
    127   std::vector<talk_base::SocketAddress> stun_hosts_;
    128   std::string relay_token_;
    129   std::string agent_;
    130   int attempts_;
    131 };
    132 
    133 }  // namespace cricket
    134 
    135 #endif  // TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_
    136