From 5da8d4562742ad498b2fdb9d24eed578477aeaac Mon Sep 17 00:00:00 2001 From: Anatoly Kalin Date: Mon, 4 Sep 2023 16:24:36 +0300 Subject: [PATCH] [EN-7412] Implement DXFeedConnect sample --- CMakeLists.txt | 61 ++++++++++-------- README.md | 2 - samples/cpp/DxFeedConnect/CMakeLists.txt | 81 ++++++++++++++++++++++++ samples/cpp/DxFeedConnect/src/main.cpp | 70 ++++++++++++++++++++ src/internal/CMakeLists.txt | 52 +++++++++++++++ src/internal/utils/CmdArgsUtils.cpp | 2 + 6 files changed, 240 insertions(+), 28 deletions(-) create mode 100644 samples/cpp/DxFeedConnect/CMakeLists.txt create mode 100644 samples/cpp/DxFeedConnect/src/main.cpp create mode 100644 src/internal/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c89dd4f..3dabc7a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,24 +93,26 @@ add_subdirectory(third_party/fmt-10.0.0) # find_package(utf8cpp) # find_package(fmt) -set(dxFeedNativeAPIInternalSources - src/internal/CEntryPointErrors.cpp - src/internal/Isolate.cpp - src/internal/JavaObjectHandler.cpp - src/internal/EventClassList.cpp - src/internal/SymbolList.cpp - src/internal/Common.cpp -) - -set(dxFeedNativeAPIInternalUtilsSources - src/internal/utils/StringUtils.cpp - src/internal/utils/EnumUtils.cpp - src/internal/utils/CmdArgsUtils.cpp -) - -set(dxFeedNativeAPIInternalUtilsDebugSources - src/internal/utils/debug/Debug.cpp -) +#set(dxFeedNativeAPIInternalSources +# src/internal/CEntryPointErrors.cpp +# src/internal/Isolate.cpp +# src/internal/JavaObjectHandler.cpp +# src/internal/EventClassList.cpp +# src/internal/SymbolList.cpp +# src/internal/Common.cpp +#) +# +#set(dxFeedNativeAPIInternalUtilsSources +# src/internal/utils/StringUtils.cpp +# src/internal/utils/EnumUtils.cpp +# src/internal/utils/CmdArgsUtils.cpp +#) +# +#set(dxFeedNativeAPIInternalUtilsDebugSources +# src/internal/utils/debug/Debug.cpp +#) + +add_subdirectory(src/internal) set(dxFeedNativeAPIAPISources src/api/DXEndpoint.cpp @@ -208,9 +210,12 @@ elseif (UNIX) endif () add_library(${PROJECT_NAME}_static STATIC - ${dxFeedNativeAPIInternalSources} - ${dxFeedNativeAPIInternalUtilsSources} - ${dxFeedNativeAPIInternalUtilsDebugSources} +# ${dxFeedNativeAPIInternalSources} +# ${dxFeedNativeAPIInternalUtilsSources} +# ${dxFeedNativeAPIInternalUtilsDebugSources} + +# $ + ${dxFeedNativeAPIAPISources} ${dxFeedNativeAPIAPIOsubSources} ${dxFeedNativeAPISymbolsSources} @@ -224,9 +229,12 @@ add_library(${PROJECT_NAME}_static STATIC ) add_library(${PROJECT_NAME} SHARED - ${dxFeedNativeAPIInternalSources} - ${dxFeedNativeAPIInternalUtilsSources} - ${dxFeedNativeAPIInternalUtilsDebugSources} +# ${dxFeedNativeAPIInternalSources} +# ${dxFeedNativeAPIInternalUtilsSources} +# ${dxFeedNativeAPIInternalUtilsDebugSources} + +# $ + ${dxFeedNativeAPIAPISources} ${dxFeedNativeAPIAPIOsubSources} ${dxFeedNativeAPISymbolsSources} @@ -305,8 +313,8 @@ if (DXFCXX_USE_PRECOMPILED_HEADERS) target_precompile_headers(${PROJECT_NAME}_static PRIVATE include/dxfeed_graal_cpp_api/internal/PrecompiledHeaders.hpp) endif () -target_link_libraries(${PROJECT_NAME}_static PUBLIC DxFeedGraalNativeSdk utf8cpp fmt::fmt-header-only) -target_link_libraries(${PROJECT_NAME} PUBLIC DxFeedGraalNativeSdk utf8cpp fmt::fmt-header-only) +target_link_libraries(${PROJECT_NAME}_static PUBLIC DxFeedGraalNativeSdk dxfcxx::internal utf8cpp fmt::fmt-header-only) +target_link_libraries(${PROJECT_NAME} PUBLIC DxFeedGraalNativeSdk dxfcxx::internal utf8cpp fmt::fmt-header-only) if (DXFCXX_ENABLE_ASAN_UBSAN) target_compile_options(${PROJECT_NAME} PRIVATE "-fsanitize=address" "-fsanitize=undefined") @@ -330,6 +338,7 @@ if (DXFCXX_BUILD_SAMPLES) add_subdirectory(samples/cpp/WriteTapeFile) add_subdirectory(samples/cpp/ConvertTapeFile) add_subdirectory(samples/cpp/DxFeedFileParser) + add_subdirectory(samples/cpp/DxFeedConnect) endif () if (DXFCXX_BUILD_TOOLS) diff --git a/README.md b/README.md index 164260f4..1782b3ad 100644 --- a/README.md +++ b/README.md @@ -243,10 +243,8 @@ be downloaded from [Release](https://github.com/dxFeed/dxfeed-graal-cxx-api/rele ## Samples * [ConvertTapeFile](https://github.com/dxFeed/dxfeed-graal-cxx-api/blob/main/samples/cpp/ConvertTapeFile/src/main.cpp) demonstrates how to convert one tape file to another tape file with optional intermediate processing or filtering - * [DxFeedFileParser](https://github.com/dxFeed/dxfeed-graal-cxx-api/blob/main/samples/cpp/DxFeedFileParser/src/main.cpp) is a simple demonstration of how events are read form a tape file * [DxFeedSample](https://github.com/dxFeed/dxfeed-graal-cxx-api/blob/main/samples/cpp/DxFeedSample/src/main.cpp) diff --git a/samples/cpp/DxFeedConnect/CMakeLists.txt b/samples/cpp/DxFeedConnect/CMakeLists.txt new file mode 100644 index 00000000..69c7a0ef --- /dev/null +++ b/samples/cpp/DxFeedConnect/CMakeLists.txt @@ -0,0 +1,81 @@ +# Copyright (c) 2023 Devexperts LLC. +# SPDX-License-Identifier: MPL-2.0 + +cmake_minimum_required(VERSION 3.21) + +if (POLICY CMP0092) + cmake_policy(SET CMP0092 NEW) +endif () + +if (POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) +endif () + +project(DxFeedConnect LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_C_STANDARD 11) +set(CXX_EXTENSIONS OFF) +set(C_EXTENSIONS OFF) + +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(CMAKE_MACOSX_RPATH ON) + set(CMAKE_SKIP_BUILD_RPATH ON) + set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) + set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF) + set(CMAKE_BUILD_RPATH_USE_ORIGIN ON) + set(CMAKE_INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path;@executable_path;@executable_path/../Frameworks") +elseif (UNIX) + set(CMAKE_SKIP_BUILD_RPATH ON) + set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) + set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF) + set(CMAKE_BUILD_RPATH_USE_ORIGIN ON) + set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN/../lib64:$ORIGIN/../lib:$ORIGIN") +endif () + +add_executable(${PROJECT_NAME} src/main.cpp) + +if (DXFCXX_ENABLE_VS_ASAN) + target_compile_options(${PROJECT_NAME} PRIVATE "/fsanitize=address") + target_link_options(${PROJECT_NAME} PRIVATE "/fsanitize=address") + + target_compile_definitions(${PROJECT_NAME} + PUBLIC + $<$: + _DISABLE_VECTOR_ANNOTATION + _DISABLE_STRING_ANNOTATION + > + ) +endif () + +if (DXFCXX_ENABLE_ASAN_UBSAN) + target_link_libraries(${PROJECT_NAME} PRIVATE dxfcxx) + target_compile_definitions(${PROJECT_NAME} PRIVATE DXFCPP_USE_DLLS) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + $) + + target_compile_options(${PROJECT_NAME} PRIVATE "-fsanitize=address" "-fsanitize=undefined") + target_link_options(${PROJECT_NAME} PRIVATE "-fsanitize=address" "-fsanitize=undefined") +else () + target_link_libraries(${PROJECT_NAME} PRIVATE dxfcxx) + target_compile_definitions(${PROJECT_NAME} PRIVATE DXFCPP_USE_DLLS) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + $) +# target_link_libraries(${PROJECT_NAME} PRIVATE dxfcxx::static) +# +# add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different +# $ +# $) +endif () + +if (DXFCXX_INSTALL AND DXFCXX_INSTALL_SAMPLES) + install(TARGETS ${PROJECT_NAME}) + + if (WIN32) + install(FILES $ DESTINATION ${CMAKE_INSTALL_BINDIR}) + endif () +endif () \ No newline at end of file diff --git a/samples/cpp/DxFeedConnect/src/main.cpp b/samples/cpp/DxFeedConnect/src/main.cpp new file mode 100644 index 00000000..5397097f --- /dev/null +++ b/samples/cpp/DxFeedConnect/src/main.cpp @@ -0,0 +1,70 @@ +// Copyright (c) 2023 Devexperts LLC. +// SPDX-License-Identifier: MPL-2.0 + +#include + +#include +#include + +using namespace dxfcpp; +using namespace dxfcpp::literals; +using namespace std::literals; + +void printUsage() { + auto usageString = R"( +Usage: +DxFeedFileParser + +Where: + file - Is a file name. + type - Is comma-separated list of dxfeed event types ()" + + dxfcpp::enum_utils::getEventTypeEnumNamesList() + R"(). + symbol - Is comma-separated list of symbol names to get events for (e.g. "IBM,AAPL,MSFT").)"; + + std::cout << usageString << std::endl; +} + +int main(int argc, char *argv[]) { + if (argc < 4) { + printUsage(); + + return 0; + } + + std::atomic eventCounter{}; + std::mutex ioMtx{}; + + // Parse args. + std::string fileName = argv[1]; + auto types = CmdArgsUtils::parseTypes(argv[2]); + auto symbols = CmdArgsUtils::parseSymbols(argv[3]); + + // Create endpoint specifically for file parsing. + auto endpoint = DXEndpoint::create(DXEndpoint::Role::STREAM_FEED); + auto feed = endpoint->getFeed(); + + // Subscribe to a specified event and symbol. + auto sub = feed->createSubscription(types); + sub->addEventListener([&eventCounter, &ioMtx](const auto &events) { + std::lock_guard lock{ioMtx}; + + for (auto &&e : events) { + std::cout << (++eventCounter) << ": " << e << "\n"; + } + }); + + // Add symbols. + sub->addSymbols(symbols); + + // Connect endpoint to a file. + endpoint->connect("file:" + fileName + "[speed=max]"); + + // Wait until file is completely parsed. + endpoint->awaitNotConnected(); + + // Close endpoint when we're done. + // This method will gracefully close endpoint, waiting while data processing completes. + endpoint->closeAndAwaitTermination(); + + return 0; +} \ No newline at end of file diff --git a/src/internal/CMakeLists.txt b/src/internal/CMakeLists.txt new file mode 100644 index 00000000..a36efea4 --- /dev/null +++ b/src/internal/CMakeLists.txt @@ -0,0 +1,52 @@ +# Copyright (c) 2023 Devexperts LLC. +# SPDX-License-Identifier: MPL-2.0 + +cmake_minimum_required(VERSION 3.21) + +project(dxFeedGraalCxxApi_Internal) + +if (POLICY CMP0092) + cmake_policy(SET CMP0092 NEW) +endif () + +if (POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) +endif () + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CXX_EXTENSIONS OFF) +set(C_EXTENSIONS OFF) + +set(dxFeedGraalCxxApi_Internal_Sources + CEntryPointErrors.cpp + Isolate.cpp + JavaObjectHandler.cpp + EventClassList.cpp + SymbolList.cpp + Common.cpp +) + +set(dxFeedGraalCxxApi_Internal_Utils_Sources + utils/StringUtils.cpp + utils/EnumUtils.cpp + utils/CmdArgsUtils.cpp +) + +set(dxFeedGraalCxxApi_Internal_UtilsDebug_Sources + utils/debug/Debug.cpp +) + + +add_library(dxFeedGraalCxxApi_Internal OBJECT + ${dxFeedGraalCxxApi_Internal_Sources} + ${dxFeedGraalCxxApi_Internal_Utils_Sources} + ${dxFeedGraalCxxApi_Internal_UtilsDebug_Sources} +) + +add_library(dxfcxx::internal ALIAS dxFeedGraalCxxApi_Internal) + +target_include_directories(dxFeedGraalCxxApi_Internal PRIVATE ../../include ../../third_party/range-v3-0.12/include) +target_link_libraries(dxFeedGraalCxxApi_Internal PRIVATE DxFeedGraalNativeSdk utf8cpp fmt::fmt-header-only) \ No newline at end of file diff --git a/src/internal/utils/CmdArgsUtils.cpp b/src/internal/utils/CmdArgsUtils.cpp index df97ef5e..0079f7a2 100644 --- a/src/internal/utils/CmdArgsUtils.cpp +++ b/src/internal/utils/CmdArgsUtils.cpp @@ -18,6 +18,8 @@ #include #include +#include + namespace dxfcpp {