-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbidder.cpp
215 lines (188 loc) · 6.26 KB
/
bidder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* BiddingAgent.cpp
*
* Created on: Oct 26, 2013
* Author: rtbkit
*/
#include <string>
#include <memory>
#include "lwrtb/bidder.h"
#include "soa/jsoncpp/value.h"
#include "plugins/bidding_agent/bidding_agent.h"
using namespace std;
using namespace RTBKIT;
using namespace Datacratic;
namespace lwrtb {
struct Bidder::impl
{
shared_ptr<ServiceProxies> prx_;
unique_ptr<BiddingAgent> bidding_agent_;
};
std::unique_ptr<int> pl;
Bidder::Bidder(const string& name,
const string& svc_prx_config)
: name_ (name)
, pimpl_ (new impl())
, swig_bres_cb_ (nullptr)
, swig_devr_cb_ (nullptr)
, swig_err_cb_ (nullptr)
, swig_breq_cb_ (nullptr)
{
pimpl_->prx_.reset (new ServiceProxies);
if (svc_prx_config.length())
pimpl_->prx_->bootstrap (Json::parse(svc_prx_config));
pimpl_->bidding_agent_.reset (new BiddingAgent(pimpl_->prx_, name));
// pimpl_->bidding_agent_->strictMode(false);
}
Bidder::~Bidder()
{
}
void Bidder::start(bool sync)
{
if (sync)
pimpl_->bidding_agent_->startSync();
else
pimpl_->bidding_agent_->start();
}
void
Bidder::init()
{
pimpl_->bidding_agent_->strictMode(false);
if (bid_request_cb_)
{
pimpl_->bidding_agent_->onBidRequest = [this] (double timestamp,
Id & id,
shared_ptr<RTBKIT::BidRequest> br,
const Bids& bids,
double timeLeftMs,
const Json::Value & augmentations,
const WinCostModel& wcm) {
lwrtb::BidRequestEvent res {
timestamp,
id.toString(),
br->toJsonStr(),
bids.toJson().toString(),
timeLeftMs,
augmentations.toString(),
wcm.toJson().toString()};
this->bid_request_cb_ (res);
};
}
if (error_cb_)
{
pimpl_->bidding_agent_->onError = [this](double timestamp,
const std::string& description,
const std::vector<std::string> originalError) {
lwrtb::ErrorEvent res {
timestamp,
description,
originalError };
this->error_cb_(res);
};
}
if (bid_result_cb_)
{
static auto my_convert = [] (const RTBKIT::BidStatus& s) {
if (s== RTBKIT::BidStatus::BS_WIN) return lwrtb::BidStatus::WIN;
if (s== RTBKIT::BidStatus::BS_LOSS) return lwrtb::BidStatus::LOSS;
if (s== RTBKIT::BidStatus::BS_TOOLATE) return lwrtb::BidStatus::TOOLATE;
if (s== RTBKIT::BidStatus::BS_INVALID) return lwrtb::BidStatus::INVALID;
if (s== RTBKIT::BidStatus::BS_LOSTBID) return lwrtb::BidStatus::LOSTBID;
if (s== RTBKIT::BidStatus::BS_DROPPEDBID) return lwrtb::BidStatus::DROPPEDBID;
if (s== RTBKIT::BidStatus::BS_NOBUDGET) return lwrtb::BidStatus::NOBUDGET;
return lwrtb::BidStatus::BUG;
};
pimpl_->bidding_agent_->onWin = [this] (const RTBKIT::BidResult &br) {
lwrtb::BidResultEvent res {
my_convert(br.result),
br.timestamp,
br.auctionId.toString(),
br.spotNum,
br.secondPrice.toString(),
br.request.get() ? br.request->toJsonStr() : "",
br.ourBid.toJson().toString(),
br.metadata.toString(),
br.augmentations.toString()
};
this->bid_result_cb_ (res);
};
pimpl_->bidding_agent_->onLoss = pimpl_->bidding_agent_->onWin;
pimpl_->bidding_agent_->onNoBudget = pimpl_->bidding_agent_->onWin;
pimpl_->bidding_agent_->onTooLate = pimpl_->bidding_agent_->onWin;
pimpl_->bidding_agent_->onDroppedBid = pimpl_->bidding_agent_->onWin;
pimpl_->bidding_agent_->onInvalidBid = pimpl_->bidding_agent_->onWin;
}
if (delivery_event_cb_)
{
pimpl_->bidding_agent_->onImpression = [this] (const RTBKIT::DeliveryEvent& de) {
lwrtb::DeliveryEvent ode {
de.event,
de.timestamp.secondsSinceEpoch(),
de.auctionId.toString(),
de.spotId.toString(),
de.spotIndex,
de.bidRequest->toJsonStr(),
de.augmentations,
de.bid.toJson().toString(),
de.win.toJson().toString(),
de.campaignEvents.toJson().toString()
};
for (const auto& v: de.visits)
ode.visits.emplace_back(v.toJson().toString());
};
pimpl_->bidding_agent_->onClick = pimpl_->bidding_agent_->onImpression;
pimpl_->bidding_agent_->onVisit = pimpl_->bidding_agent_->onImpression;
}
pimpl_->bidding_agent_->init ();
}
void
Bidder::shutdown()
{
pimpl_->bidding_agent_->shutdown ();
}
void
Bidder::doConfig(const string& config)
{
pimpl_->bidding_agent_->doConfigJson(Json::parse(config));
}
void
Bidder::doBid(const string& id,
const string& bids,
const string& meta,
const string& wmc)
{
pimpl_->bidding_agent_->doBid (
Id(id),
Bids::fromJson(bids),
meta.size() ? Json::parse(meta) : Json::Value(),
wmc.size() ? WinCostModel::fromJson(Json::parse(wmc)) : WinCostModel());
}
void Bidder::setBidRequestCb (BidRequestCb& cb)
{
this->swig_breq_cb_ = &cb ;
bid_request_cb_ = [&] (const lwrtb::BidRequestEvent& ev) {
this->swig_breq_cb_->call (*this, ev);
};
}
void Bidder::setDeliveryCb (DeliveryCb& cb)
{
this->swig_devr_cb_ = &cb ;
delivery_event_cb_ = [&] (const DeliveryEvent& de) {
this->swig_devr_cb_->call (*this,de);
};
}
void Bidder::setBidResultCb (BidResultCb& cb)
{
this->swig_bres_cb_ = &cb ;
bid_result_cb_ = [&] (const BidResultEvent& br) {
this->swig_bres_cb_->call (*this,br);
};
}
void Bidder::setErrorCb (ErrorCb& cb)
{
this->swig_err_cb_ = &cb ;
error_cb_ = [&] (const ErrorEvent& err) {
this->swig_err_cb_->call (*this,err);
};
}
} /* namespace lwrtb */