RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
plugins/adserver/http_adserver_connector.h
00001 /* http_adserver_connector.h                                       -*- C++ -*-
00002    Wolfgang Sourdeau, April 2013
00003    Copyright (c) 2013 Datacratic.  All rights reserved.
00004 */
00005 
00006 
00007 #pragma once
00008 
00009 #include <string>
00010 #include <vector>
00011 
00012 #include "soa/service/http_endpoint.h"
00013 #include "soa/service/json_endpoint.h"
00014 
00015 #include "adserver_connector.h"
00016 
00017 
00018 namespace RTBKIT {
00019 
00020 /****************************************************************************/
00021 /* HTTPADSERVERCONNECTIONHANDLER                                            */
00022 /****************************************************************************/
00023 
00024 class HttpAdServerHttpEndpoint;
00025 
00026 typedef std::function<void (const HttpHeader & header,
00027                             const Json::Value & json,
00028                             const std::string & jsonStr)>
00029     HttpAdServerRequestCb;
00030 
00031 struct HttpAdServerConnectionHandler
00032     : public Datacratic::JsonConnectionHandler {
00033     HttpAdServerConnectionHandler(HttpAdServerHttpEndpoint & endpoint,
00034                                   const HttpAdServerRequestCb & requestCb);
00035 
00036     virtual void handleJson(const HttpHeader & header,
00037                             const Json::Value & json,
00038                             const std::string & jsonStr);
00039 
00040 private:
00041     HttpAdServerHttpEndpoint & endpoint_;
00042     const HttpAdServerRequestCb & requestCb_;
00043 };
00044 
00045 
00046 /****************************************************************************/
00047 /* HTTPADSERVERHTTPENDPOINT                                                 */
00048 /****************************************************************************/
00049 
00050 struct HttpAdServerHttpEndpoint : public Datacratic::HttpEndpoint {
00051     HttpAdServerHttpEndpoint(int port,
00052                              const HttpAdServerRequestCb & requestCb);
00053     HttpAdServerHttpEndpoint(HttpAdServerHttpEndpoint && otherEndpoint);
00054 
00055     ~HttpAdServerHttpEndpoint();
00056 
00057     HttpAdServerHttpEndpoint & operator =
00058         (const HttpAdServerHttpEndpoint& other);
00059 
00060     int getPort() const;
00061 
00062     /* carbon logging */
00063     typedef std::function<void (const char * eventName,
00064                                 EventType,
00065                                 float)> OnEvent;
00066     OnEvent onEvent;
00067 
00068     void doEvent(const char * eventName, EventType type = ET_COUNT,
00069                  float value = 1.0, const char * units = "")
00070       const
00071     {
00072         if (onEvent) {
00073             std::string prefixedName(name() + "." + eventName);
00074             onEvent(prefixedName.c_str(), type, value);
00075         }
00076     }
00077 
00078     virtual std::shared_ptr<ConnectionHandler> makeNewHandler();
00079 
00080 private:
00081     int port_;
00082     HttpAdServerRequestCb requestCb_;
00083 };
00084         
00085 /****************************************************************************/
00086 /* HTTPADSERVERCONNECTOR                                                    */
00087 /****************************************************************************/
00088 
00089 struct HttpAdServerConnector : public AdServerConnector {
00090     HttpAdServerConnector(const std::string & serviceName,
00091                           const std::shared_ptr<Datacratic::ServiceProxies>
00092                           & proxy);
00093     ~HttpAdServerConnector() {
00094         shutdown();
00095     }
00096 
00097     void registerEndpoint(int port, const HttpAdServerRequestCb & requestCb);
00098 
00099     void init(const std::shared_ptr<ConfigurationService> & config);
00100     void shutdown();
00101 
00102     void bindTcp();
00103 
00104     void start();
00105 
00106 private:
00107     std::vector<HttpAdServerHttpEndpoint> endpoints_;
00108 };
00109 
00110 } //namespace RTBKIT
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator