From 3edee42971a4b65cb2830c5ca24a5076f5f770e4 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 27 Jul 2013 12:38:52 +0100 Subject: [PATCH] Call OnResponseReady with an identifier for the request, add a WebRequest class --- src/client/Client.cpp | 12 ++ src/client/Client.h | 2 + src/client/requestbroker/APIRequest.cpp | 8 +- src/client/requestbroker/APIRequest.h | 4 +- src/client/requestbroker/ImageRequest.cpp | 4 +- src/client/requestbroker/ImageRequest.h | 2 +- src/client/requestbroker/RequestBroker.cpp | 7 +- src/client/requestbroker/RequestBroker.h | 8 +- src/client/requestbroker/RequestListener.h | 2 +- .../requestbroker/ThumbRenderRequest.cpp | 4 +- src/client/requestbroker/ThumbRenderRequest.h | 2 +- src/client/requestbroker/WebRequest.cpp | 134 ++++++++++++++++++ src/client/requestbroker/WebRequest.h | 17 +++ src/gui/interface/AvatarButton.cpp | 2 +- src/gui/interface/AvatarButton.h | 2 +- src/gui/interface/SaveButton.cpp | 2 +- src/gui/interface/SaveButton.h | 2 +- src/gui/profile/ProfileActivity.cpp | 2 +- src/gui/profile/ProfileActivity.h | 2 +- src/gui/save/LocalSaveActivity.cpp | 2 +- src/gui/save/LocalSaveActivity.h | 2 +- src/gui/save/ServerSaveActivity.cpp | 2 +- src/gui/save/ServerSaveActivity.h | 2 +- 23 files changed, 197 insertions(+), 29 deletions(-) create mode 100644 src/client/requestbroker/WebRequest.cpp create mode 100644 src/client/requestbroker/WebRequest.h diff --git a/src/client/Client.cpp b/src/client/Client.cpp index a9f70329b..9d819e645 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -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 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 diff --git a/src/client/Client.h b/src/client/Client.h index 6429b1850..24f5782ea 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -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 GetSaveData(int saveID, int saveDate); + LoginStatus Login(std::string username, std::string password, User & user); void ClearThumbnailRequests(); std::vector * SearchSaves(int start, int count, std::string query, std::string sort, std::string category, int & resultCount); diff --git a/src/client/requestbroker/APIRequest.cpp b/src/client/requestbroker/APIRequest.cpp index 10b2a8b06..aa42f1067 100644 --- a/src/client/requestbroker/APIRequest.cpp +++ b/src/client/requestbroker/APIRequest.cpp @@ -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 postData, APIResultParser * parser, ListenerHandle listener): - RequestBroker::Request(API, listener) +APIRequest::APIRequest(std::string url, std::map postData, APIResultParser * parser, ListenerHandle listener, int identifier): + RequestBroker::Request(API, listener, identifier) { Post = true; PostData = postData; diff --git a/src/client/requestbroker/APIRequest.h b/src/client/requestbroker/APIRequest.h index 6e5bbc5b5..9001d57ab 100644 --- a/src/client/requestbroker/APIRequest.h +++ b/src/client/requestbroker/APIRequest.h @@ -10,8 +10,8 @@ public: std::string URL; std::map PostData; void * HTTPContext; - APIRequest(std::string url, APIResultParser * parser, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0)); - APIRequest(std::string url, std::map, 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, APIResultParser * parser, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0); virtual RequestBroker::ProcessResponse Process(RequestBroker & rb); virtual ~APIRequest(); virtual void Cleanup(); diff --git a/src/client/requestbroker/ImageRequest.cpp b/src/client/requestbroker/ImageRequest.cpp index 9c8d42c68..74c231204 100644 --- a/src/client/requestbroker/ImageRequest.cpp +++ b/src/client/requestbroker/ImageRequest.cpp @@ -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; diff --git a/src/client/requestbroker/ImageRequest.h b/src/client/requestbroker/ImageRequest.h index 2fc0b62db..58a63ad9b 100644 --- a/src/client/requestbroker/ImageRequest.h +++ b/src/client/requestbroker/ImageRequest.h @@ -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(); diff --git a/src/client/requestbroker/RequestBroker.cpp b/src/client/requestbroker/RequestBroker.cpp index 21194ce1a..9744fb0b5 100644 --- a/src/client/requestbroker/RequestBroker.cpp +++ b/src/client/requestbroker/RequestBroker.cpp @@ -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() { diff --git a/src/client/requestbroker/RequestBroker.h b/src/client/requestbroker/RequestBroker.h index f6454a9e5..58084f0ad 100644 --- a/src/client/requestbroker/RequestBroker.h +++ b/src/client/requestbroker/RequestBroker.h @@ -17,6 +17,7 @@ class RequestBroker: public Singleton { 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 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(); diff --git a/src/client/requestbroker/RequestListener.h b/src/client/requestbroker/RequestListener.h index 1f53b2a89..b67805c7c 100644 --- a/src/client/requestbroker/RequestListener.h +++ b/src/client/requestbroker/RequestListener.h @@ -7,5 +7,5 @@ public: RequestListener() { ListenerRand = rand(); } virtual ~RequestListener() {} - virtual void OnResponseReady(void * response) {} + virtual void OnResponseReady(void * response, int identifier) {} }; diff --git a/src/client/requestbroker/ThumbRenderRequest.cpp b/src/client/requestbroker/ThumbRenderRequest.cpp index 1b3852973..1ddcef58b 100644 --- a/src/client/requestbroker/ThumbRenderRequest.cpp +++ b/src/client/requestbroker/ThumbRenderRequest.cpp @@ -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; diff --git a/src/client/requestbroker/ThumbRenderRequest.h b/src/client/requestbroker/ThumbRenderRequest.h index 97b39827d..739b0e7eb 100644 --- a/src/client/requestbroker/ThumbRenderRequest.h +++ b/src/client/requestbroker/ThumbRenderRequest.h @@ -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(); diff --git a/src/client/requestbroker/WebRequest.cpp b/src/client/requestbroker/WebRequest.cpp new file mode 100644 index 000000000..fffdcc804 --- /dev/null +++ b/src/client/requestbroker/WebRequest.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#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 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(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::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(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*)ResultObject; + ResultObject = NULL; + } +} diff --git a/src/client/requestbroker/WebRequest.h b/src/client/requestbroker/WebRequest.h new file mode 100644 index 000000000..fff5760ff --- /dev/null +++ b/src/client/requestbroker/WebRequest.h @@ -0,0 +1,17 @@ +#include +#include "RequestBroker.h" + +class WebRequest: public RequestBroker::Request +{ +public: + bool Post; + std::string URL; + std::map PostData; + void * HTTPContext; + WebRequest(std::string url, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0); + WebRequest(std::string url, std::map, ListenerHandle listener = ListenerHandle(0, (RequestListener*)0), int identifier = 0); + virtual RequestBroker::ProcessResponse Process(RequestBroker & rb); + virtual ~WebRequest(); + virtual void Cleanup(); +}; + diff --git a/src/gui/interface/AvatarButton.cpp b/src/gui/interface/AvatarButton.cpp index e385c778c..9176964f3 100644 --- a/src/gui/interface/AvatarButton.cpp +++ b/src/gui/interface/AvatarButton.cpp @@ -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) diff --git a/src/gui/interface/AvatarButton.h b/src/gui/interface/AvatarButton.h index 26b434831..d13f7477d 100644 --- a/src/gui/interface/AvatarButton.h +++ b/src/gui/interface/AvatarButton.h @@ -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(); diff --git a/src/gui/interface/SaveButton.cpp b/src/gui/interface/SaveButton.cpp index 7ea4198e8..7e67e488e 100644 --- a/src/gui/interface/SaveButton.cpp +++ b/src/gui/interface/SaveButton.cpp @@ -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) diff --git a/src/gui/interface/SaveButton.h b/src/gui/interface/SaveButton.h index 7bb77c452..4b9682af2 100644 --- a/src/gui/interface/SaveButton.h +++ b/src/gui/interface/SaveButton.h @@ -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; } diff --git a/src/gui/profile/ProfileActivity.cpp b/src/gui/profile/ProfileActivity.cpp index d690aa579..6c667a436 100644 --- a/src/gui/profile/ProfileActivity.cpp +++ b/src/gui/profile/ProfileActivity.cpp @@ -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) { diff --git a/src/gui/profile/ProfileActivity.h b/src/gui/profile/ProfileActivity.h index ed10e6511..53966e37b 100644 --- a/src/gui/profile/ProfileActivity.h +++ b/src/gui/profile/ProfileActivity.h @@ -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(); }; diff --git a/src/gui/save/LocalSaveActivity.cpp b/src/gui/save/LocalSaveActivity.cpp index 65e9dfb78..a29598763 100644 --- a/src/gui/save/LocalSaveActivity.cpp +++ b/src/gui/save/LocalSaveActivity.cpp @@ -133,7 +133,7 @@ void LocalSaveActivity::OnDraw() } } -void LocalSaveActivity::OnResponseReady(void * imagePtr) +void LocalSaveActivity::OnResponseReady(void * imagePtr, int identifier) { if(thumbnail) delete thumbnail; diff --git a/src/gui/save/LocalSaveActivity.h b/src/gui/save/LocalSaveActivity.h index 4668dd3c3..47d3938d1 100644 --- a/src/gui/save/LocalSaveActivity.h +++ b/src/gui/save/LocalSaveActivity.h @@ -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(); }; diff --git a/src/gui/save/ServerSaveActivity.cpp b/src/gui/save/ServerSaveActivity.cpp index 7e83847cc..c12979995 100644 --- a/src/gui/save/ServerSaveActivity.cpp +++ b/src/gui/save/ServerSaveActivity.cpp @@ -335,7 +335,7 @@ void ServerSaveActivity::OnDraw() } } -void ServerSaveActivity::OnResponseReady(void * imagePtr) +void ServerSaveActivity::OnResponseReady(void * imagePtr, int identifier) { if(thumbnail) delete thumbnail; diff --git a/src/gui/save/ServerSaveActivity.h b/src/gui/save/ServerSaveActivity.h index c18425b2b..267939b82 100644 --- a/src/gui/save/ServerSaveActivity.h +++ b/src/gui/save/ServerSaveActivity.h @@ -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: