RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* slave_banker.h -*- C++ -*- 00002 Jeremy Barens, 8 November 2012 00003 Copyright (c) 2012 Datacratic Inc. All rights reserved. 00004 00005 Slave "banker" object that authorizes spend. 00006 */ 00007 00008 #ifndef __banker__slave_banker_h__ 00009 #define __banker__slave_banker_h__ 00010 00011 #include "banker.h" 00012 #include "soa/service/zmq_endpoint.h" 00013 #include "soa/service/typed_message_channel.h" 00014 #include "soa/service/rest_proxy.h" 00015 #include <thread> 00016 00017 namespace RTBKIT { 00018 00019 00020 /*****************************************************************************/ 00021 /* SLAVE BUDGET CONTROLLER */ 00022 /*****************************************************************************/ 00023 00027 struct SlaveBudgetController 00028 : public BudgetController, public Accountant, public RestProxy { 00029 00030 SlaveBudgetController(); 00031 00032 ~SlaveBudgetController() 00033 { 00034 shutdown(); 00035 } 00036 00037 void init(std::shared_ptr<ConfigurationService> config, 00038 const std::string & serviceClass = "rtbBanker") 00039 { 00040 RestProxy::initServiceClass(config, serviceClass, "zeromq"); 00041 } 00042 00043 virtual void addAccount(const AccountKey & account, 00044 const OnBudgetResult & onResult); 00045 00046 virtual void topupTransfer(const AccountKey & account, 00047 CurrencyPool amount, 00048 const OnBudgetResult & onResult); 00049 00050 virtual void setBudget(const std::string & topLevelAccount, 00051 CurrencyPool amount, 00052 const OnBudgetResult & onResult); 00053 00054 virtual void addBudget(const std::string & topLevelAccount, 00055 CurrencyPool amount, 00056 const OnBudgetResult & onResult); 00057 00058 virtual void 00059 getAccountList(const AccountKey & prefix, 00060 int depth, 00061 std::function<void (std::exception_ptr, 00062 std::vector<AccountKey> &&)> onResult); 00063 00064 virtual void 00065 getAccountSummary(const AccountKey & account, 00066 int depth, 00067 std::function<void (std::exception_ptr, 00068 AccountSummary &&)> onResult); 00069 00070 virtual void 00071 getAccount(const AccountKey & account, 00072 std::function<void (std::exception_ptr, 00073 Account &&)> onResult); 00074 00075 static OnDone budgetResultCallback(const OnBudgetResult & onResult); 00076 }; 00077 00078 00079 /*****************************************************************************/ 00080 /* SLAVE BANKER */ 00081 /*****************************************************************************/ 00082 00087 struct SlaveBanker : public Banker, public RestProxy { 00088 00089 SlaveBanker(std::shared_ptr<zmq::context_t> context); 00090 00091 ~SlaveBanker() 00092 { 00093 shutdown(); 00094 } 00095 00096 SlaveBanker(std::shared_ptr<zmq::context_t> context, 00097 std::shared_ptr<ConfigurationService> config, 00098 const std::string & accountSuffix, 00099 const std::string & bankerServiceClass = "rtbBanker"); 00100 00110 void init(std::shared_ptr<ConfigurationService> config, 00111 const std::string & accountSuffix, 00112 const std::string & serviceClass = "rtbBanker"); 00113 00118 virtual void addSpendAccount(const AccountKey & account, 00119 CurrencyPool accountFloat, 00120 std::function<void (std::exception_ptr, ShadowAccount&&)> onDone); 00121 00122 ShadowAccount getAccount(const AccountKey & account) 00123 { 00124 return accounts.activateAccount(account); 00125 } 00126 00127 virtual bool authorizeBid(const AccountKey & account, 00128 const std::string & item, 00129 Amount amount) 00130 { 00131 return accounts.authorizeBid(account, item, amount); 00132 } 00133 00134 virtual void commitBid(const AccountKey & account, 00135 const std::string & item, 00136 Amount amountPaid, 00137 const LineItems & lineItems) 00138 { 00139 accounts.commitBid(account, item, amountPaid, lineItems); 00140 } 00141 00142 virtual Amount detachBid(const AccountKey & account, 00143 const std::string & item) 00144 { 00145 return accounts.detachBid(account, item); 00146 } 00147 00148 virtual void attachBid(const AccountKey & account, 00149 const std::string & item, 00150 Amount amountAuthorized) 00151 { 00152 accounts.attachBid(account, item, amountAuthorized); 00153 } 00154 00155 virtual void commitDetachedBid(const AccountKey & account, 00156 Amount amountAuthorized, 00157 Amount amountPaid, 00158 const LineItems & lineItems) 00159 { 00160 return accounts.commitDetachedBid(account, 00161 amountAuthorized, amountPaid, 00162 lineItems); 00163 } 00164 00165 virtual void forceWinBid(const AccountKey & account, 00166 Amount amountPaid, 00167 const LineItems & lineItems) 00168 { 00169 return accounts.forceWinBid(account, amountPaid, lineItems); 00170 } 00171 00175 ShadowAccount syncAccountSync(const AccountKey & account); 00176 00180 void syncAccount(const AccountKey & account, 00181 std::function<void (std::exception_ptr, 00182 ShadowAccount &&)> onDone); 00183 00185 void syncAllSync(); 00186 00188 void syncAll(std::function<void (std::exception_ptr)> onDone 00189 = std::function<void (std::exception_ptr)>()); 00190 00192 ShadowAccount getAccountStateDebug(AccountKey accountKey) const 00193 { 00194 return accounts.getAccount(accountKey); 00195 } 00196 00197 /* Logging */ 00198 virtual void logBidEvents(const Datacratic::EventRecorder & eventRecorder) 00199 { 00200 accounts.logBidEvents(eventRecorder); 00201 } 00202 00203 private: 00204 ShadowAccounts accounts; 00205 00208 TypedMessageSink<AccountKey> createdAccounts; 00209 std::string accountSuffix; 00210 00212 void reportSpend(uint64_t numTimeoutsExpired); 00213 Date reportSpendSent; 00214 00216 void reauthorizeBudget(uint64_t numTimeoutsExpired); 00217 Date reauthorizeBudgetSent; 00218 00221 void onSyncResult(const AccountKey & accountKey, 00222 std::function<void (std::exception_ptr, 00223 ShadowAccount &&)> onDone, 00224 std::exception_ptr exc, 00225 Account&& masterAccount); 00226 00229 void onInitializeResult(const AccountKey & accountKey, 00230 std::function<void (std::exception_ptr, 00231 ShadowAccount &&)> onDone, 00232 std::exception_ptr exc, 00233 Account&& masterAccount); 00234 00237 void onReauthorizeBudgetMessage(const AccountKey & account, 00238 std::exception_ptr exc, 00239 int responseCode, 00240 const std::string & payload); 00241 00243 std::string getShadowAccountStr(const AccountKey & account) const 00244 { 00245 return account.childKey(accountSuffix).toString(); 00246 } 00247 }; 00248 00249 } // naemspace RTBKIT 00250 00251 #endif /* __banker__slave_banker_h__ */