The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
campaign_server.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef CAMPAIGN_SERVER_HPP_INCLUDED
16 #define CAMPAIGN_SERVER_HPP_INCLUDED
17 
19 #include "network.hpp"
20 #include "server/input_stream.hpp"
21 
22 #include "utils/functional.hpp"
23 #include <boost/scoped_ptr.hpp>
24 #include <boost/unordered_map.hpp>
25 
26 namespace campaignd {
27 
28 /**
29  * Legacy add-ons server.
30  */
31 class server : private boost::noncopyable
32 {
33 public:
34  explicit server(const std::string& cfg_file,
35  size_t min_threads = 10,
36  size_t max_threads = 0);
37  ~server();
38 
39  /**
40  * Runs the server request processing loop.
41  */
42  void run();
43 
44 private:
45  /**
46  * Client request information object.
47  *
48  * Contains data and metadata associated with a single request from a
49  * remote add-ons client, in a light-weight format for passing to request
50  * handlers.
51  */
52  struct request
53  {
54  const std::string& cmd;
55  const config& cfg;
56 
59 
60  /**
61  * Constructor.
62  *
63  * @param reqcmd Request command.
64  * @param reqcfg Request WML body.
65  * @param reqsock Client socket that initiated the request.
66  *
67  * @note Neither @a reqcmd nor @a reqcfg are copied into instances, so
68  * they are required to exist for as long as every @a request
69  * instance that uses them.
70  */
71  request(const std::string& reqcmd,
72  const config& reqcfg,
73  network::connection reqsock)
74  : cmd(reqcmd)
75  , cfg(reqcfg)
76  , sock(reqsock)
77  , addr(network::ip_address(sock))
78  {}
79  };
80 
81  typedef std::function<void (server*, const request& req)> request_handler;
82  typedef std::map<std::string, request_handler> request_handlers_table;
83 
86 
87  bool read_only_;
88  int compress_level_; /**< Used for add-on archives. */
89 
90  boost::scoped_ptr<input_stream> input_; /**< Server control socket. */
91 
92  std::map<std::string, std::string> hooks_;
93  request_handlers_table handlers_;
94 
96 
99 
100  int port_;
101 
104 
105  /**
106  * Reads the server configuration from WML.
107  *
108  * @return The configured listening port number.
109  */
110  int load_config();
111 
112  /**
113  * Writes the server configuration WML back to disk.
114  */
115  void write_config();
116 
117  /**
118  * Reads the add-ons upload blacklist from WML.
119  */
120  void load_blacklist();
121 
122  /**
123  * Fires a hook script.
124  */
125  void fire(const std::string& hook, const std::string& addon);
126 
127  /** Retrieves the contents of the [campaigns] WML node. */
128  const config& campaigns() const { return cfg_.child("campaigns"); }
129 
130  /** Retrieves the contents of the [campaigns] WML node. */
131  config& campaigns() { return cfg_.child("campaigns"); }
132 
133  /** Retrieves a campaign by id if found, or a null config otherwise. */
134  config& get_campaign(const std::string& id) { return campaigns().find_child("campaign", "name", id); }
135 
136  /** Retrieves the contents of the [server_info] WML node. */
137  const config& server_info() const { return cfg_.child("server_info"); }
138 
139  /** Retrieves the contents of the [server_info] WML node. */
140  config& server_info() { return cfg_.child("server_info"); }
141 
142  //
143  // Request handling.
144  //
145 
146  /**
147  * Registers client request handlers.
148  *
149  * This is called by the class constructor. Individual handlers must be
150  * methods of this class that take a single parameter of type @a request
151  * and they are registered using the @a register_handler method.
152  *
153  * When adding new handlers, make sure to update the implementation of
154  * this method accordingly so they are recognized and invoked at runtime.
155  */
156  void register_handlers();
157 
158  /**
159  * Registers a single request handler.
160  *
161  * @param cmd The request command, corresponding to the name of the [tag}
162  * with the request body (e.g. "handle_request_terms").
163  * @param func The request function. This should be a class method passed
164  * as a @a std::bind function object that takes a single
165  * parameter of type @a request.
166  */
167  void register_handler(const std::string& cmd, const request_handler& func);
168 
169  void handle_request_campaign_list(const request&);
170  void handle_request_campaign(const request&);
171  void handle_request_terms(const request&);
172  void handle_upload(const request&);
173  void handle_delete(const request&);
174  void handle_change_passphrase(const request&);
175 
176  //
177  // Generic responses.
178  //
179 
180  /**
181  * Send a client an informational message.
182  *
183  * The WML sent consists of a document containing a single @p [message]
184  * child with a @a message attribute holding the value of @a msg.
185  */
186  void send_message(const std::string& msg, network::connection sock);
187 
188  /**
189  * Send a client an error message.
190  *
191  * The WML sent consists of a document containing a single @p [error] child
192  * with a @a message attribute holding the value of @a msg. In addition to
193  * sending the error to the client, a line with the client IP and message
194  * is recorded to the server log.
195  */
196  void send_error(const std::string& msg, network::connection sock);
197 };
198 
199 } // end namespace campaignd
200 
201 #endif
High level network layer for config object transport.
Definition: network.cpp:212
std::string feedback_url_format_
Legacy add-ons server.
void handle_request_terms(const request &)
void handle_delete(const request &)
Client request information object.
config & campaigns()
Retrieves the contents of the [campaigns] WML node.
const config & server_info() const
Retrieves the contents of the [server_info] WML node.
void fire(const std::string &hook, const std::string &addon)
Fires a hook script.
request_handlers_table handlers_
std::map< std::string, std::string > hooks_
void load_blacklist()
Reads the add-ons upload blacklist from WML.
void register_handlers()
Registers client request handlers.
const std::string cfg_file_
std::map< std::string, request_handler > request_handlers_table
const config & campaigns() const
Retrieves the contents of the [campaigns] WML node.
std::string blacklist_file_
Add-on blacklist table.
Definition: blacklist.hpp:44
const network::connection sock
void handle_change_passphrase(const request &)
const network::manager net_manager_
request(const std::string &reqcmd, const config &reqcfg, network::connection reqsock)
Constructor.
void send_error(const std::string &msg, network::connection sock)
Send a client an error message.
void write_config()
Writes the server configuration WML back to disk.
const network::server_manager server_manager_
boost::scoped_ptr< input_stream > input_
Server control socket.
config & server_info()
Retrieves the contents of the [server_info] WML node.
void handle_request_campaign(const request &)
A server manager causes listening on a given port to occur for the duration of its lifetime...
Definition: network.hpp:81
void send_message(const std::string &msg, network::connection sock)
Send a client an informational message.
int load_config()
Reads the server configuration from WML.
void register_handler(const std::string &cmd, const request_handler &func)
Registers a single request handler.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
void handle_request_campaign_list(const request &)
std::function< void(server *, const request &req)> request_handler
std::string ip_address(connection connection_num)
Function to get the remote ip address of a socket.
Definition: network.cpp:1179
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:658
config & find_child(const std::string &key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:1010
server(const std::string &cfg_file, size_t min_threads=10, size_t max_threads=0)
int connection
Definition: network.hpp:74
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void run()
Runs the server request processing loop.
void handle_upload(const request &)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
config & get_campaign(const std::string &id)
Retrieves a campaign by id if found, or a null config otherwise.
int compress_level_
Used for add-on archives.