2013-07-18 17:18:40 -04:00
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2013 Glenn De Jonghe
|
|
|
|
// 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 3
|
|
|
|
// 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; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#ifndef HEADER_ONLINE_REQUEST_HPP
|
|
|
|
#define HEADER_ONLINE_REQUEST_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "utils/cpp2011.h"
|
|
|
|
#include "utils/string_utils.hpp"
|
2013-07-19 17:20:32 -04:00
|
|
|
#include "utils/synchronised.hpp"
|
|
|
|
#include "io/file_manager.hpp"
|
2013-07-18 17:18:40 -04:00
|
|
|
|
2013-07-19 17:20:32 -04:00
|
|
|
namespace Online{
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a request for the http connector. They will be sorted by priorities.
|
|
|
|
* \ingroup online
|
|
|
|
*/
|
|
|
|
class Request
|
|
|
|
{
|
2013-07-20 20:47:48 -04:00
|
|
|
public:
|
|
|
|
/** List of different types of requests which are used as key in the response map
|
|
|
|
*/
|
|
|
|
enum RequestType {
|
|
|
|
RT_QUIT,
|
|
|
|
RT_SIGN_IN,
|
|
|
|
RT_SIGN_OUT,
|
|
|
|
RT_SIGN_UP,
|
|
|
|
RT_SERVER_CREATION,
|
|
|
|
RT_SERVER_JOIN
|
|
|
|
};
|
|
|
|
|
2013-07-18 17:18:40 -04:00
|
|
|
protected:
|
|
|
|
/** The priority of this request. The higher the value the more
|
|
|
|
important this request is. */
|
|
|
|
int m_priority;
|
|
|
|
/** Cancel this request if it is active. */
|
|
|
|
bool m_cancel;
|
|
|
|
|
2013-07-20 20:47:48 -04:00
|
|
|
const RequestType m_type;
|
|
|
|
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
/** True if the memory for this Request should be managed by
|
|
|
|
* http connector (i.e. this object is freed once the request
|
|
|
|
* is handled). Otherwise the memory is not freed, so it must
|
|
|
|
* be freed by the calling function. */
|
|
|
|
bool m_manage_memory;
|
|
|
|
|
2013-07-20 20:47:48 -04:00
|
|
|
Synchronised<bool> m_done;
|
2013-07-19 18:12:36 -04:00
|
|
|
|
2013-07-18 17:18:40 -04:00
|
|
|
virtual void beforeOperation() {}
|
|
|
|
virtual void operation() = 0;
|
2013-07-19 18:12:36 -04:00
|
|
|
virtual void afterOperation();
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2013-07-20 20:47:48 -04:00
|
|
|
Request(RequestType type, int priority, bool manage_memory=true);
|
2013-07-18 17:18:40 -04:00
|
|
|
virtual ~Request();
|
|
|
|
|
|
|
|
void execute();
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Returns the priority of this request. */
|
2013-07-20 20:47:48 -04:00
|
|
|
int getPriority() const { return m_priority; }
|
2013-07-18 17:18:40 -04:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Signals that this request should be canceled. */
|
2013-07-20 20:47:48 -04:00
|
|
|
void cancel() { m_cancel = true; }
|
2013-07-18 17:18:40 -04:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Returns if this request is to be canceled. */
|
2013-07-20 20:47:48 -04:00
|
|
|
bool isCancelled() const { return m_cancel; }
|
2013-07-18 17:18:40 -04:00
|
|
|
// ------------------------------------------------------------------------
|
2013-07-20 20:47:48 -04:00
|
|
|
/** Specifies if the memory should be managed by http manager. */
|
|
|
|
void setManageMemory(bool m) { m_manage_memory = m; }
|
2013-07-18 17:18:40 -04:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Returns if the memory for this object should be managed by
|
|
|
|
* by network_http (i.e. freed once the request is handled). */
|
2013-07-20 20:47:48 -04:00
|
|
|
bool manageMemory() const { return m_manage_memory; }
|
|
|
|
|
|
|
|
bool isDone() const { return m_done.getAtomic(); }
|
2013-07-18 17:18:40 -04:00
|
|
|
|
2013-07-20 20:47:48 -04:00
|
|
|
RequestType getType() const { return m_type; }
|
2013-07-19 18:12:36 -04:00
|
|
|
|
2013-07-20 20:47:48 -04:00
|
|
|
virtual bool isAllowedToAdd() { return true; }
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
/** This class is used by the priority queue to sort requests by priority.
|
|
|
|
*/
|
|
|
|
class Compare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Compares two requests, returns if the first request has a lower
|
|
|
|
* priority than the second one. */
|
|
|
|
bool operator() (const Request *a, const Request *b) const
|
|
|
|
{ return a->getPriority() < b->getPriority(); }
|
|
|
|
}; // Compare
|
|
|
|
}; // Request
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
class QuitRequest : public Request
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
QuitRequest();
|
|
|
|
virtual void operation() OVERRIDE {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPRequest : public Request
|
|
|
|
{
|
|
|
|
|
|
|
|
protected :
|
|
|
|
|
|
|
|
typedef std::map<std::string, std::string> Parameters;
|
|
|
|
Parameters * m_parameters;
|
|
|
|
|
|
|
|
/** The progress indicator. 0 till it is started and the first
|
|
|
|
* packet is downloaded. At the end eithe -1 (error) or 1
|
|
|
|
* (everything ok) at the end. */
|
2013-07-19 17:20:32 -04:00
|
|
|
Synchronised<float> m_progress;
|
|
|
|
std::string m_url;
|
2013-07-18 17:18:40 -04:00
|
|
|
|
2013-07-19 17:20:32 -04:00
|
|
|
virtual void afterOperation();
|
2013-07-18 17:18:40 -04:00
|
|
|
std::string downloadPage();
|
|
|
|
|
|
|
|
static int progressDownload(void *clientp,
|
|
|
|
double dltotal,
|
|
|
|
double dlnow,
|
|
|
|
double ultotal,
|
|
|
|
double ulnow);
|
|
|
|
|
|
|
|
static size_t WriteCallback(void *contents,
|
|
|
|
size_t size,
|
|
|
|
size_t nmemb,
|
|
|
|
void *userp);
|
2013-07-19 18:12:36 -04:00
|
|
|
virtual void callback() {}
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
public :
|
2013-07-20 20:47:48 -04:00
|
|
|
HTTPRequest(RequestType type, const std::string & url = "");
|
2013-07-18 17:18:40 -04:00
|
|
|
virtual ~HTTPRequest();
|
|
|
|
|
|
|
|
void setParameter(const std::string & name, const std::string &value){
|
2013-07-20 20:47:48 -04:00
|
|
|
(*m_parameters)[name] = value;
|
2013-07-18 17:18:40 -04:00
|
|
|
};
|
|
|
|
void setParameter(const std::string & name, const irr::core::stringw &value){
|
2013-07-20 20:47:48 -04:00
|
|
|
(*m_parameters)[name] = irr::core::stringc(value.c_str()).c_str();
|
2013-07-18 17:18:40 -04:00
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void setParameter(const std::string & name, const T& value){
|
2013-07-20 20:47:48 -04:00
|
|
|
(*m_parameters)[name] = StringUtils::toString(value);
|
2013-07-18 17:18:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the current progress. */
|
|
|
|
float getProgress() const { return m_progress.getAtomic(); }
|
|
|
|
/** Sets the current progress. */
|
|
|
|
void setProgress(float f) { m_progress.setAtomic(f); }
|
|
|
|
|
|
|
|
const std::string &getURL() const {return m_url;}
|
2013-07-19 21:38:44 -04:00
|
|
|
void setURL(const std::string & url) { m_url = url;}
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
virtual bool isAllowedToAdd() OVERRIDE;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class XMLRequest : public HTTPRequest
|
|
|
|
{
|
|
|
|
protected :
|
2013-07-20 10:21:17 -04:00
|
|
|
|
|
|
|
XMLNode * m_result;
|
|
|
|
irr::core::stringw m_info;
|
|
|
|
bool m_success;
|
|
|
|
|
|
|
|
virtual void operation() OVERRIDE;
|
|
|
|
virtual void afterOperation() OVERRIDE;
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
public :
|
2013-07-20 20:47:48 -04:00
|
|
|
XMLRequest(RequestType type, const std::string & url = "");
|
2013-07-20 10:21:17 -04:00
|
|
|
|
|
|
|
virtual XMLNode * getResult() const { return m_result; }
|
|
|
|
const irr::core::stringw & getInfo() const { return m_info; }
|
|
|
|
bool isSuccess() const { return m_success; }
|
|
|
|
|
2013-07-18 17:18:40 -04:00
|
|
|
};
|
2013-07-19 17:20:32 -04:00
|
|
|
} //namespace Online
|
2013-07-18 17:18:40 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|