From 20e55fb67c8752c7dbcd68afbb9d0e3b52df0bd9 Mon Sep 17 00:00:00 2001 From: "Ignacio R. Morelle" Date: Thu, 23 Apr 2015 21:29:34 -0300 Subject: [PATCH] campaignd: Add dedicated type for parsing admin commands --- src/campaign_server/campaign_server.cpp | 6 +- src/campaign_server/control.hpp | 114 ++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/campaign_server/control.hpp diff --git a/src/campaign_server/campaign_server.cpp b/src/campaign_server/campaign_server.cpp index c0dcb26a5dfe..66069ec40b3c 100644 --- a/src/campaign_server/campaign_server.cpp +++ b/src/campaign_server/campaign_server.cpp @@ -31,6 +31,7 @@ #include "addon/validation.hpp" #include "campaign_server/addon_utils.hpp" #include "campaign_server/blacklist.hpp" +#include "campaign_server/control.hpp" #include "version.hpp" #include "util.hpp" @@ -287,8 +288,9 @@ void server::run() std::string admin_cmd; if(input_ && input_->read_line(admin_cmd)) { - // process command - if(admin_cmd == "shut_down") { + control_line ctl = admin_cmd; + + if(ctl == "shut_down") { break; } } diff --git a/src/campaign_server/control.hpp b/src/campaign_server/control.hpp new file mode 100644 index 000000000000..4075fa2bde6b --- /dev/null +++ b/src/campaign_server/control.hpp @@ -0,0 +1,114 @@ +/* + Copyright (C) 2015 by Ignacio Riquelme Morelle + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#ifndef CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED +#define CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED + +#include "serialization/string_utils.hpp" + +#include +#include + +namespace campaignd +{ + +/** + * Represents a server control line written to a communication socket. + * + * Control lines are plain text command lines using the ASCII space character + * (0x20) as command separator. This type is really only used to keep the code + * pretty. + */ +class control_line +{ +public: + /** + * Parses a control line string. + */ + control_line(const std::string& str) : args_(utils::split(str, ' ')) + { + if(args_.empty()) { + args_.push_back(""); + } + } + + /** + * Whether the control line is empty. + */ + bool empty() const + { + // Because of how utils::split() works, this can only happen if there + // are no other arguments. + return args_[0].empty(); + } + + /** + * Returns the control command. + * + * Equivalent to calling arg(0). + */ + operator const std::string&() const + { + return cmd(); + } + + /** + * Returns the control command. + * + * Equivalent to calling arg(0). + */ + const std::string& cmd() const + { + return args_[0]; + } + + /** + * Returns the total number of arguments, not including the command itself. + */ + size_t args_count() const + { + return args_.size() - 1; + } + + /** + * Returns the nth argument. + * + * @throws std::out_of_range @a n exceeds args_count(). + */ + const std::string& operator[](size_t n) const + { + return arg(n); + } + + /** + * Returns the nth argument. + * + * @throws std::out_of_range @a n exceeds args_count(). + */ + const std::string& arg(size_t n) const + { + if(n > args_count()) { + throw std::out_of_range("control line argument range exceeded"); + } + + return args_[n]; + } + +private: + std::vector args_; +}; + +} // end namespace campaignd + +#endif // CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED