Skip to content

Commit

Permalink
campaignd: Add dedicated type for parsing admin commands
Browse files Browse the repository at this point in the history
  • Loading branch information
irydacea committed Apr 24, 2015
1 parent 1cb3d67 commit 20e55fb
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/campaign_server/campaign_server.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
}
}
Expand Down
114 changes: 114 additions & 0 deletions src/campaign_server/control.hpp
@@ -0,0 +1,114 @@
/*
Copyright (C) 2015 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
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 <stdexcept>
#include <vector>

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<std::string> args_;
};

} // end namespace campaignd

#endif // CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED

0 comments on commit 20e55fb

Please sign in to comment.