RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 00009 #include "rtbkit/core/router/router.h" 00010 #include "rtbkit/plugins/bidding_agent/bidding_agent.h" 00011 #include <map> 00012 #include <functional> 00013 #include <iostream> 00014 #include <sstream> 00015 00016 using namespace Datacratic; 00017 using namespace RTBKIT; 00018 00019 int main() { 00020 auto proxies = std::make_shared<ServiceProxies>(); 00021 00022 BiddingAgent agent(proxies); 00023 AgentConfig config; 00024 config.account = { "testCampaign", "testStrategy" }; 00025 config.maxInFlight = 20000; 00026 config.minTimeAvailableMs = 0; 00027 config.creatives.push_back(Creative::sampleLB); 00028 config.creatives.push_back(Creative::sampleWS); 00029 config.creatives.push_back(Creative::sampleBB); 00030 00031 bool bidding = false; 00032 00033 agent.onError = [&] ( 00034 double timestamp, 00035 const std::string & error, 00036 const std::vector<std::string> & message) 00037 { 00038 std::cout << "agent got error: " << error 00039 << " from message: " << message 00040 << std::endl; 00041 }; 00042 00043 agent.onBidRequest = [&] ( 00044 double timestamp, 00045 const Id & id, 00046 std::shared_ptr<BidRequest> br, 00047 const Bids & bids, 00048 double timeLeftMs, 00049 const Json::Value & aug) 00050 { 00051 std::cout << "agent got bid request " << id << std::endl; 00052 if (bidding) { 00053 agent.doBid(id, bids, Json::Value()); 00054 } 00055 }; 00056 00057 auto onResult = [&] (const BidResult & args) { 00058 std::cout << "agent got result " << args.result << std::endl; 00059 }; 00060 00061 agent.onWin = 00062 agent.onLoss = 00063 agent.onNoBudget = 00064 agent.onTooLate = 00065 agent.onInvalidBid = 00066 agent.onDroppedBid = onResult; 00067 00068 agent.onTooLate = [&] (const BidResult & args) { 00069 std::cout << "agent got too late" << std::endl; 00070 }; 00071 00072 std::map<std::string, std::function<void(std::istream &)>> commands; 00073 00074 commands.insert(std::make_pair("help", [&](std::istream & args) { 00075 std::cout << "here are the possible commands:" << std::endl; 00076 for(auto i : commands) { 00077 std::cout << "- " << i.first << std::endl; 00078 } 00079 })); 00080 00081 commands.insert(std::make_pair("zookeeper", [&](std::istream & args) { 00082 std::string host; args >> host; if(host.empty()) host = "localhost:2181"; 00083 std::string path; args >> path; if(path.empty()) path = "CWD"; 00084 std::cout << "using url=" << host << " prefix=" << path << std::endl; 00085 proxies->useZookeeper(host, path); 00086 })); 00087 00088 commands.insert(std::make_pair("carbon", [&](std::istream & args) { 00089 std::string host; args >> host; 00090 std::string path; args >> path; if(path.empty()) path = "CWD"; 00091 std::cout << "using url=" << host << " prefix=" << path << std::endl; 00092 proxies->logToCarbon(host, path); 00093 })); 00094 00095 commands.insert(std::make_pair("dump", [&](std::istream & args) { 00096 proxies->config->dump(std::cout); 00097 std::cout << std::endl; 00098 })); 00099 00100 commands.insert(std::make_pair("start", [&](std::istream & args) { 00101 std::string name; args >> name; if(name.empty()) name = "test"; 00102 agent.init(); 00103 agent.start(); 00104 std::cout << "agent started name=" << name << std::endl; 00105 })); 00106 00107 commands.insert(std::make_pair("doconfig", [&](std::istream & args) { 00108 std::cout << "setting config" << std::endl << "value=" << config.toJson() << std::endl; 00109 agent.doConfig(config); 00110 })); 00111 00112 commands.insert(std::make_pair("bid", [&](std::istream & args) { 00113 bidding ^= 1; 00114 std::cout << "now bidding=" << bidding << std::endl; 00115 })); 00116 00117 for(;;) { 00118 std::cout << "$>" << std::flush; 00119 00120 std::string line; 00121 getline(std::cin, line); 00122 00123 if(line == "quit") { 00124 break; 00125 } 00126 00127 std::istringstream args(line); 00128 std::string command; 00129 args >> command; 00130 00131 auto i = commands.find(command); 00132 if(commands.end() == i) { 00133 std::cout << "unknown command '" << command << "'" << std::endl; 00134 } 00135 else { 00136 i->second(args); 00137 } 00138 } 00139 }