Call OnResponseReady with an identifier for the request, add a WebRequest class

This commit is contained in:
Simon Robertshaw 2013-07-27 12:38:52 +01:00
parent 39acce4502
commit 3edee42971
23 changed files with 197 additions and 29 deletions

View File

@ -45,6 +45,7 @@
#include "gui/preview/Comment.h"
#include "ClientListener.h"
#include "requestbroker/RequestBroker.h"
#include "requestbroker/WebRequest.h"
#include "requestbroker/APIRequest.h"
#include "requestbroker/APIResultParser.h"
@ -1196,6 +1197,17 @@ std::vector<unsigned char> Client::GetSaveData(int saveID, int saveDate)
return saveData;
}
RequestBroker::Request * Client::GetSaveDataAsync(int saveID, int saveDate)
{
std::stringstream urlStream;
if(saveDate){
urlStream << "http://" << STATICSERVER << "/" << saveID << "_" << saveDate << ".cps";
} else {
urlStream << "http://" << STATICSERVER << "/" << saveID << ".cps";
}
return new WebRequest(urlStream.str());
}
RequestBroker::Request * Client::SaveUserInfoAsync(UserInfo info)
{
class StatusParser: public APIResultParser

View File

@ -136,8 +136,10 @@ public:
RequestBroker::Request * GetUserInfoAsync(std::string username);
RequestBroker::Request * SaveUserInfoAsync(UserInfo info);
RequestBroker::Request * GetSaveDataAsync(int saveID, int saveDate);
unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength);
std::vector<unsigned char> GetSaveData(int saveID, int saveDate);
LoginStatus Login(std::string username, std::string password, User & user);
void ClearThumbnailRequests();
std::vector<SaveInfo*> * SearchSaves(int start, int count, std::string query, std::string sort, std::string category, int & resultCount);

View File

@ -9,8 +9,8 @@
#include "client/HTTP.h"
#include "APIResultParser.h"
APIRequest::APIRequest(std::string url, APIResultParser * parser, ListenerHandle listener):
RequestBroker::Request(API, listener)
APIRequest::APIRequest(std::string url, APIResultParser * parser, ListenerHandle listener, int identifier):
RequestBroker::Request(API, listener, identifier)
{
Post = false;
HTTPContext = NULL;
@ -18,8 +18,8 @@ APIRequest::APIRequest(std::string url, APIResultParser * parser, ListenerHandle
URL = url;
}
APIRequest::APIRequest(std::string url, std::map<std::string, std::string> postData, APIResultParser * parser, ListenerHandle listener):
RequestBroker::Request(API, listener)
APIRequest::APIRequest(std::string url, std::map<std::string, std::string> postData, APIResultParser * parser, ListenerHandle listener, int identifier):
RequestBroker::Request(API, listener, identifier)
{
Post = true;
PostData = postData;

View File

@ -10,8 +10,8 @@ public:
std::string URL;
std::map<std::string, std::string> PostData;
void * HTTPContext;
APIRequest(std::string url, APIResultParser * parser, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0));
APIRequest(std::string url, std::map<std::string, std::string>, APIResultParser * parser, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0));
APIRequest(std::string url, APIResultParser * parser, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0);
APIRequest(std::string url, std::map<std::string, std::string>, APIResultParser * parser, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0);
virtual RequestBroker::ProcessResponse Process(RequestBroker & rb);
virtual ~APIRequest();
virtual void Cleanup();

View File

