From addff775e96e666f2732cb4796e44f215a9cd75c Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 3 Nov 2023 23:12:50 -0400 Subject: [PATCH] pretty damn good, really --- CMakeLists.txt | 5 +- .../CMakeLists.txt | 19 +- .../bts-cme-websocket-listener/cme_message.h | 30 + .../cme_message_demuxer.cpp | 91 +++ .../cme_message_demuxer.h | 45 ++ .../cme_message_handler.h | 22 + .../cme_message_logger.h | 20 + .../cme_subscription_id.h | 10 + .../cme_websocket_session.cpp | 210 +++++++ .../cme_websocket_session.h | 49 ++ .../cme_websocket_session_config.h | 38 ++ .../bts-cme-websocket-listener/main.cpp | 66 +++ .../messages/channel.h | 18 + .../messages/cme_book_level.h | 33 ++ .../messages/cme_instrument.h | 90 +++ .../messages/cme_instrument_metadata.h | 33 ++ .../messages/cme_message_header.h | 53 ++ .../messages/cme_subscription.h | 17 + .../messages/cme_subscription_payload.h | 20 + .../messages/private_order_qty.h | 0 .../messages/top_of_book.h | 22 + .../messages/trade_summary.h | 13 +- .../messages/trade_summary_message.h | 19 + .../messages/trade_summary_payload.h | 19 + .../messages/trade_ticker_request.h | 0 .../sample_req.json | 0 .../ssl_certificate_verifier.cpp | 0 .../ssl_certificate_verifier.h | 0 .../tls_initializer.cpp | 2 +- .../tls_initializer.h | 0 .../cme_websocket_config.json | 16 +- .../cme_connection_metadata.cpp | 1 - .../cme_connection_metadata.h | 112 ---- .../bts-websocket-listener/cme_oauth_config.h | 44 -- .../cme_websocket_config.h | 45 -- .../cme_websocket_session.cpp | 257 --------- .../cme_websocket_session.h | 37 -- .../bts-websocket-listener/main.cpp | 47 -- .../messages/message_header.h | 17 - .../messages/trade_summary_message.h | 20 - .../messages/trade_summary_payload.h | 19 - .../bts-websocket-listener/scratch_client.cpp | 179 ------ .../bts-websocket-listener/scratch_client.h | 10 - .../files/cme_subscription_status.json | 21 + .../files/cme_websocket_config.json | 27 +- src/lib_bts-http-client/CMakeLists.txt | 23 - src/lib_bts-http/CMakeLists.txt | 13 +- .../bts-http}/curl_requestor.h | 0 .../bts-http}/http_client.cpp | 2 +- .../bts-http}/http_client.h | 7 + src/lib_bts-http/bts-http/oauth_request.cpp | 18 + src/lib_bts-http/bts-http/oauth_request.h | 24 + src/lib_bts-http/bts-http/oauth_requestor.h | 70 +++ src/lib_bts-http/bts-http/oauth_response.h | 9 +- src/lib_bts-json/CMakeLists.txt | 28 - src/lib_bts-json/bts-json/JsonReader.cpp | 519 ------------------ src/lib_bts-system/CMakeLists.txt | 15 + src/lib_bts-system/bts-system/TryCatch.h | 28 - src/lib_bts-system/bts-system/bts_assert.h | 1 - src/lib_bts-system/bts-system/delegate.h | 83 +++ .../{EnumMacros.h => enum_macros.h} | 0 .../bts-system/json/json_dumper.h} | 11 +- .../bts-system/json/json_reader.cpp | 518 +++++++++++++++++ .../bts-system/json/json_reader.h} | 69 ++- src/lib_bts-system/bts-system/overloaded.h | 11 + .../bts-system/threading/thread_config.h | 30 + .../bts-system/threading/thread_manager.cpp | 117 ++++ .../bts-system/threading/thread_manager.h | 48 ++ src/lib_bts-system/bts-system/value_wrapper.h | 163 ++++++ src/lib_bts-websocket/CMakeLists.txt | 18 + .../bts-websocket/websocket_access_token.h | 26 + .../bts-websocket/websocket_authorization.h | 81 +++ .../websocket_authorization_type.h | 23 + .../bts-websocket/websocket_client.cpp | 273 +++++++++ .../bts-websocket/websocket_client.h | 42 ++ .../bts-websocket/websocket_client_config.h | 65 +++ 76 files changed, 2693 insertions(+), 1438 deletions(-) rename src/{app_bts-websocket-listener => app_bts-cme-websocket-listener}/CMakeLists.txt (52%) create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.cpp create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_handler.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_logger.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_subscription_id.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.cpp create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session_config.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/main.cpp create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/channel.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_book_level.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument_metadata.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_message_header.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription_payload.h rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/messages/private_order_qty.h (100%) create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/top_of_book.h rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/messages/trade_summary.h (56%) create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_message.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_payload.h create mode 100644 src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_ticker_request.h rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/sample_req.json (100%) rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/ssl_certificate_verifier.cpp (100%) rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/ssl_certificate_verifier.h (100%) rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/tls_initializer.cpp (95%) rename src/{app_bts-websocket-listener/bts-websocket-listener => app_bts-cme-websocket-listener/bts-cme-websocket-listener}/tls_initializer.h (100%) rename src/{app_bts-websocket-listener => app_bts-cme-websocket-listener}/cme_websocket_config.json (57%) delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.cpp delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/cme_oauth_config.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_config.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.cpp delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/main.cpp delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/messages/message_header.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_message.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_payload.h delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.cpp delete mode 100644 src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.h create mode 100644 src/data_files/files/cme_subscription_status.json delete mode 100644 src/lib_bts-http-client/CMakeLists.txt rename src/{lib_bts-http-client/bts-http-client => lib_bts-http/bts-http}/curl_requestor.h (100%) rename src/{lib_bts-http-client/bts-http-client => lib_bts-http/bts-http}/http_client.cpp (91%) rename src/{lib_bts-http-client/bts-http-client => lib_bts-http/bts-http}/http_client.h (66%) create mode 100644 src/lib_bts-http/bts-http/oauth_request.cpp create mode 100644 src/lib_bts-http/bts-http/oauth_request.h create mode 100644 src/lib_bts-http/bts-http/oauth_requestor.h delete mode 100644 src/lib_bts-json/CMakeLists.txt delete mode 100644 src/lib_bts-json/bts-json/JsonReader.cpp delete mode 100644 src/lib_bts-system/bts-system/TryCatch.h create mode 100644 src/lib_bts-system/bts-system/delegate.h rename src/lib_bts-system/bts-system/{EnumMacros.h => enum_macros.h} (100%) rename src/{lib_bts-json/bts-json/JsonDumper.h => lib_bts-system/bts-system/json/json_dumper.h} (75%) create mode 100644 src/lib_bts-system/bts-system/json/json_reader.cpp rename src/{lib_bts-json/bts-json/JsonReader.h => lib_bts-system/bts-system/json/json_reader.h} (76%) create mode 100644 src/lib_bts-system/bts-system/overloaded.h create mode 100644 src/lib_bts-system/bts-system/threading/thread_config.h create mode 100644 src/lib_bts-system/bts-system/threading/thread_manager.cpp create mode 100644 src/lib_bts-system/bts-system/threading/thread_manager.h create mode 100644 src/lib_bts-system/bts-system/value_wrapper.h create mode 100644 src/lib_bts-websocket/CMakeLists.txt create mode 100644 src/lib_bts-websocket/bts-websocket/websocket_access_token.h create mode 100644 src/lib_bts-websocket/bts-websocket/websocket_authorization.h create mode 100644 src/lib_bts-websocket/bts-websocket/websocket_authorization_type.h create mode 100644 src/lib_bts-websocket/bts-websocket/websocket_client.cpp create mode 100644 src/lib_bts-websocket/bts-websocket/websocket_client.h create mode 100644 src/lib_bts-websocket/bts-websocket/websocket_client_config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b89f046c4..7feb72d8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,10 +113,9 @@ else() # subdirs if (BUILD_SPARKD) add_subdirectory(src/lib_bts-system) - add_subdirectory(src/lib_bts-json) add_subdirectory(src/lib_bts-http) - add_subdirectory(src/lib_bts-http-client) - add_subdirectory(src/app_bts-websocket-listener) + add_subdirectory(src/lib_bts-websocket) + add_subdirectory(src/app_bts-cme-websocket-listener) add_subdirectory(src/server/sparkd) add_subdirectory(src/data_files) endif() diff --git a/src/app_bts-websocket-listener/CMakeLists.txt b/src/app_bts-cme-websocket-listener/CMakeLists.txt similarity index 52% rename from src/app_bts-websocket-listener/CMakeLists.txt rename to src/app_bts-cme-websocket-listener/CMakeLists.txt index cbb82d19e..bdc7646dd 100644 --- a/src/app_bts-websocket-listener/CMakeLists.txt +++ b/src/app_bts-cme-websocket-listener/CMakeLists.txt @@ -1,18 +1,5 @@ cmake_minimum_required(VERSION 3.23) -project(bts-websocket-listener) - -FetchContent_MakeAvailable(curlpp) -FetchContent_GetProperties( - curlpp - SOURCE_DIR CURLPP_SOURCE_DIR - BINARY_DIR CURLPP_BINARY_DIR) - -message("CMAKE_BUILD_RPATH before: ${CMAKE_BUILD_RPATH}") -message("CMAKE_INSTALL_RPATH before: ${CMAKE_INSTALL_RPATH}") -list(APPEND CMAKE_BUILD_RPATH ${curlpp_BINARY_DIR}) -# set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH};${curlpp_BINARY_DIR}") -message("CMAKE_BUILD_RPATH after : ${CMAKE_BUILD_RPATH}") -message("CMAKE_INSTALL_RPATH after : ${CMAKE_INSTALL_RPATH}") +project(bts-cme-websocket-listener) file( GLOB_RECURSE ${PROJECT_NAME}_Sources @@ -20,9 +7,7 @@ file( ${PROJECT_NAME}/*.cpp) add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http-client curl) - -target_link_libraries(${PROJECT_NAME} PUBLIC crypto curlpp ssl) +target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http bts-websocket) # # cli11 # FetchContent_Declare( diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message.h new file mode 100644 index 000000000..684605be8 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message.h @@ -0,0 +1,30 @@ +#pragma once + +#include "bts-cme-websocket-listener/messages/cme_message_header.h" +#include +#include + +namespace bts::websocket { + +template +class CmeMessageT { + public: + CmeMessageHeader header; + Payload payload; +}; + +using CmeMessage = CmeMessageT; + +template +inline void to_json(nlohmann::json &j, const CmeMessageT &msg) { + j["header"] = msg.header; + j["payload"] = msg.payload; +} + +template +inline void from_json(const nlohmann::json &j, CmeMessageT &msg) { + j.at("header").get_to(msg.header); + j.at("payload").get_to(msg.payload); +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.cpp b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.cpp new file mode 100644 index 000000000..6e4ebc10c --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.cpp @@ -0,0 +1,91 @@ +#include "cme_message_demuxer.h" +#include "bts-cme-websocket-listener/cme_message.h" +#include "bts-cme-websocket-listener/cme_message_handler.h" +#include "bts-cme-websocket-listener/messages/cme_message_header.h" +#include "bts-cme-websocket-listener/messages/top_of_book.h" +#include "bts-cme-websocket-listener/messages/trade_summary_payload.h" +#include "bts-system/json/json_dumper.h" +#include "bts-system/json/json_reader.h" + +#include +#include +#include +#include +#include + +namespace bts::websocket { + +using nlohmann::json; +using std::string; +using std::vector; + +#define _ASSERT_MSGF(condition, ...) \ + bts_assert_msgf(condition, "CmeMessageDemuxer::" __VA_ARGS__) +#define _LOG(level, ...) LOG(md, level, "CmeMessageDemuxer::" __VA_ARGS__) +#define _DIAG(...) _LOG(Diag, __VA_ARGS__) +#define _INFO(...) _LOG(Info, __VA_ARGS__) +#define _WARN(...) _LOG(Warn, __VA_ARGS__) +#define _ERROR(...) _LOG(Error, __VA_ARGS__) + +struct CmeMessageDemuxer_Handler { + vector queue; + CmeMessageDemuxer::Callback callback; + CmeMessageHandler *handler; + + void flush() { + if (!queue.empty()) { + callback(queue); + queue.clear(); + } + } +}; + +struct CmeMessageDemuxer::PImpl { + std::unordered_map handlers; +}; + +CmeMessageDemuxer::CmeMessageDemuxer() : me_(std::make_unique()) {} +CmeMessageDemuxer::~CmeMessageDemuxer() = default; + +CmeMessageDemuxer::Callback &CmeMessageDemuxer::add_handler( + const std::string &msg_type) { + auto [it, is_new] = me_->handlers.try_emplace(msg_type); + if (!is_new) { + throw std::runtime_error{fmt::format( + "add_handler. Already added handler for message type '{}'", + msg_type)}; + } + return it->second.callback; +} + +void CmeMessageDemuxer::remove_handler(const string &msg_type) { + me_->handlers.erase(msg_type); +} + +void CmeMessageDemuxer::add_handler(const string &msg_type, CmeMessageHandler* handler) { + auto [it, is_new] = me_->handlers.try_emplace(msg_type); + if (!is_new) { + throw std::runtime_error{fmt::format( + "add_handler. Already added handler for message type '{}'", + msg_type)}; + } + it->second.handler = handler; +} + +void CmeMessageDemuxer::on_message(const string &msg_str) { + _INFO("on_message\n{}", msg_str); + CmeMessage msg; + try { + nlohmann::json::parse(msg_str).get_to(msg); + } catch (const std::exception &ex) { + _ERROR("on_message. Failed to parse json. {}\n{}", ex.what(), msg_str); + return; + } + // Pass msg to handler for this msg type + const auto hnd = me_->handlers.find(msg.header.messageType); + if (hnd != me_->handlers.end()) { + hnd->second.queue.push_back(msg); + } +} + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.h new file mode 100644 index 000000000..fe0080282 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_demuxer.h @@ -0,0 +1,45 @@ +#pragma once + +#include "bts-cme-websocket-listener/cme_message.h" +#include "bts-system/delegate.h" + +#include +#include +#include +#include + +namespace bts::websocket { + +class CmeMessageHandler; + +class CmeMessageDemuxer { + public: + CmeMessageDemuxer(); + ~CmeMessageDemuxer(); + + using Callback = + bts::system::Delegate &)>; + + void on_message(const std::string &msg); + + /** + * Adds a handler for the specified \p msg_type + * + * @return A bindable delegate, which will be invoked with messages of the + * specified \p msg_type + */ + Callback &add_handler(const std::string &msg_type); + + void add_handler(const std::string &msg_type, CmeMessageHandler *handler); + + /** + * @brief Removes the handler for the specified \p msg_type + */ + void remove_handler(const std::string &msg_type); + + private: + struct PImpl; + std::unique_ptr me_; +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_handler.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_handler.h new file mode 100644 index 000000000..a73c604ab --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_handler.h @@ -0,0 +1,22 @@ +#pragma once + +#include "bts-cme-websocket-listener/cme_message.h" +#include "bts-cme-websocket-listener/messages/top_of_book.h" +#include "bts-cme-websocket-listener/messages/trade_summary_message.h" + +#include + +namespace bts::websocket { + +class CmeMessageHandler { + public: + virtual void on_messages(const std::vector &payload); +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void CmeMessageHandler::on_messages(const std::vector &) {} + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_logger.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_logger.h new file mode 100644 index 000000000..b90e4a8c4 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_message_logger.h @@ -0,0 +1,20 @@ +#pragma once + +#include "bts-cme-websocket-listener/cme_message.h" +#include "bts-cme-websocket-listener/cme_message_handler.h" +#include "bts-system/json/json_dumper.h" + +#include +#include + +namespace bts::websocket { + +struct CmeMessageLogger : public bts::websocket::CmeMessageHandler { + void on_messages(const std::vector &msgs) override { + nlohmann::json wrapped = msgs; + LOG(Md, Info, "CmeMessageLogger::on_messages.\n{}", + bts::system::json::JsonDumper::dump(wrapped)); + } +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_subscription_id.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_subscription_id.h new file mode 100644 index 000000000..95e0e275f --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_subscription_id.h @@ -0,0 +1,10 @@ +#pragma once + +#include "bts-system/value_wrapper.h" + +namespace bts::websocket { + +using CmeSubscriptionId = + system::ValueWrapper; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.cpp b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.cpp new file mode 100644 index 000000000..85dc0b19f --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.cpp @@ -0,0 +1,210 @@ +#include "cme_websocket_session.h" +#include "bts-cme-websocket-listener/cme_message.h" +#include "bts-cme-websocket-listener/cme_message_demuxer.h" +#include "bts-cme-websocket-listener/cme_message_handler.h" +#include "bts-cme-websocket-listener/cme_subscription_id.h" +#include "bts-cme-websocket-listener/cme_websocket_session_config.h" +#include "bts-cme-websocket-listener/messages/channel.h" +#include "bts-cme-websocket-listener/messages/cme_message_header.h" +#include "bts-cme-websocket-listener/messages/cme_subscription.h" +#include "bts-cme-websocket-listener/messages/cme_subscription_payload.h" +#include "bts-cme-websocket-listener/messages/top_of_book.h" +#include "bts-cme-websocket-listener/messages/trade_summary_message.h" +#include "bts-cme-websocket-listener/messages/trade_summary_payload.h" +#include "bts-cme-websocket-listener/ssl_certificate_verifier.h" +#include "bts-cme-websocket-listener/tls_initializer.h" +#include "bts-http/QueryParams.h" +#include "bts-http/URL.h" +#include "bts-http/curl_requestor.h" +#include "bts-http/http_client.h" +#include "bts-http/http_request.h" +#include "bts-http/http_response.h" +#include "bts-http/oauth_request.h" +#include "bts-http/oauth_requestor.h" +#include "bts-http/oauth_response.h" +#include "bts-system/TimeSpan.h" +#include "bts-system/TimeStamp.h" +#include "bts-system/UNUSED.h" +#include "bts-system/bts_assert.h" +#include "bts-system/json/json_dumper.h" +#include "bts-system/json/json_reader.h" +#include "bts-system/overloaded.h" +#include "bts-websocket/websocket_client.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::websocket { + +using namespace std::chrono_literals; + +using http::OauthRequestor; +using nlohmann::json; +using std::optional; +using std::string; +using std::unordered_map; +using std::vector; +using system::json::JsonDumper; +using system::json::JsonReader; +using websocketpp::config::asio_tls_client; + +// using WebsocketClient = +// websocketpp::client; +// using ConnectionHdl = websocketpp::connection_hdl; +// using ConnectionPtr = websocketpp::client::connection_ptr; +using MessageTypePtr = websocketpp::config::core_client::message_type::ptr; +using SslContext = boost::asio::ssl::context; + +// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1 + +#define _ASSERT_MSGF(condition, ...) \ + bts_assert_msgf(condition, "CmeMessageDemuxer::" __VA_ARGS__) +#define _LOG(level, ...) LOG(md, level, "CmeMessageDemuxer::" __VA_ARGS__) +#define _DIAG(...) _LOG(Diag, __VA_ARGS__) +#define _INFO(...) _LOG(Info, __VA_ARGS__) +#define _WARN(...) _LOG(Warn, __VA_ARGS__) +#define _ERROR(...) _LOG(Error, __VA_ARGS__) + +struct CmeWebsocketSession::PImpl { + PImpl(const CmeWebsocketSessionConfig &cfg) + : cfg(cfg), client(cfg.websocket) {} + + CmeWebsocketSessionConfig cfg; + WebsocketClient client; + CmeMessageDemuxer demuxer; + CmeSubscriptionId sub_id_counter{0}; + string server_name; + std::optional run_thread; + int session_id{session_id_counter++}; + + bool client_is_open = false; + + ~PImpl() { + if (run_thread) { + run_thread->join(); + } + } + + void on_login_status(const vector &msgs) { + // If there is an error, log it and return. + if (msgs.size() != 1) { + _ERROR( + "on_login_status. Expected exactly 1 message in json payload:\n{}", + JsonDumper::dump(json{msgs})); + return; + } + auto &msg = msgs.at(0); + _INFO("on_login_status:\n{}", JsonDumper::dump(msg)); + if (msg.payload.at("status").get() != "authenticated") { + _ERROR( + "on_login_status. status not authenticated:\n{}", + JsonDumper::dump(json{msgs})); + return; + } + } + + void on_subscription_status(const vector &msgs) { + // If there is an error, log it and return. +#define __ERROR \ + _ERROR( \ + "on_subscription_status. Unexpected response:\n{}", \ + JsonDumper::dump(json{msgs})); \ + return + + if (msgs.size() != 1) { + __ERROR; + } + auto &msg = msgs.at(0); + _INFO("on_subscription_status:\n{}", JsonDumper::dump(msg)); + JSON_READ_ELSE(msg, payload, json, __ERROR); + JSON_READ_ELSE(payload, status, string, __ERROR); + if (status != "authenticated") { + __ERROR; + } +#undef __ERROR + } + + void on_ts(const vector &msgs) { + // If there is an error, log it and return. +#define __ERROR \ + _ERROR("on_ts. Unexpected response:\n{}", JsonDumper::dump(json{msgs})); \ + return + + if (msgs.size() != 1) { + __ERROR; + } + auto &msg = msgs.at(0); + _INFO("on_ts:\n{}", JsonDumper::dump(msg)); + // JSON_READ_ELSE(msg, payload, json, __ERROR); + // JSON_READ_ELSE(payload, status, string, __ERROR); + // if (status != "authenticated") { + // __ERROR; + // } +#undef __ERROR + } + + void on_message(const string &payload) { demuxer.on_message(payload); } + + private: + static std::atomic_int session_id_counter; +}; + +CmeWebsocketSession::CmeWebsocketSession(CmeWebsocketSessionConfig cfg) + : me_(std::make_unique(std::move(cfg))) { + // Incoming messages are demuxed per 'messageType' (a json field assumed to + // be present in every message). + + // Bind the demuxer directly to the websocket client (so it will receive raw + // messages from the server) + me_->client.message_handler().bind<&CmeMessageDemuxer::on_message>( + me_->demuxer); + + // Bind 'messageType' handlers to the demuxer + me_->demuxer.add_handler("loginStatus").bind<&PImpl::on_login_status>(*me_); + me_->demuxer.add_handler("subscriptionStatus") + .bind<&PImpl::on_login_status>(*me_); + me_->demuxer.add_handler("TS").bind<&PImpl::on_ts>(*me_); +} + +CmeWebsocketSession::~CmeWebsocketSession() = default; + +void CmeWebsocketSession::start() { me_->client.connect(); } + +void CmeWebsocketSession::subscribe( + const std::vector &channels, + const std::vector &data_types, + CmeMessageHandler *handler) { + CmeSubscriptionId sub_id{(*me_->sub_id_counter)++}; + CmeSubscription sub{ + CmeMessageHeader{"subscribe", std::to_string(sub_id.value())}, + CmeSubscriptionPayload{data_types, channels}}; + for (const auto &dt : data_types) { + me_->demuxer.add_handler(dt).bind<&CmeMessageHandler::on_messages>( + *handler); + } + me_->client.send_text(JsonDumper::dump(sub)); +} + +std::atomic_int CmeWebsocketSession::PImpl::session_id_counter = 0; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.h new file mode 100644 index 000000000..5f4928dbe --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session.h @@ -0,0 +1,49 @@ +#pragma once +#include "bts-cme-websocket-listener/cme_message_handler.h" +#include "bts-cme-websocket-listener/cme_subscription_id.h" +#include "bts-cme-websocket-listener/messages/channel.h" +#include "bts-http/URL.h" +#include "bts-system/delegate.h" +#include "bts-system/value_wrapper.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::websocket { + +class CmeOauthConfig; +class CmeWebsocketSessionConfig; + +class CmeWebsocketSession { + public: + explicit CmeWebsocketSession(CmeWebsocketSessionConfig cfg); + ~CmeWebsocketSession(); + + void start(); + + /** + * @brief Subscribes to the specified channel specified \p channel + * @return A bindable delegate, which will be invoked with data associated + * with the specified \p channel + */ + void subscribe( + const std::vector &channels, + const std::vector &data_types, + CmeMessageHandler *handler); + + private: + struct PImpl; + std::unique_ptr me_; +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session_config.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session_config.h new file mode 100644 index 000000000..128185cc1 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/cme_websocket_session_config.h @@ -0,0 +1,38 @@ +#pragma once + +#include "bts-cme-websocket-listener/messages/cme_subscription_payload.h" +#include "bts-http/URL.h" +#include "bts-http/oauth_request.h" +#include "bts-system/json/json_reader.h" +#include "bts-system/threading/thread_config.h" +#include "bts-websocket/websocket_client_config.h" + +#include +#include +#include + +namespace bts::websocket { + +class CmeWebsocketSessionConfig { + public: + // Needed to obtain the access token (ignored if access token is non-empty) + websocket::WebsocketClientConfig websocket; + std::vector subscription_payloads; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void to_json(nlohmann::json &j, const CmeWebsocketSessionConfig &cfg) { + j["websocket"] = cfg.websocket; + j["subscription_payloads"] = cfg.subscription_payloads; +} + +inline void from_json(const nlohmann::json &j, CmeWebsocketSessionConfig &cfg) { + system::json::JsonReader jr{&j}; + jr.read("websocket", cfg.websocket); + jr.read("subscription_payloads", cfg.subscription_payloads); +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/main.cpp b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/main.cpp new file mode 100644 index 000000000..fd910711a --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/main.cpp @@ -0,0 +1,66 @@ +#include "bts-cme-websocket-listener/cme_message_handler.h" +#include "bts-cme-websocket-listener/cme_websocket_session.h" +#include "bts-cme-websocket-listener/cme_websocket_session_config.h" +#include "bts-cme-websocket-listener/messages/channel.h" +#include "bts-system/UNUSED.h" +#include "bts-system/json/json_dumper.h" +#include "bts-system/json/json_reader.h" +#include "bts-websocket/websocket_client.h" +#include "bts-cme-websocket-listener/cme_message_logger.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using bts::system::json::JsonReader; +using bts::websocket::CmeWebsocketSessionConfig; +using CLI::App; +using std::ifstream; +using std::string; + +static CmeWebsocketSessionConfig parse_config(const string &config_arg) { + ifstream cfg_ifs{config_arg}; + const auto cfg_j = nlohmann::json::parse(cfg_ifs); + JsonReader cfg_jr{&cfg_j}; + CmeWebsocketSessionConfig cfg; + bts_assert(cfg_jr.read(cfg)); + cfg.websocket.timeout = bts::core::TimeSpan::from_seconds(20); + return cfg; +} + +static void run(const std::string &config_arg) { + const auto cfg = parse_config(config_arg); + + struct CurlInit { + CurlInit() { curlpp::initialize(); } + ~CurlInit() { curlpp::terminate(); } + } _CURL_INIT; + + bts::websocket::CmeMessageLogger cme_handler; + bts::websocket::CmeWebsocketSession session{cfg}; + session.start(); + session.subscribe( + std::vector{{"XCME", "FUT", "BTC"}}, + std::vector{"TRD"}, &cme_handler); + static const int sleep_secs = 3; + while (true) { + std::this_thread::sleep_for(std::chrono::seconds(sleep_secs)); + } +} + +int main(int argc, char **argv) { + App app{"Cme websocket listener", "bts-websocket-listener"}; + string config_arg; + app.add_option( + "--config", config_arg, "Path to CmeWebsocketConfig json file") + ->required(); + app.callback([&config_arg]() { run(config_arg); }); + CLI11_PARSE(app, argc, argv); +} diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/channel.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/channel.h new file mode 100644 index 000000000..9d34d1f0e --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/channel.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +namespace bts::websocket { + +class Channel { + public: + std::string exchangeMic; + std::string productType; + std::string productCode; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + Channel, exchangeMic, productType, productCode); + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_book_level.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_book_level.h new file mode 100644 index 000000000..47767a644 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_book_level.h @@ -0,0 +1,33 @@ +#pragma once + +#include "bts-system/TimeStamp.h" + +#include +#include + +namespace bts::websocket { + +class CmeBookLevel { + public: + core::TimeStamp lastUpdateTime; + std::uint32_t orderCnt; + double price; + double qty; +}; + +inline void to_json(nlohmann ::json &j, const CmeBookLevel &l) { + j["lastUpdateTime"] = l.lastUpdateTime.to_iso_string(); + j["orderCnt"] = l.orderCnt; + j["price"] = l.price; + j["qty"] = l.qty; +} + +inline void from_json(const nlohmann ::json &j, CmeBookLevel &l) { + l.lastUpdateTime = core::TimeStamp::from_iso_string( + j.at("lastUpdateTime").get()); + j.at("orderCnt").get_to(l.orderCnt); + j.at("price").get_to(l.price); + j.at("qty").get_to(l.qty); +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument.h new file mode 100644 index 000000000..2d37a9e59 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument.h @@ -0,0 +1,90 @@ +#pragma once + +#include "bts-system/TimeStamp.h" +#include +#include +#include + +namespace bts::websocket { + +class CmeInstrument { + public: + std::string guid; + std::int64_t guidInt; + std::int64_t productGuidInt; + std::string instrumentName; + std::optional globexSecurityId; + std::string globexSymbol; + core::TimeStamp positionRemovalDate; + core::TimeStamp lastUpdated; + core::TimeStamp lastTradeDate; + std::optional lastNoticeDate; + core::TimeStamp lastDeliveryDate; + std::optional globexLastTradeDate; + std::optional globexFirstTradeDate; + core::TimeStamp firstTradeDate; + std::optional firstNoticeDate; + core::TimeStamp firstDeliveryDate; + core::TimeStamp finalSettlementDate; + std::optional initialInventoryDueDate; + std::optional lastInventoryDueDate; + std::optional lastEfpDate; + std::optional strikePxCcy; + std::optional putCallIndicator; + std::optional priceBandD1; + std::optional priceBand; + std::optional zeroPriceEligible; + std::optional originalContractSize; + core::TimeStamp contractMonth; + std::string flexIndicator; + // TODO: like 40 more fields :()! + template + static void write_opt( + const std::optional &dest, + nlohmann::json &j, + const std::string &field) { + if (dest) { + j.at(field) = *dest; + } + } + + template + static void read_opt( + nlohmann::json &j, const std::string &field, std::optional &dest) { + auto it = j.find(field); + if (it != j.end()) { + it->get_to(dest.emplace()); + } + } +}; + +inline void to_json(nlohmann ::json &j, const CmeInstrument &i) { + j["guid"] = i.guid; + j["guidInt"] = i.guidInt; + j["productGuidInt"] = i.productGuidInt; + j["instrumentName"] = i.instrumentName; + if (i.globexSecurityId) { + j["globexSecurityId"] = *i.globexSecurityId; + } + j["globexSymbol"] = i.globexSymbol; + if (i.positionRemovalDate) { + // TODO + // j["positionRemovalDate"] = {}; + } + j["lastUpdated"] = i.lastUpdated; + j["lastTradeDate"] = i.lastTradeDate; +} + +inline void from_json(const nlohmann ::json &j, CmeInstrument &i) { + j.at("guid").get_to(i.guid); + j.at("guidInt").get_to(i.guidInt); + j.at("productGuidInt").get_to(i.productGuidInt); + j.at("instrumentName").get_to(i.instrumentName); + j.at("globexSecurityId").get_to(i.globexSecurityId); + j.at("globexSymbol").get_to(i.globexSymbol); + j.at("positionRemovalDate").get_to(i.positionRemovalDate); + j.at("lastUpdated").get_to(i.lastUpdated); + j.at("lastTradeDate").get_to(i.lastTradeDate); +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument_metadata.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument_metadata.h new file mode 100644 index 000000000..1ebe6c520 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_instrument_metadata.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +namespace bts::websocket { + +class CmeInstrumentMetadata { + public: + std::string definitionSource; + std::string exchangeMic; + std::int64_t id; + std::uint32_t marketSegmentId; + std::uint32_t periodCode; + std::string productCode; + std::string productType; + std::string productGroup; + std::string symbol; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + CmeInstrumentMetadata, + definitionSource, + exchangeMic, + id, + marketSegmentId, + periodCode, + productCode, + productType, + productGroup, + symbol); + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_message_header.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_message_header.h new file mode 100644 index 000000000..bcb6f1169 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_message_header.h @@ -0,0 +1,53 @@ +#pragma once + +#include "bts-system/TimeStamp.h" +#include "bts-system/json/json_reader.h" +#include "bts-cme-websocket-listener/cme_subscription_id.h" + +#include +#include +#include +#include + +namespace bts::websocket { + +class CmeMessageHeader { + public: + std::string messageType{}; + std::optional requestId{}; + std::optional sentTime{}; + std::string token{}; + std::optional version{}; +}; + +inline void to_json(nlohmann ::json &j, const CmeMessageHeader &h) { + j["messageType"] = h.messageType; + if (h.sentTime) { + j["sentTime"] = h.sentTime->to_iso_string(); + } + if (h.version) { + j["version"] = *h.version; + } + if (h.requestId) { + j["requestId"] = *h.requestId; + } +} + +inline void from_json(const nlohmann ::json &j, CmeMessageHeader &h) { + j.at("messageType").get_to(h.messageType); + std::string sentTime; + h.sentTime = + system::json::JsonReader{&j}.read("sentTime", sentTime) + ? decltype(h.sentTime){core::TimeStamp::from_iso_string(sentTime)} + : std::nullopt; + std::string version; + h.version = system::json::JsonReader{&j}.read("version", version) + ? decltype(h.version){version} + : std::nullopt; + std::string requestId; + h.requestId = system::json::JsonReader{&j}.read("requestId", requestId) + ? decltype(h.requestId){requestId} + : std::nullopt; +}; + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription.h new file mode 100644 index 000000000..b06de83de --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription.h @@ -0,0 +1,17 @@ +#pragma once + +#include "bts-cme-websocket-listener/messages/cme_subscription_payload.h" +#include "bts-cme-websocket-listener/messages/cme_message_header.h" +#include + +namespace bts::websocket { + +class CmeSubscription { + public: + CmeMessageHeader header{}; + CmeSubscriptionPayload payload{}; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CmeSubscription, header, payload); + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription_payload.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription_payload.h new file mode 100644 index 000000000..3208b1a86 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/cme_subscription_payload.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include + +#include "bts-cme-websocket-listener/messages/channel.h" + +namespace bts::websocket { + +class CmeSubscriptionPayload { + public: + std::vector subscriptionMessageTypes; + std::vector subscriptions; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + CmeSubscriptionPayload, subscriptionMessageTypes, subscriptions); + +} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/messages/private_order_qty.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/private_order_qty.h similarity index 100% rename from src/app_bts-websocket-listener/bts-websocket-listener/messages/private_order_qty.h rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/private_order_qty.h diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/top_of_book.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/top_of_book.h new file mode 100644 index 000000000..f6d8ad8be --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/top_of_book.h @@ -0,0 +1,22 @@ +#pragma once + +#include "bts-cme-websocket-listener/messages/cme_book_level.h" +#include "bts-cme-websocket-listener/messages/cme_instrument_metadata.h" +#include +#include +#include + +namespace bts::websocket { + +class TopOfBookPayload { + public: + std::string tradingStatus; + CmeInstrumentMetadata instrument; + std::vector askLevel; + std::vector bidLevel; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + TopOfBookPayload, tradingStatus, instrument, askLevel, bidLevel); + +} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary.h similarity index 56% rename from src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary.h rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary.h index 90a7868e1..26a3356ea 100644 --- a/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary.h +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include @@ -17,7 +16,13 @@ class TradeSummary { std::string tradeUpdateAction; }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummary, aggressorSide, mdTradeEntryId, tradePrice, tradeQty, tradeOrderCount, - tradeUpdateAction) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + TradeSummary, + aggressorSide, + mdTradeEntryId, + tradePrice, + tradeQty, + tradeOrderCount, + tradeUpdateAction) -} // namespace bts::websocket +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_message.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_message.h new file mode 100644 index 000000000..567a8d0b0 --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_message.h @@ -0,0 +1,19 @@ +#pragma once + +#include "bts-cme-websocket-listener/messages/cme_message_header.h" +#include "bts-cme-websocket-listener/messages/trade_summary_payload.h" + +#include +#include + +namespace bts::websocket { + +class TradeSummaryMessage { + public: + CmeMessageHeader header; + TradeSummaryPayload payload; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryMessage, header, payload) + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_payload.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_payload.h new file mode 100644 index 000000000..08bbdf11f --- /dev/null +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_summary_payload.h @@ -0,0 +1,19 @@ +#pragma once + +#include "bts-cme-websocket-listener/messages/trade_summary.h" + +#include +#include + +namespace bts::websocket { + +class TradeSummaryPayload { + public: + std::int64_t lastUpdateTime; + TradeSummary tradeSummary; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + TradeSummaryPayload, lastUpdateTime, tradeSummary) + +} // namespace bts::websocket diff --git a/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_ticker_request.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/messages/trade_ticker_request.h new file mode 100644 index 000000000..e69de29bb diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/sample_req.json b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/sample_req.json similarity index 100% rename from src/app_bts-websocket-listener/bts-websocket-listener/sample_req.json rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/sample_req.json diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/ssl_certificate_verifier.cpp b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/ssl_certificate_verifier.cpp similarity index 100% rename from src/app_bts-websocket-listener/bts-websocket-listener/ssl_certificate_verifier.cpp rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/ssl_certificate_verifier.cpp diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/ssl_certificate_verifier.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/ssl_certificate_verifier.h similarity index 100% rename from src/app_bts-websocket-listener/bts-websocket-listener/ssl_certificate_verifier.h rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/ssl_certificate_verifier.h diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/tls_initializer.cpp b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/tls_initializer.cpp similarity index 95% rename from src/app_bts-websocket-listener/bts-websocket-listener/tls_initializer.cpp rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/tls_initializer.cpp index 57c681312..406a35a72 100644 --- a/src/app_bts-websocket-listener/bts-websocket-listener/tls_initializer.cpp +++ b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/tls_initializer.cpp @@ -1,5 +1,5 @@ #include "tls_initializer.h" -#include "bts-websocket-listener/ssl_certificate_verifier.h" +#include "bts-cme-websocket-listener/ssl_certificate_verifier.h" #include #include #include diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/tls_initializer.h b/src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/tls_initializer.h similarity index 100% rename from src/app_bts-websocket-listener/bts-websocket-listener/tls_initializer.h rename to src/app_bts-cme-websocket-listener/bts-cme-websocket-listener/tls_initializer.h diff --git a/src/app_bts-websocket-listener/cme_websocket_config.json b/src/app_bts-cme-websocket-listener/cme_websocket_config.json similarity index 57% rename from src/app_bts-websocket-listener/cme_websocket_config.json rename to src/app_bts-cme-websocket-listener/cme_websocket_config.json index 7a55fff48..a7c6c8511 100644 --- a/src/app_bts-websocket-listener/cme_websocket_config.json +++ b/src/app_bts-cme-websocket-listener/cme_websocket_config.json @@ -11,5 +11,19 @@ "endpoint": "as/token.oauth2", "client_id": "api_thinkalpha321", "client_secret": "4T39A4t_G#7erR$TYgrJ9hmT" - } + }, + "subscription_payloads": [ + { + "subscriptionMessageTypes": [ + "TRD" + ], + "subscriptions": [ + { + "exchangeMic": "XCME", + "productType": "FUT", + "productCode": "BTC" + } + ] + } + ] } diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.cpp b/src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.cpp deleted file mode 100644 index f2ec72934..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "cme_connection_metadata.h" diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.h b/src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.h deleted file mode 100644 index 3ce5f9f6c..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/cme_connection_metadata.h +++ /dev/null @@ -1,112 +0,0 @@ -#pragma once -#include "bts-system/logging.h" -#include "scratch_client.h" -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -typedef websocketpp::client client; - -namespace bts::websocket { - -class CmeConnectionMetadata { - public: - typedef websocketpp::lib::shared_ptr ptr; - - CmeConnectionMetadata( - int id, websocketpp::connection_hdl hdl, std::string uri) - : m_id(id), m_hdl(hdl), m_status("Connecting"), m_uri(uri), - m_server("N/A") {} - - void on_open(client *c, websocketpp::connection_hdl hdl) { - LOG(Md, Info, "CmeConnectionMetadata::on_open {}", std::string{"nice"}); - - std::string text{R"({ - "header": { - "messageType": "subscribe", - "sentTime": "", - "version": "", - "requestId": "1", - "token": "" - }, - "payload": { - "subscriptionMessageTypes": ["TRD"], - "subscriptions": [ - { - "exchangeMic": "XCME", - "productType": "FUT", - "productCode": "BTC" - } - ] - } - })"}; - m_status = "Open"; - std::cout << "SEND IT!!!!!!" << std::endl; - websocketpp::lib::error_code ec; - c->send(hdl, text, websocketpp::frame::opcode::value::TEXT, ec); - if (ec) { - std::cout << "DAMNIT!!!!" << std::endl; - } - client::connection_ptr con = c->get_con_from_hdl(hdl); - m_server = con->get_response_header("Server"); - } - - void on_fail(client *c, websocketpp::connection_hdl hdl) { - m_status = "Failed"; - - client::connection_ptr con = c->get_con_from_hdl(hdl); - m_server = con->get_response_header("Server"); - m_error_reason = con->get_ec().message(); - } - - void on_close(client *c, websocketpp::connection_hdl hdl) { - m_status = "Closed"; - client::connection_ptr con = c->get_con_from_hdl(hdl); - std::stringstream s; - s << "close code: " << con->get_remote_close_code() << " (" - << websocketpp::close::status::get_string(con->get_remote_close_code()) - << "), close reason: " << con->get_remote_close_reason(); - m_error_reason = s.str(); - } - - websocketpp::connection_hdl get_hdl() const { return m_hdl; } - - int get_id() const { return m_id; } - - std::string get_status() const { return m_status; } - - friend std::ostream &operator<<( - std::ostream &out, CmeConnectionMetadata const &data); - - private: - int m_id; - websocketpp::connection_hdl m_hdl; - std::string m_status; - std::string m_uri; - std::string m_server; - std::string m_error_reason; -}; - -inline std::ostream &operator<<( - std::ostream &out, CmeConnectionMetadata const &data) { - out << "> URI: " << data.m_uri << "\n" - << "> Status: " << data.m_status << "\n" - << "> Remote Server: " - << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n" - << "> Error/close reason: " - << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason); - - return out; -} - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/cme_oauth_config.h b/src/app_bts-websocket-listener/bts-websocket-listener/cme_oauth_config.h deleted file mode 100644 index 6dddadedc..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/cme_oauth_config.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "bts-http/URL.h" - -namespace bts::websocket { - -class CmeOauthConfig { - public: - std::string protocol{"https"}; - std::string host; - std::uint16_t port{443}; - std::string endpoint; - std::string client_id; - std::string client_secret; - - io::URL get_url() const; - void get_url(io::URL &dest) const; -}; - -////////////////////////////////////////////////// -// Definition -////////////////////////////////////////////////// - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( - CmeOauthConfig, protocol, host, port, endpoint, client_id, client_secret); - -inline io::URL CmeOauthConfig::get_url() const { - io::URL url; - get_url(url); - return url; -} - -inline void CmeOauthConfig::get_url(io::URL &dest) const { - dest.fill_host(host); - dest.fill_path(endpoint); - dest.fill_port(port); - dest.fill_protocol("https"); -} - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_config.h b/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_config.h deleted file mode 100644 index df84870c8..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_config.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include -#include - -#include "bts-http/URL.h" -#include "bts-websocket-listener/cme_oauth_config.h" - -namespace bts::websocket { - -class CmeWebsocketConfig { - public: - // Needed to obtain the access token (ignored if access token is non-empty) - CmeOauthConfig oauth; - std::string access_token; - std::string protocol{"wss"}; - std::string host; - std::uint16_t port{443}; - std::string endpoint; - - io::URL get_url() const; - void get_url(io::URL &dest) const; -}; - -////////////////////////////////////////////////// -// Definition -////////////////////////////////////////////////// - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( - CmeWebsocketConfig, oauth, access_token, protocol, host, port, endpoint); - -inline io::URL CmeWebsocketConfig::get_url() const { - io::URL url; - get_url(url); - return url; -} - -inline void CmeWebsocketConfig::get_url(io::URL &dest) const { - dest.fill_host(host); - dest.fill_path(endpoint); - dest.fill_port(port); - dest.fill_protocol(protocol); -} - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.cpp b/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.cpp deleted file mode 100644 index 8f3adf399..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.cpp +++ /dev/null @@ -1,257 +0,0 @@ -#include "cme_websocket_session.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bts-http-client/curl_requestor.h" -#include "bts-http-client/http_client.h" -#include "bts-http/QueryParams.h" -#include "bts-http/URL.h" -#include "bts-http/http_request.h" -#include "bts-http/http_response.h" -#include "bts-http/oauth_response.h" -#include "bts-json/JsonDumper.h" -#include "bts-json/JsonReader.h" -#include "bts-system/UNUSED.h" -#include "bts-system/bts_assert.h" -#include "bts-websocket-listener/cme_connection_metadata.h" -#include "bts-websocket-listener/cme_oauth_config.h" -#include "bts-websocket-listener/cme_websocket_config.h" -#include "bts-websocket-listener/ssl_certificate_verifier.h" -#include "bts-websocket-listener/tls_initializer.h" - -typedef websocketpp::client client; -typedef websocketpp::lib::shared_ptr - context_ptr; -typedef std::map con_list; - -using websocketpp::lib::bind; -using websocketpp::lib::placeholders::_1; -using websocketpp::lib::placeholders::_2; - -namespace bts::websocket { - -using namespace std::chrono_literals; -namespace beast = boost::beast; - -using bts::http::HTTPClient; -using bts::http::HTTPRequest; -using bts::http::HTTPResponse; -using std::string; - -// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1 -using tcp = boost::asio::ip::tcp; // from - -static HTTPRequest build_oauth_request(const CmeOauthConfig &cfg) { - HTTPRequest http_req; - http_req.url = cfg.get_url().to_string(); - io::QueryParams qp; - qp["grant_type"] = "client_credentials"; - qp["client_id"] = cfg.client_id; - qp["client_secret"] = cfg.client_secret; - http_req.body = qp.to_string(); - return http_req; -} - -static OauthResponse request_oauth_token(const CmeOauthConfig &cfg) { - HTTPRequest req = build_oauth_request(cfg); - LOG(Gen, Info, "Requesting oauth:\n{}", io::JsonDumper::dump(req)); - HTTPClient client; - HTTPResponse res = client.post(req); - LOG(Gen, Info, "Received oauth response:\n{}", io::JsonDumper::dump(res)); - const auto j = nlohmann::json::parse(res.body()); - return io::JsonReader{&j}.read(); -} - -struct CmeWebsocketSession::PImpl { - CmeWebsocketConfig cfg; - CmeConnectionMetadata::ptr conn_state; - client m_endpoint; - - bool client_is_open = false; - - void connect() { - const auto url = cfg.get_url(); - string path = url.path(); - if (path.starts_with('/')) { - path.erase(path.begin()); - } - const auto url_str = fmt::format("wss://{}/{}", url.host(), url.path()); - try { - // Set up verbose logging - m_endpoint.set_access_channels(websocketpp::log::alevel::all); - m_endpoint.clear_access_channels( - websocketpp::log::alevel::frame_payload); - m_endpoint.set_error_channels(websocketpp::log::elevel::all); - - // Set up handlers - m_endpoint.init_asio(); - m_endpoint.set_message_handler([&](auto hdl, auto msg) { - (void)hdl; - std::cout << "CmeWebsocketSession::on_message " << std::endl; - std::cout << msg->get_payload() << std::endl; - LOG(Md, Info, "CmeWebsocketSession::on_message {}", - msg->get_payload()); - }); - m_endpoint.set_open_handler([this](auto hdl) { - (void)(hdl); - LOG(gen, Info, "on_open {}", string{"yay!"}); - client_is_open = true; - }); - m_endpoint.set_tls_init_handler([](auto) { - auto result = - websocketpp::lib::make_shared( - boost::asio::ssl::context::sslv23_client); - - result->set_verify_mode(boost::asio::ssl::verify_none); - - return result; - }); - m_endpoint.set_close_handler([this](auto hdl) { - client_is_open = false; - auto con = m_endpoint.get_con_from_hdl(hdl); - std::cout << "close handler: " << con->get_ec().message() - << std::endl; - }); - - m_endpoint.set_fail_handler([this](auto hdl) { - auto con = m_endpoint.get_con_from_hdl(hdl); - std::cout << "fail handler: " << con->get_ec().message() - << std::endl; - }); - websocketpp::lib::error_code ec; - websocketpp::client< - websocketpp::config::asio_tls_client>::connection_ptr con = - m_endpoint.get_connection(url_str, ec); - if (ec) { - bts_assert_msgf( - false, "Failed to get_connection: {}", ec.message()); - } - - // Set authorization header - con->append_header("Authorization", "Bearer " + cfg.access_token); - m_endpoint.connect(con); - auto run_thread = std::thread{[this]() { m_endpoint.run(); }}; - while (!client_is_open) { - sleep(1); //TODO: use an mutex instead - } - int heartbeat_interval = 2000; //in ms: TODO extract from message - bool subscribed = false; - while (true) { - std::this_thread::sleep_for( - std::chrono::milliseconds(heartbeat_interval)); - if (subscribed) { - continue; - } - subscribed = true; - string text{R"({ -"header": { - "messageType": "subscribe", - "sentTime": "", - "version": "", - "requestId": "1", - "token": "" -}, -"payload": { - "subscriptionMessageTypes": ["TRD"], - "subscriptions": [ - { - "exchangeMic": "XCME", - "productType": "FUT", - "productCode": "BTC" - } - ] -} -})"}; - websocketpp::lib::error_code write_ec; - m_endpoint.send( - con, text, websocketpp::frame::opcode::value::TEXT, write_ec); - std::cout << "write_ec: " << write_ec.message() << std::endl; - // client.send( - // con, "{\"op\":1, \"d\":null}", - // websocketpp::frame::opcode::text); - } - run_thread.join(); - - // Connect - m_endpoint.connect(con); - m_endpoint.run(); - } catch (const websocketpp::exception &ex) { - LOG(Md, Error, "Failed to connect to cme websocket server: {}", - string{ex.what()}); - } - } - - void close(websocketpp::close::status::value code, std::string reason) { - websocketpp::lib::error_code ec; - LOG(Md, Warn, "Closing connection {}...", this->conn_state->get_id()); - m_endpoint.close(conn_state->get_hdl(), code, reason, ec); - if (ec) { - LOG(Md, Error, "Error initiating close: {}", ec.message()); - } - } -}; - -CmeWebsocketSession::CmeWebsocketSession(CmeWebsocketConfig cfg) - : me_(std::make_unique()) { - me_->cfg = std::move(cfg); - me_->connect(); - string text{R"({ - "header": { - "messageType": "subscribe", - "sentTime": "", - "version": "", - "requestId": "1", - "token": "" - }, - "payload": { - "subscriptionMessageTypes": ["TRD"], - "subscriptions": [ - { - "exchangeMic": "XCME", - "productType": "FUT", - "productCode": "BTC" - } - ] - } - })"}; - // BoostExample3::run( - // me_->cfg.host, std::to_string(me_->cfg.port), me_->cfg.endpoint, text, - // me_->cfg.access_token); -} - -CmeWebsocketSession::~CmeWebsocketSession() = default; - -CmeWebsocketSession CmeWebsocketSession::create(CmeWebsocketConfig cfg) { - std::cout << io::JsonDumper::dump(cfg) << std::endl; - // bts_assert(false); - if (cfg.access_token.empty()) { - auto oauth_res = request_oauth_token(cfg.oauth); - const auto oauth_res_str = io::JsonDumper::dump(oauth_res); - cfg.access_token = oauth_res.access_token; - if (cfg.access_token.empty()) { - throw std::runtime_error{"Failed to obtain oauth access token"}; - } - } - return CmeWebsocketSession{std::move(cfg)}; -} - -int CmeWebsocketSession::run(const CmeWebsocketConfig &cfg) { - auto session = create(cfg); - return 0; -} - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.h b/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.h deleted file mode 100644 index 84dba6f78..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/cme_websocket_session.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "bts-http/URL.h" -#include "bts-websocket-listener/cme_connection_metadata.h" - -namespace bts::websocket { - -class CmeOauthConfig; -class CmeWebsocketConfig; - -class CmeWebsocketSession { - public: - explicit CmeWebsocketSession(CmeWebsocketConfig cfg); - ~CmeWebsocketSession(); - - static CmeWebsocketSession create(CmeWebsocketConfig cfg); - - static int run(const CmeWebsocketConfig &cfg); - void connect(); - void close(); - CmeConnectionMetadata::ptr get_metadata() const; - - private: - struct PImpl; - std::unique_ptr me_; -}; - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/main.cpp b/src/app_bts-websocket-listener/bts-websocket-listener/main.cpp deleted file mode 100644 index 10e33e0d4..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/main.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include -#include "bts-json/JsonDumper.h" -#include "bts-json/JsonReader.h" -#include "bts-system/UNUSED.h" -#include "bts-websocket-listener/cme_websocket_config.h" -#include "bts-websocket-listener/cme_websocket_session.h" - -using bts::io::JsonReader; -using bts::websocket::CmeWebsocketConfig; -using bts::websocket::CmeWebsocketSession; -using CLI::App; -using std::ifstream; -using std::string; - -static CmeWebsocketConfig parse_config(const string &config_arg) { - ifstream cfg_ifs{config_arg}; - const auto cfg_j = nlohmann::json::parse(cfg_ifs); - JsonReader cfg_jr{&cfg_j}; - CmeWebsocketConfig cfg; - bts_assert(cfg_jr.read(cfg)); - return cfg; -} - -static void run(const std::string &config_arg) { - const auto cfg = parse_config(config_arg); - curlpp::initialize(); - CmeWebsocketSession::run(cfg); - curlpp::terminate(); -} - -int main(int argc, char **argv) { - App app{"Cme websocket listener", "bts-websocket-listener"}; - string config_arg; - app.add_option( - "--config", config_arg, "Path to CmeWebsocketConfig json file") - ->required(); - app.callback([&config_arg]() { run(config_arg); }); - CLI11_PARSE(app, argc, argv); -} diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/messages/message_header.h b/src/app_bts-websocket-listener/bts-websocket-listener/messages/message_header.h deleted file mode 100644 index 07edced67..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/messages/message_header.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "bts-system/TimeStamp.h" - -#include -#include - -namespace bts::websocket { - -class MessageHeader { - public: - const std::string messageType; - const std::optional sentTime; - const std::int64_t version; -}; - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_message.h b/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_message.h deleted file mode 100644 index 384a02651..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_message.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "bts-websocket-listener/messages/message_header.h" -#include "bts-websocket-listener/messages/trade_summary_payload.h" - -#include -#include -#include - -namespace bts::websocket { - -class TradeSummaryMessage { - public: - const MessageHeader header; - const TradeSummaryPayload payload; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryMessage, header, payload) - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_payload.h b/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_payload.h deleted file mode 100644 index 0a250dbe9..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/messages/trade_summary_payload.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "bts-websocket-listener/messages/trade_summary.h" - -#include -#include -#include - -namespace bts::websocket { - -class TradeSummaryPayload { - public: - const std::int64_t lastUpdateTime; - const TradeSummary tradeSummary; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryPayload, lastUpdateTime, tradeSummary) - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.cpp b/src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.cpp deleted file mode 100644 index 664a08244..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.cpp +++ /dev/null @@ -1,179 +0,0 @@ -#include "scratch_client.h" -#include "bts-websocket-listener/cme_connection_metadata.h" -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -// typedef websocketpp::client client; - -namespace bts::websocket { - -using connection_metadata = CmeConnectionMetadata; - -class websocket_endpoint { - public: - websocket_endpoint() : m_next_id(0) { - m_endpoint.clear_access_channels(websocketpp::log::alevel::all); - m_endpoint.clear_error_channels(websocketpp::log::elevel::all); - - m_endpoint.init_asio(); - m_endpoint.start_perpetual(); - - m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint)); - } - - ~websocket_endpoint() { - m_endpoint.stop_perpetual(); - - for (con_list::const_iterator it = m_connection_list.begin(); - it != m_connection_list.end(); ++it) { - if (it->second->get_status() != "Open") { - // Only close open connections - continue; - } - - std::cout << "> Closing connection " << it->second->get_id() - << std::endl; - - websocketpp::lib::error_code ec; - m_endpoint.close( - it->second->get_hdl(), websocketpp::close::status::going_away, "", - ec); - if (ec) { - std::cout << "> Error closing connection " << it->second->get_id() - << ": " << ec.message() << std::endl; - } - } - - m_thread->join(); - } - - int connect(std::string const &uri) { - websocketpp::lib::error_code ec; - - client::connection_ptr con = m_endpoint.get_connection(uri, ec); - - if (ec) { - std::cout << "> Connect initialization error: " << ec.message() - << std::endl; - return -1; - } - - int new_id = m_next_id++; - connection_metadata::ptr metadata_ptr( - new connection_metadata(new_id, con->get_handle(), uri)); - m_connection_list[new_id] = metadata_ptr; - - con->set_open_handler(websocketpp::lib::bind( - &connection_metadata::on_open, metadata_ptr, &m_endpoint, - websocketpp::lib::placeholders::_1)); - con->set_fail_handler(websocketpp::lib::bind( - &connection_metadata::on_fail, metadata_ptr, &m_endpoint, - websocketpp::lib::placeholders::_1)); - con->set_close_handler(websocketpp::lib::bind( - &connection_metadata::on_close, metadata_ptr, &m_endpoint, - websocketpp::lib::placeholders::_1)); - - m_endpoint.connect(con); - - return new_id; - } - - void close( - int id, websocketpp::close::status::value code, std::string reason) { - websocketpp::lib::error_code ec; - - con_list::iterator metadata_it = m_connection_list.find(id); - if (metadata_it == m_connection_list.end()) { - std::cout << "> No connection found with id " << id << std::endl; - return; - } - - m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec); - if (ec) { - std::cout << "> Error initiating close: " << ec.message() << std::endl; - } - } - - connection_metadata::ptr get_metadata(int id) const { - con_list::const_iterator metadata_it = m_connection_list.find(id); - if (metadata_it == m_connection_list.end()) { - return connection_metadata::ptr(); - } else { - return metadata_it->second; - } - } - - private: - typedef std::map con_list; - - client m_endpoint; - websocketpp::lib::shared_ptr m_thread; - - con_list m_connection_list; - int m_next_id; -}; - -int ScratchClient::run() { - bool done = false; - std::string input; - websocket_endpoint endpoint; - - while (!done) { - std::cout << "Enter Command: "; - std::getline(std::cin, input); - - if (input == "quit") { - done = true; - } else if (input == "help") { - std::cout << "\nCommand List:\n" - << "connect \n" - << "close [] " - "[]\n" - << "show \n" - << "help: Display this help text\n" - << "quit: Exit the program\n" - << std::endl; - } else if (input.substr(0, 7) == "connect") { - int id = endpoint.connect(input.substr(8)); - if (id != -1) { - std::cout << "> Created connection with id " << id << std::endl; - } - } else if (input.substr(0, 5) == "close") { - std::stringstream ss(input); - - std::string cmd; - int id; - int close_code = websocketpp::close::status::normal; - std::string reason; - - ss >> cmd >> id >> close_code; - std::getline(ss, reason); - - endpoint.close(id, close_code, reason); - } else if (input.substr(0, 4) == "show") { - int id = atoi(input.substr(5).c_str()); - - connection_metadata::ptr metadata = endpoint.get_metadata(id); - if (metadata) { - std::cout << *metadata << std::endl; - } else { - std::cout << "> Unknown connection id " << id << std::endl; - } - } else { - std::cout << "> Unrecognized Command" << std::endl; - } - } - - return 0; -} - -} // namespace bts::websocket diff --git a/src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.h b/src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.h deleted file mode 100644 index d35dd0342..000000000 --- a/src/app_bts-websocket-listener/bts-websocket-listener/scratch_client.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace bts::websocket { - -class ScratchClient { -public: - static int run(); -}; - -} // namespace bts::websocket diff --git a/src/data_files/files/cme_subscription_status.json b/src/data_files/files/cme_subscription_status.json new file mode 100644 index 000000000..aab4abc99 --- /dev/null +++ b/src/data_files/files/cme_subscription_status.json @@ -0,0 +1,21 @@ +{ + "header": { + "messageType": "subscriptionStatus", + "requestId": 0, + "sentTime": "2023-11-01T15:21:12.376000000Z", + "version": "1.0", + "sequenceNumber": "2" + }, + "payload": { + "status": "subscribed", + "subscriptionMessageTypes": [ + "TRD" + ], + "subscriptions": [ + { + "productType": "FUT", + "productCode": "BTC" + } + ] + } +} diff --git a/src/data_files/files/cme_websocket_config.json b/src/data_files/files/cme_websocket_config.json index 7a55fff48..2db76d7fe 100644 --- a/src/data_files/files/cme_websocket_config.json +++ b/src/data_files/files/cme_websocket_config.json @@ -1,15 +1,18 @@ { - "protocol": "wss", - "host": "markets.api.uat.cmegroup.com", - "port": 0, - "endpoint": "marketdatastream/v1", - "access_token": "", - "oauth": { - "protocol": "https", - "host": "authnr.cmegroup.com", - "port": 0, - "endpoint": "as/token.oauth2", - "client_id": "api_thinkalpha321", - "client_secret": "4T39A4t_G#7erR$TYgrJ9hmT" + "websocket": { + "url": "wss://markets.api.uat.cmegroup.com/marketdatastream/v1", + "auth": { + "oauth": { + "url": "https://authnr.cmegroup.com/as/token.oauth2", + "client_id": "api_thinkalpha321", + "client_secret": "4T39A4t_G#7erR$TYgrJ9hmT" + } + } + }, + "subscription_payloads": [], + "thread": { + "name_rx": "websocket_thread", + "exclude": false, + "cores": [1] } } diff --git a/src/lib_bts-http-client/CMakeLists.txt b/src/lib_bts-http-client/CMakeLists.txt deleted file mode 100644 index d2d335030..000000000 --- a/src/lib_bts-http-client/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cmake_minimum_required(VERSION 3.23) -project(bts-http-client) -include(FetchContent) -include (GNUInstallDirs) - -file( - GLOB_RECURSE ${PROJECT_NAME}_Sources - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - ${PROJECT_NAME}/*.cpp) -add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(${PROJECT_NAME} PUBLIC bts-http bts-json bts-system) - -# curlpp -FetchContent_Declare( - curlpp - GIT_REPOSITORY git@github.com:jpbarrette/curlpp.git - GIT_TAG v0.8.1 - SYSTEM - OVERRIDE_FIND_PACKAGE) -FetchContent_MakeAvailable(curlpp) -target_include_directories(${PROJECT_NAME} PUBLIC ${curlpp_SOURCE_DIR}/include) -target_link_directories(${PROJECT_NAME} PUBLIC ${curlpp_BINARY_DIR}) diff --git a/src/lib_bts-http/CMakeLists.txt b/src/lib_bts-http/CMakeLists.txt index 2483d0d31..12dc1a104 100644 --- a/src/lib_bts-http/CMakeLists.txt +++ b/src/lib_bts-http/CMakeLists.txt @@ -7,4 +7,15 @@ file( ${PROJECT_NAME}/*.cpp) add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(${PROJECT_NAME} PUBLIC bts-json bts-system) +target_link_libraries(${PROJECT_NAME} PUBLIC bts-system) + +# curlpp +FetchContent_Declare( + curlpp + GIT_REPOSITORY git@github.com:jpbarrette/curlpp.git + GIT_TAG v0.8.1 + SYSTEM + OVERRIDE_FIND_PACKAGE) +FetchContent_MakeAvailable(curlpp) +target_include_directories(${PROJECT_NAME} PUBLIC ${curlpp_SOURCE_DIR}/include) +target_link_directories(${PROJECT_NAME} PUBLIC ${curlpp_BINARY_DIR}) diff --git a/src/lib_bts-http-client/bts-http-client/curl_requestor.h b/src/lib_bts-http/bts-http/curl_requestor.h similarity index 100% rename from src/lib_bts-http-client/bts-http-client/curl_requestor.h rename to src/lib_bts-http/bts-http/curl_requestor.h diff --git a/src/lib_bts-http-client/bts-http-client/http_client.cpp b/src/lib_bts-http/bts-http/http_client.cpp similarity index 91% rename from src/lib_bts-http-client/bts-http-client/http_client.cpp rename to src/lib_bts-http/bts-http/http_client.cpp index 1cfa00a01..da6fb7d1b 100644 --- a/src/lib_bts-http-client/bts-http-client/http_client.cpp +++ b/src/lib_bts-http/bts-http/http_client.cpp @@ -2,7 +2,7 @@ #include -#include "bts-http-client/curl_requestor.h" +#include "bts-http/curl_requestor.h" #include "bts-http/http_request.h" #include "bts-http/http_response.h" diff --git a/src/lib_bts-http-client/bts-http-client/http_client.h b/src/lib_bts-http/bts-http/http_client.h similarity index 66% rename from src/lib_bts-http-client/bts-http-client/http_client.h rename to src/lib_bts-http/bts-http/http_client.h index cf0192578..94719d2a9 100644 --- a/src/lib_bts-http-client/bts-http-client/http_client.h +++ b/src/lib_bts-http/bts-http/http_client.h @@ -7,7 +7,14 @@ class HTTPResponse; class HTTPClient { public: + /** + * Performs an http GET request + */ HTTPResponse get(const HTTPRequest &request); + + /** + * Performs an http POST request + */ HTTPResponse post(const HTTPRequest &request); }; diff --git a/src/lib_bts-http/bts-http/oauth_request.cpp b/src/lib_bts-http/bts-http/oauth_request.cpp new file mode 100644 index 000000000..aeffa3c7d --- /dev/null +++ b/src/lib_bts-http/bts-http/oauth_request.cpp @@ -0,0 +1,18 @@ +#include "oauth_request.h" + +namespace bts::http { + +// std::string OauthRequest::perform() { +// switch (cfg.websocket.auth.type().value) { +// case WebsocketAuthorizationType::None: +// break; +// case WebsocketAuthorizationType::AccessToken: +// break; +// case WebsocketAuthorizationType::OauthRequest: +// break; +// default: +// break; +// } +// } + +} // namespace bts::http diff --git a/src/lib_bts-http/bts-http/oauth_request.h b/src/lib_bts-http/bts-http/oauth_request.h new file mode 100644 index 000000000..1e817b9ff --- /dev/null +++ b/src/lib_bts-http/bts-http/oauth_request.h @@ -0,0 +1,24 @@ +#pragma once + +#include "bts-http/URL.h" + +#include +#include +#include + +namespace bts::http { + +class OauthRequest { + public: + std::string url; + std::string client_id; + std::string client_secret; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(OauthRequest, url, client_id, client_secret); + +} // namespace bts::http diff --git a/src/lib_bts-http/bts-http/oauth_requestor.h b/src/lib_bts-http/bts-http/oauth_requestor.h new file mode 100644 index 000000000..3e5120659 --- /dev/null +++ b/src/lib_bts-http/bts-http/oauth_requestor.h @@ -0,0 +1,70 @@ +#pragma once + +#include "bts-http/http_client.h" +#include "bts-http/http_headers.h" +#include "bts-http/http_request.h" +#include "bts-http/http_response.h" +#include "bts-http/oauth_request.h" +#include "bts-http/oauth_response.h" +#include "bts-system/json/json_dumper.h" +#include "bts-system/json/json_reader.h" +#include "bts-system/logging.h" + +namespace bts::http { + +// Be sure to undef macros at end of file (this is a header) +#define _LOG(level, ...) LOG(md, level, "OauthRequestor::" __VA_ARGS__) +#define _INFO(...) _LOG(Info, __VA_ARGS__) +#define _ERROR(...) _LOG(Error, __VA_ARGS__) + +class OauthRequestor { + public: + /** + * Requests access token. + * @param cfg Configuration for the request + * @param require_success Whether or not to throw an exception if the + * request fails (i.e., access token is empty or undefined) + */ + static OauthResponse request( + const OauthRequest &cfg, bool require_success = true); +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline OauthResponse OauthRequestor::request( + const OauthRequest &cfg, bool require_success) { + using namespace bts::http; + using namespace bts::io; + _INFO("request. \n{}", system::json::JsonDumper::dump(cfg)); + HTTPRequest req; + req.url = cfg.url; + QueryParams qp; + qp["grant_type"] = "client_credentials"; + qp["client_id"] = cfg.client_id; + qp["client_secret"] = cfg.client_secret; + req.body = qp.to_string(); + // _INFO("CmeOauthRequestor::request. Sending\n{}", JsonDumper::dump(req)); + HTTPClient client; + HTTPResponse res = client.post(req); + // _INFO("CmeOauthRequestor::request. Received\n{}", JsonDumper::dump(res)); + const auto j = nlohmann::json::parse(res.body()); + auto oauth = system::json::JsonReader{&j}.read(); + if ((*oauth.access_token).empty()) { + // _ERROR("request. Request failed (access token was undefined or empty)"); + if (require_success) { + throw std::runtime_error{"Failed to obtain oauth access token"}; + } + } + return oauth; +} + +#undef _ASSERT_MSGF +#undef _LOG +#undef _DIAG +#undef _INFO +#undef _WARN +#undef _ERROR + +} // namespace bts::http diff --git a/src/lib_bts-http/bts-http/oauth_response.h b/src/lib_bts-http/bts-http/oauth_response.h index 2c238d2c3..828c41e6f 100644 --- a/src/lib_bts-http/bts-http/oauth_response.h +++ b/src/lib_bts-http/bts-http/oauth_response.h @@ -1,14 +1,15 @@ #pragma once +#include "bts-websocket/websocket_access_token.h" #include #include #include -namespace bts::websocket { +namespace bts::http { class OauthResponse { - public: - std::string access_token; + public: + websocket::WebsocketAccessToken access_token; std::string token_type; /** * The number of seconds until the token expires @@ -19,4 +20,4 @@ class OauthResponse { NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( OauthResponse, access_token, token_type, expires_in); -} // namespace bts::websocket +} // namespace bts::http diff --git a/src/lib_bts-json/CMakeLists.txt b/src/lib_bts-json/CMakeLists.txt deleted file mode 100644 index 90647c60e..000000000 --- a/src/lib_bts-json/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -cmake_minimum_required(VERSION 3.23) -project(bts-json) -include(FetchContent) - -file( - GLOB_RECURSE ${PROJECT_NAME}_Sources - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - ${PROJECT_NAME}/*.cpp) -add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) -target_link_libraries(${PROJECT_NAME} PUBLIC - bts-system - debug fmtd - optimized fmt) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - -# nlohmann json -FetchContent_Declare( - nlohmann_json - GIT_REPOSITORY git@github.com:nlohmann/json.git - GIT_TAG v3.11.1 - CMAKE_ARGS - -DJSON_BuildTests=OFF - -DJSON_SystemInclude=ON - SYSTEM - OVERRIDE_FIND_PACKAGE) -FetchContent_MakeAvailable(nlohmann_json) -find_package(nlohmann_json REQUIRED) -target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json) diff --git a/src/lib_bts-json/bts-json/JsonReader.cpp b/src/lib_bts-json/bts-json/JsonReader.cpp deleted file mode 100644 index bb54efc7b..000000000 --- a/src/lib_bts-json/bts-json/JsonReader.cpp +++ /dev/null @@ -1,519 +0,0 @@ -#include "JsonReader.h" - -#include -#include -#include -#include - -#include "bts-system/TryCatch.h" -#include "bts-system/datetime.h" - -namespace bts::io { - -using core::TimeStamp; -using nlohmann::json; -using std::int32_t; -using std::int64_t; -using std::numeric_limits; -using std::stoull; -using std::string; -using std::uint32_t; -using std::uint64_t; - -// 'read' fn bodies should be wrapped between BEGIN/END_READ_JSON tags. -// These macros abstract out repetative exception handling code (and a nullptr check). - -#define BEGIN_READ_JSON \ - if (json_ == nullptr) { \ - return false; \ - } \ - try - -#define END_READ_JSON(type_name) \ - catch (const std::exception &e) { \ - LOG(Gen, Diag, "Failed to parse json to '{}'. Exception: {}", \ - std::string{type_name}, std::string{e.what()}); \ - } \ - catch (...) { \ - bts_assert_msgf( \ - false, "Failed to parse json to '{}'. Exception: unknown", \ - std::string{type_name}); \ - } \ - return false; - -// The idea here is we parse numbers to wide types (either int64, uint64, or double), -// then cast that value to the (potentially narrower) output type. This macro evaluates -// to true if 'wide_val' is within the bounds of the result type (otherwise false). -#define NUM_WITHIN_BOUNDS(wide_val, result_t) \ - (wide_val >= \ - static_cast(numeric_limits::min()) && \ - wide_val <= \ - static_cast(numeric_limits::max())) - -static const nlohmann::json *get_field_json( - const nlohmann::json *json, const std::string &name) { - if (!json || !json->is_object()) { - return nullptr; - } - auto it = json->find(name); - if (it == json->end()) { - return nullptr; - } - return &it.value(); -} - -JsonReader::JsonReader(const nlohmann::json *json) : json_(json) {} - -JsonReader::JsonReader(const nlohmann::json *json, const std::string &name) - : json_(get_field_json(json, name)) {} - -bool JsonReader::try_parse( - const std::string &str, nlohmann::json &result, std::string *err) { - try { - result = nlohmann::json::parse(str); - return true; - } - CATCH_AND_HANDLE_MSG([err](const auto &msg) { - if (err != nullptr) { - *err = msg; - } - }) - return false; -} - -bool JsonReader::read(const string &name, string &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, int32_t &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, uint32_t &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, int64_t &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, uint64_t &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, float &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, double &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(const string &name, TimeStamp &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read_epoch_nanos(const string &name, TimeStamp &value) const { - return JsonReader{json_, name}.read_epoch_nanos(value); -} - -bool JsonReader::read_epoch_micros(const string &name, TimeStamp &value) const { - return JsonReader{json_, name}.read_epoch_micros(value); -} - -bool JsonReader::read_epoch_millis(const string &name, TimeStamp &value) const { - return JsonReader{json_, name}.read_epoch_millis(value); -} - -bool JsonReader::read(const string &name, nlohmann::json &result) const { - return JsonReader{json_, name}.read(result); -} - -bool JsonReader::read(int32_t &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::number_float: { - const auto d = json_->get(); - result = static_cast(d); - if (result != d) { - LOG(Gen, Warn, - "Potential loss of data converting field of type " - "'number_float' ({}) to int32 ({})", - d, result); - } - return true; - } - case nlohmann::json::value_t::number_integer: { - const auto il = json_->get(); - if (!NUM_WITHIN_BOUNDS(il, int32_t)) { - LOG(Gen, Error, - "Failed to convert field of type 'number_integer' ({}) to " - "uint32: value is negative.", - il); - } - result = static_cast(il); - return true; - } - case nlohmann::json::value_t::number_unsigned: { - const auto ul = json_->get(); - if (ul > static_cast(numeric_limits::max())) { - LOG(Gen, Error, - "Failed to convert field of type 'number_unsigned' ({}) to " - "int32: value is too large.", - ul); - return false; - } - result = static_cast(ul); - return true; - } - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - if (!str.empty()) { - result = std::stoi(str); - return true; - } - break; - } - default: - return false; - } - } - END_READ_JSON("int32") -} - -bool JsonReader::read(uint32_t &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::number_float: { - const auto d = json_->get(); - result = static_cast(d); - if (result != d) { - LOG(Gen, Warn, - "Potential loss of data converting field of type " - "'number_float' ({}) to uint32 ({})", - d, result); - } - return true; - } - case nlohmann::json::value_t::number_integer: { - const auto i = json_->get(); - if (!NUM_WITHIN_BOUNDS(i, uint32_t)) { - LOG(Gen, Error, - "Failed to convert field of type 'number_integer' ({}) to " - "uint32: value is negative.", - i); - return false; - } - result = static_cast(i); - return true; - } - case nlohmann::json::value_t::number_unsigned: { - const auto ul = json_->get(); - if (ul > static_cast(numeric_limits::max())) { - LOG(Gen, Error, - "Failed to convert field of type 'number_unsigned' ({}) to " - "int32: value is too large.", - ul); - return false; - } - result = static_cast(ul); - return true; - } - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - if (!str.empty()) { - result = std::stoul(str); - return true; - } - break; - } - default: - return false; - } - } - END_READ_JSON("uint32") -} - -bool JsonReader::read(int64_t &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::number_float: { - const auto d = json_->get(); - if (result != d) { - LOG(Gen, Warn, - "Potential loss of data converting field of type " - "'number_float' ({}) to int64 ({})", - d, result); - } - result = static_cast(d); - return true; - } - case nlohmann::json::value_t::number_integer: { - result = json_->get(); - return true; - } - case nlohmann::json::value_t::number_unsigned: { - const auto ul = json_->get(); - if (NUM_WITHIN_BOUNDS(ul, int64_t)) { - LOG(Gen, Error, - "Potential loss of data converting field of type " - "'number_unsigned' ({}) to int64 ({})", - ul, result); - return false; - } - result = static_cast(ul); - return true; - } - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - if (!str.empty()) { - result = std::stol(str); - return true; - } - break; - } - default: - return false; - } - } - END_READ_JSON("int64") -} - -bool JsonReader::read(uint64_t &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::number_float: { - const auto d = json_->get(); - if (result != d) { - LOG(Gen, Warn, - "Potential loss of data converting field of type " - "'number_float' ({}) to uint64 ({})", - d, result); - } - result = static_cast(d); - return true; - } - case nlohmann::json::value_t::number_integer: { - const auto il = json_->get(); - if (NUM_WITHIN_BOUNDS(il, uint64_t)) { - LOG(Gen, Error, - "Potential loss of data converting field of type " - "'number_integer' ({}) to uint64 ({})", - il, result); - return false; - } - result = static_cast(il); - return true; - } - case nlohmann::json::value_t::number_unsigned: { - result = json_->get(); - return true; - } - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - if (!str.empty()) { - result = std::stoul(str); - return true; - } - break; - } - default: - return false; - } - } - END_READ_JSON("uint64") -} - -bool JsonReader::read(float &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::number_float: - case nlohmann::json::value_t::number_integer: - case nlohmann::json::value_t::number_unsigned: - json_->get_to(result); - return true; - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - if (!str.empty()) { - result = std::stof(str); - return true; - } - break; - } - default: - return false; - } - } - END_READ_JSON("float") -} - -bool JsonReader::read(double &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::number_float: - case nlohmann::json::value_t::number_integer: - case nlohmann::json::value_t::number_unsigned: - json_->get_to(result); - return true; - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - if (!str.empty()) { - result = std::stod(str); - return true; - } - break; - } - default: - return false; - } - } - END_READ_JSON("double") -} - -bool JsonReader::read(string &result) const { - BEGIN_READ_JSON { - json_->get_to(result); - return true; - } - END_READ_JSON("string") -} - -bool JsonReader::read(nlohmann::json &result) const { - BEGIN_READ_JSON { - json_->get_to(result); - return true; - } - END_READ_JSON("json-object") -} - -bool JsonReader::read_flat(std::vector &result) const { - if (json_ == nullptr) { - return false; - } - switch (json_->type()) { - case json::value_t::array: - return std::all_of(json_->begin(), json_->end(), [&](const auto &j) { - return JsonReader{&j}.read_flat(result); - }); - case json::value_t::null: - return false; - default: - read(result.emplace_back()); - return true; - } -} - -bool JsonReader::parse_time_stamp_(const string &str, TimeStamp &value) const { - return parse_iso_time_stamp_(str, value) || - parse_epoch_nanos_time_stamp_(str, value); -} - -bool JsonReader::parse_iso_time_stamp_( - const string &str, TimeStamp &value) const { - value = TimeStamp::from_iso_string(str); - return true; -} - -bool JsonReader::parse_epoch_nanos_time_stamp_( - const string &str, TimeStamp &value) const { - uint64_t nanos = stoull(str); - value = TimeStamp{nanos}; - return true; -} - -bool JsonReader::read(TimeStamp &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::null: - case nlohmann::json::value_t::object: - case nlohmann::json::value_t::array: - case nlohmann::json::value_t::boolean: - case nlohmann::json::value_t::binary: - case nlohmann::json::value_t::discarded: - return false; - case nlohmann::json::value_t::string: { - const auto str = json_->get(); - return parse_iso_time_stamp_(str, result) || - parse_epoch_nanos_time_stamp_(str, result); - } - case nlohmann::json::value_t::number_integer: - case nlohmann::json::value_t::number_unsigned: - case nlohmann::json::value_t::number_float: - // Maybe we could assume seconds here, idk though...when in doubt, prob - // better to be cautious than cavalier - return false; - } - } - END_READ_JSON("TimeStamp") -} - -bool JsonReader::read_iso( - const std::string &name, core::TimeStamp &result) const { - return JsonReader{json_, name}.read_iso(result); -} - -bool JsonReader::read_iso(core::TimeStamp &result) const { - BEGIN_READ_JSON { - switch (json_->type()) { - case nlohmann::json::value_t::string: { - return parse_iso_time_stamp_(json_->get(), result); - } - default: - return false; - } - } - END_READ_JSON("TimeStamp") -} - -bool JsonReader::read_epoch_nanos(TimeStamp &value) const { - int64_t nanos; - if (!read(nanos)) { - return false; - } - value = TimeStamp::from_nanos(nanos); - return true; -} - -bool JsonReader::read_epoch_micros(TimeStamp &value) const { - int64_t micros; - if (!read(micros)) { - return false; - } - value = TimeStamp::from_micros(micros); - return true; -} - -bool JsonReader::read_epoch_millis(TimeStamp &value) const { - int64_t millis; - if (!read(millis)) { - return false; - } - value = TimeStamp::from_millis(millis); - return true; -} - -bool JsonReader::read_optional_epoch_millis( - std::optional &result) const { - if (json_ == nullptr) { - return false; - } - if (json_->is_null()) { - result = std::nullopt; - return true; - } - return read_epoch_millis(result.emplace()); -} - -bool JsonReader::read_optional_epoch_millis( - const std::string &name, std::optional &result) const { - if (!json_ || !json_->is_object()) { - return false; - } - auto it = json_->find(name); - if (it == json_->end()) { - return false; - } - return JsonReader{&*it}.read_epoch_millis(result.emplace()); -} - -} // namespace bts::io diff --git a/src/lib_bts-system/CMakeLists.txt b/src/lib_bts-system/CMakeLists.txt index db5dfee4a..09ace4c60 100644 --- a/src/lib_bts-system/CMakeLists.txt +++ b/src/lib_bts-system/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.23) project(bts-system) +include(FetchContent) file( GLOB_RECURSE ${PROJECT_NAME}_Sources @@ -10,3 +11,17 @@ target_link_libraries(${PROJECT_NAME} PUBLIC debug fmtd optimized fmt) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# nlohmann json +FetchContent_Declare( + nlohmann_json + GIT_REPOSITORY git@github.com:nlohmann/json.git + GIT_TAG v3.11.1 + CMAKE_ARGS + -DJSON_BuildTests=OFF + -DJSON_SystemInclude=ON + SYSTEM + OVERRIDE_FIND_PACKAGE) +FetchContent_MakeAvailable(nlohmann_json) +find_package(nlohmann_json REQUIRED) +target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json) diff --git a/src/lib_bts-system/bts-system/TryCatch.h b/src/lib_bts-system/bts-system/TryCatch.h deleted file mode 100644 index a8441c2a9..000000000 --- a/src/lib_bts-system/bts-system/TryCatch.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#define CATCH_AND_LOG(subsys, level, label) \ - catch (const std::exception &ex) { \ - LOG(subsys, level, label ": {}", std::string{ex.what()}); \ - } \ - catch (...) { \ - LOG(subsys, level, label ": Unknown exception"); \ - } - -#define CATCH_AND_HANDLE(err_var_name, impl) \ - catch (const std::exception &err_var_name##_ex) { \ - const auto &err_var_name = err_var_name##_ex.what(); \ - impl; \ - } \ - catch (...) { \ - const std::string err_var_name{"Unknown exception"}; \ - impl; \ - } - -// Normalizes 'std::exception' and '...' handlers into single callback -#define CATCH_AND_HANDLE_MSG(impl) \ - catch (const std::exception &ex) { \ - impl(std::string{ex.what()}); \ - } \ - catch (...) { \ - impl(std::string{"Unknown exception"}); \ - } diff --git a/src/lib_bts-system/bts-system/bts_assert.h b/src/lib_bts-system/bts-system/bts_assert.h index ab13485cf..9c1c55055 100644 --- a/src/lib_bts-system/bts-system/bts_assert.h +++ b/src/lib_bts-system/bts-system/bts_assert.h @@ -4,7 +4,6 @@ #include #include -// #include "lib/logging/logging.h" #include "bts_debugger.h" #ifdef _WIN32 diff --git a/src/lib_bts-system/bts-system/delegate.h b/src/lib_bts-system/bts-system/delegate.h new file mode 100644 index 000000000..9f49025f0 --- /dev/null +++ b/src/lib_bts-system/bts-system/delegate.h @@ -0,0 +1,83 @@ +#pragma once +#include +#include + +namespace bts::system { + +// Provides a very fast, very small roughly equivalent to +// std::function. Unlike std::function instances of Delegate do not +// copy the call back they are bound to. +template +class Delegate; + +template +class Delegate { + + private: + void *target_obj_; + R (*invoke_)(void *, Arg...); + + template + struct handler_traits; + template + struct handler_traits { + using HandlerType = T; + using HandlerReturnType = Re; + }; + template + static R invoke_member_handler(void *handler_obj, Args... arg) { + using HandlerMethod = decltype(handler_member); + static_assert(std::is_member_function_pointer::value); + using HandlerType = typename handler_traits::HandlerType; + return (((HandlerType *)handler_obj)->*handler_member)( + std::forward(arg)...); + } + template + static R invoke_static_handler(void *, Args... arg) { + return handler(std::forward(arg)...); + } + + static R null_target(Arg...) { + if constexpr (std::is_same::value) + return; + else + return {}; + } + + public: + Delegate() : target_obj_(), invoke_() { bind<&Delegate::null_target>(); } + + Delegate(Delegate const &d) + : target_obj_(d.target_obj), invoke_(d.invoke_) {} + + Delegate &operator=(Delegate const &d) { + target_obj_ = d.target_obj_; + invoke_ = d.invoke_; + return *this; + } + + template + void bind(C &handler_obj) { + target_obj_ = &handler_obj; + bind<&C::operator()>(handler_obj); + } + + template + void bind(typename handler_traits::HandlerType + &handler_obj) { + target_obj_ = &handler_obj; + invoke_ = invoke_member_handler; + } + + template + void bind() { + target_obj_ = nullptr; + invoke_ = invoke_static_handler; + } + + R operator()(Arg... arg) { + return invoke_(target_obj_, std::forward(arg)...); + } +}; + +} // namespace bts::system diff --git a/src/lib_bts-system/bts-system/EnumMacros.h b/src/lib_bts-system/bts-system/enum_macros.h similarity index 100% rename from src/lib_bts-system/bts-system/EnumMacros.h rename to src/lib_bts-system/bts-system/enum_macros.h diff --git a/src/lib_bts-json/bts-json/JsonDumper.h b/src/lib_bts-system/bts-system/json/json_dumper.h similarity index 75% rename from src/lib_bts-json/bts-json/JsonDumper.h rename to src/lib_bts-system/bts-system/json/json_dumper.h index 390d7d425..a4dd49e5b 100644 --- a/src/lib_bts-json/bts-json/JsonDumper.h +++ b/src/lib_bts-system/bts-system/json/json_dumper.h @@ -3,7 +3,7 @@ #include #include -namespace bts::io { +namespace bts::system::json { class JsonDumper { public: @@ -16,6 +16,8 @@ class JsonDumper { Iter last, int indentation = 2, const char *separator = ",\n"); + + static std::string dump(const nlohmann::json &value, int indentation = 2); }; ////////////////////////////////////////////////// @@ -29,6 +31,11 @@ std::string JsonDumper::dump(const T &value, int indentation) { return j.dump(indentation); } +inline std::string JsonDumper::dump( + const nlohmann::json &value, int indentation) { + return value.dump(indentation); +} + template std::string JsonDumper::dump( Iter first, Iter last, int indentation, const char *separator) { @@ -45,4 +52,4 @@ std::string JsonDumper::dump( return str.str(); } -} // namespace bts::io +} // namespace bts::system::json diff --git a/src/lib_bts-system/bts-system/json/json_reader.cpp b/src/lib_bts-system/bts-system/json/json_reader.cpp new file mode 100644 index 000000000..762eeaddf --- /dev/null +++ b/src/lib_bts-system/bts-system/json/json_reader.cpp @@ -0,0 +1,518 @@ +#include "json_reader.h" +#include "bts-system/datetime.h" + +#include +#include +#include +#include + +namespace bts::system::json { + +using core::TimeStamp; +using nlohmann::json; +using std::int32_t; +using std::int64_t; +using std::numeric_limits; +using std::stoull; +using std::string; +using std::uint32_t; +using std::uint64_t; + +// 'read' fn bodies should be wrapped between BEGIN/END_READ_JSON tags. +// These macros abstract out repetative exception handling code (and a nullptr check). + +#define BEGIN_READ_JSON \ + if (json_ == nullptr) { \ + return false; \ + } \ + try + +#ifdef LOG_JSON_ERRORS +#define END_READ_JSON(type_name) \ + catch (const std::exception &e) { \ + LOG(Gen, Error, "Failed to parse json to '{}'. Exception: {}", \ + std::string{type_name}, std::string{e.what()}); \ + } \ + return false; +#else +#define END_READ_JSON(type_name) \ + catch (...) { \ + } \ + return false; +#endif + +// The idea here is we parse numbers to wide types (either int64, uint64, or double), +// then cast that value to the (potentially narrower) output type. This macro evaluates +// to true if 'wide_val' is within the bounds of the result type (otherwise false). +#define NUM_WITHIN_BOUNDS(wide_val, result_t) \ + (wide_val >= \ + static_cast(numeric_limits::min()) && \ + wide_val <= \ + static_cast(numeric_limits::max())) + +static const nlohmann::json *get_field_json( + const nlohmann::json *json, const std::string &name) { + if (!json || !json->is_object()) { + return nullptr; + } + auto it = json->find(name); + if (it == json->end()) { + return nullptr; + } + return &it.value(); +} + +JsonReader::JsonReader(const nlohmann::json *json) : json_(json) {} + +JsonReader::JsonReader(const nlohmann::json *json, const std::string &name) + : json_(get_field_json(json, name)) {} + +bool JsonReader::try_parse( + const std::string &str, nlohmann::json &result, std::string *err) { + try { + result = nlohmann::json::parse(str); + return true; + } catch (const std::exception &ex) { + if (err != nullptr) { + *err = ex.what(); + } + } + return false; +} + +bool JsonReader::read(const string &name, string &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, int32_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, uint32_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, int64_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, uint64_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, float &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, double &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, TimeStamp &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read_epoch_nanos(const string &name, TimeStamp &value) const { + return JsonReader{json_, name}.read_epoch_nanos(value); +} + +bool JsonReader::read_epoch_micros(const string &name, TimeStamp &value) const { + return JsonReader{json_, name}.read_epoch_micros(value); +} + +bool JsonReader::read_epoch_millis(const string &name, TimeStamp &value) const { + return JsonReader{json_, name}.read_epoch_millis(value); +} + +bool JsonReader::read(const string &name, nlohmann::json &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(int32_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + result = static_cast(d); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to int32 ({})", + d, result); + } + return true; + } + case nlohmann::json::value_t::number_integer: { + const auto il = json_->get(); + if (!NUM_WITHIN_BOUNDS(il, int32_t)) { + LOG(Gen, Error, + "Failed to convert field of type 'number_integer' ({}) to " + "uint32: value is negative.", + il); + } + result = static_cast(il); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + const auto ul = json_->get(); + if (ul > static_cast(numeric_limits::max())) { + LOG(Gen, Error, + "Failed to convert field of type 'number_unsigned' ({}) to " + "int32: value is too large.", + ul); + return false; + } + result = static_cast(ul); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stoi(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("int32") +} + +bool JsonReader::read(uint32_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + result = static_cast(d); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to uint32 ({})", + d, result); + } + return true; + } + case nlohmann::json::value_t::number_integer: { + const auto i = json_->get(); + if (!NUM_WITHIN_BOUNDS(i, uint32_t)) { + LOG(Gen, Error, + "Failed to convert field of type 'number_integer' ({}) to " + "uint32: value is negative.", + i); + return false; + } + result = static_cast(i); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + const auto ul = json_->get(); + if (ul > static_cast(numeric_limits::max())) { + LOG(Gen, Error, + "Failed to convert field of type 'number_unsigned' ({}) to " + "int32: value is too large.", + ul); + return false; + } + result = static_cast(ul); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stoul(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("uint32") +} + +bool JsonReader::read(int64_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to int64 ({})", + d, result); + } + result = static_cast(d); + return true; + } + case nlohmann::json::value_t::number_integer: { + result = json_->get(); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + const auto ul = json_->get(); + if (NUM_WITHIN_BOUNDS(ul, int64_t)) { + LOG(Gen, Error, + "Potential loss of data converting field of type " + "'number_unsigned' ({}) to int64 ({})", + ul, result); + return false; + } + result = static_cast(ul); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stol(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("int64") +} + +bool JsonReader::read(uint64_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to uint64 ({})", + d, result); + } + result = static_cast(d); + return true; + } + case nlohmann::json::value_t::number_integer: { + const auto il = json_->get(); + if (NUM_WITHIN_BOUNDS(il, uint64_t)) { + LOG(Gen, Error, + "Potential loss of data converting field of type " + "'number_integer' ({}) to uint64 ({})", + il, result); + return false; + } + result = static_cast(il); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + result = json_->get(); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stoul(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("uint64") +} + +bool JsonReader::read(float &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: + case nlohmann::json::value_t::number_integer: + case nlohmann::json::value_t::number_unsigned: + json_->get_to(result); + return true; + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stof(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("float") +} + +bool JsonReader::read(double &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: + case nlohmann::json::value_t::number_integer: + case nlohmann::json::value_t::number_unsigned: + json_->get_to(result); + return true; + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stod(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("double") +} + +bool JsonReader::read(string &result) const { + BEGIN_READ_JSON { + json_->get_to(result); + return true; + } + END_READ_JSON("string") +} + +bool JsonReader::read(nlohmann::json &result) const { + BEGIN_READ_JSON { + json_->get_to(result); + return true; + } + END_READ_JSON("json-object") +} + +bool JsonReader::read_flat(std::vector &result) const { + if (json_ == nullptr) { + return false; + } + switch (json_->type()) { + case json::value_t::array: + return std::all_of(json_->begin(), json_->end(), [&](const auto &j) { + return JsonReader{&j}.read_flat(result); + }); + case json::value_t::null: + return false; + default: + read(result.emplace_back()); + return true; + } +} + +bool JsonReader::parse_time_stamp_(const string &str, TimeStamp &value) const { + return parse_iso_time_stamp_(str, value) || + parse_epoch_nanos_time_stamp_(str, value); +} + +bool JsonReader::parse_iso_time_stamp_( + const string &str, TimeStamp &value) const { + value = TimeStamp::from_iso_string(str); + return true; +} + +bool JsonReader::parse_epoch_nanos_time_stamp_( + const string &str, TimeStamp &value) const { + uint64_t nanos = stoull(str); + value = TimeStamp{nanos}; + return true; +} + +bool JsonReader::read(TimeStamp &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::null: + case nlohmann::json::value_t::object: + case nlohmann::json::value_t::array: + case nlohmann::json::value_t::boolean: + case nlohmann::json::value_t::binary: + case nlohmann::json::value_t::discarded: + return false; + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + return parse_iso_time_stamp_(str, result) || + parse_epoch_nanos_time_stamp_(str, result); + } + case nlohmann::json::value_t::number_integer: + case nlohmann::json::value_t::number_unsigned: + case nlohmann::json::value_t::number_float: + // Maybe we could assume seconds here, idk though...when in doubt, prob + // better to be cautious than cavalier + return false; + } + } + END_READ_JSON("TimeStamp") +} + +bool JsonReader::read_iso( + const std::string &name, core::TimeStamp &result) const { + return JsonReader{json_, name}.read_iso(result); +} + +bool JsonReader::read_iso(core::TimeStamp &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::string: { + return parse_iso_time_stamp_(json_->get(), result); + } + default: + return false; + } + } + END_READ_JSON("TimeStamp") +} + +bool JsonReader::read_epoch_nanos(TimeStamp &value) const { + int64_t nanos; + if (!read(nanos)) { + return false; + } + value = TimeStamp::from_nanos(nanos); + return true; +} + +bool JsonReader::read_epoch_micros(TimeStamp &value) const { + int64_t micros; + if (!read(micros)) { + return false; + } + value = TimeStamp::from_micros(micros); + return true; +} + +bool JsonReader::read_epoch_millis(TimeStamp &value) const { + int64_t millis; + if (!read(millis)) { + return false; + } + value = TimeStamp::from_millis(millis); + return true; +} + +bool JsonReader::read_optional_epoch_millis( + std::optional &result) const { + if (json_ == nullptr) { + return false; + } + if (json_->is_null()) { + result = std::nullopt; + return true; + } + return read_epoch_millis(result.emplace()); +} + +bool JsonReader::read_optional_epoch_millis( + const std::string &name, std::optional &result) const { + if (!json_ || !json_->is_object()) { + return false; + } + auto it = json_->find(name); + if (it == json_->end()) { + return false; + } + return JsonReader{&*it}.read_epoch_millis(result.emplace()); +} + +} // namespace bts::system::json diff --git a/src/lib_bts-json/bts-json/JsonReader.h b/src/lib_bts-system/bts-system/json/json_reader.h similarity index 76% rename from src/lib_bts-json/bts-json/JsonReader.h rename to src/lib_bts-system/bts-system/json/json_reader.h index b6e247db5..be64d878f 100644 --- a/src/lib_bts-json/bts-json/JsonReader.h +++ b/src/lib_bts-system/bts-system/json/json_reader.h @@ -1,15 +1,50 @@ #pragma once +#include "bts-system/TimeStamp.h" +#include "bts-system/bts_assert.h" +#include "bts-system/logging.h" +#include "bts-system/overloaded.h" + #include #include #include #include +#include -#include "bts-system/TimeStamp.h" -#include "bts-system/bts_assert.h" -#include "bts-system/logging.h" +namespace bts::system::json { -namespace bts::io { +#ifdef LOG_JSON_READ_ERRORS +#define JSON_READ_AS(json, field, type, dest) \ + type dest; \ + JsonReader{&json}.read(#field, dest) + +#define JSON_READ_AS_ELSE(js, field, type, dest, else) \ + type dest; \ + if (!JsonReader{&js}.read(#field, dest)) { \ + _ERROR( \ + "{}. Failed to read {} field '{}'", string{#field}, \ + string{__FUNCTION__}, system::json::JsonDumper::dump(js)); \ + else; \ + } +#else +#define JSON_READ_AS(json, field, type, dest) \ + type dest; \ + JsonReader{&json}.read(#field, dest); + +#define JSON_READ_AS_ELSE(json, field, type, dest, else) \ + type dest; \ + if (!JsonReader{&json}.read(#field, dest)) { \ + else; \ + } +#endif + +#define JSON_READ(json, field, type) JSON_READ_AS(json, field, type, field) + +/** + * Reads the json fields. If the read fails, performs the 'else' action + */ +#define JSON_READ_ELSE(j, field, type, else) \ + JSON_READ_AS_ELSE(j, field, type, field, else) class JsonReader { public: @@ -100,6 +135,12 @@ class JsonReader { template bool read_or(const std::string &name, T &result, T default_value) const; + template + int read_variant( + const std::string &name1, + const std::string &name2, + std::variant &result); + private: const nlohmann::json *json_; @@ -288,6 +329,24 @@ bool JsonReader::read_flat( return true; } +template +int JsonReader::read_variant( + const std::string &name1, + const std::string &name2, + std::variant &result) { + auto it = json_->find(name1); + if (it != json_->end()) { + result = it->get(); + return 1; + } + it = json_->find(name2); + if (it != json_->end()) { + result = it->get(); + return 2; + } + return 0; +} + #undef CATCH_PARSE_ERRORS -} // namespace bts::io +} // namespace bts::system::json diff --git a/src/lib_bts-system/bts-system/overloaded.h b/src/lib_bts-system/bts-system/overloaded.h new file mode 100644 index 000000000..11e7d1255 --- /dev/null +++ b/src/lib_bts-system/bts-system/overloaded.h @@ -0,0 +1,11 @@ +#pragma once + +// #if __cplusplus >= 201703L +#include +// Helper to visit std::variant +namespace bts { +template struct overloaded : Ts... { using Ts::operator()...; }; +template +overloaded(Ts...) -> overloaded; // not needed as of C++20 +} // namespace bts +// #endif diff --git a/src/lib_bts-system/bts-system/threading/thread_config.h b/src/lib_bts-system/bts-system/threading/thread_config.h new file mode 100644 index 000000000..25dc1002a --- /dev/null +++ b/src/lib_bts-system/bts-system/threading/thread_config.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include +#include + +namespace bts::system::threading { + +class ThreadConfig { + public: + std::string name_rx; + bool exclude; + std::vector cores; +}; + +inline void to_json( + nlohmann ::json &nlohmann_json_j, const ThreadConfig &nlohmann_json_t) { + nlohmann_json_j["name_rx"] = nlohmann_json_t.name_rx; + nlohmann_json_j["exclude"] = nlohmann_json_t.exclude; + nlohmann_json_j["cores"] = nlohmann_json_t.cores; +} +inline void from_json( + const nlohmann ::json &nlohmann_json_j, ThreadConfig &nlohmann_json_t) { + nlohmann_json_j.at("name_rx").get_to(nlohmann_json_t.name_rx); + nlohmann_json_j.at("exclude").get_to(nlohmann_json_t.exclude); + nlohmann_json_j.at("cores").get_to(nlohmann_json_t.cores); +}; + +} // namespace bts::system::threading diff --git a/src/lib_bts-system/bts-system/threading/thread_manager.cpp b/src/lib_bts-system/bts-system/threading/thread_manager.cpp new file mode 100644 index 000000000..7a6b76305 --- /dev/null +++ b/src/lib_bts-system/bts-system/threading/thread_manager.cpp @@ -0,0 +1,117 @@ +#include "thread_manager.h" +#include "bts-system/bts_assert.h" +#include "bts-system/logging.h" +#include "bts-system/threading/thread_config.h" + +#include +#include +#include +#include +#include +#include + +namespace bts::system::threading { + +using std::function; +using std::string; +using std::uint32_t; +using std::vector; + +ThreadConfig ThreadManager::get_thread_cfg(const string &name) { + for (const auto &cfg : this->threads) { + std::smatch match; + std::regex rgx{cfg.name_rx}; + if (std::regex_search(name, match, rgx)) { + return cfg; + } + } + bts_assert_msg( + false, fmt::format("No configuration found for thread {}", name)); +} + +vector ThreadManager::get_affinity(const string &name) { + vector result; + auto cfg = get_thread_cfg(name); + if (cfg.exclude) { + auto total_cpus = sysconf(_SC_NPROCESSORS_ONLN); + auto &cores = cfg.cores; + for (int i = 0; i < total_cpus; i++) { + if (std::find(cores.begin(), cores.end(), i) == cores.end()) { + result.push_back(i); + } + } + } else { + for (const auto &core : cfg.cores) { + result.push_back(core); + } + } + return result; +} + +void configure_thread( + const string &name, const ThreadConfig &cfg, pthread_t t) { + if (!cfg.cores.empty()) { + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + + if (cfg.exclude) { + auto total_cpus = sysconf(_SC_NPROCESSORS_ONLN); + for (int i = 0; i < total_cpus; i++) { + CPU_SET(i, &cpuset); + } + for (const auto &core : cfg.cores) { + CPU_CLR(core, &cpuset); + } + } else { + for (const auto &core : cfg.cores) { + CPU_SET(core, &cpuset); + } + } + + int rc = pthread_setaffinity_np(t, sizeof(cpu_set_t), &cpuset); + if (rc != 0) { + LOG(Gen, Error, + "Could not set core affinity for {} on thread {}. Error " + "code={}", + name, cfg.name_rx, rc); + } + } +} + +void ThreadManager::set_thread_name(const string &name) { + prctl(PR_SET_NAME, name.c_str(), 0, 0, 0); +} + +void ThreadManager::configure_current_thread(const string &name) { + configure_thread(name, get_thread_cfg(name), pthread_self()); + set_thread_name(name); +} + +void ThreadManager::configure_current_thread( + const string &name, ThreadConfig const &cfg) { + configure_thread(name, cfg, pthread_self()); + set_thread_name(name); +} + +std::thread ThreadManager::make_thread_from_function( + const string &name, function const &c) { + auto result = std::thread([c, name]() { + set_thread_name(name); + c(); + }); + + configure_thread(name, get_thread_cfg(name), result.native_handle()); + return result; +} + +std::thread ThreadManager::make_thread_from_function( + const string &name, function const &c, ThreadConfig const &cfg) { + auto result = std::thread([c, name]() { + set_thread_name(name); + c(); + }); + configure_thread(name, cfg, result.native_handle()); + return result; +} + +} // namespace bts::system::threading diff --git a/src/lib_bts-system/bts-system/threading/thread_manager.h b/src/lib_bts-system/bts-system/threading/thread_manager.h new file mode 100644 index 000000000..c5f41975f --- /dev/null +++ b/src/lib_bts-system/bts-system/threading/thread_manager.h @@ -0,0 +1,48 @@ +#pragma once + +#include "bts-system/threading/thread_config.h" + +#include +#include +#include + +namespace bts::system::threading { + +class ThreadManager { + public: + explicit ThreadManager(std::vector threads); + + std::thread make_thread_from_function( + std::string const &name, + std::function const &c, + ThreadConfig const &cfg); + + std::thread make_thread_from_function( + std::string const &name, std::function const &c); + + template + std::thread make_thread(std::string const &name, C callable) { + return make_thread_from_function(name, std::function(callable)); + } + + template + std::thread make_thread( + std::string const &name, C callable, ThreadConfig const &cfg) { + return make_thread_from_function( + name, std::function(callable), cfg); + } + + void configure_current_thread(std::string const &name); + void configure_current_thread(std::string const &name, ThreadConfig const &); + + ThreadConfig get_thread_cfg(std::string const &); + + std::vector get_affinity(std::string const &name); + + private: + std::vector threads; + + static void set_thread_name(const std::string &name); +}; + +} // namespace bts::system::threading diff --git a/src/lib_bts-system/bts-system/value_wrapper.h b/src/lib_bts-system/bts-system/value_wrapper.h new file mode 100644 index 000000000..078426394 --- /dev/null +++ b/src/lib_bts-system/bts-system/value_wrapper.h @@ -0,0 +1,163 @@ +#pragma once + +#include +#include + +namespace bts::system { + +template +class ValueWrapper { + public: + typedef ValueT Value; + + constexpr ValueWrapper() = default; + constexpr explicit ValueWrapper(const ValueT &v); + constexpr explicit ValueWrapper(ValueT &&v); + + constexpr ValueWrapper(const ValueWrapper &v) = default; + constexpr ValueWrapper(ValueWrapper &&v) = default; + + ValueWrapper &operator=(ValueWrapper const &v) = default; + ValueWrapper &operator=(ValueWrapper &&v) = default; + + constexpr const ValueT &value() const; + constexpr ValueT &value(); + + private: + ValueT v_; +}; + +template +constexpr bool operator==( + const ValueWrapper &lhs, const ValueWrapper &rhs); + +template +constexpr bool operator!=( + const ValueWrapper &lhs, const ValueWrapper &rhs); + +template +constexpr bool operator>( + const ValueWrapper &lhs, const ValueWrapper &rhs); + +template +constexpr bool operator>=( + const ValueWrapper &lhs, const ValueWrapper &rhs); + +template +constexpr bool operator<( + const ValueWrapper &lhs, const ValueWrapper &rhs); + +template +constexpr bool operator<=( + const ValueWrapper &lhs, const ValueWrapper &rhs); + +template +std::ostream &operator<<(std::ostream &s, ValueWrapper const &v); + +} // namespace bts::system + +namespace std { + +template +struct hash> { + std::size_t operator()(const bts::system::ValueWrapper &k) const; +}; + +} // namespace std + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +namespace bts::system { + +template +constexpr ValueWrapper::ValueWrapper(const ValueT &v) : v_(v) {} + +template +constexpr ValueWrapper::ValueWrapper(ValueT &&v) + : v_(std::move(v)) {} + +template +constexpr ValueT const &ValueWrapper::value() const { + return v_; +} + +template +constexpr ValueT &ValueWrapper::value() { + return v_; +} + +#define BIN_OP(result_t, op) \ + template \ + constexpr result_t operator op( \ + const ValueWrapper &lhs, \ + const ValueWrapper &rhs) { \ + return result_t(lhs.value() op rhs.value()); \ + } + +BIN_OP(bool, ==) +BIN_OP(bool, !=) +BIN_OP(bool, >) +BIN_OP(bool, >=) +BIN_OP(bool, <) +BIN_OP(bool, <=) + +#undef BIN_OP + +template +V &operator*(ValueWrapper &v); + +template +const V &operator*(const ValueWrapper &v); + +// template +// constexpr V* operator->(ValueWrapper &v) noexcept; + +// template +// constexpr const V* operator->(const ValueWrapper &v) noexcept; + +template +std::ostream &operator<<(std::ostream &s, ValueWrapper const &v); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +V &operator*(ValueWrapper &v) { + return v.value(); +} + +// template +// constexpr V *operator->(ValueWrapper &v) noexcept { +// return &v.value(); +// } + +// template +// constexpr const V *operator->(const ValueWrapper &v) noexcept { +// return &v.value(); +// } + +template +const V &operator*(const ValueWrapper &v) { + return v.value(); +} + +template +std::ostream &operator<<(std::ostream &s, ValueWrapper const &v) { + s << v.value(); + return s; +} + +} // namespace bts::system + +namespace std { + +template +std::size_t hash>::operator()( + const bts::system::ValueWrapper &k) const { + return hash()(k.value()); +} + +} // namespace std diff --git a/src/lib_bts-websocket/CMakeLists.txt b/src/lib_bts-websocket/CMakeLists.txt new file mode 100644 index 000000000..63eacea36 --- /dev/null +++ b/src/lib_bts-websocket/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.23) +project(bts-websocket) + +file( + GLOB_RECURSE ${PROJECT_NAME}_Sources + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_NAME}/*.cpp) +add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http curl) + +FetchContent_MakeAvailable(curlpp) +FetchContent_GetProperties( + curlpp + SOURCE_DIR CURLPP_SOURCE_DIR + BINARY_DIR CURLPP_BINARY_DIR) +list(APPEND CMAKE_BUILD_RPATH ${curlpp_BINARY_DIR}) +target_link_libraries(${PROJECT_NAME} PUBLIC crypto curlpp ssl) diff --git a/src/lib_bts-websocket/bts-websocket/websocket_access_token.h b/src/lib_bts-websocket/bts-websocket/websocket_access_token.h new file mode 100644 index 000000000..b637ed403 --- /dev/null +++ b/src/lib_bts-websocket/bts-websocket/websocket_access_token.h @@ -0,0 +1,26 @@ +#pragma once + +#include "bts-system/value_wrapper.h" + +#include +#include + +namespace bts::websocket { + +/** + * Represents an access token for a websocket server. To use it, the websocket + * client should include the following header in the handshake request: + * `Authorization:Bearer \p access_token`. + */ +using WebsocketAccessToken = + system::ValueWrapper; + +inline void from_json(const nlohmann::json &j, WebsocketAccessToken &t) { + *t = j.get(); +} + +inline void to_json(nlohmann::json &j, const WebsocketAccessToken &t) { + j = *t; +} + +} // namespace bts::websocket diff --git a/src/lib_bts-websocket/bts-websocket/websocket_authorization.h b/src/lib_bts-websocket/bts-websocket/websocket_authorization.h new file mode 100644 index 000000000..a2c1e9d06 --- /dev/null +++ b/src/lib_bts-websocket/bts-websocket/websocket_authorization.h @@ -0,0 +1,81 @@ +#pragma once + +#include "bts-http/oauth_request.h" +#include "bts-system/json/json_reader.h" +#include "bts-system/overloaded.h" +#include "bts-system/value_wrapper.h" +#include "bts-websocket/websocket_access_token.h" +#include "bts-websocket/websocket_authorization_type.h" + +#include +#include +#include +#include + +namespace bts::websocket { + +class WebsocketAuthorization { + public: + http::OauthRequest &oauth(); + const http::OauthRequest &oauth() const; + + WebsocketAccessToken &access_token(); + const WebsocketAccessToken &access_token() const; + + WebsocketAuthorizationType type() const; + + friend void to_json(nlohmann::json &j, const WebsocketAuthorization &cfg); + friend void from_json(const nlohmann::json &j, WebsocketAuthorization &cfg); + + private: + std::variant value_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline http::OauthRequest &WebsocketAuthorization::oauth() { + return std::get(value_); +} +inline const http::OauthRequest &WebsocketAuthorization::oauth() const { + return std::get(value_); +} + +inline WebsocketAccessToken &WebsocketAuthorization::access_token() { + return std::get(value_); +} + +inline const WebsocketAccessToken & +WebsocketAuthorization::access_token() const { + return std::get(value_); +} + +inline WebsocketAuthorizationType WebsocketAuthorization::type() const { + return std::visit( + overloaded{ + [](const WebsocketAccessToken &) { + return WebsocketAuthorizationType::AccessToken; + }, + [](const http::OauthRequest &) { + return WebsocketAuthorizationType::OauthRequest; + }, + []() { return WebsocketAuthorizationType::None; }}, + value_); +} + +inline void to_json(nlohmann ::json &j, const WebsocketAuthorization &c) { + std::visit( + overloaded{ + [&](const WebsocketAccessToken &t) { j["access_token"] = *t; }, + [&](const http::OauthRequest &o) { j["oauth"] = o; }, + }, + c.value_); +} + +inline void from_json(const nlohmann ::json &j, WebsocketAuthorization &c) { + system::json::JsonReader jr{&j}; + jr.read_variant("access_token", "oauth", c.value_); +}; + +} // namespace bts::websocket diff --git a/src/lib_bts-websocket/bts-websocket/websocket_authorization_type.h b/src/lib_bts-websocket/bts-websocket/websocket_authorization_type.h new file mode 100644 index 000000000..512b07965 --- /dev/null +++ b/src/lib_bts-websocket/bts-websocket/websocket_authorization_type.h @@ -0,0 +1,23 @@ +#pragma once + +#include "bts-system/FMT_IMPL.h" +#include "bts-system/enum_macros.h" + +#include + +namespace bts::websocket { + +// clang-format off +#define FOR_ALL_VALUES(per_value) \ + per_value(WebsocketAuthorizationType, None) \ + per_value(WebsocketAuthorizationType, AccessToken) \ + per_value(WebsocketAuthorizationType, OauthRequest) +// clang-format on + +ENUM_CLASS_DEF(WebsocketAuthorizationType, FOR_ALL_VALUES); + +#undef FOR_ALL_VALUES + +} // namespace bts::websocket + +FMT_IMPL(bts::websocket::WebsocketAuthorizationType, t, "{}", t.name()); diff --git a/src/lib_bts-websocket/bts-websocket/websocket_client.cpp b/src/lib_bts-websocket/bts-websocket/websocket_client.cpp new file mode 100644 index 000000000..449ddb58c --- /dev/null +++ b/src/lib_bts-websocket/bts-websocket/websocket_client.cpp @@ -0,0 +1,273 @@ +#include "websocket_client.h" + +#include "bts-http/QueryParams.h" +#include "bts-http/URL.h" +#include "bts-http/curl_requestor.h" +#include "bts-http/http_client.h" +#include "bts-http/http_request.h" +#include "bts-http/http_response.h" +#include "bts-http/oauth_requestor.h" +#include "bts-http/oauth_response.h" +#include "bts-system/TimeSpan.h" +#include "bts-system/TimeStamp.h" +#include "bts-system/UNUSED.h" +#include "bts-system/bts_assert.h" +#include "bts-system/json/json_dumper.h" +#include "bts-system/json/json_reader.h" +#include "bts-websocket/websocket_access_token.h" +#include "bts-websocket/websocket_authorization_type.h" +#include "bts-websocket/websocket_client_config.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::websocket { + +using namespace std::chrono_literals; + +using nlohmann::json; +using std::optional; +using std::string; +using websocketpp::config::asio_tls_client; + +using WebsocketppClient = + websocketpp::client; +using ConnectionHdl = websocketpp::connection_hdl; +using ConnectionPtr = WebsocketppClient::connection_ptr; +using MessageTypePtr = websocketpp::config::core_client::message_type::ptr; +using SslContext = boost::asio::ssl::context; + +// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1 + +#define _ASSERT_MSGF(condition, ...) \ + bts_assert_msgf(condition, "WebsocketClient::" __VA_ARGS__) +#define _LOG(level, ...) LOG(md, level, "WebsocketClient::" __VA_ARGS__) +#define _DIAG(...) _LOG(Diag, __VA_ARGS__) +#define _INFO(...) _LOG(Info, __VA_ARGS__) +#define _WARN(...) _LOG(Warn, __VA_ARGS__) +#define _ERROR(...) _LOG(Error, __VA_ARGS__) + +struct WebsocketClient::PImpl { + WebsocketClientConfig cfg; + WebsocketppClient client; + WebsocketppClient::connection_ptr con; + std::optional message_handler; + bool client_is_open{false}; + std::size_t sub_id_counter{0}; + string server_name; + std::optional run_thread; + int client_id{client_id_counter++}; + + ~PImpl() { + if (run_thread) { + run_thread->join(); + } + } + + void close(websocketpp::close::status::value code, std::string reason) { + _WARN("Closing connection {}", client_id); + websocketpp::lib::error_code ec; + client.close(con->get_handle(), code, reason, ec); + if (ec) { + _ERROR( + "Error while closing connection {}. {}", client_id, ec.message()); + } + } + + websocketpp::lib::error_code send_text(const string &text) { + websocketpp::lib::error_code ec; + _INFO("Sending message. {}", text); + client.send(con, text, websocketpp::frame::opcode::value::TEXT, ec); + return ec; + } + + WebsocketAccessToken get_access_token() { + switch (cfg.auth.type().value) { + case websocket::WebsocketAuthorizationType::AccessToken: + return cfg.auth.access_token(); + case websocket::WebsocketAuthorizationType::OauthRequest: + return WebsocketAccessToken{ + http::OauthRequestor::request(cfg.auth.oauth()).access_token}; + default: + return {}; + } + } + + /** + * Connects to the websocket server + * + * @param timeout The amount of time to (optionally) block thread waiting + * for connection + */ + void connect() { + const auto access_token = get_access_token(); + _INFO( + "Connecting to server. client_id: {}; cfg:\n{}", client_id, + system::json::JsonDumper::dump(cfg)); + try { + init_logging(); + init_handlers(); + // Get connection + websocketpp::lib::error_code ec; + con = client.get_connection(cfg.url, ec); + if (ec) { + _ERROR( + "Error connecting to server. client_id: {}. {}", client_id, + ec.message()); + return; + } + // Add auth header (if non-empty) + if (!access_token.value().empty()) { + con->append_header("Authorization", "Bearer " + *access_token); + } + // Connect + client.connect(con); + _ASSERT_MSGF( + !run_thread, + "Connection thread has already been created. client_id: {}", + client_id); + run_thread = std::thread{[this]() { + _INFO("run_thread begin. client_id: {}", client_id); + client.run(); + }}; + // Wait for connect + if (cfg.timeout) { + wait_for_connect(*cfg.timeout); + } + _INFO("Connection opened. client_id: {}", client_id); + } catch (const websocketpp::exception &ex) { + _ERROR( + "Error connecting to server. client_id: {}. {}", client_id, + string{ex.what()}); + } + } + + bool wait_for_connect(core::TimeSpan timeout) { + auto start = core::TimeStamp::now(); + // Wait for client to connect + while (!client_is_open) { + _INFO("{}", "Waiting for connection to open..."); + std::this_thread::sleep_for(std::chrono::seconds(1)); + auto t = core::TimeStamp::now() - start; + if (t > timeout) { + _INFO( + "Timed out waiting for connection after {} secs. client_id: " + "{}", + t.total_seconds(), client_id); + return false; + } + } + return true; + } + + private: + static std::atomic_int client_id_counter; + + auto on_tls_init(ConnectionHdl /*hdl*/) { + auto result = + websocketpp::lib::make_shared(SslContext::sslv23_client); + result->set_verify_mode(boost::asio::ssl::verify_none); + return result; + } + + void init_handlers() { + client.init_asio(); + client.set_message_handler( + [this](auto hdl, auto msg) { on_message(hdl, msg); }); + client.set_open_handler([this](auto hdl) { on_open(hdl); }); + client.set_tls_init_handler( + [this](auto hdl) { return on_tls_init(hdl); }); + client.set_close_handler([this](auto hdl) { on_close(hdl); }); + client.set_fail_handler([this](auto hdl) { + auto con = client.get_con_from_hdl(hdl); + auto ec = con->get_ec(); + if (ec) { + _ERROR( + "websocket failed with error. client_id: {}. {}", client_id, + string{ec.message()}); + } else { + _INFO("websocket failed. client_id: {}", client_id); + } + }); + } + + void init_logging() { + client.set_access_channels(websocketpp::log::alevel::all); + client.clear_access_channels(websocketpp::log::alevel::frame_payload); + client.set_error_channels(websocketpp::log::elevel::all); + } + + void on_open(ConnectionHdl hdl) { + auto con = client.get_con_from_hdl(hdl); + auto ec = con->get_ec(); + if (ec) { + _ERROR("websocket opened with error. {}", string{ec.message()}); + } else { + _INFO("{}", "websocket opened"); + } + client_is_open = true; + } + + void on_message(ConnectionHdl /*hdl*/, MessageTypePtr msg) { + if (!message_handler) { + _ERROR("on message. No handler.\n{}", msg->get_payload()); + return; + } + _INFO("on message.\n{}", msg->get_payload()); + (*message_handler)(msg->get_payload()); + } + + void on_close(ConnectionHdl hdl) { + client_is_open = false; + auto con = client.get_con_from_hdl(hdl); + auto ec = con->get_ec(); + if (ec) { + _ERROR("websocket closed with error. {}", string{ec.message()}); + } else { + _INFO("{}", string{"websocket closed"}); + } + } +}; + +WebsocketClient::WebsocketClient(WebsocketClientConfig cfg) + : me_(std::make_unique()) { + me_->cfg = std::move(cfg); +} + +WebsocketClient::~WebsocketClient() = default; + +std::atomic_int WebsocketClient::PImpl::client_id_counter = 0; + +WebsocketClient::MessageHandler &WebsocketClient::message_handler() { + if (!me_->message_handler) { + me_->message_handler.emplace(); + } + return *me_->message_handler; +} + +void WebsocketClient::connect() { me_->connect(); } + +void WebsocketClient::send_text(const string &text) { me_->send_text(text); } + +} // namespace bts::websocket diff --git a/src/lib_bts-websocket/bts-websocket/websocket_client.h b/src/lib_bts-websocket/bts-websocket/websocket_client.h new file mode 100644 index 000000000..eb0840ba2 --- /dev/null +++ b/src/lib_bts-websocket/bts-websocket/websocket_client.h @@ -0,0 +1,42 @@ +#pragma once + +#include "bts-http/URL.h" +#include "bts-system/value_wrapper.h" +#include "bts-system/delegate.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::websocket { + +class WebsocketClientConfig; + +class WebsocketClient { + public: + explicit WebsocketClient(WebsocketClientConfig cfg); + ~WebsocketClient(); + + using MessageTypePtr = websocketpp::config::core_client::message_type::ptr; + using MessageHandler = system::Delegate; + + void connect(); + MessageHandler &message_handler(); + void send_text(const std::string &text); + + private: + struct PImpl; + std::unique_ptr me_; +}; + +} // namespace bts::websocket diff --git a/src/lib_bts-websocket/bts-websocket/websocket_client_config.h b/src/lib_bts-websocket/bts-websocket/websocket_client_config.h new file mode 100644 index 000000000..7c14bebaf --- /dev/null +++ b/src/lib_bts-websocket/bts-websocket/websocket_client_config.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include +#include + +#include "bts-http/oauth_request.h" +#include "bts-system/json/json_reader.h" +#include "bts-system/threading/thread_config.h" +#include "bts-websocket/websocket_authorization.h" + +namespace bts::websocket { + +class WebsocketClientConfig { + public: + /** + * The websocket server url + */ + std::string url; + /** + * Authorization parameters for the websocket handshake + */ + WebsocketAuthorization auth; + /** + * Thread configuration for the websocket client's response handlers + */ + system::threading::ThreadConfig thread; + + std::optional timeout; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void to_json( + nlohmann ::json &nlohmann_json_j, + const WebsocketClientConfig &nlohmann_json_t) { + nlohmann_json_j["url"] = nlohmann_json_t.url; + nlohmann_json_j["auth"] = nlohmann_json_t.auth; + nlohmann_json_j["thread"] = nlohmann_json_t.thread; +} +inline void from_json( + const nlohmann ::json &nlohmann_json_j, + WebsocketClientConfig &nlohmann_json_t) { + nlohmann_json_j.at("url").get_to(nlohmann_json_t.url); + nlohmann_json_j.at("auth").get_to(nlohmann_json_t.auth); + // nlohmann_json_j.at("thread").get_to(nlohmann_json_t.thread); +}; + +// inline void to_json(nlohmann::json &j, const WebsocketClientConfig &cfg) { +// j["url"] = cfg.url; +// j["thread"] = cfg.thread; +// j["auth"] = cfg.auth; +// } + +// inline void from_json(const nlohmann::json &j, WebsocketClientConfig &cfg) { +// system::json::JsonReader jr{&j}; +// jr.read("url", cfg.url); +// jr.read("thread", cfg.thread); +// jr.read("auth", cfg.auth); +// }; + +} // namespace bts::websocket