The Battle for Wesnoth  1.15.0-dev
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
addons_client Class Reference

Add-ons (campaignd) client class. More...

#include <client.hpp>

Classes

struct  install_result
 
struct  invalid_server_address
 
struct  not_connected_to_server
 
struct  user_disconnect
 
struct  user_exit
 

Public Types

enum  install_outcome { install_outcome::success, install_outcome::failure, install_outcome::abort }
 

Public Member Functions

 addons_client (const addons_client &)=delete
 
addons_clientoperator= (const addons_client &)=delete
 
 addons_client (const std::string &address)
 Constructor. More...
 
void connect ()
 Try to establish a connection to the add-ons server. More...
 
void disconnect ()
 Disconnect from the add-on server. More...
 
const std::string & get_last_server_error () const
 Returns the last error message sent by the server, or an empty string. More...
 
const std::string & get_last_server_error_data () const
 Returns the last error message extra data sent by the server, or an empty string. More...
 
bool is_connected ()
 Returns true if the client is connected to the server. More...
 
bool request_addons_list (config &cfg)
 Request the add-ons list from the server. More...
 
bool request_distribution_terms (std::string &terms)
 Request the add-ons server distribution terms message. More...
 
install_result install_addon_with_checks (const addons_list &addons, const addon_info &addon)
 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. More...
 
bool upload_addon (const std::string &id, std::string &response_message, config &cfg)
 Requests the specified add-on to be uploaded. More...
 
bool delete_remote_addon (const std::string &id, std::string &response_message)
 Requests the specified add-on to be removed from the server. More...
 

Private Types

enum  transfer_mode { transfer_mode::download, transfer_mode::connect, transfer_mode::upload }
 

Private Member Functions

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. More...
 
bool install_addon (config &archive_cfg, const addon_info &info)
 Installs the specified add-on using an archive received from the server. More...
 
bool try_fetch_addon (const addon_info &addon)
 
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. More...
 
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. More...
 
void check_connected () const
 Makes sure the add-ons server connection is working. More...
 
void send_request (const config &request, config &response)
 Sends a request to the add-ons server. More...
 
void send_simple_request (const std::string &request_string, config &response)
 Sends a simple request message to the add-ons server. More...
 
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. More...
 
bool update_last_error (config &response_cfg)
 

Private Attributes

std::string addr_
 
std::string host_
 
std::string port_
 
std::unique_ptr< network_asio::connectionconn_
 
std::string last_error_
 
std::string last_error_data_
 

Detailed Description

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.

Definition at line 29 of file client.hpp.

Member Enumeration Documentation

◆ install_outcome

Enumerator
success 
failure 
abort 

Definition at line 32 of file client.hpp.

◆ transfer_mode

enum addons_client::transfer_mode
strongprivate
Enumerator
download 
connect 
upload 

Definition at line 130 of file client.hpp.

Constructor & Destructor Documentation

◆ addons_client() [1/2]

addons_client::addons_client ( const addons_client )
delete

◆ addons_client() [2/2]

addons_client::addons_client ( const std::string &  address)
explicit

Constructor.

Parameters
addressAdd-ons server host address (i.e. localhost:15999).

Definition at line 45 of file client.cpp.

References addr_, default_campaignd_port, host_, port_, and utils::split().

Member Function Documentation

◆ check_connected()

void addons_client::check_connected ( ) const
private

Makes sure the add-ons server connection is working.

Definition at line 507 of file client.cpp.

References conn_, and ERR_ADDONS.

Referenced by send_request(), and wait_for_transfer_done().

◆ connect()

void addons_client::connect ( )

Try to establish a connection to the add-ons server.

Definition at line 66 of file client.cpp.

References addr_, conn_, connect, host_, LOG_ADDONS, port_, VGETTEXT, and wait_for_transfer_done().

Referenced by ad_hoc_addon_fetch_session().

◆ delete_remote_addon()

bool addons_client::delete_remote_addon ( const std::string &  id,
std::string &  response_message 
)

Requests the specified add-on to be removed from the server.

This method reads the add-on upload passphrase from the associated .pbl file.

Returns
true on success, false on failure. Retrieve the error message with get_last_server_error.
Parameters
idId. of the add-on to take down.
response_messageThe server response message on success, such as "add-on accepted".

Definition at line 197 of file client.cpp.

References config::add_child(), config::child(), font::escape_text(), get_addon_pbl_info(), LOG_ADDONS, make_addon_title(), send_request(), update_last_error(), VGETTEXT, and wait_for_transfer_done().

Referenced by gui2::dialogs::addon_manager::delete_addon(), and is_connected().

◆ disconnect()

void addons_client::disconnect ( )
inline

Disconnect from the add-on server.

Definition at line 63 of file client.hpp.

References conn_.

◆ do_check_before_overwriting_addon()

bool addons_client::do_check_before_overwriting_addon ( const addon_info addon)
private

Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it.

Definition at line 434 of file client.cpp.

References _(), utils::bullet_list(), font::escape_text(), have_addon_in_vcs_tree(), have_addon_pbl_info(), addon_info::id, gui2::OK, gui2::show_message(), addon_info::title, VGETTEXT, and gui2::dialogs::message::yes_no_buttons.

Referenced by install_addon_with_checks().

◆ do_resolve_addon_dependencies()

addons_client::install_result addons_client::do_resolve_addon_dependencies ( const addons_list addons,
const addon_info addon 
)
private

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

Definition at line 330 of file client.cpp.

References _(), _n(), abort, ADDON_INSTALLED_UPGRADABLE, ADDON_NONE, utils::bullet_list(), get_addon_tracking_info(), lg::info(), is_addon_installed(), make_addon_title(), gui2::OK, preferences::options(), addons_client::install_result::outcome, addon_info::resolve_dependencies(), gui2::dialogs::modal_dialog::show(), gui2::show_message(), addon_tracking_info::state, success, addon_info::title, try_fetch_addon(), font::unicode_bullet, cursor::WAIT, addons_client::install_result::wml_changed, and gui2::dialogs::message::yes_no_buttons.

Referenced by install_addon_with_checks().

◆ download_addon()

bool addons_client::download_addon ( config archive_cfg,
const std::string &  id,
const std::string &  title,
bool  increase_downloads = true 
)
private

Downloads the specified add-on from the server.

Returns
true on success, false on failure. Retrieve the error message with get_last_server_error.
Parameters
idAdd-on id.
titleAdd-on title, used for status display.
archive_cfgConfig object to hold the downloaded add-on archive data.
increase_downloadsWhether to request the server to increase the add-on's download count or not (e.g. when upgrading).

Definition at line 229 of file client.cpp.

References config::add_child(), config::clear(), font::escape_text(), LOG_ADDONS, send_request(), update_last_error(), VGETTEXT, and wait_for_transfer_done().

Referenced by try_fetch_addon().

◆ get_last_server_error()

const std::string& addons_client::get_last_server_error ( ) const
inline

Returns the last error message sent by the server, or an empty string.

Definition at line 66 of file client.hpp.

References last_error_.

Referenced by gui2::dialogs::addon_manager::delete_addon(), gui2::dialogs::addon_manager::publish_addon(), and try_fetch_addon().

◆ get_last_server_error_data()

const std::string& addons_client::get_last_server_error_data ( ) const
inline

Returns the last error message extra data sent by the server, or an empty string.

Definition at line 69 of file client.hpp.

References last_error_data_.

Referenced by gui2::dialogs::addon_manager::publish_addon().

◆ install_addon()

bool addons_client::install_addon ( config archive_cfg,
const addon_info info 
)
private

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.

Definition at line 250 of file client.cpp.

References config::add_child(), check_case_insensitive_duplicates(), check_names_legal(), font::escape_text(), config::find_child(), addon_info::id, LOG_ADDONS, remove_local_addon(), gui2::show_error_message(), addon_info::title, unarchive_addon(), VGETTEXT, cursor::WAIT, write(), addon_info::write_minimal(), and WRN_ADDONS.

Referenced by try_fetch_addon().

◆ install_addon_with_checks()

addons_client::install_result addons_client::install_addon_with_checks ( const addons_list addons,
const addon_info addon 
)

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.

Definition at line 467 of file client.cpp.

References abort, do_check_before_overwriting_addon(), do_resolve_addon_dependencies(), failure, addons_client::install_result::outcome, success, try_fetch_addon(), and addons_client::install_result::wml_changed.

Referenced by ad_hoc_addon_fetch_session(), gui2::dialogs::addon_manager::install_addon(), is_connected(), and gui2::dialogs::addon_manager::update_all_addons().

◆ is_connected()

bool addons_client::is_connected ( )
inline

Returns true if the client is connected to the server.

Definition at line 72 of file client.hpp.

References conn_, delete_remote_addon(), install_addon_with_checks(), request_addons_list(), request_distribution_terms(), and upload_addon().

◆ operator=()

