#include "../shared/SimpleLogFile.h" #include "../shared/PollSet.h" #include "../shared/GlobalConfigFile.h" #include "CandleClient.h" ///////////////////////////////////////////////////////////////////// // CandleClient::GetIntraday ///////////////////////////////////////////////////////////////////// void CandleClient::GetIntraday::sendResult(std::string const &result) { _result = result; _conditionVar.set(); } std::string const &CandleClient::GetIntraday::getResult() { _conditionVar.waitForever(); return _result; } ///////////////////////////////////////////////////////////////////// // CandleClient::GetDaily ///////////////////////////////////////////////////////////////////// void CandleClient::GetDaily::sendResult(std::string const &result) { _result = result; _conditionVar.set(); } std::string const &CandleClient::GetDaily::getResult() { _conditionVar.waitForever(); return _result; } ///////////////////////////////////////////////////////////////////// // CandleClient ///////////////////////////////////////////////////////////////////// std::string CandleClient::makeStartList(AllRowTimes const &allRowTimes) { std::string result; for (auto it = allRowTimes.begin(); it != allRowTimes.end(); it++) { if (!result.empty()) result += ','; result += ntoa(it->start); } return result; } std::string CandleClient::makeEndList(AllRowTimes const &allRowTimes) { std::string result; for (auto it = allRowTimes.begin(); it != allRowTimes.end(); it++) { if (!result.empty()) result += ','; result += ntoa(it->end); } return result; } SingleCandle CandleClient::extractSingleCandle(std::string const &bytes) { SingleCandle::ByStartTime result; const bool success = SingleCandle::unmarshal(bytes, result); if ((!success) || (result.size() > 1)) { // This should never happen. TclList msg; msg<second; else return SingleCandle(); } CandleClient::CandleClient() : ForeverServerConnection("CandleClient") { getAndParseAddress("candle_server"); start(); } CandleClient::~CandleClient() { // Not implemented. It's not really needed. assert(false); } void CandleClient::onNewConnection(TalkWithServer *talkWithServer) { { TalkWithServer::Message message; message["command"] = "login"; message["username"] = getConfigItem("candle_server_username"); message["password"] = getConfigItem("candle_server_password"); // TODO it's tempting to use the session id, like a normal client would. talkWithServer->sendMessage(message, this, ciLogin, true); } // Send/resend any old messages. { std::vector< TodaysCandles * > toResend; toResend.reserve(_todaysCandlesPending.size()); for (auto it = _todaysCandlesPending.begin(); it != _todaysCandlesPending.end(); it++) toResend.push_back(it->second); _todaysCandlesPending.clear(); for (auto it = toResend.cbegin(); it != toResend.end(); it++) sendRequestToServer(talkWithServer, *it); } { std::vector< GetIntraday * > toResend; toResend.reserve(_getIntradayPending.size()); for (auto it = _getIntradayPending.begin(); it != _getIntradayPending.end(); it++) toResend.push_back(it->second); _getIntradayPending.clear(); for (auto it = toResend.cbegin(); it != toResend.end(); it++) sendRequestToServer(talkWithServer, *it); } { std::vector< GetDaily * > toResend; toResend.reserve(_getDailyPending.size()); for (auto it = _getDailyPending.begin(); it != _getDailyPending.end(); it++) toResend.push_back(it->second); _getDailyPending.clear(); for (auto it = toResend.cbegin(); it != toResend.end(); it++) sendRequestToServer(talkWithServer, *it); } } void CandleClient::sendRequestToServer(TalkWithServer *connection, TodaysCandles *request) { TalkWithServer::Message message; message["command"] = "get_todays_candles"; message["symbol"] = request->getSymbol(); TalkWithServer::CancelId messageId = connection->sendMessage(message, this, ciTodaysCandles, false); _todaysCandlesPending[messageId] = request; } void CandleClient::onTodaysCandlesMessage(std::string const &bytes, TalkWithServer::CancelId cancelId) { const std::map< TalkWithServer::CancelId, TodaysCandles * >::iterator it = _todaysCandlesPending.find(cancelId); if (it == _todaysCandlesPending.end()) { // This should never happen. TclList msg; msg<second; _todaysCandlesPending.erase(it); request->callbackId = request->_finalCallbackId; const bool success = SingleCandle::unmarshal(bytes, request->data); request->_finalListener->newRequest(request); if (!success) { // This should never happen. TclList msg; msg<getSymbol(); message["start"] = request->getStart(); message["end"] = request->getEnd(); TalkWithServer::CancelId messageId = connection->sendMessage(message, this, ciIntradayCandle, false); _getIntradayPending[messageId] = request; } void CandleClient::onIntradayMessage(std::string const &bytes, TalkWithServer::CancelId cancelId) { const std::map< TalkWithServer::CancelId, GetIntraday * >::iterator it = _getIntradayPending.find(cancelId); if (it == _getIntradayPending.end()) { // This should never happen. TclList msg; msg<second; _getIntradayPending.erase(it); request->sendResult(bytes); } /* static std::string debugDump(TalkWithServer::Message const &message) { TclList result; for (TalkWithServer::Message::const_iterator it = message.begin(); it != message.end(); it++) result<<(TclList()<first<second); return result; } */ void CandleClient::sendRequestToServer(TalkWithServer *connection, GetDaily *request) { TalkWithServer::Message message; message["command"] = "get_daily_candle"; message["symbol"] = request->getSymbol(); message["date"] = request->getDate(); //std::cout<<"CandleClient::sendRequestToServer(daily)" // <sendMessage(message, this, ciDailyCandle, false); _getDailyPending[messageId] = request; } void CandleClient::onDailyMessage(std::string const &bytes, TalkWithServer::CancelId cancelId) { const std::map< TalkWithServer::CancelId, GetDaily * >::iterator it = _getDailyPending.find(cancelId); if (it == _getDailyPending.end()) { // This should never happen. TclList msg; msg<second; _getDailyPending.erase(it); request->sendResult(bytes); } void CandleClient::onMessage(std::string bytes, int clientId, TalkWithServer::CancelId cancelId) { //std::cout<<(std::string)(TclList()<