Skip to content

Commit

Permalink
Add confing.yaml file and parse configure information from it
Browse files Browse the repository at this point in the history
  • Loading branch information
MakingL committed Dec 31, 2019
1 parent f624cd5 commit b27f83a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
target_link_libraries(MiniFTPD crypt)
target_link_libraries(MiniFTPD libyaml-cpp.a)
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server:
listen_host: "0.0.0.0" # server 端监听的主机地址
listen_port: 2222 # 命令连接的监听的端口
32 changes: 32 additions & 0 deletions src/configure.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string>();
SERVER_LISTEN_PORT = server_config["listen_port"].as<int>();

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));

}

}
20 changes: 20 additions & 0 deletions src/configure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by MLee on 2019/12/31.
//

#ifndef MINIFTPD_CONFIGURE_H
#define MINIFTPD_CONFIGURE_H

#include <string>
#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
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit b27f83a

Please sign in to comment.