RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
plugins/adserver/adserver_connector.cc
00001 /* adserver_connector.cc
00002    Jeremy Barnes, 19 December 2012
00003    Copyright (c) 2012 Datacratic Inc.  All rights reserved.
00004 
00005    Base class to connect to an ad server.  We also have an http ad server
00006    connector that builds on top of this.
00007 */
00008 
00009 
00010 #include "adserver_connector.h"
00011 
00012 #include "rtbkit/common/auction_events.h"
00013 
00014 
00015 using namespace std;
00016 
00017 namespace RTBKIT {
00018 
00019 
00020 /*****************************************************************************/
00021 /* POST AUCTION PROXY                                                        */
00022 /*****************************************************************************/
00023 
00024 AdServerConnector::
00025 AdServerConnector(const string & serviceName,
00026                   const shared_ptr<Datacratic::ServiceProxies> & proxy)
00027     : ServiceBase(serviceName, proxy),
00028       toPostAuctionService_(proxy->zmqContext)
00029 {
00030 }
00031 
00032 AdServerConnector::
00033 ~AdServerConnector()
00034 {
00035 }
00036 
00037 void
00038 AdServerConnector::
00039 init(shared_ptr<ConfigurationService> config)
00040 {
00041     shared_ptr<ServiceProxies> services = getServices();
00042 
00043     registerServiceProvider(serviceName_, { "adServer" });
00044     services->config->removePath(serviceName());
00045 
00046     toPostAuctionService_.init(config, ZMQ_XREQ);
00047     toPostAuctionService_.connectToServiceClass("rtbPostAuctionService",
00048                                                 "events");
00049 
00050 #if 0 // later, for when we have multiple
00051 
00052     toPostAuctionServices_.init(getServices()->config, name);
00053     toPostAuctionServices_.connectHandler = [=] (const string & connectedTo) {
00054         cerr << "AdServerConnector is connected to post auction service "
00055         << connectedTo << endl;
00056     };
00057     toPostAuctionServices_.connectAllServiceProviders("rtbPostAuctionService",
00058                                                       "agents");
00059     toPostAuctionServices_.connectToServiceClass();
00060 #endif
00061 }
00062 
00063 void
00064 AdServerConnector::
00065 start()
00066 {
00067     startTime_ = Date::now();
00068     recordHit("up");
00069 }
00070 
00071 void
00072 AdServerConnector::
00073 shutdown()
00074 {
00075 }
00076 
00077 void
00078 AdServerConnector::
00079 recordUptime()
00080     const
00081 {
00082     recordLevel(Date::now().secondsSince(startTime_), "uptime");
00083 }
00084 
00085 void
00086 AdServerConnector::
00087 publishWin(const Id & auctionId,
00088            const Id & adSpotId,
00089            Amount winPrice,
00090            Date timestamp,
00091            const JsonHolder & winMeta,
00092            const UserIds & ids,
00093            const AccountKey & account,
00094            Date bidTimestamp)
00095 {
00096     recordHit("receivedEvent");
00097     recordHit("event.WIN");
00098 
00099     PostAuctionEvent event;
00100     event.type = PAE_WIN;
00101     event.auctionId = auctionId;
00102     event.adSpotId = adSpotId;
00103     event.winPrice = winPrice;
00104     event.timestamp = timestamp;
00105     event.metadata = winMeta;
00106     event.uids = ids;
00107     event.account = account;
00108     event.bidTimestamp = bidTimestamp;
00109 
00110     string str = ML::DB::serializeToString(event);
00111     toPostAuctionService_.sendMessage("WIN", str);
00112 }
00113 
00114 void
00115 AdServerConnector::
00116 publishLoss(const Id & auctionId,
00117             const Id & adSpotId,
00118             Date timestamp,
00119             const JsonHolder & lossMeta,
00120             const AccountKey & account,
00121             Date bidTimestamp)
00122 {
00123     recordHit("receivedEvent");
00124     recordHit("event.LOSS");
00125 
00126     PostAuctionEvent event;
00127     event.type = PAE_LOSS;
00128     event.auctionId = auctionId;
00129     event.adSpotId = adSpotId;
00130     event.timestamp = timestamp;
00131     event.metadata = lossMeta;
00132     event.account = account;
00133     event.bidTimestamp = bidTimestamp;
00134 
00135     string str = ML::DB::serializeToString(event);
00136     toPostAuctionService_.sendMessage("LOSS", str);
00137 }
00138 
00139 void
00140 AdServerConnector::
00141 publishCampaignEvent(const string & label,
00142                      const Id & auctionId,
00143                      const Id & adSpotId,
00144                      Date timestamp,
00145                      const JsonHolder & impressionMeta,
00146                      const UserIds & ids)
00147 { 
00148     recordHit("receivedEvent");
00149     recordHit("event." + label);
00150 
00151     PostAuctionEvent event;
00152     event.type = PAE_CAMPAIGN_EVENT;
00153     event.label = label;
00154     event.auctionId = auctionId;
00155     event.adSpotId = adSpotId;
00156     event.timestamp = timestamp;
00157     event.uids = ids;
00158     event.metadata = impressionMeta;
00159 
00160     string str = ML::DB::serializeToString(event);
00161     toPostAuctionService_.sendMessage("EVENT", str);
00162 }
00163 
00164 } // namespace RTBKIT
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator