The Battle for Wesnoth  1.15.0-dev
client.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
3  2008 - 2015 by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://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 #pragma once
17 
18 #include "addon/info.hpp"
20 #include "network_asio.hpp"
21 
22 /**
23  * Add-ons (campaignd) client class.
24  *
25  * This class encapsulates much of the logic behind the campaignd
26  * add-ons server interaction for the client-side. Most networking
27  * operations with it are implemented here.
28  */
30 {
31 public:
33 
35  {
38  };
39 
42  struct user_exit {};
43  struct user_disconnect {};
44 
45  addons_client(const addons_client&) = delete;
46  addons_client& operator=(const addons_client&) = delete;
47 
48  /**
49  * Constructor.
50  *
51  * @param address Add-ons server host address (i.e. localhost:15999).
52  */
53  explicit addons_client(const std::string& address);
54 
55  /**
56  * Try to establish a connection to the add-ons server.
57  */
58  void connect();
59 
60  /**
61  * Disconnect from the add-on server.
62  */
63  void disconnect() { conn_.reset(); }
64 
65  /** Returns the last error message sent by the server, or an empty string. */
66  const std::string& get_last_server_error() const { return last_error_; }
67 
68  /** Returns the last error message extra data sent by the server, or an empty string. */
69  const std::string& get_last_server_error_data() const { return last_error_data_; }
70 
71  /** Returns true if the client is connected to the server. */
72  bool is_connected() { return conn_ != nullptr; }
73 
74  /**
75  * Request the add-ons list from the server.
76  *
77  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
78  *
79  * @param cfg A config object whose contents are replaced with
80  * the server's list if available, cleared otherwise.
81  */
82  bool request_addons_list(config& cfg);
83 
84  /**
85  * Request the add-ons server distribution terms message.
86  */
87  bool request_distribution_terms(std::string& terms);
88 
89  /**
90  * Do a 'smart' fetch of an add-on, checking to avoid overwrites for devs and resolving dependencies, using gui interaction to handle issues that arise
91  * Returns: outcome: abort in case the user chose to abort because of an issue
92  * failure in case we resolved checks and dependencies, but fetching this particular add-on failed
93  * success otherwise
94  * wml_changed: indicates if new wml content was installed at any point
95  */
97 
98  /**
99  * Requests the specified add-on to be uploaded.
100  *
101  * This method reads the add-on upload passphrase and other data
102  * from the associated .pbl file. If the .pbl file doesn't have a
103  * passphrase, a new, random one will be automatically generated
104  * and written to the file for the user.
105  *
106  * @todo Notify the user about the automatic passphrase.
107  *
108  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
109  *
110  * @param id Id. of the add-on to upload.
111  * @param response_message The server response message on success, such as "add-on accepted".
112  * @param cfg The pbl config of the add-on with the specified id.
113  */
114  bool upload_addon(const std::string& id, std::string& response_message, config& cfg);
115 
116  /**
117  * Requests the specified add-on to be removed from the server.
118  *
119  * This method reads the add-on upload passphrase from the associated
120  * .pbl file.
121  *
122  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
123  *
124  * @param id Id. of the add-on to take down.
125  * @param response_message The server response message on success, such as "add-on accepted".
126  */
127  bool delete_remote_addon(const std::string& id, std::string& response_message);
128 
129 private:
130  enum class transfer_mode {download, connect, upload};
131 
132  std::string addr_;
133  std::string host_;
134  std::string port_;
135  std::unique_ptr<network_asio::connection> conn_;
136  std::string last_error_;
137  std::string last_error_data_;
138 
139  /**
140  * Downloads the specified add-on from the server.
141  *
142  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
143  *
144  * @param id Add-on id.
145  * @param title Add-on title, used for status display.
146  * @param archive_cfg Config object to hold the downloaded add-on archive data.
147  * @param increase_downloads Whether to request the server to increase the add-on's
148  * download count or not (e.g. when upgrading).
149  */
150  bool download_addon(config& archive_cfg, const std::string& id, const std::string& title, bool increase_downloads = true);
151 
152  /**
153  * Installs the specified add-on using an archive received from the server.
154  *
155  * An _info.cfg file will be added to the local directory for the add-on
156  * to keep track of version and dependency information.
157  */
158  bool install_addon(config& archive_cfg, const addon_info& info);
159 
160  // Asks the client to download and install an addon, reporting errors in a gui dialog. Returns true if new content was installed, false otherwise.
161  bool try_fetch_addon(const addon_info& addon);
162 
163  /**
164  * Warns the user about unresolved dependencies and installs them if they choose to do so.
165  * Returns: outcome: abort in case the user chose to abort because of an issue
166  * success otherwise
167  * wml_change: indicates if new wml content was installed
168  */
170 
171  /** Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it. */
173 
174  /** Makes sure the add-ons server connection is working. */
175  void check_connected() const;
176 
177  /**
178  * Sends a request to the add-ons server.
179  *
180  * @note This is an asynchronous operation. @a display_status_window
181  * should be called afterwards to wait for it to finish.
182  *
183  * @param request The client request WML.
184  * @param response A config object whose contents are replaced
185  * with the server response WML.
186  */
187  void send_request(const config& request, config& response);
188 
189  /**
190  * Sends a simple request message to the add-ons server.
191  *
192  * The real request sent consists of a WML object with an empty
193  * child node whose name corresponds to @a request_string
194  *
195  * @note This is an asynchronous operation. @a display_status_window
196  * should be called afterwards to wait for it to finish.
197  *
198  * @param request_string The client request string.
199  * @param response A config object whose contents are replaced
200  * with the server response WML.
201  */
202  void send_simple_request(const std::string& request_string, config& response);
203 
204  /**
205  * Waits for a network transfer, displaying a status window.
206  *
207  * The window is displayed with the specified contents. This
208  * method doesn't return until the network transfer is complete. It
209  * will throw a @a user_exit exception if the user cancels the
210  * transfer by canceling the status window.
211  */
212  void wait_for_transfer_done(const std::string& status_message, transfer_mode mode = transfer_mode::download);
213 
214  bool update_last_error(config& response_cfg);
215 };
install_result do_resolve_addon_dependencies(const addons_list &addons, const addon_info &addon)
Warns the user about unresolved dependencies and installs them if they choose to do so...
Definition: client.cpp:329
const std::string & get_last_server_error() const
Returns the last error message sent by the server, or an empty string.
Definition: client.hpp:66
std::string port_
Definition: client.hpp:134
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:228
std::string host_
Definition: client.hpp:133
logger & info()
Definition: log.cpp:90
bool try_fetch_addon(const addon_info &addon)
Definition: client.cpp:310
bool update_last_error(config &response_cfg)
Definition: client.cpp:490
addons_client(const addons_client &)=delete
std::string last_error_
Definition: client.hpp:136
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:249
void check_connected() const
Makes sure the add-ons server connection is working.
Definition: client.cpp:504
void send_request(const config &request, config &response)
Sends a request to the add-ons server.
Definition: client.cpp:513
const std::string & get_last_server_error_data() const
Returns the last error message extra data sent by the server, or an empty string. ...
Definition: client.hpp:69
addons_client & operator=(const addons_client &)=delete
void wait_for_transfer_done(const std::string &status_message, transfer_mode mode=transfer_mode::download)
Waits for a network transfer, displaying a status window.
Definition: client.cpp:563
bool is_connected()
Returns true if the client is connected to the server.
Definition: client.hpp:72
void disconnect()
Disconnect from the add-on server.
Definition: client.hpp:63
void connect()
Try to establish a connection to the add-ons server.
Definition: client.cpp:65
Add-ons (campaignd) client class.
Definition: client.hpp:29
std::string addr_
Definition: client.hpp:132
bool do_check_before_overwriting_addon(const addon_info &addon)
Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it...
Definition: client.cpp:431
install_outcome outcome
Definition: client.hpp:36
std::string last_error_data_
Definition: client.hpp:137
std::unique_ptr< network_asio::connection > conn_
Definition: client.hpp:135
bool upload_addon(const std::string &id, std::string &response_message, config &cfg)
Requests the specified add-on to be uploaded.
Definition: client.cpp:112
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
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:196
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:96
std::map< std::string, addon_info > addons_list
Definition: info.hpp:26
install_result install_addon_with_checks(const addons_list &addons, const addon_info &addon)
Do a &#39;smart&#39; fetch of an add-on, checking to avoid overwrites for devs and resolving dependencies...
Definition: client.cpp:464
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:79
void send_simple_request(const std::string &request_string, config &response)
Sends a simple request message to the add-ons server.
Definition: client.cpp:521