RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
examples/mock_ad_server_connector.h
00001 
00009 #include "soa/service/service_utils.h"
00010 #include "soa/service/service_base.h"
00011 #include "soa/service/json_endpoint.h"
00012 #include "soa/service/zmq_named_pub_sub.h"
00013 #include "rtbkit/plugins/adserver/http_adserver_connector.h"
00014 #include "rtbkit/common/auction_events.h"
00015 
00016 namespace RTBKIT {
00017 
00018 /******************************************************************************/
00019 /* MOCK AD SERVER CONNECTOR                                                   */
00020 /******************************************************************************/
00021 
00028 struct MockAdServerConnector : public HttpAdServerConnector
00029 {
00030     MockAdServerConnector(const std::string& serviceName,
00031                           std::shared_ptr<Datacratic::ServiceProxies> proxies)
00032         : HttpAdServerConnector(serviceName, proxies),
00033           publisher(getServices()->zmqContext) {
00034     }
00035 
00036     MockAdServerConnector(Datacratic::ServiceProxyArguments & args,
00037                           const std::string& serviceName)
00038         : HttpAdServerConnector(serviceName, args.makeServiceProxies()),
00039           publisher(getServices()->zmqContext) {
00040     }
00041 
00042     void init(int port) {
00043         auto services = getServices();
00044 
00045         // Prepare a simple JSON handler that already parsed the incoming HTTP payload so that it can
00046         // create the requied post auction object.
00047         auto handleEvent = [&](const Datacratic::HttpHeader & header,
00048                                const Json::Value & json,
00049                                const std::string & text) {
00050             this->handleEvent(PostAuctionEvent(json));
00051         };
00052         registerEndpoint(port, handleEvent);
00053         
00054         // And initialize the generic publisher on a predefined range of ports to try avoiding that
00055         // collision between different kind of service occurs.
00056         publisher.init(services->config, serviceName() + "/logger");
00057         publisher.bindTcp(services->ports->getRange("adServer/logger"));
00058 
00059         HttpAdServerConnector::init(services->config);
00060     }
00061 
00062 
00063     void start() {
00064         recordLevel(1.0, "up");
00065         publisher.start();
00066     }
00067 
00068 
00069     void shutdown() {
00070         publisher.shutdown();
00071         HttpAdServerConnector::shutdown();
00072     }
00073 
00074 
00075     void handleEvent(PostAuctionEvent const & event) {
00076         if(event.type == PAE_WIN) {
00077             publishWin(event.auctionId,
00078                        event.adSpotId,
00079                        event.winPrice,
00080                        event.timestamp,
00081                        Json::Value(),
00082                        event.uids,
00083                        event.account,
00084                        Date::now());
00085 
00086             Date now = Date::now();
00087             publisher.publish("WIN", now.print(3), event.auctionId.toString(), event.winPrice.toString(), "0");
00088         }
00089     }
00090 
00091 
00094     Datacratic::ZmqNamedPublisher publisher;
00095 };
00096 
00097 } // namepsace RTBKIT
00098 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator