From b27f83ab0ff60e07471222bbe32a483f37984892 Mon Sep 17 00:00:00 2001 From: MichaelLee Date: Tue, 31 Dec 2019 21:50:35 +0800 Subject: [PATCH] Add confing.yaml file and parse configure information from it --- CMakeLists.txt | 9 ++++++++- config.yaml | 3 +++ src/configure.cpp | 32 ++++++++++++++++++++++++++++++++ src/configure.h | 20 ++++++++++++++++++++ src/main.cpp | 9 +++++---- 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 config.yaml create mode 100644 src/configure.cpp create mode 100644 src/configure.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cd1eb76..b4e8777 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) # 并将名称保存到 DIR_SRCS 变量 aux_source_directory(src/ DIR_SRCS) +set(INC_DIR include) # 头文件 +set(LINK_DIR lib/) # 链接库的位置 + +include_directories(${INC_DIR}) # 头文件 +link_directories(${LINK_DIR}) # 链接库的位置 + # 指定生成目标 add_executable(MiniFTPD ${DIR_SRCS}) # 添加链接库 -target_link_libraries(MiniFTPD crypt) \ No newline at end of file +target_link_libraries(MiniFTPD crypt) +target_link_libraries(MiniFTPD libyaml-cpp.a) diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..0053a5c --- /dev/null +++ b/config.yaml @@ -0,0 +1,3 @@ +server: + listen_host: "0.0.0.0" # server 端监听的主机地址 + listen_port: 2222 # 命令连接的监听的端口 \ No newline at end of file diff --git a/src/configure.cpp b/src/configure.cpp new file mode 100644 index 0000000..b4bc025 --- /dev/null +++ b/src/configure.cpp @@ -0,0 +1,32 @@ +// +// Created by MLee on 2019/12/31. +// + +#include "configure.h" +#include "common.h" +#include "utility.h" + +namespace configure { + const char *config_file = "config.yaml"; /* 配置文件 */ + + std::string SERVER_LISTEN_HOST; + int SERVER_LISTEN_PORT; + + void parse_config_file() { + YAML::Node node = YAML::LoadFile(configure::config_file); + + if (!node["server"].IsDefined()) { + std::cerr << "Server config information isn't configured correctly" << std::endl; + return; + } + + auto server_config = node["server"]; + SERVER_LISTEN_HOST = server_config["listen_host"].as(); + SERVER_LISTEN_PORT = server_config["listen_port"].as(); + + utility::debug_info(std::string("Server listen host: ") + SERVER_LISTEN_HOST); + utility::debug_info(std::string("Server listen port: ") + std::to_string(SERVER_LISTEN_PORT)); + + } + +} \ No newline at end of file diff --git a/src/configure.h b/src/configure.h new file mode 100644 index 0000000..157b841 --- /dev/null +++ b/src/configure.h @@ -0,0 +1,20 @@ +// +// Created by MLee on 2019/12/31. +// + +#ifndef MINIFTPD_CONFIGURE_H +#define MINIFTPD_CONFIGURE_H + +#include +#include "../include/yaml-cpp/yaml.h" + +namespace configure { + /* 配置信息 */ + extern std::string SERVER_LISTEN_HOST; + extern int SERVER_LISTEN_PORT; + + void parse_config_file(); /* 从配置文件中解析配置信息 */ + +} + +#endif //MINIFTPD_CONFIGURE_H diff --git a/src/main.cpp b/src/main.cpp index ddc5272..2fd10ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,11 +5,9 @@ #include "pipe_wrapper.h" #include "signal_wrapper.h" #include "ftp_handler.h" +#include "configure.h" -const int SERVER_LISTEN_PORT = 2222; -const char *SERVER_LISTEN_HOST = "0.0.0.0"; - int signal_pipe_fd[2]; void signal_handler(int sig) { @@ -23,7 +21,10 @@ void signal_handler(int sig) { int main() { utility::debug_info("MiniFTPD Start"); - CLTCPServer tcp_server(SERVER_LISTEN_HOST, SERVER_LISTEN_PORT); + /* 解析配置文件中的参数 */ + configure::parse_config_file(); + + CLTCPServer tcp_server(configure::SERVER_LISTEN_HOST.c_str(), configure::SERVER_LISTEN_PORT); int listen_fd = tcp_server.start_listen(); tcp::get_local_ip(nullptr);