Merge branch 'campaignd/adminctl'

This commit is contained in:
Ignacio R. Morelle 2015-04-24 00:09:31 -03:00
commit 07739ea261
3 changed files with 184 additions and 4 deletions

View File

@ -32,6 +32,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"
@ -164,7 +165,11 @@ int server::load_config()
// Open the control socket if enabled.
if(!cfg_["control_socket"].empty()) {
input_.reset(new input_stream(cfg_["control_socket"]));
const std::string& path = cfg_["control_socket"].str();
if(!input_.get() || input_->path() != path) {
input_.reset(new input_stream(cfg_["control_socket"]));
}
}
// Ensure the campaigns list WML exists even if empty, other functions
@ -285,18 +290,66 @@ void server::run()
}
try {
bool force_flush = false;
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") {
LOG_CS << "Shut down requested by admin, shutting down...\n";
break;
} else if(ctl == "readonly") {
if(ctl.args_count()) {
cfg_["read_only"] = read_only_ = utils::string_bool(ctl[1], true);
}
LOG_CS << "Read only mode: " << (read_only_ ? "enabled" : "disabled") << '\n';
} else if(ctl == "flush") {
force_flush = true;
LOG_CS << "Flushing config to disk...\n";
} else if(ctl == "reload") {
if(ctl.args_count()) {
if(ctl[1] == "blacklist") {
LOG_CS << "Reloading blacklist...\n";
load_blacklist();
} else {
ERR_CS << "Unrecognized admin reload argument: " << ctl[1] << '\n';
}
} else {
LOG_CS << "Reloading all configuration...\n";
need_reload = 1;
// Avoid flush timer ellapsing
continue;
}
} else if(ctl == "setpass") {
if(ctl.args_count() != 2) {
ERR_CS << "Incorrect number of arguments for 'setpass'\n";
} else {
const std::string& addon_id = ctl[1];
const std::string& newpass = ctl[2];
config& campaign = campaigns().find_child("campaign", "name", addon_id);
if(!campaign) {
ERR_CS << "Add-on '" << addon_id << "' not found, cannot set passphrase\n";
} else if(newpass.empty()) {
// Shouldn't happen!
ERR_CS << "Add-on passphrases may not be empty!\n";
} else {
campaign["passphrase"] = newpass;
write_config();
LOG_CS << "New passphrase set for '" << addon_id << "'\n";
}
}
} else {
LOG_CS << "Unrecognized admin command: " << ctl.full() << '\n';
}
}
const time_t cur_ts = monotonic_clock();
// Write config to disk every ten minutes.
if(labs(cur_ts - last_ts) >= 10*60) {
if(force_flush || labs(cur_ts - last_ts) >= 10*60) {
write_config();
last_ts = cur_ts;
}

View File

@ -0,0 +1,122 @@
/*
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];
}
/**
* Return the full command line string.
*/
std::string full() const
{
return utils::join(args_, " ");
}
private:
std::vector<std::string> args_;
};
} // end namespace campaignd
#endif // CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED

View File

@ -27,6 +27,11 @@ public:
bool read_line(std::string& str);
void stop();
const std::string& path() const
{
return path_;
}
private:
input_stream(const input_stream&);
void operator=(const input_stream&);