diff options
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/Client.cpp | 28 | ||||
| -rw-r--r-- | src/client/Client.h | 5 | ||||
| -rw-r--r-- | src/client/RequestBroker.cpp | 419 | ||||
| -rw-r--r-- | src/client/RequestBroker.h | 155 | ||||
| -rw-r--r-- | src/client/RequestListener.h | 11 | ||||
| -rw-r--r-- | src/client/ThumbnailBroker.cpp | 395 | ||||
| -rw-r--r-- | src/client/ThumbnailBroker.h | 111 | ||||
| -rw-r--r-- | src/client/ThumbnailListener.h | 12 |
8 files changed, 614 insertions, 522 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp index af650a9..78a7e2e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -42,7 +42,7 @@ #include "search/Thumbnail.h" #include "preview/Comment.h" #include "ClientListener.h" -#include "ThumbnailBroker.h" +#include "RequestBroker.h" #include "cajun/reader.h" #include "cajun/writer.h" @@ -634,7 +634,7 @@ std::vector<std::pair<std::string, std::string> > Client::GetServerNotifications void Client::Tick() { //Check thumbnail queue - ThumbnailBroker::Ref().FlushThumbQueue(); + RequestBroker::Ref().FlushThumbQueue(); //Check status on version check request if(versionCheckRequest && http_async_req_status(versionCheckRequest)) @@ -826,7 +826,7 @@ void Client::WritePrefs() void Client::Shutdown() { - ThumbnailBroker::Ref().Shutdown(); + RequestBroker::Ref().Shutdown(); ClearThumbnailRequests(); http_done(); @@ -1188,6 +1188,28 @@ std::vector<unsigned char> Client::GetSaveData(int saveID, int saveDate) return saveData; } +VideoBuffer * Client::GetAvatar(std::string username) +{ + lastError = ""; + int dataStatus; + int dataLength = 0; + unsigned char * data; + std::stringstream urlStream; + urlStream << "http://" << STATICSERVER << "/avatars/" << username << ".pti"; + + data = (unsigned char *)http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength); + if(data && dataStatus == 200) + { + std::vector<char> responseData(data, data+dataLength); + return format::PTIToVideoBuffer(responseData); + } + else if(data) + { + free(data); + } + return NULL; +} + LoginStatus Client::Login(std::string username, std::string password, User & user) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index 0b40012..c824d28 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -17,6 +17,7 @@ class SaveInfo; class SaveFile; class SaveComment; class GameSave; +class VideoBuffer; enum LoginStatus { LoginOkay, LoginError @@ -41,7 +42,7 @@ public: UpdateInfo(int time, std::string file, BuildType type) : Major(0), Minor(0), Build(0), Time(time), File(file), Type(type) {} }; -class ThumbnailListener; +class RequestListener; class ClientListener; class Client: public Singleton<Client> { private: @@ -125,6 +126,8 @@ public: RequestStatus AddComment(int saveID, std::string comment); + VideoBuffer * GetAvatar(std::string username); + 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); diff --git a/src/client/RequestBroker.cpp b/src/client/RequestBroker.cpp new file mode 100644 index 0000000..8ecd5cc --- /dev/null +++ b/src/client/RequestBroker.cpp @@ -0,0 +1,419 @@ +#include <algorithm> +#include <iostream> +#include <typeinfo> +#include <time.h> +#include "RequestBroker.h" +#include "RequestListener.h" +#include "Client.h" +#include "HTTP.h" +#include "GameSave.h" +#include "search/Thumbnail.h" +#include "simulation/SaveRenderer.h" + +//Asynchronous Thumbnail render & request processing + +RequestBroker::RequestBroker() +{ + thumbnailQueueRunning = false; + + //listenersMutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_init (&listenersMutex, NULL); + + + pthread_mutex_init (&runningMutex, NULL); + + + pthread_mutex_init (&requestQueueMutex, NULL); + + + pthread_mutex_init (&completeQueueMutex, NULL); +} + +RequestBroker::~RequestBroker() +{ + +} + +void RequestBroker::assureRunning() +{ + pthread_mutex_lock(&runningMutex); + bool running = thumbnailQueueRunning; + thumbnailQueueRunning = true; + pthread_mutex_unlock(&runningMutex); + + if(!running) + { +#ifdef DEBUG + std::cout << typeid(*this).name() << " Starting background thread for new " << __FUNCTION__ << " request" << std::endl; +#endif + pthread_create(&thumbnailQueueThread, 0, &RequestBroker::thumbnailQueueProcessHelper, this); + } +} + +void RequestBroker::Shutdown() +{ + pthread_mutex_lock(&runningMutex); + if(thumbnailQueueRunning) + { + thumbnailQueueRunning = false; + pthread_mutex_unlock(&runningMutex); + pthread_join(thumbnailQueueThread, NULL); + } + else + pthread_mutex_unlock(&runningMutex); + + std::vector<Request*>::iterator req = activeRequests.begin(); + while(req != activeRequests.end()) + { + (*req)->Cleanup(); + delete (*req); + req++; + } +} + +void RequestBroker::RenderThumbnail(GameSave * gameSave, int width, int height, RequestListener * tListener) +{ + RenderThumbnail(gameSave, true, true, width, height, tListener); +} + +void RequestBroker::RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, RequestListener * tListener) +{ + ListenerHandle handle = AttachRequestListener(tListener); + + ThumbRenderRequest * r = new ThumbRenderRequest(new GameSave(*gameSave), decorations, fire, width, height, handle); + + pthread_mutex_lock(&requestQueueMutex); + requestQueue.push_back(r); + pthread_mutex_unlock(&requestQueueMutex); + + assureRunning(); +} + +void RequestBroker::RetrieveThumbnail(int saveID, int saveDate, int width, int height, RequestListener * tListener) +{ + std::stringstream urlStream; + urlStream << "http://" << STATICSERVER << "/" << saveID; + if(saveDate) + { + urlStream << "_" << saveDate; + } + urlStream << "_small.pti"; + + RetrieveImage(urlStream.str(), width, height, tListener); +} + +void RequestBroker::RetrieveAvatar(std::string username, int width, int height, RequestListener * tListener) +{ + std::stringstream urlStream; + urlStream << "http://" << STATICSERVER << "/avatars/" << username << ".pti"; + + RetrieveImage(urlStream.str(), width, height, tListener); +} + +void RequestBroker::RetrieveImage(std::string imageUrl, int width, int height, RequestListener * tListener) +{ + ListenerHandle handle = AttachRequestListener(tListener); + + ImageRequest * r = new ImageRequest(imageUrl, width, height, handle); + + pthread_mutex_lock(&requestQueueMutex); + requestQueue.push_back(r); + pthread_mutex_unlock(&requestQueueMutex); + + assureRunning(); +} + +void * RequestBroker::thumbnailQueueProcessHelper(void * ref) +{ + ((RequestBroker*)ref)->thumbnailQueueProcessTH(); + return NULL; +} + +void RequestBroker::FlushThumbQueue() +{ + pthread_mutex_lock(&completeQueueMutex); + while(completeQueue.size()) + { + if(CheckRequestListener(completeQueue.front()->Listener)) + { + completeQueue.front()->Listener.second->OnResponseReady(completeQueue.front()->ResultObject); + } + else + { +#ifdef DEBUG + std::cout << typeid(*this).name() << " Listener lost, discarding request" << std::endl; +#endif + completeQueue.front()->Cleanup(); + } + delete completeQueue.front(); + completeQueue.pop(); + } + pthread_mutex_unlock(&completeQueueMutex); +} + +void RequestBroker::thumbnailQueueProcessTH() +{ + time_t lastAction = time(NULL); + pthread_mutex_lock(&runningMutex); + thumbnailQueueRunning = true; + pthread_mutex_unlock(&runningMutex); + while(true) + { + //Shutdown after 2 seconds of idle + if(time(NULL) - lastAction > 2) + { +#ifdef DEBUG + std::cout << typeid(*this).name() << " Idle shutdown" << std::endl; +#endif + break; + } + + + pthread_mutex_lock(&runningMutex); + bool running = thumbnailQueueRunning; + pthread_mutex_unlock(&runningMutex); + if(!running) + { +#ifdef DEBUG + std::cout << typeid(*this).name() << " Requested shutdown" << std::endl; +#endif + break; + } + + if(activeRequests.size()) + { + std::vector<Request*>::iterator req = activeRequests.begin(); + while(req != activeRequests.end()) + { + ProcessResponse resultStatus = OK; + Request * r = *req; + switch(r->Type) + { + case Request::ThumbnailRender: + resultStatus = processThumbnailRender(*(ThumbRenderRequest*)r); + break; + case Request::Image: + resultStatus = processImage(*(ImageRequest*)r); + break; + } + if(resultStatus == Duplicate || resultStatus == Failed || resultStatus == Finished) + { + req = activeRequests.erase(req); + } + else + { + req++; + } + } + lastAction = time(NULL); + } + + //Move any items from the request queue to the processing queue + pthread_mutex_lock(&requestQueueMutex); + std::vector<Request*>::iterator newReq = requestQueue.begin(); + while(newReq != requestQueue.end()) + { + if(activeRequests.size() > 5) + { + break; + } + else + { + activeRequests.push_back(*newReq); + newReq = requestQueue.erase(newReq); + } + } + pthread_mutex_unlock(&requestQueueMutex); + } + pthread_mutex_lock(&runningMutex); + thumbnailQueueRunning = false; + pthread_mutex_unlock(&runningMutex); +} + +RequestBroker::ProcessResponse RequestBroker::processThumbnailRender(ThumbRenderRequest & request) +{ +#ifdef DEBUG + std::cout << typeid(*this).name() << " Processing render request" << std::endl; +#endif + Thumbnail * thumbnail = SaveRenderer::Ref().Render(request.Save, request.Decorations, request.Fire); + delete request.Save; + request.Save = NULL; + + if(thumbnail) + { + thumbnail->Resize(request.Width, request.Height); + request.ResultObject = (void*)thumbnail; + requestComplete(&request); + return Finished; + } + else + { + return Failed; + } + return Failed; +} + +RequestBroker::ProcessResponse RequestBroker::processImage(ImageRequest & request) +{ + VideoBuffer * image = NULL; + + //Have a look at the thumbnail cache + for(std::deque<std::pair<std::string, VideoBuffer*> >::iterator iter = imageCache.begin(), end = imageCache.end(); iter != end; ++iter) + { + if((*iter).first == request.URL) + { + image = (*iter).second; +#ifdef DEBUG + std::cout << typeid(*this).name() << " " << request.URL << " found in cache" << std::endl; +#endif + } + } + + if(!image) + { + if(request.HTTPContext) + { + if(http_async_req_status(request.HTTPContext)) + { + pixel * imageData; + char * data; + int status, data_size, imgw, imgh; + data = http_async_req_stop(request.HTTPContext, &status, &data_size); + + if (status == 200 && data) + { + imageData = Graphics::ptif_unpack(data, data_size, &imgw, &imgh); + free(data); + + if(imageData) + { + //Success! + image = new VideoBuffer(imageData, imgw, imgh); + free(imageData); + } + else + { + //Error thumbnail + image = new VideoBuffer(32, 32); + image->SetCharacter(14, 14, 'x', 255, 255, 255, 255); + } + + if(imageCache.size() >= THUMB_CACHE_SIZE) + { + //Remove unnecessary from thumbnail cache + delete imageCache.front().second; + imageCache.pop_front(); + } + imageCache.push_back(std::pair<std::string, VideoBuffer*>(request.URL, image)); + } + else + { + #ifdef DEBUG + std::cout << typeid(*this).name() << " Request for " << request.URL << " failed with status " << status << std::endl; + #endif + if(data) + free(data); + + return Failed; + } + } + } + else + { + //Check for ongoing requests + for(std::vector<Request*>::iterator iter = activeRequests.begin(), end = activeRequests.end(); iter != end; ++iter) + { + if((*iter)->Type != Request::Image) + continue; + ImageRequest * otherReq = (ImageRequest*)(*iter); + if(otherReq->URL == request.URL && otherReq != &request) + { + #ifdef DEBUG + std::cout << typeid(*this).name() << " Request for " << request.URL << " found, appending." << std::endl; + #endif + //Add the current listener to the item already being requested + (*iter)->Children.push_back(&request); + return Duplicate; + } + } + + //If it's not already being requested, request it + #ifdef DEBUG + std::cout << typeid(*this).name() << " Creating new request for " << request.URL << std::endl; + #endif + request.HTTPContext = http_async_req_start(NULL, (char *)request.URL.c_str(), NULL, 0, 0); + request.RequestTime = time(NULL); + } + } + + if(image) + { + + //Create a copy, to seperate from the cache + VideoBuffer * myVB = new VideoBuffer(*image); + myVB->Resize(request.Width, request.Height, true); + request.ResultObject = (void*)myVB; + requestComplete(&request); + for(std::vector<Request*>::iterator childIter = request.Children.begin(), childEnd = request.Children.end(); childIter != childEnd; ++childIter) + { + if((*childIter)->Type == Request::Image) + { + ImageRequest * childReq = (ImageRequest*)*childIter; + VideoBuffer * tempImage = new VideoBuffer(*image); + tempImage->Resize(childReq->Width, childReq->Height, true); + childReq->ResultObject = (void*)tempImage; + requestComplete(*childIter); + } + } + return Finished; + } + + return OK; +} + +void RequestBroker::requestComplete(Request * completedRequest) +{ + pthread_mutex_lock(&completeQueueMutex); + completeQueue.push(completedRequest); + pthread_mutex_unlock(&completeQueueMutex); +} + + +void RequestBroker::RetrieveThumbnail(int saveID, int width, int height, RequestListener * tListener) +{ + RetrieveThumbnail(saveID, 0, width, height, tListener); +} + +bool RequestBroker::CheckRequestListener(ListenerHandle handle) +{ + pthread_mutex_lock(&listenersMutex); + int count = std::count(validListeners.begin(), validListeners.end(), handle); + pthread_mutex_unlock(&listenersMutex); + + return count; +} + +ListenerHandle RequestBroker::AttachRequestListener(RequestListener * tListener) +{ + ListenerHandle handle = ListenerHandle(tListener->ListenerRand, tListener); + pthread_mutex_lock(&listenersMutex); + validListeners.push_back(handle); + pthread_mutex_unlock(&listenersMutex); + return handle; +} + +void RequestBroker::DetachRequestListener(RequestListener * tListener) +{ + pthread_mutex_lock(&listenersMutex); + + std::vector<ListenerHandle>::iterator iter = validListeners.begin(); + while (iter != validListeners.end()) + { + if(*iter == ListenerHandle(tListener->ListenerRand, tListener)) + iter = validListeners.erase(iter); + else + ++iter; + } + + pthread_mutex_unlock(&listenersMutex); +}
\ No newline at end of file diff --git a/src/client/RequestBroker.h b/src/client/RequestBroker.h new file mode 100644 index 0000000..9244e91 --- /dev/null +++ b/src/client/RequestBroker.h @@ -0,0 +1,155 @@ +#pragma once +#include <queue> +#include <list> +#include <utility> +#include <deque> +#include <pthread.h> +#undef GetUserName //God dammit microsoft! + +#include "Singleton.h" + +class GameSave; +class VideoBuffer; +class RequestListener; +typedef std::pair<int, RequestListener*> ListenerHandle; +class RequestBroker: public Singleton<RequestBroker> +{ +private: + + enum ProcessResponse { Finished, OK, Canceled, Failed, Duplicate }; + + class Request + { + public: + enum RequestType { ThumbnailRender, Image }; + RequestType Type; + void * ResultObject; + ListenerHandle Listener; + std::vector<Request*> Children; + Request(RequestType type, ListenerHandle listener) + { + Type = type; + Listener = listener; + ResultObject = NULL; + } + virtual ~Request() + { + std::vector<Request*>::iterator iter = Children.begin(); + while(iter != Children.end()) + { + delete (*iter); + iter++; + } + } + virtual void Cleanup() + { + std::vector<Request*>::iterator iter = Children.begin(); + while(iter != Children.end()) + { + (*iter)->Cleanup(); + iter++; + } + } + }; + + class ThumbRenderRequest: public Request + { + public: + int Width, Height; + bool Decorations; + bool Fire; + GameSave * Save; + ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener): + Request(ThumbnailRender, listener) + { + Save = save; + Width = width; + Height = height; + Decorations = decorations; + Fire = fire; + } + virtual ~ThumbRenderRequest() + { + if(Save) + delete Save; + } + virtual void Cleanup() + { + Request::Cleanup(); + if(ResultObject) + { + delete ((VideoBuffer*)ResultObject); + ResultObject = NULL; + } + } + }; + + class ImageRequest: public Request + { + public: + int Width, Height; + std::string URL; + int RequestTime; + void * HTTPContext; + ImageRequest(std::string url, int width, int height, ListenerHandle listener): + Request(Image, listener) + { + URL = url; + HTTPContext = NULL; + Width = width; + Height = height; + } + virtual ~ImageRequest() {} + virtual void Cleanup() + { + Request::Cleanup(); + if(ResultObject) + { + delete ((VideoBuffer*)ResultObject); + ResultObject = NULL; + } + } + }; + + pthread_mutex_t listenersMutex; + pthread_mutex_t runningMutex; + pthread_mutex_t requestQueueMutex; + pthread_mutex_t completeQueueMutex; + + pthread_t thumbnailQueueThread; + bool thumbnailQueueRunning; + + std::vector<ListenerHandle> validListeners; + + std::deque<std::pair<std::string, VideoBuffer*> > imageCache; + + std::queue<Request*> completeQueue; + std::vector<Request*> requestQueue; + std::vector<Request*> activeRequests; + + static void * thumbnailQueueProcessHelper(void * ref); + void thumbnailQueueProcessTH(); + void assureRunning(); + + ProcessResponse processThumbnailRender(ThumbRenderRequest & request); + ProcessResponse processImage(ImageRequest & request); + + void requestComplete(Request * completedRequest); + +public: + RequestBroker(); + virtual ~RequestBroker(); + void Shutdown(); + + void FlushThumbQueue(); + void RetrieveImage(std::string imageUrl, int width, int height, RequestListener * tListener); + void RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, RequestListener * tListener); + void RenderThumbnail(GameSave * gameSave, int width, int height, RequestListener * tListener); + 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); + + bool CheckRequestListener(ListenerHandle handle); + ListenerHandle AttachRequestListener(RequestListener * tListener); + void DetachRequestListener(RequestListener * tListener); +};
\ No newline at end of file diff --git a/src/client/RequestListener.h b/src/client/RequestListener.h new file mode 100644 index 0000000..1f53b2a --- /dev/null +++ b/src/client/RequestListener.h @@ -0,0 +1,11 @@ +#pragma once + +class RequestListener +{ +public: + int ListenerRand; + RequestListener() { ListenerRand = rand(); } + virtual ~RequestListener() {} + + virtual void OnResponseReady(void * response) {} +}; diff --git a/src/client/ThumbnailBroker.cpp b/src/client/ThumbnailBroker.cpp deleted file mode 100644 index 390af13..0000000 --- a/src/client/ThumbnailBroker.cpp +++ /dev/null @@ -1,395 +0,0 @@ -#include <algorithm> -#include <iostream> -#include <typeinfo> -#include <time.h> -#include "ThumbnailBroker.h" -#include "ThumbnailListener.h" -#include "Client.h" -#include "HTTP.h" -#include "GameSave.h" -#include "search/Thumbnail.h" -#include "simulation/SaveRenderer.h" - -//Asynchronous Thumbnail render & request processing - -ThumbnailBroker::ThumbnailBroker() -{ - thumbnailQueueRunning = false; - //thumbnailQueueMutex = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_init (&thumbnailQueueMutex, NULL); - - //listenersMutex = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_init (&listenersMutex, NULL); - - - pthread_mutex_init (&runningMutex, NULL); -} - -ThumbnailBroker::~ThumbnailBroker() -{ - for(std::deque<std::pair<ThumbnailID, Thumbnail*> >::iterator iter = thumbnailCache.begin(), end = thumbnailCache.end(); iter != end; ++iter) - { - delete (*iter).second; - } -} - -void ThumbnailBroker::assureRunning() -{ - pthread_mutex_lock(&runningMutex); - bool running = thumbnailQueueRunning; - thumbnailQueueRunning = true; - pthread_mutex_unlock(&runningMutex); - - if(!running) - { -#ifdef DEBUG - std::cout << typeid(*this).name() << " Starting background thread for new " << __FUNCTION__ << " request" << std::endl; -#endif - pthread_create(&thumbnailQueueThread, 0, &ThumbnailBroker::thumbnailQueueProcessHelper, this); - } -} - -void ThumbnailBroker::Shutdown() -{ - pthread_mutex_lock(&runningMutex); - if(thumbnailQueueRunning) - { - thumbnailQueueRunning = false; - pthread_mutex_unlock(&runningMutex); - pthread_join(thumbnailQueueThread, NULL); - } - else - pthread_mutex_unlock(&runningMutex); - - - for (std::list<ThumbnailRequest>::iterator iter = currentRequests.begin(), end = currentRequests.end(); iter != end; ++iter) - { - ThumbnailRequest req = *iter; - if(req.HTTPContext) - { - http_async_req_close(req.HTTPContext); - } - } -} - -void ThumbnailBroker::RenderThumbnail(GameSave * gameSave, int width, int height, ThumbnailListener * tListener) -{ - RenderThumbnail(gameSave, true, true, width, height, tListener); -} - -void ThumbnailBroker::RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, ThumbnailListener * tListener) -{ - AttachThumbnailListener(tListener); - pthread_mutex_lock(&thumbnailQueueMutex); - renderRequests.push_back(ThumbRenderRequest(new GameSave(*gameSave), decorations, fire, width, height, ListenerHandle(tListener->ListenerRand, tListener))); - pthread_mutex_unlock(&thumbnailQueueMutex); - - assureRunning(); -} - -void ThumbnailBroker::RetrieveThumbnail(int saveID, int saveDate, int width, int height, ThumbnailListener * tListener) -{ - AttachThumbnailListener(tListener); - pthread_mutex_lock(&thumbnailQueueMutex); - thumbnailRequests.push_back(ThumbnailRequest(saveID, saveDate, width, height, ListenerHandle(tListener->ListenerRand, tListener))); - pthread_mutex_unlock(&thumbnailQueueMutex); - - assureRunning(); -} - -void * ThumbnailBroker::thumbnailQueueProcessHelper(void * ref) -{ - ((ThumbnailBroker*)ref)->thumbnailQueueProcessTH(); - return NULL; -} - -void ThumbnailBroker::FlushThumbQueue() -{ - pthread_mutex_lock(&thumbnailQueueMutex); - while(thumbnailComplete.size()) - { - if(CheckThumbnailListener(thumbnailComplete.front().first)) - { - thumbnailComplete.front().first.second->OnThumbnailReady(thumbnailComplete.front().second); - } - else - { -#ifdef DEBUG - std::cout << typeid(*this).name() << " Listener lost, discarding request" << std::endl; -#endif - delete thumbnailComplete.front().second; - } - thumbnailComplete.pop_front(); - } - pthread_mutex_unlock(&thumbnailQueueMutex); -} - -void ThumbnailBroker::thumbnailQueueProcessTH() -{ - time_t lastAction = time(NULL); - pthread_mutex_lock(&runningMutex); - thumbnailQueueRunning = true; - pthread_mutex_unlock(&runningMutex); - while(true) - { - //Shutdown after 2 seconds of idle - if(time(NULL) - lastAction > 2) - { -#ifdef DEBUG - std::cout << typeid(*this).name() << " Idle shutdown" << std::endl; -#endif - break; - } - - - pthread_mutex_lock(&runningMutex); - bool running = thumbnailQueueRunning; - pthread_mutex_unlock(&runningMutex); - if(!running) - { -#ifdef DEBUG - std::cout << typeid(*this).name() << " Requested shutdown" << std::endl; -#endif - break; - } - - - //Renderer - pthread_mutex_lock(&thumbnailQueueMutex); - if(renderRequests.size()) - { - lastAction = time(NULL); - ThumbRenderRequest req; - req = renderRequests.front(); - renderRequests.pop_front(); - pthread_mutex_unlock(&thumbnailQueueMutex); - -#ifdef DEBUG - std::cout << typeid(*this).name() << " Processing render request" << std::endl; -#endif - - Thumbnail * thumbnail = SaveRenderer::Ref().Render(req.Save, req.Decorations, req.Fire); - delete req.Save; - - if(thumbnail) - { - thumbnail->Resize(req.Width, req.Height); - - pthread_mutex_lock(&thumbnailQueueMutex); - thumbnailComplete.push_back(std::pair<ListenerHandle, Thumbnail*>(req.CompletedListener, thumbnail)); - pthread_mutex_unlock(&thumbnailQueueMutex); - } - } - else - { - pthread_mutex_unlock(&thumbnailQueueMutex); - } - - //Renderer - pthread_mutex_lock(&thumbnailQueueMutex); - if(thumbnailRequests.size()) - { - lastAction = time(NULL); - Thumbnail * thumbnail = NULL; - - ThumbnailRequest req; - req = thumbnailRequests.front(); - - //Check the cache - for(std::deque<std::pair<ThumbnailID, Thumbnail*> >::iterator iter = thumbnailCache.begin(), end = thumbnailCache.end(); iter != end; ++iter) - { - if((*iter).first == req.ID) - { - thumbnail = (*iter).second; -#ifdef DEBUG - std::cout << typeid(*this).name() << " " << req.ID.SaveID << ":" << req.ID.SaveDate << " found in cache" << std::endl; -#endif - } - } - - if(thumbnail) - { - //Got thumbnail from cache - thumbnailRequests.pop_front(); - pthread_mutex_unlock(&thumbnailQueueMutex); - - for(std::vector<ThumbnailSpec>::iterator specIter = req.SubRequests.begin(), specEnd = req.SubRequests.end(); specIter != specEnd; ++specIter) - { - Thumbnail * tempThumbnail = new Thumbnail(*thumbnail); - tempThumbnail->Resize((*specIter).Width, (*specIter).Height); - - pthread_mutex_lock(&thumbnailQueueMutex); - thumbnailComplete.push_back(std::pair<ListenerHandle, Thumbnail*>((*specIter).CompletedListener, tempThumbnail)); - pthread_mutex_unlock(&thumbnailQueueMutex); - } - } - else - { - //Check for ongoing requests - bool requested = false; - for(std::list<ThumbnailRequest>::iterator iter = currentRequests.begin(), end = currentRequests.end(); iter != end; ++iter) - { - if((*iter).ID == req.ID) - { - requested = true; - -#ifdef DEBUG - std::cout << typeid(*this).name() << " Request for " << req.ID.SaveID << ":" << req.ID.SaveDate << " found, appending." << std::endl; -#endif - - //Add the current listener to the item already being requested - (*iter).SubRequests.push_back(req.SubRequests.front()); - } - } - - if(requested) - { - //Already requested - thumbnailRequests.pop_front(); - pthread_mutex_unlock(&thumbnailQueueMutex); - } - else if(currentRequests.size() < IMGCONNS) //If it's not already being requested and we still have more space for a new connection, request it - { - thumbnailRequests.pop_front(); - pthread_mutex_unlock(&thumbnailQueueMutex); - - //If it's not already being requested, request it - std::stringstream urlStream; - urlStream << "http://" << STATICSERVER << "/" << req.ID.SaveID; - if(req.ID.SaveDate) - { - urlStream << "_" << req.ID.SaveDate; - } - urlStream << "_small.pti"; - -#ifdef DEBUG - std::cout << typeid(*this).name() << " Creating new request for " << req.ID.SaveID << ":" << req.ID.SaveDate << std::endl; -#endif - - req.HTTPContext = http_async_req_start(NULL, (char *)urlStream.str().c_str(), NULL, 0, 0); - req.RequestTime = time(NULL); - currentRequests.push_back(req); - } - else - { - //Already full of requests - pthread_mutex_unlock(&thumbnailQueueMutex); - - } - } - } - else - { - pthread_mutex_unlock(&thumbnailQueueMutex); - } - - std::list<ThumbnailRequest>::iterator iter = currentRequests.begin(); - while (iter != currentRequests.end()) - { - lastAction = time(NULL); - - ThumbnailRequest req = *iter; - Thumbnail * thumbnail = NULL; - - if(http_async_req_status(req.HTTPContext)) - { - - pixel * thumbData; - char * data; - int status, data_size, imgw, imgh; - data = http_async_req_stop(req.HTTPContext, &status, &data_size); - - if (status == 200 && data) - { - thumbData = Graphics::ptif_unpack(data, data_size, &imgw, &imgh); - free(data); - - if(thumbData) - { - thumbnail = new Thumbnail(req.ID.SaveID, req.ID.SaveID, thumbData, ui::Point(imgw, imgh)); - free(thumbData); - } - else - { - //Error thumbnail - VideoBuffer errorThumb(128, 128); - errorThumb.SetCharacter(64, 64, 'x', 255, 255, 255, 255); - - thumbnail = new Thumbnail(req.ID.SaveID, req.ID.SaveID, errorThumb.Buffer, ui::Point(errorThumb.Width, errorThumb.Height)); - } - - if(thumbnailCache.size() >= THUMB_CACHE_SIZE) - { - delete thumbnailCache.front().second; - thumbnailCache.pop_front(); - } - thumbnailCache.push_back(std::pair<ThumbnailID, Thumbnail*>(req.ID, thumbnail)); - - for(std::vector<ThumbnailSpec>::iterator specIter = req.SubRequests.begin(), specEnd = req.SubRequests.end(); specIter != specEnd; ++specIter) - { - Thumbnail * tempThumbnail = new Thumbnail(*thumbnail); - tempThumbnail->Resize((*specIter).Width, (*specIter).Height); - - pthread_mutex_lock(&thumbnailQueueMutex); - thumbnailComplete.push_back(std::pair<ListenerHandle, Thumbnail*>((*specIter).CompletedListener, tempThumbnail)); - pthread_mutex_unlock(&thumbnailQueueMutex); - } - } - else - { -#ifdef DEBUG - std::cout << typeid(*this).name() << " Request for " << req.ID.SaveID << ":" << req.ID.SaveDate << " failed with status " << status << std::endl; -#endif - if(data) - free(data); - } - iter = currentRequests.erase(iter); - } - else - { - ++iter; - } - } - - } - pthread_mutex_lock(&runningMutex); - thumbnailQueueRunning = false; - pthread_mutex_unlock(&runningMutex); -} - -void ThumbnailBroker::RetrieveThumbnail(int saveID, int width, int height, ThumbnailListener * tListener) -{ - RetrieveThumbnail(saveID, 0, width, height, tListener); -} - -bool ThumbnailBroker::CheckThumbnailListener(ListenerHandle handle) -{ - pthread_mutex_lock(&listenersMutex); - int count = std::count(validListeners.begin(), validListeners.end(), handle); - pthread_mutex_unlock(&listenersMutex); - - return count; -} - -void ThumbnailBroker::AttachThumbnailListener(ThumbnailListener * tListener) -{ - pthread_mutex_lock(&listenersMutex); - validListeners.push_back(ListenerHandle(tListener->ListenerRand, tListener)); - pthread_mutex_unlock(&listenersMutex); -} - -void ThumbnailBroker::DetachThumbnailListener(ThumbnailListener * tListener) -{ - pthread_mutex_lock(&listenersMutex); - - std::vector<ListenerHandle>::iterator iter = validListeners.begin(); - while (iter != validListeners.end()) - { - if(*iter == ListenerHandle(tListener->ListenerRand, tListener)) - iter = validListeners.erase(iter); - else - ++iter; - } - - pthread_mutex_unlock(&listenersMutex); -} diff --git a/src/client/ThumbnailBroker.h b/src/client/ThumbnailBroker.h deleted file mode 100644 index 5160479..0000000 --- a/src/client/ThumbnailBroker.h +++ /dev/null @@ -1,111 +0,0 @@ -#pragma once -#include <queue> -#include <list> -#include <utility> -#include <deque> -#include <pthread.h> -#undef GetUserName //God dammit microsoft! - -#include "Singleton.h" - -class GameSave; -class Thumbnail; -class ThumbnailListener; -typedef std::pair<int, ThumbnailListener*> ListenerHandle; -class ThumbnailBroker: public Singleton<ThumbnailBroker> -{ -private: - class ThumbnailSpec - { - public: - int Width, Height; - ListenerHandle CompletedListener; - ThumbnailSpec(int width, int height, ListenerHandle completedListener) : - Width(width), Height(height), CompletedListener(completedListener) {} - }; - - class ThumbnailID - { - public: - int SaveID, SaveDate; - bool operator ==(const ThumbnailID & second) - { - return SaveID == second.SaveID && SaveDate == second.SaveDate; - } - ThumbnailID(int saveID, int saveDate) : SaveID(saveID), SaveDate(saveDate) {} - ThumbnailID() : SaveID(0), SaveDate(0) {} - }; - - class ThumbnailRequest - { - public: - bool Complete; - void * HTTPContext; - int RequestTime; - - ThumbnailID ID; - std::vector<ThumbnailSpec> SubRequests; - - ThumbnailRequest(int saveID, int saveDate, int width, int height, ListenerHandle completedListener) : - ID(saveID, saveDate), Complete(false), HTTPContext(NULL), RequestTime(0) - { - SubRequests.push_back(ThumbnailSpec(width, height, completedListener)); - } - ThumbnailRequest() : Complete(false), HTTPContext(NULL), RequestTime(0) {} - }; - - class ThumbRenderRequest - { - public: - int Width, Height; - bool Decorations; - bool Fire; - GameSave * Save; - ListenerHandle CompletedListener; - ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle completedListener) : - Save(save), Width(width), Height(height), CompletedListener(completedListener), Decorations(decorations), Fire(fire) {} - ThumbRenderRequest() : Save(0), Decorations(true), Fire(true), Width(0), Height(0), CompletedListener(ListenerHandle(0, (ThumbnailListener*)NULL)) {} - }; - - //Thumbnail retreival - /*int thumbnailCacheNextID; - Thumbnail * thumbnailCache[THUMB_CACHE_SIZE]; - void * activeThumbRequests[IMGCONNS]; - int activeThumbRequestTimes[IMGCONNS]; - int activeThumbRequestCompleteTimes[IMGCONNS]; - std::string activeThumbRequestIDs[IMGCONNS];*/ - - pthread_mutex_t thumbnailQueueMutex; - pthread_mutex_t listenersMutex; - pthread_mutex_t runningMutex; - pthread_t thumbnailQueueThread; - bool thumbnailQueueRunning; - std::deque<ThumbnailRequest> thumbnailRequests; - std::deque<ThumbRenderRequest> renderRequests; - - std::deque<std::pair<ListenerHandle, Thumbnail*> > thumbnailComplete; - std::list<ThumbnailRequest> currentRequests; - std::deque<std::pair<ThumbnailID, Thumbnail*> > thumbnailCache; - - - std::vector<ListenerHandle> validListeners; - - static void * thumbnailQueueProcessHelper(void * ref); - void thumbnailQueueProcessTH(); - void assureRunning(); - -public: - ThumbnailBroker(); - virtual ~ThumbnailBroker(); - void Shutdown(); - - void FlushThumbQueue(); - void RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, ThumbnailListener * tListener); - void RenderThumbnail(GameSave * gameSave, int width, int height, ThumbnailListener * tListener); - void RetrieveThumbnail(int saveID, int saveDate, int width, int height, ThumbnailListener * tListener); - void RetrieveThumbnail(int saveID, int width, int height, ThumbnailListener * tListener); - - bool CheckThumbnailListener(ListenerHandle handle); - void AttachThumbnailListener(ThumbnailListener * tListener); - void DetachThumbnailListener(ThumbnailListener * tListener); -};
\ No newline at end of file diff --git a/src/client/ThumbnailListener.h b/src/client/ThumbnailListener.h deleted file mode 100644 index 97bdef5..0000000 --- a/src/client/ThumbnailListener.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -class Thumbnail; -class ThumbnailListener -{ -public: - int ListenerRand; - ThumbnailListener() { ListenerRand = rand(); } - virtual ~ThumbnailListener() {} - - virtual void OnThumbnailReady(Thumbnail * thumb) {} -}; |
