bonus-edge-proxy/CMakeLists.txt

241 lines
7.0 KiB
CMake

cmake_minimum_required(VERSION 3.16)
project(EdgeProxy LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# --- 查找并链接 spdlog ---
find_package(spdlog REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread)
message(STATUS "Found Boost version: ${Boost_VERSION}")
find_package(PahoMqttCpp REQUIRED)
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/src/vendor
)
add_library(edge_proxy_lib STATIC
# Add the refactored network and protocol files
src/network/tcp_server.cc
src/protocol/protocol.cc
src/protocol/protocol_handler.cc
# Existing files
src/SystemMonitor.cc
src/mqtt_client.cpp
src/mqtt_router.cpp
)
target_link_libraries(edge_proxy_lib PRIVATE
spdlog::spdlog
Boost::system
Boost::thread
PahoMqttCpp::paho-mqttpp3
pthread
nlohmann_json
)
Gem
Of course. Based on our previous work and the CMakeLists.txt you've provided, here is a complete main.cpp function designed to act as the application's entry point, along with the necessary modifications to your CMakeLists.txt to build it.
The main function will orchestrate the startup and shutdown of the TCP server, using the components we have already designed.
File 1: src/main.cpp (New File)
This file creates and runs the TCPServer. It initializes the logging system, sets up the Boost.Asio I/O context, handles graceful shutdown signals (Ctrl+C), and blocks until the server is told to stop.
C++
#include "network/tcp_server.h" // Use the refactored header location
#include "spdlog/spdlog.h"
#include <boost/asio.hpp>
#include <csignal>
#include <iostream>
// Global io_context for the ASIO services
// We make it a pointer to control its lifetime explicitly if needed,
// but for this simple main, a global object is fine.
boost::asio::io_context g_io_context;
/**
* @brief Handles termination signals (SIGINT, SIGTERM).
*
* This function gracefully stops the Boost.Asio io_context, which in turn
* will cause the io_context.run() call in main() to unblock and allow
* the program to exit cleanly.
*
* @param signum The signal number received.
*/
void signalHandler(int signum) {
spdlog::warn("Interrupt signal ({}) received. Shutting down.", signum);
// Post a stop event to the io_context. This is a thread-safe way
// to tell the io_context to stop its event loop.
g_io_context.stop();
}
int main(int argc, char* argv[]) {
// --- 1. Initialization ---
// Setup logging (using a simple basic logger here, you can configure it more)
try {
spdlog::set_level(spdlog::level::debug); // Set global log level to debug
spdlog::info("Edge Proxy starting up...");
} catch (const spdlog::spdlog_ex& ex) {
std::cerr << "Log initialization failed: " << ex.what() << std::endl;
return 1;
}
// Register signal handlers for graceful shutdown
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
// --- 2. Service Setup ---
try {
// Define the port from a constant or configuration file
constexpr uint16_t port = 12345;
// Create the TCPServer instance. It will start accepting connections
// immediately within the io_context's event loop.
TCPServer server(g_io_context, port);
// --- 3. Run Service ---
spdlog::info("Server is running. Press Ctrl+C to exit.");
// Start the Boost.Asio event loop. This call will block until
// g_io_context.stop() is called (e.g., by our signal handler).
g_io_context.run();
} catch (const std::exception& e) {
spdlog::critical("An unhandled exception occurred: {}", e.what());
return 1;
}
// --- 4. Shutdown ---
spdlog::info("Server has been shut down gracefully. Exiting.");
return 0;
}
File 2: CMakeLists.txt (Modified)
Here are the modifications to your CMakeLists.txt. I have:
Added the new source files from our refactoring (network/ and protocol/ directories) to the edge_proxy_lib library target.
Added an include directory so the compiler can find the headers (#include "network/tcp_server.h").
Uncommented and activated the main application executable target, edge_proxy.
Linked the edge_proxy executable against our library.
CMake
cmake_minimum_required(VERSION 3.16)
project(EdgeProxy LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Add the 'src' directory to the include path so that #include "network/tcp_server.h" works
include_directories(src)
# --- Find required packages ---
find_package(spdlog REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread)
message(STATUS "Found Boost version: ${Boost_VERSION}")
find_package(PahoMqttCpp REQUIRED)
# --- Define external libraries ---
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/src/vendor
)
# --- Core Logic Library ---
# All core logic is compiled into a static library.
# This makes it reusable for the main app, tests, etc.
add_library(edge_proxy_lib STATIC
# Add the refactored network and protocol files
src/network/tcp_server.cc
src/protocol/protocol.cc
src/protocol/protocol_handler.cc
# Existing files
src/SystemMonitor.cc
src/mqtt_client.cpp
src/mqtt_router.cpp
)
target_link_libraries(edge_proxy_lib PRIVATE
spdlog::spdlog
Boost::system
Boost::thread
PahoMqttCpp::paho-mqttpp3
pthread
nlohmann_json
)
# =================================================================
# Main Application Target (UNCOMMENTED AND ACTIVATED)
# =================================================================
add_executable(edge_proxy
src/main.cpp
)
target_link_libraries(edge_proxy PRIVATE
edge_proxy_lib
)
# =================================================================
# 测试目标
# =================================================================
add_executable(test
src/test.cc
src/test-new.cc
)
target_link_libraries(test PRIVATE
spdlog::spdlog
Boost::system
Boost::thread
)
# =================================================================
# 独立的 MQTT 功能测试目标
# =================================================================
# 创建一个新的可执行文件,名为 mqtt_test_runner
# add_executable(mqtt_test_runner
# src/mqtt_test.cpp
# )
# target_link_libraries(mqtt_test_runner PRIVATE
# edge_proxy_lib
# )
# =================================================================
# gtest
# =================================================================
# enable_testing()
# include(FetchContent)
# FetchContent_Declare(
# googletest
# URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
# )
# FetchContent_MakeAvailable(googletest)
# add_executable(run_tests
# tests/mqtt_integration_test.cpp
# )
# target_link_libraries(run_tests PRIVATE
# GTest::gtest_main
# PahoMqttCpp::paho-mqttpp3
# pthread
# )
# include(GoogleTest)
# gtest_discover_tests(run_tests)