![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* bidding_agent.h -*- C++ -*- 00002 Rémi Attab, 14 December 2011 00003 Copyright (c) 2011 Datacratic. All rights reserved. 00004 00005 Simple remote interface to the router. 00006 */ 00007 00008 00009 #ifndef __rtb__bidding_agent_h__ 00010 #define __rtb__bidding_agent_h__ 00011 00012 00013 #include "rtbkit/common/auction.h" 00014 #include "rtbkit/common/bids.h" 00015 #include "rtbkit/common/auction_events.h" 00016 #include "soa/service/zmq.hpp" 00017 #include "soa/service/carbon_connector.h" 00018 #include "soa/jsoncpp/json.h" 00019 #include "soa/types/id.h" 00020 #include "soa/service/service_base.h" 00021 #include "soa/service/zmq_endpoint.h" 00022 #include "soa/service/typed_message_channel.h" 00023 00024 #include <boost/function.hpp> 00025 #include <boost/noncopyable.hpp> 00026 #include <boost/thread/barrier.hpp> 00027 #include <boost/smart_ptr.hpp> 00028 #include <boost/make_shared.hpp> 00029 #include <string> 00030 #include <vector> 00031 #include <thread> 00032 #include <map> 00033 00034 00035 namespace RTBKIT { 00036 00037 /******************************************************************************/ 00038 /* BIDDING AGENT */ 00039 /******************************************************************************/ 00040 00050 struct BiddingAgent : public ServiceBase, public MessageLoop { 00051 00052 BiddingAgent(std::shared_ptr<ServiceProxies> proxies, 00053 const std::string & name = "bidding_agent"); 00054 BiddingAgent(ServiceBase & parent, 00055 const std::string & name = "bidding_agent"); 00056 00057 ~BiddingAgent(); 00058 00059 BiddingAgent(const BiddingAgent& other) = delete; 00060 BiddingAgent& operator=(const BiddingAgent& other) = delete; 00061 00065 std::string agentName; 00066 00070 void strictMode(bool strict) { requiresAllCB = strict; } 00071 00072 void init(); 00073 // Gets rid of the MessageLoop's default param for the JS wrappers. 00074 void start() { MessageLoop::start(); } 00075 void shutdown(); 00076 00077 00078 /**************************************************************************/ 00079 /* AGENT CONTROLS */ 00080 /**************************************************************************/ 00081 00088 void doBid(Id id, const Bids& bids, const Json::Value& meta = Json::Value()); 00089 00099 void doConfig(const AgentConfig& config); 00100 void doConfigJson(Json::Value config); 00101 00102 00111 void doPong(const std::string & fromRouter, Date sent, Date received, 00112 const std::vector<std::string> & payload); 00113 00114 00115 /**************************************************************************/ 00116 /* CALLBACKS */ 00117 /**************************************************************************/ 00118 // The odd double typedefs is to simplify the JS wrappers. 00119 00120 00121 typedef void (BidRequestCb) ( 00122 double timestamp, // Start time of the auction. 00123 Id id, // Auction id 00124 std::shared_ptr<BidRequest> bidRequest, 00125 const Bids& bids, // Impressions available for bidding 00126 double timeLeftMs, // Time left of the bid request. 00127 Json::Value augmentations); // Data from the augmentors. 00128 typedef boost::function<BidRequestCb> BidRequestCbFn; 00129 00154 BidRequestCbFn onBidRequest; 00155 00156 00157 typedef void (ResultCb) (const BidResult & args); 00158 typedef boost::function<ResultCb> ResultCbFn; 00159 00162 ResultCbFn onWin; 00163 00165 ResultCbFn onLoss; 00166 00169 ResultCbFn onNoBudget; 00170 00173 ResultCbFn onTooLate; 00174 00176 ResultCbFn onDroppedBid; 00177 00180 ResultCbFn onInvalidBid; 00181 00182 00183 typedef void (DeliveryCb) (const DeliveryEvent & args); 00184 typedef boost::function<DeliveryCb> DeliveryCbFn; 00185 00187 DeliveryCbFn onImpression; 00188 00190 DeliveryCbFn onClick; 00191 00193 DeliveryCbFn onVisit; 00194 00195 00196 typedef void (PingCb) (const std::string & fromRouter, 00197 Date timestamp, 00198 const std::vector<std::string> & args); 00199 typedef boost::function<PingCb> PingCbFn; 00200 00209 PingCbFn onPing; 00210 00211 00212 typedef void (ErrorCb) (double timestamp, 00213 std::string description, 00214 std::vector<std::string> originalError); 00215 typedef boost::function<ErrorCb> ErrorCbFn; 00216 00220 ErrorCbFn onError; 00221 00222 private: 00223 00225 struct RouterMessage { 00226 RouterMessage(const std::string & toRouter = "", 00227 const std::string & type = "", 00228 const std::vector<std::string> & payload 00229 = std::vector<std::string>()) 00230 : toRouter(toRouter), 00231 type(type), 00232 payload(payload) 00233 { 00234 } 00235 00236 std::string toRouter; 00237 std::string type; 00238 std::vector<std::string> payload; 00239 }; 00240 00241 ZmqMultipleNamedClientBusProxy toRouters; 00242 ZmqMultipleNamedClientBusProxy toPostAuctionServices; 00243 ZmqNamedClientBusProxy toConfigurationAgent; 00244 TypedMessageSink<RouterMessage> toRouterChannel; 00245 00246 struct RequestStatus { 00247 Date timestamp; 00248 std::string fromRouter; 00249 }; 00250 00251 std::map<Id, RequestStatus> requests; 00252 std::mutex requestsLock; // Protects concurrent writes to requests 00253 00254 bool requiresAllCB; 00255 00256 00262 std::mutex configLock; 00263 std::string config; // The agent's configuration. 00264 00265 void sendConfig(const std::string& newConfig = ""); 00266 00267 void checkMessageSize(const std::vector<std::string>& msg, int expectedSize); 00268 00269 // void doHeartbeat(); 00270 00271 void handleRouterMessage(const std::string & fromRouter, 00272 const std::vector<std::string>& msg); 00273 void handleError(const std::vector<std::string>& msg, ErrorCbFn& callback); 00274 void handleBidRequest(const std::string & fromRouter, 00275 const std::vector<std::string>& msg, BidRequestCbFn& callback); 00276 void handleWin( 00277 const std::vector<std::string>& msg, ResultCbFn& callback); 00278 void handleResult( 00279 const std::vector<std::string>& msg, ResultCbFn& callback); 00280 void handleDelivery( 00281 const std::vector<std::string>& msg, DeliveryCbFn& callback); 00282 void handlePing(const std::string & fromRouter, 00283 const std::vector<std::string>& msg, PingCbFn& callback); 00284 }; 00285 00286 00287 00288 } // namespace RTBKIT 00289 00290 #endif // __rtb__bidding_agent_h__ 00291 00292