@ -4,8 +4,8 @@
#include "graphics/Graphics.h"
#include "client/HTTP.h"
ImageRequest::ImageRequest(std::string url, int width, int height, ListenerHandle listener):
Request(Image, listener)
ImageRequest::ImageRequest(std::string url, int width, int height, ListenerHandle listener, int identifier):
Request(Image, listener, identifier)
{
URL = url;
HTTPContext = NULL;

View File

@ -7,7 +7,7 @@ public:
std::string URL;
int RequestTime;
void * HTTPContext;
ImageRequest(std::string url, int width, int height, ListenerHandle listener);
ImageRequest(std::string url, int width, int height, ListenerHandle listener, int identifier = 0);
virtual RequestBroker::ProcessResponse Process(RequestBroker & rb);
virtual ~ImageRequest();
virtual void Cleanup();

View File

@ -114,7 +114,7 @@ void RequestBroker::RetrieveAvatar(std::string username, int width, int height,
RetrieveImage(urlStream.str(), width, height, tListener);
}
void RequestBroker::Start(Request * request, RequestListener * tListener)
void RequestBroker::Start(Request * request, RequestListener * tListener, int identifier)
{
ListenerHandle handle = AttachRequestListener(tListener);
@ -152,7 +152,7 @@ void RequestBroker::FlushThumbQueue()
{
if(CheckRequestListener(completeQueue.front()->Listener))
{
completeQueue.front()->Listener.second->OnResponseReady(completeQueue.front()->ResultObject);
completeQueue.front()->Listener.second->OnResponseReady(completeQueue.front()->ResultObject, completeQueue.front()->Identifier);
}
else
{
@ -288,11 +288,12 @@ void RequestBroker::DetachRequestListener(RequestListener * tListener)
pthread_mutex_unlock(&listenersMutex);
}
RequestBroker::Request::Request(RequestType type, ListenerHandle listener)
RequestBroker::Request::Request(RequestType type, ListenerHandle listener, int identifier)
{
Type = type;
Listener = listener;
ResultObject = NULL;
Identifier = identifier;
}
RequestBroker::Request::~Request()
{

View File

@ -17,6 +17,7 @@ class RequestBroker: public Singleton<RequestBroker>
{
friend class ImageRequest;
friend class APIRequest;
friend class WebRequest;
friend class ThumbRenderRequest;
public:
class Request;
@ -56,7 +57,7 @@ public:
void RetrieveThumbnail(int saveID, int saveDate, int width, int height, RequestListener * tListener);
void RetrieveThumbnail(int saveID, int width, int height, RequestListener * tListener);
void RetrieveAvatar(std::string username, int width, int height, RequestListener * tListener);
void Start(Request * request, RequestListener * tLIstener);
void Start(Request * request, RequestListener * tLIstener, int identifier = 0);
bool CheckRequestListener(ListenerHandle handle);
ListenerHandle AttachRequestListener(RequestListener * tListener);
@ -65,12 +66,13 @@ public:
class Request
{
public:
enum RequestType { ThumbnailRender, Image, API };
enum RequestType { ThumbnailRender, Image, API, Web };
int Identifier;
RequestType Type;
void * ResultObject;
ListenerHandle Listener;
std::vector<Request*> Children;
Request(RequestType type, ListenerHandle listener);
Request(RequestType type, ListenerHandle listener, int identifier);
virtual ProcessResponse Process(RequestBroker & rb) { return Failed; }
virtual ~Request();
virtual void Cleanup();

View File

@ -7,5 +7,5 @@ public:
RequestListener() { ListenerRand = rand(); }
virtual ~RequestListener() {}
virtual void OnResponseReady(void * response) {}
virtual void OnResponseReady(void * response, int identifier) {}
};

View File

@ -5,8 +5,8 @@
#include "graphics/Graphics.h"
#include "simulation/SaveRenderer.h"
ThumbRenderRequest::ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener):
RequestBroker::Request(ThumbnailRender, listener)
ThumbRenderRequest::ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener, int identifier):
RequestBroker::Request(ThumbnailRender, listener, identifier)
{
Save = save;
Width = width;

View File

@ -8,7 +8,7 @@ public:
bool Decorations;
bool Fire;
GameSave * Save;
ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener);
ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener, int identifier = 0);
virtual RequestBroker::ProcessResponse Process(RequestBroker & rb);
virtual ~ThumbRenderRequest();
virtual void Cleanup();

View File

@ -0,0 +1,134 @@
#include <iostream>
#include <vector>
#include <typeinfo>
#include <cstdlib>
#include <cstring>
#include "Config.h"
#include "Format.h"
#include "client/Client.h"
#include "WebRequest.h"
#include "client/HTTP.h"
#include "APIResultParser.h"
WebRequest::WebRequest(std::string url, ListenerHandle listener, int identifier):
RequestBroker::Request(API, listener, identifier)
{
Post = false;
HTTPContext = NULL;
URL = url;
}
WebRequest::WebRequest(std::string url, std::map<std::string, std::string> postData, ListenerHandle listener, int identifier):
RequestBroker::Request(API, listener, identifier)
{
Post = true;
PostData = postData;
HTTPContext = NULL;
URL = url;
}
RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
{
if(HTTPContext)
{
if(http_async_req_status(HTTPContext))
{
char * data;
int status, data_size;
data = http_async_req_stop(HTTPContext, &status, &data_size);
if (status == 200 && data)
{
void * resultObject = new std::vector<unsigned char>(data, data+data_size);
if(resultObject)
{
this->ResultObject = resultObject;
rb.requestComplete(this);
free(data);
return RequestBroker::Finished;
}
else
{
std::cout << typeid(*this).name() << " Request for " << URL << " could not be parsed: " << data << std::endl;
free(data);
return RequestBroker::Failed;
}
}
else
{
//#ifdef DEBUG
std::cout << typeid(*this).name() << " Request for " << URL << " failed with status " << status << std::endl;
//#endif
if(data)
free(data);
return RequestBroker::Failed;
}
}
}
else
{
std::cout << typeid(*this).name() << " New Request for " << URL << std::endl;
if(Post)
{
char ** postNames = new char*[PostData.size() + 1];
char ** postData = new char*[PostData.size()];
int * postLength = new int[PostData.size()];
int i = 0;
std::map<std::string, std::string>::iterator iter = PostData.begin();
while(iter != PostData.end())
{
std::string name = iter->first;
std::string data = iter->second;
char * cName = new char[name.length() + 1];
char * cData = new char[data.length() + 1];
std::strcpy(cName, name.c_str());
std::strcpy(cData, data.c_str());
postNames[i] = cName;
postData[i] = cData;
postLength[i] = data.length();
i++;
iter++;
}
postNames[i] = NULL;
if(Client::Ref().GetAuthUser().ID)
{
std::cout << typeid(*this).name() << " Authenticated " << std::endl;
User user = Client::Ref().GetAuthUser();
char userName[12];
char *userSession = new char[user.SessionID.length() + 1];
std::strcpy(userName, format::NumberToString<int>(user.ID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, userName, NULL, userSession);
delete userSession;
}
else
{
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, NULL, NULL, NULL);
}
}
else
{
HTTPContext = http_async_req_start(NULL, (char *)URL.c_str(), NULL, 0, 0);
}
}
return RequestBroker::OK;
}
WebRequest::~WebRequest()
{
}
void WebRequest::Cleanup()
{
Request::Cleanup();
if(ResultObject)
{
delete (std::vector<unsigned char>*)ResultObject;
ResultObject = NULL;
}
}

View File

@ -0,0 +1,17 @@
#include <map>
#include "RequestBroker.h"
class WebRequest: public RequestBroker::Request
{
public:
bool Post;
std::string URL;
std::map<std::string, std::string> PostData;
void * HTTPContext;
WebRequest(std::string url, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0);
WebRequest(std::string url, std::map<std::string, std::string>, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0);
virtual RequestBroker::ProcessResponse Process(RequestBroker & rb);
virtual ~WebRequest();
virtual void Cleanup();
};

View File

@ -40,7 +40,7 @@ void AvatarButton::Tick(float dt)
}
}
void AvatarButton::OnResponseReady(void * imagePtr)
void AvatarButton::OnResponseReady(void * imagePtr, int identifier)
{
VideoBuffer * image = (VideoBuffer*)imagePtr;
if(image)

View File

@ -38,7 +38,7 @@ public:
virtual void Draw(const Point& screenPos);
virtual void Tick(float dt);
virtual void OnResponseReady(void * imagePtr);
virtual void OnResponseReady(void * imagePtr, int identifier);
virtual void DoAction();

View File

@ -128,7 +128,7 @@ SaveButton::~SaveButton()
delete file;
}
void SaveButton::OnResponseReady(void * imagePtr)
void SaveButton::OnResponseReady(void * imagePtr, int identifier)
{
VideoBuffer * image = (VideoBuffer*)imagePtr;
if(image)

View File

@ -58,7 +58,7 @@ public:
virtual void Draw(const Point& screenPos);
virtual void Tick(float dt);
virtual void OnResponseReady(void * imagePtr);
virtual void OnResponseReady(void * imagePtr, int identifier);
void SetSelected(bool selected_) { selected = selected_; }
bool GetSelected() { return selected; }

View File

@ -167,7 +167,7 @@ void ProfileActivity::setUserInfo(UserInfo newInfo)
//exit(0);
}
void ProfileActivity::OnResponseReady(void * userDataPtr)
void ProfileActivity::OnResponseReady(void * userDataPtr, int identifier)
{
if(loading)
{

View File

@ -18,7 +18,7 @@ class ProfileActivity: public WindowActivity, public RequestListener {
public:
ProfileActivity(std::string username);
virtual ~ProfileActivity();
virtual void OnResponseReady(void * userDataPtr);
virtual void OnResponseReady(void * userDataPtr, int identifier);
virtual void OnDraw();
};

View File

@ -133,7 +133,7 @@ void LocalSaveActivity::OnDraw()
}
}
void LocalSaveActivity::OnResponseReady(void * imagePtr)
void LocalSaveActivity::OnResponseReady(void * imagePtr, int identifier)
{
if(thumbnail)
delete thumbnail;

View File

@ -34,6 +34,6 @@ public:
void saveWrite(std::string finalFilename);
virtual void Save();
virtual void OnDraw();
virtual void OnResponseReady(void * imagePtr);
virtual void OnResponseReady(void * imagePtr, int identifier);
virtual ~LocalSaveActivity();
};

View File

@ -335,7 +335,7 @@ void ServerSaveActivity::OnDraw()
}
}
void ServerSaveActivity::OnResponseReady(void * imagePtr)
void ServerSaveActivity::OnResponseReady(void * imagePtr, int identifier)
{
if(thumbnail)
delete thumbnail;

View File

@ -31,7 +31,7 @@ public:
virtual void ShowPublishingInfo();
virtual void ShowRules();
virtual void OnDraw();
virtual void OnResponseReady(void * imagePtr);
virtual void OnResponseReady(void * imagePtr, int identifier);
virtual void OnTick(float dt);
virtual ~ServerSaveActivity();
protected: