addon/client.hpp

Go to the documentation of this file.
00001 /* $Id: client.hpp 54223 2012-05-19 09:13:41Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004                  2008 - 2012 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 #ifndef ADDON_CLIENT_HPP_INCLUDED
00018 #define ADDON_CLIENT_HPP_INCLUDED
00019 
00020 #include "gui/dialogs/network_transmission.hpp"
00021 #include <boost/noncopyable.hpp>
00022 #include "network_asio.hpp"
00023 
00024 struct addon_info;
00025 
00026 /**
00027  * Add-ons (campaignd) client class.
00028  *
00029  * This class encapsulates much of the logic behind the campaignd
00030  * add-ons server interaction for the client-side. Most networking
00031  * operations with it are implemented here.
00032  */
00033 class addons_client : private boost::noncopyable
00034 {
00035 public:
00036     struct invalid_server_address {};
00037     struct not_connected_to_server {};
00038     struct user_exit {};
00039 
00040     /**
00041      * Constructor.
00042      *
00043      * @param disp    Display object on which to display a status dialog.
00044      * @param address Add-ons server host address (i.e. localhost:15999).
00045      */
00046     addons_client(display& disp, const std::string& address);
00047 
00048     ~addons_client();
00049 
00050     /** Returns the last error message sent by the server, or an empty string. */
00051     const std::string& get_last_server_error() const { return last_error_; }
00052 
00053     /**
00054      * Request the add-ons list from the server.
00055      *
00056      * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
00057      *
00058      * @param cfg A config object whose contents are replaced with
00059      *            the server's list.
00060      */
00061     bool request_addons_list(config& cfg);
00062 
00063     /**
00064      * Request the add-ons server distribution terms message.
00065      */
00066     bool request_distribution_terms(std::string& terms);
00067 
00068     /**
00069      * Downloads the specified add-on from the server.
00070      *
00071      * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
00072      *
00073      * @todo FIXME Refactor this again once I figure out a better way
00074      * to not transfer so much information in the method signature! Perhaps
00075      * we'd reask the server for the add-ons list and extract the information
00076      * from there again, since there isn't any way to request info for a
00077      * single entry atm.
00078      *
00079      * @param id          Add-on id.
00080      * @param title       Add-on title, used for status display.
00081      * @param archive_cfg Config object to hold the downloaded add-on archive data.
00082      */
00083     bool download_addon(config& archive_cfg, const std::string& id, const std::string& title);
00084 
00085     /**
00086      * Installs the specified add-on using an archive received from the server.
00087      *
00088      * An _info.cfg file will be added to the local directory for the add-on
00089      * to keep track of version and dependency information.
00090      *
00091      * @todo FIXME Refactor this again once I figure out a better way
00092      * to not transfer so much information in the method signature! Perhaps
00093      * we'd reask the server for the add-ons list and extract the information
00094      * from there again, since there isn't any way to request info for a
00095      * single entry atm.
00096      */
00097     bool install_addon(config& archive_cfg, const addon_info& info);
00098 
00099     /**
00100      * Requests the specified add-on to be uploaded.
00101      *
00102      * This method reads the add-on upload passphrase and other data
00103      * from the associated .pbl file. If the .pbl file doesn't have a
00104      * passphrase, a new, random one will be automatically generated
00105      * and written to the file for the user.
00106      *
00107      * @todo Notify the user about the automatic passphrase.
00108      *
00109      * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
00110      *
00111      * @param id               Id. of the add-on to upload.
00112      * @param response_message The server response message on success, such as "add-on accepted".
00113      */
00114     bool upload_addon(const std::string& id, std::string& response_message);
00115 
00116     /**
00117      * Requests the specified add-on to be removed from the server.
00118      *
00119      * This method reads the add-on upload passphrase from the associated
00120      * .pbl file.
00121      *
00122      * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
00123      *
00124      * @param id               Id. of the add-on to take down.
00125      * @param response_message The server response message on success, such as "add-on accepted".
00126      */
00127     bool delete_remote_addon(const std::string& id, std::string& response_message);
00128 
00129 private:
00130     display& disp_;
00131     std::string addr_;
00132     std::string host_;
00133     std::string port_;
00134     network_asio::connection* conn_;
00135     gui2::tnetwork_transmission* stat_;
00136     std::string last_error_;
00137 
00138     /** Makes sure the add-ons server connection is working. */
00139     void check_connected() const;
00140 
00141     /**
00142      * Sends a request to the add-ons server.
00143      *
00144      * @note This is an asynchronous operation. @a display_status_window
00145      * should be called afterwards to wait for it to finish.
00146      *
00147      * @param request  The client request WML.
00148      * @param response A config object whose contents are replaced
00149      *                 with the server response WML.
00150      */
00151     void send_request(const config& request, config& response);
00152 
00153     /**
00154      * Sends a simple request message to the add-ons server.
00155      *
00156      * The real request sent consists of a WML object with an empty
00157      * child node whose name corresponds to @a request_string
00158      *
00159      * @note This is an asynchronous operation. @a display_status_window
00160      * should be called afterwards to wait for it to finish.
00161      *
00162      * @param request_string  The client request string.
00163      * @param response        A config object whose contents are replaced
00164      *                        with the server response WML.
00165      */
00166     void send_simple_request(const std::string& request_string, config& response);
00167 
00168     /**
00169      * Waits for a network transfer, displaying a status window.
00170      *
00171      * The window is displayed with the specified contents. This
00172      * method doesn't return until the network transfer is complete. It
00173      * will throw a @a user_exit exception if the user cancels the
00174      * transfer by canceling the status window.
00175      */
00176     void wait_for_transfer_done(const std::string& status_message, bool track_upload = false);
00177 
00178     bool update_last_error(config& response_cfg);
00179 };
00180 
00181 #endif
00182 
00183 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:26 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs