Skip to content

Instantly share code, notes, and snippets.

@Gocrazy
Created July 17, 2014 02:08
Show Gist options
  • Select an option

  • Save Gocrazy/d168b477bb9af2c78285 to your computer and use it in GitHub Desktop.

Select an option

Save Gocrazy/d168b477bb9af2c78285 to your computer and use it in GitHub Desktop.
#include "smaato_exchange_connector.h"
#include "rtbkit/plugins/bid_request/openrtb_bid_request.h"
#include "rtbkit/plugins/exchange/http_auction_handler.h"
#include "rtbkit/core/agent_configuration/agent_config.h"
#include "rtbkit/openrtb/openrtb_parsing.h"
#include "rtbkit/plugins/bid_request/openrtb_bid_request.h"
#include "rtbkit/plugins/bid_request/openrtb_bid_source.h"
#include "soa/types/json_printing.h"
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include "jml/utils/file_functions.h"
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
using namespace Datacratic;
namespace RTBKIT
{
SmaatoExchangeConnector::SmaatoExchangeConnector( ServiceBase & owner, const std::string & name ) : HttpExchangeConnector( name, owner )
{
this->auctionResource = "/auctions";
this->auctionVerb = "POST";
}
SmaatoExchangeConnector::SmaatoExchangeConnector( const std::string & name, std::shared_ptr<ServiceProxies> proxies ) : HttpExchangeConnector(name, proxies)
{
this->auctionResource = "/auctions";
this->auctionVerb = "POST";
}
double SmaatoExchangeConnector::getTimeAvailableMs( HttpAuctionHandler & handler,
const HttpHeader & header,
const std::string & payload)
{
return 150;
}
double SmaatoExchangeConnector::getRoundTripTimeMs( HttpAuctionHandler & handler,
const HttpHeader & header)
{
return 30;
}
HttpResponse SmaatoExchangeConnector::getResponse(const HttpAuctionHandler & connection,
const HttpHeader & requestHeader,
const Auction & auction) const
{
const Auction::Data * current = auction.getCurrentData();
if (current->hasError())
return getErrorResponse(connection, current->error + ": " + current->details);
else
{
cerr << "smaato_exchange_connector::getResponse: current has no Error (current=auction.getCurrentData())" << endl;
cerr << "smaato_exchange_connector::getResponse: auction id: " << auction.id << endl;
cerr << "smaato_exchange_connector::getResponse: current: " << current << endl;
cerr << "smaato_exchange_connector::getResponse: auction.status(): " << auction.status()<< endl;
}
OpenRTB::BidResponse response;
response.id = auction.id;
response.bidid = auction.id;
std::map<Id, int> seatToBid;
string en = exchangeName();
cerr << "smaato_exchange_connector::getResponse: exchangeName:" << en << endl;
for (unsigned spotNum = 0; spotNum < current->responses.size();
++spotNum) {
cerr << "smaato_exchange_connector::getResponse: (looping responses) spotNum:" << spotNum << endl;
if (!current->hasValidResponse(spotNum))
{
cerr << "(!current->hasValidResponse(spotNum)) is TRUE" << endl;
continue;
}
cerr << "smaato_exchange_connector::getResponse: about to get winning bid --> current->winningResponse(spotNum)"<< endl;
// Get the winning bid
auto & resp = current->winningResponse(spotNum);
// Find how the agent is configured. We need to copy some of the
// fields into the bid.
const AgentConfig * config
= std::static_pointer_cast<const AgentConfig>
(resp.agentConfig).get();
// Get the exchange specific data for this campaign (if it exists)
bool cpinfo_ok = false;
Id seat("smaato_seat");
try{
auto cpinfo = config->getProviderData<CampaignInfo>(en);
seat = cpinfo->seat;
cpinfo_ok = true;
} catch(ML::Exception& ex) {
cerr << ex.what() << endl;
}
// Put in the fixed parts from the creative
int creativeIndex = resp.agentCreativeIndex;
auto & creative = config->creatives.at(creativeIndex);
// Get the exchange specific data for this creative (if it exists)
bool crinfo_ok = false;
Id adid;
std::string adm;
std::string nurl;
try {
auto crinfo = creative.getProviderData<CreativeInfo>(en);
adid = crinfo->adid;
adm = crinfo->adm;
nurl = crinfo->nurl;
crinfo_ok = true;
} catch(ML::Exception& ex) {
cerr << ex.what() << endl;
}
// Find the index in the seats array
int seatIndex = -1;
{
auto it = seatToBid.find(seat);
bool res = it == seatToBid.end();
cerr << "smaato_exchange_connector::getResponse: after it= seatToBid.find(seat), now it == seatToBid.end() gives: " << res << endl;
//cerr << "smaato_exchange_connector::getResponse: seatToBid.end is: "<< seatToBid.end() << endl;
//cerr << "smaato_exchange_connector::getResponse: 'it' is: " << it <<endl;
if (it == seatToBid.end()) {
cerr << "smaato_exchange_connector::getResponse: in (it==seatToBid.end()), before setup seatIndex is: " << seatToBid.size() << " seatToBid.size(): " << seatToBid.size() << endl;
seatIndex = seatToBid.size();
seatToBid[seat] = seatIndex;
response.seatbid.emplace_back();
if(cpinfo_ok) response.seatbid.back().seat = seat;
cerr << "smaato_exchange_connector::getResponse: in (it==seatToBid.end()), after setup, seatIndex is: " << seatToBid.size() << " seatToBid.size(): " << seatToBid.size() << endl;
}
else seatIndex = it->second;
}
// Get the seatBid object
OpenRTB::SeatBid & seatBid = response.seatbid.at(seatIndex);
// Add a new bid to the array
seatBid.bid.emplace_back();
auto & b = seatBid.bid.back();
// Put in the variable parts
b.id = Id(auction.id, auction.request->imp[0].id);
b.impid = auction.request->imp[spotNum].id;
b.price.val = USD_CPM(resp.price.maxPrice);
if(crinfo_ok && adid != Id("")) b.adid = adid;
if(crinfo_ok && !adm.empty()) b.adm = adm;
if(crinfo_ok && !nurl.empty()) b.nurl = nurl;
}
cerr << "smaato_exchange_connector::getResponse: before decision of the response http, seatToBid.size(): " << seatToBid.size() << endl;
if (seatToBid.empty())
{
cerr << "smaato_exchange_connector::getResponse: seatToBid.empty() is true! Sending Httpresponse(204,none, {}) ... :( " << endl;
return HttpResponse(204, "none", "{}");
}
cerr << "smaato_exchange_connector::getResponse: seatToBid.empty() is false, we keep going"<< endl;
static Datacratic::DefaultDescription<OpenRTB::BidResponse> desc;
std::ostringstream stream;
StreamJsonPrintingContext context(stream);
desc.printJsonTyped(&response, context);
cerr << Json::parse(stream.str());
cerr << "smaato_exchange_connector::getReponse: response is good! Sending HttpResponse(200, application/json, stream.str()): "<< endl;
cerr << "smaato_exchange_connector::getReponse: sent: " << stream.str() << endl;
return HttpResponse(200, "application/json", stream.str());
}
std::shared_ptr<BidRequest> SmaatoExchangeConnector::parseBidRequest(HttpAuctionHandler & handler,
Datacratic::HttpHeader const & header,
std::string const & payload)
{
std::shared_ptr<BidRequest> res;
// Check json content-type
// Check x-openrtb-version header
cerr << "smaato exchange got request" << endl << header << endl << payload << endl;
cerr << "smaato exchange got request header.resource: " << header.resource << endl;
cerr << "smaato exchange got request header.queryParams.uriEscaped(): " << header.queryParams.uriEscaped() << endl;
string nobid ("nobid");
size_t found = header.queryParams.uriEscaped().find(nobid);
char const* payload_c_str;
unsigned long payload_size;
int status = -4;
cerr << "smato exchange: typeid(payload.c_str()).name() gives: " <<abi::__cxa_demangle( typeid(payload.c_str()).name(), 0,0, &status) << endl;
cerr << "smato exchange: typeid(payload.size()).name() gives: "<<abi::__cxa_demangle(typeid(payload.size()).name(), 0, 0, &status) << endl;
if (found != string::npos)
{
// test no bid case
cerr << "smaato exchange, test::nobid!" << endl;
payload_c_str = "{}";
payload_size = 2;
}
else
{
// Parse bid request
payload_c_str = payload.c_str();
payload_size = payload.size();
}
ML::Parse_Context context("Bid Request", payload_c_str, payload_size);
//ML::Parse_Context context("Bid Request", payload.c_str(), payload.size());
res.reset(OpenRtbBidRequestParser::parseBidRequest(context, exchangeName(), exchangeName()));
cerr << res->toJson() << endl;
return res;
}
HttpResponse
SmaatoExchangeConnector::
getDroppedAuctionResponse(const HttpAuctionHandler & connection,
const std::string & reason) const
{
Json::Value response;
response["error"] = reason;
return HttpResponse(204, response);
}
HttpResponse
SmaatoExchangeConnector::
getErrorResponse(const HttpAuctionHandler & connection,
const std::string & errorMessage) const
{
Json::Value response;
response["error"] = errorMessage;
return HttpResponse(400, response);
}
void
SmaatoExchangeConnector::
setSeatBid(Auction const & auction,
int spotNum,
OpenRTB::BidResponse & response) const {
}
} // namespace RTBKIT
namespace {
using namespace RTBKIT;
struct Init {
Init() {
ExchangeConnector::registerFactory<SmaatoExchangeConnector>();
}
} init;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment