The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
client.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2008 by David White <[email protected]>
3  2008 - 2015 by Ignacio Riquelme Morelle <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #ifndef ADDON_CLIENT_HPP_INCLUDED
17 #define ADDON_CLIENT_HPP_INCLUDED
18 
20 #include <boost/noncopyable.hpp>
21 #include "network_asio.hpp"
22 
23 struct addon_info;
24 
25 /**
26  * Add-ons (campaignd) client class.
27  *
28  * This class encapsulates much of the logic behind the campaignd
29  * add-ons server interaction for the client-side. Most networking
30  * operations with it are implemented here.
31  */
32 class addons_client : private boost::noncopyable
33 {
34 public:
37  struct user_exit {};
38 
39  /**
40  * Constructor.
41  *
42  * @param v Target for UI rendering for the progress dialog.
43  * @param address Add-ons server host address (i.e. localhost:15999).
44  */
46 
48 
49  /**
50  * Try to establish a connection to the add-ons server.
51  */
52  void connect();
53 
54  /** Returns the last error message sent by the server, or an empty string. */
55  const std::string& get_last_server_error() const { return last_error_; }
56 
57  /**
58  * Request the add-ons list from the server.
59  *
60  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
61  *
62  * @param cfg A config object whose contents are replaced with
63  * the server's list if available, cleared otherwise.
64  */
65  bool request_addons_list(config& cfg);
66 
67  /**
68  * Request the add-ons server distribution terms message.
69  */
71 
72  /**
73  * Downloads the specified add-on from the server.
74  *
75  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
76  *
77  * @todo FIXME Refactor this again once I figure out a better way
78  * to not transfer so much information in the method signature! Perhaps
79  * we'd reask the server for the add-ons list and extract the information
80  * from there again, since there isn't any way to request info for a
81  * single entry atm.
82  *
83  * @param id Add-on id.
84  * @param title Add-on title, used for status display.
85  * @param archive_cfg Config object to hold the downloaded add-on archive data.
86  * @param increase_downloads Whether to request the server to increase the add-on's
87  * download count or not (e.g. when upgrading).
88  */
89  bool download_addon(config& archive_cfg, const std::string& id, const std::string& title, bool increase_downloads = true);
90 
91  /**
92  * Installs the specified add-on using an archive received from the server.
93  *
94  * An _info.cfg file will be added to the local directory for the add-on
95  * to keep track of version and dependency information.
96  *
97  * @todo FIXME Refactor this again once I figure out a better way
98  * to not transfer so much information in the method signature! Perhaps
99  * we'd reask the server for the add-ons list and extract the information
100  * from there again, since there isn't any way to request info for a
101  * single entry atm.
102  */
103  bool install_addon(config& archive_cfg, const addon_info& info);
104 
105  /**
106  * Requests the specified add-on to be uploaded.
107  *
108  * This method reads the add-on upload passphrase and other data
109  * from the associated .pbl file. If the .pbl file doesn't have a
110  * passphrase, a new, random one will be automatically generated
111  * and written to the file for the user.
112  *
113  * @todo Notify the user about the automatic passphrase.
114  *
115  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
116  *
117  * @param id Id. of the add-on to upload.
118  * @param response_message The server response message on success, such as "add-on accepted".
119  * @param cfg The pbl config of the add-on with the specified id.
120  */
121  bool upload_addon(const std::string& id, std::string& response_message, config& cfg);
122 
123  /**
124  * Requests the specified add-on to be removed from the server.
125  *
126  * This method reads the add-on upload passphrase from the associated
127  * .pbl file.
128  *
129  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
130  *
131  * @param id Id. of the add-on to take down.
132  * @param response_message The server response message on success, such as "add-on accepted".
133  */
134  bool delete_remote_addon(const std::string& id, std::string& response_message);
135 
136 private:
144 
145  /** Makes sure the add-ons server connection is working. */
146  void check_connected() const;
147 
148  /**
149  * Sends a request to the add-ons server.
150  *
151  * @note This is an asynchronous operation. @a display_status_window
152  * should be called afterwards to wait for it to finish.
153  *
154  * @param request The client request WML.
155  * @param response A config object whose contents are replaced
156  * with the server response WML.
157  */
158  void send_request(const config& request, config& response);
159 
160  /**
161  * Sends a simple request message to the add-ons server.
162  *
163  * The real request sent consists of a WML object with an empty
164  * child node whose name corresponds to @a request_string
165  *
166  * @note This is an asynchronous operation. @a display_status_window
167  * should be called afterwards to wait for it to finish.
168  *
169  * @param request_string The client request string.
170  * @param response A config object whose contents are replaced
171  * with the server response WML.
172  */
173  void send_simple_request(const std::string& request_string, config& response);
174 
175  /**
176  * Waits for a network transfer, displaying a status window.
177  *
178  * The window is displayed with the specified contents. This
179  * method doesn't return until the network transfer is complete. It
180  * will throw a @a user_exit exception if the user cancels the
181  * transfer by canceling the status window.
182  */
183  void wait_for_transfer_done(const std::string& status_message, bool track_upload = false);
184 
185  bool update_last_error(config& response_cfg);
186 };
187 
188 #endif
189 
190 
std::string port_
Definition: client.hpp:140
Dialog that tracks network transmissions.
bool download_addon(config &archive_cfg, const std::string &id, const std::string &title, bool increase_downloads=true)
Downloads the specified add-on from the server.
Definition: client.cpp:184
std::string host_
Definition: client.hpp:139
logger & info()
Definition: log.cpp:91
network_asio::connection * conn_
Definition: client.hpp:141
Definition: video.hpp:58
bool update_last_error(config &response_cfg)
Definition: client.cpp:261
addons_client(CVideo &v, const std::string &address)
Constructor.
Definition: client.cpp:35
std::string last_error_
Definition: client.hpp:143
bool install_addon(config &archive_cfg, const addon_info &info)
Installs the specified add-on using an archive received from the server.
Definition: client.cpp:205
gui2::tnetwork_transmission * stat_
Definition: client.hpp:142
void send_request(const config &request, config &response)
Sends a request to the add-ons server.
Definition: client.cpp:282
const std::string & get_last_server_error() const
Returns the last error message sent by the server, or an empty string.
Definition: client.hpp:55
void check_connected() const
Makes sure the add-ons server connection is working.
Definition: client.cpp:273
const GLdouble * v
Definition: glew.h:1359
A class that represents a TCP/IP connection.
CVideo & v_
Definition: client.hpp:137
void connect()
Try to establish a connection to the add-ons server.
Definition: client.cpp:57
Add-ons (campaignd) client class.
Definition: client.hpp:32
GLuint GLuint64EXT address
Definition: glew.h:11276
std::string addr_
Definition: client.hpp:138
void wait_for_transfer_done(const std::string &status_message, bool track_upload=false)
Waits for a network transfer, displaying a status window.
Definition: client.cpp:316
bool upload_addon(const std::string &id, std::string &response_message, config &cfg)
Requests the specified add-on to be uploaded.
Definition: client.cpp:102
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool delete_remote_addon(const std::string &id, std::string &response_message)
Requests the specified add-on to be removed from the server.
Definition: client.cpp:151
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:86
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:70
void send_simple_request(const std::string &request_string, config &response)
Sends a simple request message to the add-ons server.
Definition: client.cpp:290