RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 00008 #pragma once 00009 00010 #include "jml/utils/exc_check.h" 00011 00012 #include <iostream> 00013 #include <mutex> 00014 #include <map> 00015 00016 namespace Json { struct Value; } 00017 00018 namespace Datacratic { 00019 00020 /******************************************************************************/ 00021 /* PORT RANGE */ 00022 /******************************************************************************/ 00023 00026 struct PortRange 00027 { 00028 PortRange() : first(15000), last(16000) {} 00029 00030 PortRange(int port) : 00031 first(port), last(port + 1) 00032 { 00033 if (port == -1) { 00034 first = 15000; 00035 last = 16000; 00036 } 00037 } 00038 00039 PortRange(int first, int last) : 00040 first(first), last(last) 00041 { 00042 ExcCheckGreater(last, first, "invalid port range"); 00043 } 00044 00045 template<typename F> 00046 int bindPort(F operation) const 00047 { 00048 for(int i = first; i < last; ++i) 00049 if(operation(i)) return i; 00050 00051 return -1; 00052 } 00053 00054 Json::Value toJson() const; 00055 static PortRange fromJson(const Json::Value & val); 00056 00057 int first; 00058 int last; 00059 }; 00060 00061 00062 /******************************************************************************/ 00063 /* PORT RANGE SERVICE */ 00064 /******************************************************************************/ 00065 00077 struct PortRangeService 00078 { 00079 virtual ~PortRangeService() {} 00080 00086 virtual PortRange getRange(const std::string& name) = 0; 00087 00089 virtual void dump(std::ostream& stream = std::cerr) const {} 00090 }; 00091 00092 00093 /******************************************************************************/ 00094 /* DEFAULT PORT RANGE SERVICE */ 00095 /******************************************************************************/ 00096 00101 struct DefaultPortRangeService : public PortRangeService 00102 { 00107 DefaultPortRangeService(unsigned rangeSize = 100); 00108 00109 virtual PortRange getRange(const std::string& name); 00110 00111 virtual void dump(std::ostream& stream = std::cerr) const; 00112 00113 private: 00114 const unsigned rangeSize; 00115 unsigned currentPort; 00116 00117 mutable std::mutex lock; 00118 std::map<std::string, PortRange> rangeMap; 00119 00120 }; 00121 00122 00123 /******************************************************************************/ 00124 /* JSON PORT RANGE SERVICE */ 00125 /******************************************************************************/ 00126 00131 struct JsonPortRangeService : public PortRangeService 00132 { 00133 JsonPortRangeService(const Json::Value& config); 00134 00135 virtual PortRange getRange(const std::string& name); 00136 00137 virtual void dump(std::ostream& stream = std::cerr) const; 00138 00139 private: 00140 std::map<std::string, PortRange> rangeMap; 00141 }; 00142 00143 00144 } // namespace Datacratic