RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
plugins/exchange/http_exchange_connector.h
00001 /* http_exchange_connector.h                                       -*- C++ -*-
00002    Jeremy Barnes, 31 January 2011
00003    Copyright (c) 2011 Datacratic.  All rights reserved.
00004 
00005    Endpoint for bidding system.
00006 */
00007 
00008 #pragma once
00009 
00010 #include <boost/scoped_ptr.hpp>
00011 #include <boost/shared_ptr.hpp>
00012 #include "jml/arch/atomic_ops.h"
00013 #include "soa/service/json_endpoint.h"
00014 #include "soa/service/stats_events.h"
00015 #include "rtbkit/common/auction.h"
00016 #include <limits>
00017 #include "rtbkit/common/exchange_connector.h"
00018 #include <boost/algorithm/string.hpp>
00019 
00020 
00021 namespace RTBKIT {
00022 
00023 struct HttpExchangeConnector;
00024 struct HttpAuctionHandler;
00025 
00026 
00027 /*****************************************************************************/
00028 /* HTTP EXCHANGE CONNECTOR                                                   */
00029 /*****************************************************************************/
00030 
00033 struct HttpExchangeConnector
00034     : public ExchangeConnector,
00035       public HttpEndpoint {
00036 
00037     HttpExchangeConnector(const std::string & name,
00038                           ServiceBase & parent);
00039 
00040     HttpExchangeConnector(const std::string & name,
00041                           std::shared_ptr<ServiceProxies> proxies);
00042 
00043     ~HttpExchangeConnector();
00044 
00046     int numServingRequest() const
00047     {
00048         return numServingRequest_;
00049     }
00050     
00051     virtual Json::Value getServiceStatus() const;
00052 
00053 
00054     /*************************************************************************/
00055     /* METHODS CALLED BY THE ROUTER TO CONTROL THE EXCHANGE CONNECTOR        */
00056     /*************************************************************************/
00057 
00061     virtual void configure(const Json::Value & parameters);
00062 
00064     void configureHttp(int numThreads,
00065                        const PortRange & listenPort,
00066                        const std::string & bindHost = "*",
00067                        bool performNameLookup = false,
00068                        int backlog = DEF_BACKLOG,
00069                        const std::string & auctionResource = "/auctions",
00070                        const std::string & auctionVerb = "POST");
00071 
00073     virtual void start();
00074 
00076     virtual void shutdown();
00077 
00082     virtual void enableUntil(Date date)
00083     {
00084         this->enabledUntil = date;
00085     }
00086 
00088     bool isEnabled(Date now = Date::now()) const
00089     {
00090         return now <= enabledUntil;
00091     }
00092 
00096     virtual void setAcceptBidRequestProbability(double prob)
00097     {
00098         if (prob < 0 || prob > 1)
00099             throw ML::Exception("invalid probability for "
00100                                 "setBidRequestProbability: "
00101                                 "%f is not between 0 and 1");
00102         this->acceptAuctionProbability = prob;
00103     }
00104 
00105 
00106     /*************************************************************************/
00107     /* METHODS TO OVERRIDE FOR A GIVEN EXCHANGE                              */
00108     /*************************************************************************/
00109 
00113     virtual std::string exchangeName() const = 0;
00114 
00116     virtual std::shared_ptr<BidRequest>
00117     parseBidRequest(HttpAuctionHandler & connection,
00118                     const HttpHeader & header,
00119                     const std::string & payload);
00120 
00127     virtual double
00128     getTimeAvailableMs(HttpAuctionHandler & connection,
00129                        const HttpHeader & header,
00130                        const std::string & payload);
00131 
00141     virtual double
00142     getRoundTripTimeMs(HttpAuctionHandler & connection,
00143                        const HttpHeader & header);
00144 
00154     virtual HttpResponse
00155     getResponse(const HttpAuctionHandler & connection,
00156                 const HttpHeader & requestHeader,
00157                 const Auction & auction) const;
00158 
00162     virtual HttpResponse
00163     getDroppedAuctionResponse(const HttpAuctionHandler & connection,
00164                               const Auction & auction,
00165                               const std::string & reason) const;
00166 
00176     virtual HttpResponse
00177     getErrorResponse(const HttpAuctionHandler & connection,
00178                      const Auction & auction,
00179                      const std::string & errorMessage) const;
00180 
00189     virtual void
00190     handleUnknownRequest(HttpAuctionHandler & connection,
00191                          const HttpHeader & header,
00192                          const std::string & payload) const;
00193 
00208     virtual ExchangeCompatibility
00209     getCampaignCompatibility(const AgentConfig & config,
00210                              bool includeReasons) const;
00211     
00215     virtual ExchangeCompatibility
00216     getCreativeCompatibility(const Creative & creative,
00217                              bool includeReasons) const;
00218 
00219 protected:
00220     virtual std::shared_ptr<ConnectionHandler> makeNewHandler();
00221     virtual std::shared_ptr<HttpAuctionHandler> makeNewHandlerShared();
00222 
00224     double acceptAuctionProbability;
00225 
00230     Date enabledUntil;
00231     
00236     typedef boost::function<void (std::shared_ptr<Auction>, Date)> OnTimeout;
00237     OnTimeout onTimeout;
00238 
00242     typedef boost::function<HttpAuctionHandler * ()> HandlerFactory;
00243     HandlerFactory handlerFactory;
00244 
00245     int numServingRequest_;  
00246 
00248     int numThreads;         
00249     PortRange listenPort;
00250     std::string bindHost;
00251     bool performNameLookup;
00252     int backlog;
00253     std::string auctionResource;
00254     std::string auctionVerb;
00255 
00257     std::unordered_map<std::string, float> pingTimesByHostMs;
00258 
00260     float pingTimeUnknownHostsMs;
00261     
00262 private:
00263     friend class HttpAuctionHandler;
00264 
00265     Lock handlersLock;
00266     std::set<std::shared_ptr<HttpAuctionHandler> > handlers;
00267 
00268     void finishedWithHandler(std::shared_ptr<HttpAuctionHandler> handler);
00269 
00271     void postConstructorInit();
00272 };
00273 
00274 } // namespace RTBKIT
00275 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator