RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
common/exchange_connector.h
00001 /* exchange_connector.h                                            -*- C++ -*-
00002    Jeremy Barnes, 13 December 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Exchange connector.
00006 */
00007 
00008 #pragma once
00009 
00010 #include "soa/service/service_base.h"
00011 #include "rtbkit/common/auction.h"
00012 #include "jml/utils/unnamed_bool.h"
00013 
00014 namespace RTBKIT {
00015 
00016 class AgentConfig;
00017 class Creative;
00018 
00019 
00020 /*****************************************************************************/
00021 /* EXCHANGE CONNECTOR                                                        */
00022 /*****************************************************************************/
00023 
00033 struct ExchangeConnector: public ServiceBase {
00034 
00035     ExchangeConnector(const std::string & name,
00036                       ServiceBase & parent);
00037 
00038     ExchangeConnector(const std::string & name,
00039                       std::shared_ptr<ServiceProxies>
00040                           = std::shared_ptr<ServiceProxies>());
00041 
00042     virtual ~ExchangeConnector();
00043 
00045     typedef boost::function<void (std::shared_ptr<Auction> Auction)>
00046         OnAuction;
00047     
00053     OnAuction onNewAuction, onAuctionDone;
00054 
00055 
00056     /*************************************************************************/
00057     /* METHODS CALLED BY THE ROUTER TO CONTROL THE EXCHANGE CONNECTOR        */
00058     /*************************************************************************/
00059 
00063     virtual void configure(const Json::Value & parameters) = 0;
00064 
00066     virtual void start();
00067 
00069     virtual void shutdown();
00070 
00075     virtual void enableUntil(Date date) = 0;
00076 
00080     virtual void setAcceptBidRequestProbability(double prob) = 0;
00081 
00085     virtual std::string exchangeName() const = 0;
00086 
00087 
00088     /*************************************************************************/
00089     /* EXCHANGE COMPATIBILITY                                                */
00090     /*************************************************************************/
00091 
00092     /* This functionality is used by the router to determine which campaigns
00093        may bid on inventory from the campaign, and which creatives are
00094        eligible to be shown to fill impressions for the campaign.
00095 
00096        This is where exchange-specific logic as to required information
00097        in the creative can be implemented, and allows feedback as to why
00098        a given campaign or creative is not working on an exchange for
00099        debugging purposes.
00100        
00101        Please note that these methods are called infrequently at campaign
00102        configuration time, and apply to *all* bid requests for each
00103        campaign.  Filtering of individual bid requests is done via
00104        the tags and filters mechanism.
00105     */
00106 
00110     struct ExchangeCompatibility {
00111         ExchangeCompatibility()
00112             : isCompatible(false)
00113         {
00114         }
00115 
00116         JML_IMPLEMENT_OPERATOR_BOOL(isCompatible);
00117 
00119         void setCompatible() { isCompatible = true;  reasons.clear(); }
00120 
00121         void setIncompatible() { isCompatible = false;  reasons.clear(); }
00122 
00126         void setIncompatible(const std::string & reason, bool includeReasons)
00127         {
00128             isCompatible = false;
00129             if (includeReasons)
00130                 reasons.push_back(reason);
00131         }
00132 
00136         void setIncompatible(ML::compact_vector<std::string, 1> && reasons,
00137                              bool includeReasons)
00138         {
00139             isCompatible = false;
00140             if (includeReasons)
00141                 this->reasons = std::move(reasons);
00142         }
00143 
00144         bool isCompatible;   
00145         ML::compact_vector<std::string, 1> reasons;  
00146 
00150         std::shared_ptr<void> info;
00151     };
00152 
00156     struct CampaignCompatibility : public ExchangeCompatibility {
00157         CampaignCompatibility()
00158         {
00159         }
00160         
00161         CampaignCompatibility(const AgentConfig & config);
00162 
00163         std::vector<ExchangeCompatibility> creatives;  
00164     };
00165 
00180     virtual ExchangeCompatibility
00181     getCampaignCompatibility(const AgentConfig & config,
00182                              bool includeReasons) const;
00183     
00187     virtual ExchangeCompatibility
00188     getCreativeCompatibility(const Creative & creative,
00189                              bool includeReasons) const;
00190 
00191 
00192     /*************************************************************************/
00193     /* FILTERING                                                             */
00194     /*************************************************************************/
00195 
00235     virtual bool bidRequestPreFilter(const BidRequest & request,
00236                                      const AgentConfig & config,
00237                                      const void * info) const;
00238 
00267     virtual bool bidRequestPostFilter(const BidRequest & request,
00268                                       const AgentConfig & config,
00269                                       const void * info) const;
00270 
00277     virtual bool bidRequestCreativeFilter(const BidRequest & request,
00278                                           const AgentConfig & config,
00279                                           const void * info) const;
00280 
00281 
00282 
00283     /*************************************************************************/
00284     /* FACTORY INTERFACE                                                     */
00285     /*************************************************************************/
00286 
00288     typedef std::function<ExchangeConnector * (ServiceBase * owner, std::string name)>
00289         Factory;
00290     
00292     static void registerFactory(const std::string & exchange, Factory factory);
00293 
00295     static std::unique_ptr<ExchangeConnector>
00296     create(const std::string & exchangeType,
00297            ServiceBase & owner,
00298            const std::string & name);
00299 
00300 };
00301 
00302 
00303 } // namespace RTBKIT
00304     
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator