RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 00023 #pragma once 00024 00025 #include <string> 00026 #include <memory> 00027 #include <vector> 00028 #include "soa/types/id.h" 00029 #include "soa/types/string.h" 00030 #include "soa/types/url.h" 00031 #include "jml/utils/compact_vector.h" 00032 #include "soa/jsoncpp/value.h" 00033 #include <iostream> 00034 00035 namespace OpenRTB { 00036 00037 using std::string; 00038 using std::vector; 00039 using std::unique_ptr; 00040 using Datacratic::Id; 00041 using Datacratic::Utf8String; 00042 using Datacratic::Url; 00043 00044 typedef std::string CSList; // comma-separated list 00045 00046 template<typename T> 00047 struct Optional: public std::unique_ptr<T> { 00048 Optional() 00049 { 00050 } 00051 00052 Optional(Optional && other) 00053 : std::unique_ptr<T>(std::move(other)) 00054 { 00055 } 00056 00057 Optional(const Optional & other) 00058 { 00059 if (other) 00060 this->reset(new T(*other)); 00061 } 00062 00063 Optional & operator = (const Optional & other) 00064 { 00065 Optional newMe(other); 00066 swap(newMe); 00067 return *this; 00068 } 00069 00070 Optional & operator = (Optional && other) 00071 { 00072 Optional newMe(other); 00073 swap(newMe); 00074 return *this; 00075 } 00076 00077 void swap(Optional & other) 00078 { 00079 std::unique_ptr<T>::swap(other); 00080 } 00081 }; 00082 00083 template<typename Cls, int defValue = -1> 00084 struct TaggedEnum { 00085 TaggedEnum() 00086 : val(-1) 00087 { 00088 } 00089 00090 int val; 00091 00092 int value() const 00093 { 00094 return val; 00095 } 00096 00097 00098 #if 0 00099 operator typename Cls::Vals () const 00100 { 00101 return static_cast<typename Cls::Vals>(val); 00102 } 00103 #endif 00104 }; 00105 00106 template<typename E, int def> 00107 bool operator == (const TaggedEnum<E, def> & e1, const TaggedEnum<E, def> & e2) 00108 { 00109 return e1.val == e2.val; 00110 } 00111 00112 template<typename E, int def> 00113 bool operator != (const TaggedEnum<E, def> & e1, const TaggedEnum<E, def> & e2) 00114 { 00115 return e1.val != e2.val; 00116 } 00117 00118 template<typename E, int def> 00119 bool operator > (const TaggedEnum<E, def> & e1, const TaggedEnum<E, def> & e2) 00120 { 00121 return e1.val > e2.val; 00122 } 00123 00124 template<typename E, int def> 00125 bool operator < (const TaggedEnum<E, def> & e1, const TaggedEnum<E, def> & e2) 00126 { 00127 return e1.val < e2.val; 00128 } 00129 00130 template<typename E, int def> 00131 inline Json::Value jsonPrint(const TaggedEnum<E, def> & e) 00132 { 00133 return e.val; 00134 } 00135 00136 template<typename E, int def> 00137 inline void jsonParse(const Json::Value & j, TaggedEnum<E, def> & e) 00138 { 00139 e.val = j.asInt(); 00140 } 00141 00142 struct TaggedBool { 00143 TaggedBool() 00144 : val(-1) 00145 { 00146 } 00147 00148 int val; 00149 }; 00150 00151 template<int defValue = -1> 00152 struct TaggedBoolDef : public TaggedBool { 00153 TaggedBoolDef() 00154 : val(defValue) 00155 { 00156 } 00157 00158 int val; 00159 }; 00160 00161 struct TaggedInt { 00162 TaggedInt() 00163 : val(-1) 00164 { 00165 } 00166 00167 int value() const { return val; } 00168 00169 int val; 00170 }; 00171 00172 template<int defValue = -1> 00173 struct TaggedIntDef : TaggedInt { 00174 TaggedIntDef() 00175 : val(defValue) 00176 { 00177 } 00178 00179 int val; 00180 }; 00181 00182 struct TaggedFloat { 00183 TaggedFloat() 00184 : val(std::numeric_limits<float>::quiet_NaN()) 00185 { 00186 } 00187 00188 float val; 00189 }; 00190 00191 template<int num = -1, int den = 1> 00192 struct TaggedFloatDef : public TaggedFloat { 00193 TaggedFloatDef() 00194 : val(1.0f * num / den) 00195 { 00196 } 00197 00198 float val; 00199 }; 00200 00201 #if 0 // c++11 templated typedefs 00202 template<typename T> 00203 using List = compact_vector<T, 3>; 00204 00205 #else 00206 template<typename T> 00207 struct List: public ML::compact_vector<T, 3> { 00208 00209 #if 0 00210 typedef ML::compact_vector<T, 3> Base; 00211 00212 List() 00213 { 00214 using namespace std; 00215 cerr << "default construct" << endl; 00216 } 00217 00218 List(const List & other) 00219 : Base(std::move(other)) 00220 { 00221 using namespace std; 00222 cerr << "copy construct" << endl; 00223 } 00224 00225 List & operator = (const List & other) 00226 { 00227 using namespace std; 00228 cerr << "assignment" << endl; 00229 *(Base *)this = (Base &)other; 00230 return *this; 00231 } 00232 00233 ~List() 00234 { 00235 using namespace std; 00236 cerr << "destructor length " << this->size() << endl; 00237 } 00238 00239 List & operator = (List && other) 00240 { 00241 using namespace std; 00242 cerr << "move assignment" << endl; 00243 *(Base *)this = (Base &&)other; 00244 return *this; 00245 } 00246 00247 List(List && other) 00248 : Base(std::move(other)) 00249 { 00250 using namespace std; 00251 cerr << "move constructor from length " << this->size() << endl; 00252 } 00253 #endif 00254 }; 00255 00256 #endif 00257 00258 /*****************************************************************************/ 00259 /* MIME TYPES */ 00260 /*****************************************************************************/ 00261 00262 struct MimeType { 00263 MimeType(const std::string & type = "") 00264 : type(type) 00265 { 00266 } 00267 00268 std::string type; 00269 //int val; 00270 }; 00271 00272 00273 /*****************************************************************************/ 00274 /* CONTENT CATEGORIES */ 00275 /*****************************************************************************/ 00276 00290 struct ContentCategory { 00291 00292 ContentCategory(const std::string & val = "") 00293 : val(val) 00294 { 00295 } 00296 00297 string val; 00298 00299 #if 0 00300 ContentCategory(int l1 = -1, int l2 = -1) 00301 : l1(l1), l2(l2) 00302 { 00303 } 00304 00305 int l1; 00306 int l2; 00307 #endif 00308 }; 00309 00310 00311 /*****************************************************************************/ 00312 /* BANNER AD TYPES */ 00313 /*****************************************************************************/ 00314 00321 struct BannerAdType: public TaggedEnum<BannerAdType> { 00322 enum Vals { 00323 UNSPECIFIED = -1, 00324 00325 XHTML_TEXT = 1, 00326 XHTML_BANNER = 2, 00327 JAVASCRIPT = 3, 00328 IFRAME = 4 00329 }; 00330 }; 00331 00332 /*****************************************************************************/ 00333 /* CREATIVE ATTRIBUTES */ 00334 /*****************************************************************************/ 00335 00342 struct CreativeAttribute: public TaggedEnum<CreativeAttribute> { 00343 enum Vals { 00344 UNSPECIFIED = -1, 00345 }; 00346 }; 00347 00348 00349 /*****************************************************************************/ 00350 /* API FRAMEWORKS */ 00351 /*****************************************************************************/ 00352 00358 struct ApiFramework: public TaggedEnum<ApiFramework> { 00359 enum Vals { 00360 UNSPECIFIED = -1, 00361 00362 VPAID_1 = 1, 00363 VPAID_2 = 2, 00364 MRAID = 3, 00365 ORMMA = 4 00366 }; 00367 }; 00368 00369 00370 /*****************************************************************************/ 00371 /* AD POSITION */ 00372 /*****************************************************************************/ 00373 00384 struct AdPosition: public TaggedEnum<AdPosition, 0> { 00385 enum Vals { 00386 UNSPECIFIED = -1, 00387 00388 UNKNOWN = 0, 00389 ABOVE = 1, 00390 BETWEEN_DEPRECATED = 2, 00391 BELOW = 3, 00392 HEADER = 4, 00393 FOOTER = 5, 00394 SIDEBAR = 6, 00395 FULLSCREEN = 7 00396 }; 00397 }; 00398 00399 00400 /*****************************************************************************/ 00401 /* VIDEO LINEARITY */ 00402 /*****************************************************************************/ 00403 00416 struct VideoLinearity: public TaggedEnum<VideoLinearity> { 00417 enum Vals { 00418 UNSPECIFIED = -1, 00419 00420 IN_STREAM = 1, 00421 OVERLAY = 2 00422 }; 00423 }; 00424 00425 00426 /*****************************************************************************/ 00427 /* VIDEO BID RESPONSE PROTOCOLS */ 00428 /*****************************************************************************/ 00429 00436 struct VideoBidResponseProtocol: public TaggedEnum<VideoBidResponseProtocol> { 00437 enum Vals { 00438 UNSPECIFIED = -1, 00439 00440 VAST1 = 1, 00441 VAST2 = 2, 00442 VAST3 = 3, 00443 VAST1_WRAPPER = 4, 00444 VAST2_WRAPPER = 5, 00445 VAST3_WRAPPER = 6 00446 }; 00447 }; 00448 00449 /*****************************************************************************/ 00450 /* VIDEO PLAYBACK METHODS */ 00451 /*****************************************************************************/ 00452 00457 struct VideoPlaybackMethod: public TaggedEnum<VideoPlaybackMethod> { 00458 enum Vals { 00459 UNSPECIFIED = -1, 00460 00461 AUTO_PLAY_SOUND_ON = 1, 00462 AUTO_PLAY_SOUND_OFF = 2, 00463 CLICK_TO_PLAY = 3, 00464 MOUSE_OVER = 4 00465 }; 00466 }; 00467 00468 00469 /*****************************************************************************/ 00470 /* VIDEO START DELAY */ 00471 /*****************************************************************************/ 00472 00483 struct VideoStartDelay: public TaggedEnum<VideoStartDelay> { 00484 enum Vals { 00485 UNSPECIFIED = -1, 00486 00487 PRE_ROLL = 0, 00488 GENERIC_MID_ROLL = -1, 00489 GENERIC_POST_ROLL = -2 00490 }; 00491 }; 00492 00493 00494 /*****************************************************************************/ 00495 /* CONNECTION TYPE */ 00496 /*****************************************************************************/ 00497 00503 struct ConnectionType: public TaggedEnum<ConnectionType> { 00504 enum Vals { 00505 UNSPECIFIED = -1, 00506 00507 UNKNOWN = 0, 00508 ETHERNET = 1, 00509 WIFI = 2, 00510 CELLULAR_UNKNOWN = 3, 00511 CELLULAR_2G = 4, 00512 CELLULAR_3G = 5, 00513 CELLULAR_4G = 6 00514 }; 00515 }; 00516 00517 00518 /*****************************************************************************/ 00519 /* EXPANDABLE DIRECTION */ 00520 /*****************************************************************************/ 00521 00529 struct ExpandableDirection: public TaggedEnum<ExpandableDirection> { 00530 enum Vals { 00531 UNSPECIFIED = -1, 00532 00533 LEFT = 1, 00534 RIGHT = 2, 00535 UP = 3, 00536 DOWN = 4, 00537 FULLSCREEN = 5 00538 }; 00539 }; 00540 00541 00542 /*****************************************************************************/ 00543 /* CONTENT DELIVERY METHOD */ 00544 /*****************************************************************************/ 00545 00552 struct ContentDeliveryMethod: public TaggedEnum<ContentDeliveryMethod> { 00553 enum Vals { 00554 UNSPECIFIED = -1, 00555 00556 STREAMING = 1, 00557 PROGRESSIVE = 2 00558 }; 00559 }; 00560 00561 00562 /*****************************************************************************/ 00563 /* CONTENT CONTEXT */ 00564 /*****************************************************************************/ 00565 00575 struct ContentContext: public TaggedEnum<ContentContext> { 00576 enum Vals { 00577 UNSPECIFIED = -1, 00578 00579 VIDEO = 1, 00580 GAME = 2, 00581 MUSIC = 3, 00582 APPLICATION = 4, 00583 TEXT = 5, 00584 OTHER = 6, 00585 UNKNOWN = 7 00586 }; 00587 }; 00588 00589 00590 /*****************************************************************************/ 00591 /* VIDEO QUALITY */ 00592 /*****************************************************************************/ 00593 00600 struct VideoQuality: public TaggedEnum<VideoQuality> { 00601 enum Vals { 00602 UNSPECIFIED = -1, 00603 00604 UNKNOWN = 0, 00605 PROFESSIONAL = 1, 00606 PROSUMER = 2, 00607 USER_GENERATED = 3 00608 }; 00609 }; 00610 00611 00612 /*****************************************************************************/ 00613 /* LOCATION TYPE */ 00614 /*****************************************************************************/ 00615 00622 struct LocationType: public TaggedEnum<LocationType> { 00623 enum Vals { 00624 UNSPECIFIED = -1, 00625 00626 GPS = 1, 00627 IP_ADDRESS = 2, 00628 USER = 3 00629 }; 00630 }; 00631 00632 00633 /*****************************************************************************/ 00634 /* DEVICE TYPE */ 00635 /*****************************************************************************/ 00636 00646 struct DeviceType: public TaggedEnum<DeviceType> { 00647 enum Vals { 00648 UNSPECIFIED = -1, 00649 00650 MOBILE_OR_TABLET = 1, 00651 PC = 2, 00652 TV = 3 00653 }; 00654 }; 00655 00656 00657 /*****************************************************************************/ 00658 /* VAST COMPANION TYPES */ 00659 /*****************************************************************************/ 00660 00668 struct VastCompanionType: public TaggedEnum<VastCompanionType> { 00669 enum Vals { 00670 UNSPECIFIED = -1, 00671 00672 STATIC_RESOURCE = 1, 00673 HTML_RESOURCE = 2, 00674 IFRAME_RESOURCE = 3 00675 }; 00676 }; 00677 00678 00679 /*****************************************************************************/ 00680 /* QAG MEDIA RATINGS */ 00681 /*****************************************************************************/ 00682 00689 struct MediaRating: public TaggedEnum<MediaRating> { 00690 enum Vals { 00691 UNSPECIFIED = -1, 00692 00693 ALL_AUDIENCES = 1, 00694 OVER_12 = 2, 00695 MATURE_AUDIENCES =3 00696 }; 00697 }; 00698 00699 00700 /*****************************************************************************/ 00701 /* FRAME POSITION */ 00702 /*****************************************************************************/ 00703 00704 struct FramePosition: public TaggedEnum<FramePosition, 0> { 00705 enum Vals { 00706 UNSPECIFIED = -1, 00707 00708 IFRAME = 0, 00709 TOP_FRAME = 1 00710 }; 00711 }; 00712 00713 /*****************************************************************************/ 00714 /* SOURCE RELATIONSHIP */ 00715 /*****************************************************************************/ 00716 00717 struct SourceRelationship: public TaggedEnum<SourceRelationship> { 00718 enum Vals { 00719 UNSPECIFIED = -1, 00720 00721 INDIRECT = 0, 00722 DIRECT = 1 00723 }; 00724 }; 00725 00726 /*****************************************************************************/ 00727 /* AUCTION TYPE */ 00728 /*****************************************************************************/ 00729 00730 struct AuctionType: public TaggedEnum<AuctionType, 2> { 00731 enum Vals { 00732 UNSPECIFIED = -1, 00733 00734 FIRST_PRICE = 1, 00735 SECOND_PRICE = 2 00736 }; 00737 00738 AuctionType(Vals val = UNSPECIFIED) 00739 { 00740 this->val = val; 00741 } 00742 }; 00743 00744 00745 /*****************************************************************************/ 00746 /* BANNER */ 00747 /*****************************************************************************/ 00748 00760 struct Banner { 00761 ~Banner(); 00762 00764 List<int> w; 00765 List<int> h; 00766 00767 Id id; 00768 AdPosition pos; 00769 List<BannerAdType> btype; 00770 List<CreativeAttribute> battr; 00771 List<MimeType> mimes; 00772 FramePosition topframe; 00773 List<ExpandableDirection> expdir; 00774 List<ApiFramework> api; 00775 }; 00776 00777 00778 /*****************************************************************************/ 00779 /* VIDEO */ 00780 /*****************************************************************************/ 00781 00793 struct Video { 00794 ~Video(); 00795 List<MimeType> mimes; 00796 VideoLinearity linearity; 00797 TaggedInt minduration; 00798 TaggedInt maxduration; 00799 List<VideoBidResponseProtocol> protocol; 00800 TaggedInt w; 00801 TaggedInt h; 00802 TaggedInt startdelay; 00803 TaggedIntDef<1> sequence; 00804 List<CreativeAttribute> battr; 00805 TaggedIntDef<0> maxextended; 00806 TaggedInt minbitrate; 00807 TaggedInt maxbitrate; 00808 TaggedBoolDef<1> boxingallowed; 00809 List<VideoPlaybackMethod> playbackmethod; 00810 List<ContentDeliveryMethod> delivery; 00811 AdPosition pos; 00812 vector<Banner> companionad; 00813 List<ApiFramework> api; 00814 }; 00815 00816 00817 /*****************************************************************************/ 00818 /* PRODUCER / PUBLISHER */ 00819 /*****************************************************************************/ 00820 00828 struct Publisher { 00829 ~Publisher(); 00830 Id id; 00831 string name; 00832 List<ContentCategory> cat; 00833 string domain; 00834 }; 00835 00846 typedef Publisher Producer; 00847 00848 00849 /*****************************************************************************/ 00850 /* IMPRESSION */ 00851 /*****************************************************************************/ 00852 00863 struct Impression { 00864 ~Impression(); 00865 Id id; 00866 Optional<Banner> banner; 00867 Optional<Video> video; 00868 string displaymanager; 00869 string displaymanagerver; 00870 TaggedBoolDef<0> instl; 00871 string tagid; 00872 TaggedFloatDef<0> bidfloor; 00873 string bidfloorcur; 00874 List<string> iframebuster; 00875 Json::Value ext; 00876 }; 00877 00878 00879 /*****************************************************************************/ 00880 /* CONTENT */ 00881 /*****************************************************************************/ 00882 00899 struct Content { 00900 ~Content(); 00901 Id id; 00902 TaggedInt episode; 00903 Utf8String title; 00904 Utf8String series; 00905 Utf8String season; 00906 Url url; 00907 List<ContentCategory> cat; 00908 VideoQuality videoquality; 00909 CSList keywords; 00910 string contentrating; 00911 string userrating; 00912 string context; 00913 TaggedBool livestream; 00914 SourceRelationship sourcerelationship; 00915 Optional<Producer> producer; 00916 TaggedInt len; 00917 }; 00918 00919 00920 /*****************************************************************************/ 00921 /* CONTEXT */ 00922 /*****************************************************************************/ 00923 00926 struct Context { 00927 ~Context(); 00928 Id id; 00929 Utf8String name; 00930 Utf8String domain; 00931 List<ContentCategory> cat; 00932 List<ContentCategory> sectioncat; 00933 List<ContentCategory> pagecat; 00934 TaggedBool privacypolicy; 00935 Optional<Publisher> publisher; 00936 Optional<Content> content; 00937 CSList keywords; 00938 Json::Value ext; 00939 }; 00940 00941 00942 /*****************************************************************************/ 00943 /* SITE */ 00944 /*****************************************************************************/ 00945 00958 struct SiteInfo { 00959 Url page; 00960 Url ref; 00961 Utf8String search; 00962 }; 00963 00964 struct Site: public Context, public SiteInfo { 00965 }; 00966 00967 00968 /*****************************************************************************/ 00969 /* APP */ 00970 /*****************************************************************************/ 00971 00983 struct AppInfo { 00984 string ver; 00985 string bundle; 00986 TaggedBool paid; 00987 }; 00988 00989 struct App: public Context, public AppInfo { 00990 }; 00991 00992 00993 /*****************************************************************************/ 00994 /* GEO */ 00995 /*****************************************************************************/ 00996 01011 struct Geo { 01012 ~Geo(); 01013 TaggedFloat lat; 01014 TaggedFloat lon; 01015 TaggedBool latlonconsent; 01016 string country; 01017 string region; 01018 string regionfips104; 01019 string metro; 01020 Utf8String city; 01021 string zip; 01022 string dma; 01023 LocationType type; 01024 }; 01025 01026 01027 /*****************************************************************************/ 01028 /* DEVICE */ 01029 /*****************************************************************************/ 01030 01050 struct Device { 01051 ~Device(); 01052 TaggedBool dnt; 01053 string ua; 01054 string ip; 01055 Optional<Geo> geo; 01056 string didsha1; 01057 string didmd5; 01058 string dpidsha1; 01059 string dpidmd5; 01060 string ipv6; 01061 string carrier; 01062 string language; 01063 string make; 01064 string model; 01065 string os; 01066 string osv; 01067 TaggedBool js; 01068 ConnectionType connectiontype; 01069 DeviceType devicetype; 01070 string flashver; 01071 Json::Value ext; 01072 }; 01073 01074 01075 /*****************************************************************************/ 01076 /* SEGMENT */ 01077 /*****************************************************************************/ 01078 01090 struct Segment { 01091 Id id; 01092 string name; 01093 string value; 01094 TaggedFloat segmentusecost; 01095 }; 01096 01097 01098 /*****************************************************************************/ 01099 /* DATA */ 01100 /*****************************************************************************/ 01101 01114 struct Data { 01115 Id id; 01116 string name; 01117 vector<Segment> segment; 01118 01120 string usecostcurrency; 01121 TaggedFloat datausecost; 01122 }; 01123 01124 01125 /*****************************************************************************/ 01126 /* USER */ 01127 /*****************************************************************************/ 01128 01145 struct User { 01146 ~User(); 01147 Id id; 01148 Id buyeruid; 01149 TaggedInt yob; 01150 string gender; 01151 CSList keywords; 01152 string customdata; 01153 vector<Data> data; 01154 Geo geo; 01155 01157 TaggedInt tz; 01158 TaggedInt sessiondepth; 01159 }; 01160 01161 01162 /*****************************************************************************/ 01163 /* BID REQUEST */ 01164 /*****************************************************************************/ 01165 01166 01180 struct BidRequest { 01181 ~BidRequest(); 01182 Id id; 01183 01184 vector<Impression> imp; 01185 //unique_ptr<Context> context; // TODO: factor out of site and app 01186 Optional<Site> site; 01187 Optional<App> app; 01188 Optional<Device> device; 01189 Optional<User> user; 01190 01191 AuctionType at; 01192 TaggedInt tmax; 01193 vector<string> wseat; 01194 TaggedBool allimps; 01195 vector<string> cur; 01196 List<ContentCategory> bcat; 01197 vector<string> badv; 01198 Json::Value ext; 01199 Json::Value unparseable; 01200 }; 01201 01202 01203 /*****************************************************************************/ 01204 /* BID */ 01205 /*****************************************************************************/ 01206 01230 struct Bid { 01231 Id id; 01232 Id impid; 01233 TaggedFloat price; 01234 Id adid; 01235 string nurl; 01236 string adm; 01237 vector<string> adomain; 01238 string iurl; 01239 Id cid; 01240 Id crid; 01241 List<CreativeAttribute> attr; 01242 Json::Value ext; 01243 }; 01244 01245 01246 /*****************************************************************************/ 01247 /* SEAT BID */ 01248 /*****************************************************************************/ 01249 01263 struct SeatBid { 01264 vector<Bid> bid; 01265 Id seat; 01266 TaggedInt group; 01267 Json::Value ext; 01268 }; 01269 01270 01271 /*****************************************************************************/ 01272 /* BID RESPONSE */ 01273 /*****************************************************************************/ 01274 01290 struct BidResponse { 01291 Id id; 01292 vector<SeatBid> seatbid; 01293 Id bidid; 01294 string cur; 01295 string customData; 01296 Json::Value ext; 01297 }; 01298 01299 } // namespace OpenRTB