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