-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
client.hpp
190 lines (165 loc) · 6.61 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
/*
Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
2008 - 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 ADDON_CLIENT_HPP_INCLUDED
#define ADDON_CLIENT_HPP_INCLUDED
#include "gui/dialogs/network_transmission.hpp"
#include <boost/noncopyable.hpp>
#include "network_asio.hpp"
struct addon_info;
/**
* 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 : private boost::noncopyable
{
public:
struct invalid_server_address {};
struct not_connected_to_server {};
struct user_exit {};
/**
* Constructor.
*
* @param v Target for UI rendering for the progress dialog.
* @param address Add-ons server host address (i.e. localhost:15999).
*/
addons_client(CVideo& v, const std::string& address);
~addons_client();
/**
* Try to establish a connection to the add-ons server.
*/
void connect();
/** Returns the last error message sent by the server, or an empty string. */
const std::string& get_last_server_error() const { return last_error_; }
/**
* 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);
/**
* 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.
*
* @todo FIXME Refactor this again once I figure out a better way
* to not transfer so much information in the method signature! Perhaps
* we'd reask the server for the add-ons list and extract the information
* from there again, since there isn't any way to request info for a
* single entry atm.
*
* @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.
*
* @todo FIXME Refactor this again once I figure out a better way
* to not transfer so much information in the method signature! Perhaps
* we'd reask the server for the add-ons list and extract the information
* from there again, since there isn't any way to request info for a
* single entry atm.
*/
bool install_addon(config& archive_cfg, const addon_info& info);
/**
* 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:
CVideo& v_;
std::string addr_;
std::string host_;
std::string port_;
network_asio::connection* conn_;
gui2::tnetwork_transmission* stat_;
std::string last_error_;
/** 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, bool track_upload = false);
bool update_last_error(config& response_cfg);
};
#endif