![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* mock_exchange_connector.cc 00002 Eric Robert, 9 April 2013 00003 Copyright (c) 2012 Datacratic Inc. All rights reserved. 00004 00005 Simple mock exchange connector 00006 */ 00007 00008 #include "rtbkit/core/router/router_runner.h" 00009 #include "rtbkit/plugins/exchange/http_exchange_connector.h" 00010 #include "jml/utils/json_parsing.h" 00011 00012 namespace RTBKIT { 00013 00014 struct MockExchangeConnector : public HttpExchangeConnector { 00015 MockExchangeConnector(ServiceBase & owner, const std::string & name) : 00016 HttpExchangeConnector(name, owner) 00017 { 00018 this->auctionResource = "/bids"; 00019 this->auctionVerb = "POST"; 00020 } 00021 00022 MockExchangeConnector(const std::string & name, std::shared_ptr<ServiceProxies> proxies) : 00023 HttpExchangeConnector(name, proxies) 00024 { 00025 this->auctionResource = "/bids"; 00026 this->auctionVerb = "POST"; 00027 } 00028 00029 std::string exchangeName() const { 00030 return "mock"; 00031 } 00032 00033 void configure(Json::Value const & parameters) { 00034 numThreads = 1; 00035 listenPort = 12339; 00036 bindHost = "*"; 00037 performNameLookup = true; 00038 backlog = 128; 00039 00040 getParam(parameters, numThreads, "numThreads"); 00041 getParam(parameters, listenPort, "listenPort"); 00042 getParam(parameters, bindHost, "bindHost"); 00043 getParam(parameters, performNameLookup, "performNameLookup"); 00044 getParam(parameters, backlog, "connectionBacklog"); 00045 } 00046 00047 void start() { 00048 init(listenPort, bindHost, numThreads, true, performNameLookup, backlog); 00049 } 00050 00051 std::shared_ptr<BidRequest> parseBidRequest(HttpAuctionHandler & handler, 00052 Datacratic::HttpHeader const & header, 00053 std::string const & payload) { 00054 std::shared_ptr<BidRequest> request; 00055 request.reset(BidRequest::parse("datacratic", payload)); 00056 return request; 00057 } 00058 00059 double getTimeAvailableMs(HttpAuctionHandler & handler, 00060 const HttpHeader & header, 00061 const std::string & payload) { 00062 return 35.0; 00063 } 00064 00065 double getRoundTripTimeMs(HttpAuctionHandler & handler, 00066 const HttpHeader & header) { 00067 return 5.0; 00068 } 00069 00070 Datacratic::HttpResponse getResponse(HttpAuctionHandler const & handler, 00071 const HttpHeader & header, 00072 Auction const & auction) const { 00073 std::string result; 00074 00075 const Auction::Data * current = auction.getCurrentData(); 00076 if (current->hasError()) 00077 return getErrorResponse(handler, auction, current->error + ": " + current->details); 00078 00079 result = "{\"imp\":["; 00080 00081 bool first = true; 00082 for (unsigned spotNum = 0; spotNum < current->responses.size(); ++spotNum) { 00083 if (!current->hasValidResponse(spotNum)) 00084 continue; 00085 00086 if (!first) result += ","; 00087 first = false; 00088 00089 auto & resp = current->winningResponse(spotNum); 00090 result += ML::format("{\"id\":\"%s\",\"max_price\":%lld,\"account\":\"%s\"}", 00091 ML::jsonEscape(auction.request->imp.at(spotNum).id.toString()).c_str(), 00092 (long long)(MicroUSD_CPM(resp.price.maxPrice)), 00093 resp.account.toString('.')); 00094 } 00095 00096 result += "]}"; 00097 return HttpResponse(200, "application/json", result); 00098 } 00099 00100 int numThreads; 00101 int listenPort; 00102 std::string bindHost; 00103 bool performNameLookup; 00104 int backlog; 00105 }; 00106 00107 }