#ifndef __TopListRequest_h_ #define __TopListRequest_h_ #include #include "../shared/MiscSupport.h" #include "../shared/SmarterP.h" /* This is based heavily on TopListRequest.C. I didn't copy all of the * comments, so go back to that file or to TopListMicroService.C for more * information. * * For the most part this is a no frills implementation. However, it adds * one thing that the C# version did not have. This automatically retries * on network error. * * The best documentation of this API is in the code which receives these * requests, ../fast_alert_search/TopListMicroService.C. */ class TopListRequest { public: enum class OptionalBool { Null, False, True }; static OptionalBool optionalBool(bool value) { return value?OptionalBool::True:OptionalBool::False; } // We will translate "" to null. Somewhat sloppy. In C# we use a string? std::string collaborate; OptionalBool skipMetaData; OptionalBool useDatabase; // 0 is effectively null. That will ask the top list server to give us // data ASAP. time_t lastUpdate; bool streaming; OptionalBool saveToMru; OptionalBool collaborateColumns; OptionalBool outsideMarketHours; // Use a value of 0 to let the server fill in a default value. Presumably // it will read the value from the collaborate string. A 0 here is like // a NULL in C# version of this code. int resultCount; // We will translate "" to null. Somewhat sloppy. std::string singleSymbol; std::string sortFormula; std::string whereFormula; std::map< std::string, std::string > extraColumnFormulas; struct Data { time_t start; time_t end; std::vector< PropertyList > rows; }; std::function< void(SmarterCP< Data > data, int64_t id) > onData; struct MetaData { std::string collaborate; std::string windowName; std::vector< PropertyList > columns; std::string sortBy; // The server also sends the client cookie. I think we can finally declare // client cookie to be a dead idea. It gets passed around from place to // place but no one ever seems to use it. }; std::function< void(SmarterCP< MetaData > metaData, int64_t id) > onMetaData; // This is thread safe. This will make a copy of the TopListRequest and // store it for later. This will assign and return an id number. Use that // id number to cancel a request when you are done with it. The id number // will also be returned in the callbacks. int64_t send(); // This is thread safe. It is safe to call this more than once on the same // id. Id 0 is reserved and will be never be returned from send(). It is // safe to call cancel(0). Standard warning: This is multithreaded and it // is possible that you will receive a response after the call to cancel(). static void cancel(int64_t id); // This value will never be returned by send(). If you call cancel() on this // value, nothing will happen. This is a good default for strtolDefault(). static const int64_t UNUSED_ID = 0; TopListRequest() : skipMetaData(OptionalBool::Null), useDatabase(OptionalBool::Null), lastUpdate(0), streaming(false), saveToMru(OptionalBool::Null), collaborateColumns(OptionalBool::Null), outsideMarketHours(OptionalBool::Null), resultCount(0) { } // Thread safe. static void logIn(std::string const &userName, std::string const &password); // Thread safe. static void logOut() { logIn("", ""); } }; class TopListHistoryRequest { public: std::string collaborate; bool saveToMru; typedef TopListRequest::Data Data; std::function< void(SmarterCP< Data > data, int64_t id) > onData; typedef TopListRequest::MetaData MetaData; std::function< void(SmarterCP< MetaData > metaData, int64_t id) > onMetaData; TopListHistoryRequest() : saveToMru(false) { } // This is thread safe. This will make a copy of the TopListHistoryRequest // and store it for later. int64_t send(); }; #endif