From d9f94369cbaa75e97047e87c06d880001d10494c Mon Sep 17 00:00:00 2001 From: itsafuu Date: Thu, 25 Jan 2024 13:54:06 -0500 Subject: [PATCH] Test for linking asio manager, incomplete not working --- build/CommonBuildParameters.cmake | 8 ++ example/AsyncFile/CMakeLists.txt | 5 ++ example/AsyncFile/MNNExample.cpp | 138 ++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 example/AsyncFile/CMakeLists.txt create mode 100644 example/AsyncFile/MNNExample.cpp diff --git a/build/CommonBuildParameters.cmake b/build/CommonBuildParameters.cmake index 496ef03c..5f4419a0 100644 --- a/build/CommonBuildParameters.cmake +++ b/build/CommonBuildParameters.cmake @@ -262,6 +262,13 @@ set(xxhash_DIR "${_THIRDPARTY_BUILD_DIR}/xxhash/lib/cmake/xxhash") find_package(xxhash CONFIG REQUIRED) include_directories(${xxhash_INCLUDE_DIR}) +# -------------------------------------------------------- +# Set config of AsyncIOManager +set(AsyncIOManager_INCLUDE_DIR "${_THIRDPARTY_BUILD_DIR}/AsyncIOManager/include") +set(AsyncIOManager_LIBRARY_DIR "${_THIRDPARTY_BUILD_DIR}/AsyncIOManager/lib") +set(AsyncIOManager_DIR "${_THIRDPARTY_BUILD_DIR}/AsyncIOManager/lib/cmake/AsyncIOManager") +find_package(AsyncIOManager CONFIG REQUIRED) +include_directories(${AsyncIOManager_INCLUDE_DIR}) # -------------------------------------------------------- include_directories( @@ -299,6 +306,7 @@ endif () if (BUILD_EXAMPLES) add_subdirectory(${PROJECT_ROOT}/example ${CMAKE_BINARY_DIR}/example) endif () +add_subdirectory(${PROJECT_ROOT}/example/AsyncFile ${CMAKE_BINARY_DIR}/example/AsyncFile) install( EXPORT supergeniusTargets diff --git a/example/AsyncFile/CMakeLists.txt b/example/AsyncFile/CMakeLists.txt new file mode 100644 index 00000000..f1f84a84 --- /dev/null +++ b/example/AsyncFile/CMakeLists.txt @@ -0,0 +1,5 @@ +#add_subdirectory(application) +FILE(GLOB MNN_LIBS "${MNN_LIBRARY_DIR}/*") +add_executable(MNNExample MNNExample.cpp) +target_link_libraries(MNNExample PRIVATE AsyncIOManager) +include_directories(../include) diff --git a/example/AsyncFile/MNNExample.cpp b/example/AsyncFile/MNNExample.cpp new file mode 100644 index 00000000..70997bc4 --- /dev/null +++ b/example/AsyncFile/MNNExample.cpp @@ -0,0 +1,138 @@ +//#define BOOST_ASIO_HAS_IO_URING 1 //Linux +//#define BOOST_ASIO_DISABLE_EPOLL 0 //Maybe linux? +#include +#include +#include +#include "Singleton.hpp" +#include "FileManager.hpp" +//#include "MNNLoader.hpp" +//#include "IPFSLoader.hpp" +//#include "HTTPLoader.hpp" +//#include "SFTPLoader.hpp" +//#include "WSLoader.hpp" +#include "URLStringUtil.h" +#include + + +/** + * This program is example to loading MNN model file + */ +//#define IPFS_FILE_PATH_NAME "ipfs://example.mnn" +#define FILE_PATH_NAME "file://../test/1.mnn" +#define FILE_SAVE_NAME "file://test.mnn" +void loadhandler(boost::system::error_code ec, std::size_t n, std::vector& buffer) { + if (!ec) { + std::cout << "Received data: "; + std::cout << std::endl; + std::cout << "Handler Out Size:" << n << std::endl; + } + else { + std::cerr << "Error in async_read: " << ec.message() << std::endl; + } +} + + +int main(int argc, char **argv) +{ + std::string file_name = ""; + std::vector file_names(0); + std::cout << "argcount" << argc << std::endl; + if (argc < 2) + { + std::cout << argv[0] << " [MNN extension file]" << std::endl; + std::cout << "E.g:" << std::endl; + std::cout << "\t " << argv[0] << " file://../test/1.mnn" << std::endl; + return 1; + } + else + { + file_name = std::string(argv[1]); + for (int i = 1; i < argc; i++) + { + std::cout << "file: " << argv[i] << std::endl; + file_names.push_back(argv[i]); + } + } + // return 1; + // this should all be moved to GTest + // Break out the URL prefix, file path, and extension. + std::string url_prefix; + std::string file_path; + std::string extension; + std::string http_host; + std::string http_path; + + getURLComponents( + "https://www.example.com/test.jpg", + url_prefix, file_path, extension); + //Print the results. + //cout << "urlPrefix: " << url_prefix << endl; + //cout << "filePath: " << file_path << endl; + //cout << "extension: " << extension << endl; + //parseHTTPUrl(file_path, http_host, http_path); + //std::cout << "http host:" << http_host << std::endl; + //std::cout << "http path:" << http_path << std::endl; + + // getURLComponents("file://./test.mnn", url_prefix, file_path, extension); + // Print the results. + // cout << "urlPrefix: " << url_prefix << endl; + //cout << "filePath: " << file_path << endl; + //cout << "extension: " << extension << endl; + + // getURLComponents("ipfs://really/long/path/to/a/file/testme.jpg", + // url_prefix, file_path, extension); + // Print the results. + //cout << "urlPrefix: " << url_prefix << endl; + //cout << "filePath: " << file_path << endl; + //cout << "extension: " << extension << endl; + std::cout << "hmm" << std::endl; + + + // test plugins + if (file_name.empty()) + { + file_name = FILE_PATH_NAME; + } + + //auto ioc = std::make_shared(); + + auto injector = libp2p::injector::makeHostInjector(); + auto ioc = injector.create>(); + + // Create a work guard to keep the io_context alive + boost::asio::io_context::executor_type executor = ioc->get_executor(); + boost::asio::executor_work_guard workGuard(executor); + + auto dummyCallback = [](std::shared_ptr, std::shared_ptr>, bool parse, bool save) { + // Do nothing + }; + for (int i = 0; i < file_names.size(); i++) + { + std::cout << "LoadASync: " << file_names[i] << std::endl; + auto data = FileManager::GetInstance().LoadASync(file_names[i], true, true, ioc, dummyCallback, [](const int& status) + { + std::cout << "status: " << status << std::endl; + }); + //FileManager::GetInstance().IncrementOutstandingOperations(); + + //std::cout << "LoadFile: " << file_names[i] << std::endl; + //auto data = FileManager::GetInstance().LoadFile(file_names[i], false); + + //std::cout << "LoadFile with Parse: " << file_names[i] << std::endl; + //data = FileManager::GetInstance().LoadFile(file_names[i], true); + //std::cout << "ParseFile: " << std::endl; + //FileManager::GetInstance().ParseData("mnn", data); + //std::cout << "SaveFile: Save to " << FILE_SAVE_NAME; + //FileManager::GetInstance().SaveFile(FILE_SAVE_NAME, data); + //std::cout << std::endl; + //boost::asio::post(ioc, postload); + } + //std::thread([ioc]() { + // ioc->run(); + // }).detach(); + ioc->reset(); + ioc->run(); + + return 0; +} +