![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 00011 #ifndef __rtbkit__mock_exchange_h__ 00012 #define __rtbkit__mock_exchange_h__ 00013 00014 #include "common/account_key.h" 00015 #include "common/currency.h" 00016 #include "common/bid_request.h" 00017 #include "soa/service/service_utils.h" 00018 #include "soa/service/service_base.h" 00019 #include "soa/service/zmq_endpoint.h" 00020 #include "soa/types/id.h" 00021 #include "soa/types/date.h" 00022 #include "jml/utils/rng.h" 00023 00024 #include <netdb.h> 00025 #include <vector> 00026 00027 namespace RTBKIT { 00028 00029 /******************************************************************************/ 00030 /* MOCK EXCHANGE */ 00031 /******************************************************************************/ 00032 00033 struct MockExchange : public Datacratic::ServiceBase 00034 { 00035 MockExchange(Datacratic::ServiceProxyArguments & args, 00036 const std::string& name = "mock-exchange"); 00037 MockExchange(const std::shared_ptr<Datacratic::ServiceProxies> services, 00038 const std::string& name = "mock-exchange"); 00039 00040 ~MockExchange(); 00041 00042 void start(size_t threadCount, size_t numBidRequests, std::vector<int> const & bidPorts, std::vector<int> const & winPorts); 00043 00044 bool isDone() const { 00045 return !running; 00046 } 00047 00048 protected: 00049 00050 struct Bid 00051 { 00052 Datacratic::Id adSpotId; 00053 int maxPrice; 00054 std::string tagId; 00055 00056 AccountKey account; 00057 Datacratic::Date bidTimestamp; 00058 }; 00059 00060 private: 00061 int running; 00062 00063 struct Stream { 00064 Stream(int port); 00065 ~Stream(); 00066 void connect(); 00067 00068 addrinfo * addr; 00069 int fd; 00070 }; 00071 00072 struct BidStream : public Stream { 00073 BidStream(int port, int id) : Stream(port), id(id * port), key(0) { 00074 } 00075 00076 void sendBidRequest(const BidRequest& request); 00077 std::pair<bool, std::vector<Bid>> parseResponse(const std::string& rawResponse); 00078 std::pair<bool, std::vector<Bid>> recvBid(); 00079 00080 BidRequest makeBidRequest(); 00081 00082 long long id; 00083 long long key; 00084 }; 00085 00086 struct WinStream : public Stream { 00087 WinStream(int port) : Stream(port) { 00088 } 00089 00090 void sendWin(const BidRequest& bidRequest, const Bid& bid, const Amount& winPrice); 00091 }; 00092 00093 struct Worker { 00094 Worker(MockExchange * exchange, size_t id, int bidPort, int winPort); 00095 00096 void run(); 00097 void run(size_t requests); 00098 void bid(); 00099 00100 std::pair<bool, Amount> isWin(const BidRequest&, const Bid& bid); 00101 00102 MockExchange * exchange; 00103 BidStream bids; 00104 WinStream wins; 00105 ML::RNG rng; 00106 }; 00107 00108 std::vector<Worker> workers; 00109 boost::thread_group threads; 00110 }; 00111 00112 00113 } // namespace RTBKIT 00114 00115 #endif // __rtbkit__mock_exchange_h__
1.7.6.1