addons_client& addons_client::operator= ( const addons_client )
delete

◆ request_addons_list()

bool addons_client::request_addons_list ( config cfg)

Request the add-ons list from the server.

Returns
true on success, false on failure. Retrieve the error message with get_last_server_error.
Parameters
cfgA config object whose contents are replaced with the server's list if available, cleared otherwise.
Todo:
FIXME: get rid of this legacy "campaign"/"campaigns" silliness

Definition at line 80 of file client.cpp.

References _(), config::child(), config::clear(), send_simple_request(), swap(), update_last_error(), and wait_for_transfer_done().

Referenced by gui2::dialogs::addon_manager::fetch_addons_list(), and is_connected().

◆ request_distribution_terms()

bool addons_client::request_distribution_terms ( std::string &  terms)

Request the add-ons server distribution terms message.

Definition at line 97 of file client.cpp.

References _(), config::child(), send_simple_request(), update_last_error(), and wait_for_transfer_done().

Referenced by is_connected(), and gui2::dialogs::addon_manager::publish_addon().

◆ send_request()

void addons_client::send_request ( const config request,
config response 
)
private

Sends a request to the add-ons server.

Note
This is an asynchronous operation. display_status_window should be called afterwards to wait for it to finish.
Parameters
requestThe client request WML.
responseA config object whose contents are replaced with the server response WML.

Definition at line 516 of file client.cpp.

References check_connected(), config::clear(), and conn_.

Referenced by delete_remote_addon(), download_addon(), send_simple_request(), and upload_addon().

◆ send_simple_request()

void addons_client::send_simple_request ( const std::string &  request_string,
config response 
)
private

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 request_string

Note
This is an asynchronous operation. display_status_window should be called afterwards to wait for it to finish.
Parameters
request_stringThe client request string.
responseA config object whose contents are replaced with the server response WML.

Definition at line 524 of file client.cpp.

References config::add_child(), and send_request().

Referenced by request_addons_list(), and request_distribution_terms().

◆ try_fetch_addon()

bool addons_client::try_fetch_addon ( const addon_info addon)
private

◆ update_last_error()

bool addons_client::update_last_error ( config response_cfg)
private

◆ upload_addon()

bool addons_client::upload_addon ( const std::string &  id,
std::string &  response_message,
config cfg 
)

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.
Returns
true on success, false on failure. Retrieve the error message with get_last_server_error.
Parameters
idId. of the add-on to upload.
response_messageThe server response message on success, such as "add-on accepted".
cfgThe pbl config of the add-on with the specified id.

Definition at line 113 of file client.cpp.

References config::add_child(), addon_name_legal(), archive_addon(), check_case_insensitive_duplicates(), check_names_legal(), config::child(), font::escape_text(), randomness::generator, randomness::rng::get_random_int(), utils::join(), last_error_, last_error_data_, LOG_ADDONS, make_addon_title(), n, send_request(), set_addon_pbl_info(), update_last_error(), upload, VGETTEXT, and wait_for_transfer_done().

Referenced by is_connected(), and gui2::dialogs::addon_manager::publish_addon().

◆ wait_for_transfer_done()

void addons_client::wait_for_transfer_done ( const std::string &  status_message,
transfer_mode  mode = transfer_mode::download 
)
private

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 user_exit exception if the user cancels the transfer by canceling the status window.

Definition at line 566 of file client.cpp.

References _(), check_connected(), conn_, connect, download, gui2::dialogs::modal_dialog::show(), and upload.

Referenced by connect(), delete_remote_addon(), download_addon(), request_addons_list(), request_distribution_terms(), and upload_addon().

Member Data Documentation

◆ addr_

std::string addons_client::addr_
private

Definition at line 132 of file client.hpp.

Referenced by addons_client(), and connect().

◆ conn_

std::unique_ptr<network_asio::connection> addons_client::conn_
private

◆ host_

std::string addons_client::host_
private

Definition at line 133 of file client.hpp.

Referenced by addons_client(), and connect().

◆ last_error_

std::string addons_client::last_error_
private

Definition at line 136 of file client.hpp.

Referenced by get_last_server_error(), update_last_error(), and upload_addon().

◆ last_error_data_

std::string addons_client::last_error_data_
private

Definition at line 137 of file client.hpp.

Referenced by get_last_server_error_data(), update_last_error(), and upload_addon().

◆ port_

std::string addons_client::port_
private

Definition at line 134 of file client.hpp.

Referenced by addons_client(), and connect().


The documentation for this class was generated from the following files: