Navigation Menu

Skip to content

Commit

Permalink
Add RequestMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker authored and jacob1 committed Mar 8, 2019
1 parent 66c4920 commit af4d022
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 37 deletions.
56 changes: 56 additions & 0 deletions src/client/RequestMonitor.h
@@ -0,0 +1,56 @@
#ifndef REQUESTMONITOR_H
#define REQUESTMONITOR_H

#include <type_traits>
#include <cassert>

namespace http
{
template<class R>
class RequestMonitor
{
R *request;

protected:
RequestMonitor() :
request(nullptr)
{
}

virtual ~RequestMonitor()
{
if (request)
{
request->Cancel();
}
}

void RequestPoll()
{
if (request && request->CheckDone())
{
OnResponse(request->Finish());
request = nullptr;
}
}

template<class... Args>
void RequestSetup(Args&&... args)
{
assert(!request);
request = new R(std::forward<Args>(args)...);
}

void RequestStart()
{
assert(request);
request->Start();
}

virtual void OnResponse(typename std::result_of<decltype(&R::Finish)(R)>::type v) = 0;
};
}

#endif // REQUESTMONITOR_H


17 changes: 8 additions & 9 deletions src/gui/interface/AvatarButton.cpp
Expand Up @@ -4,7 +4,6 @@
#include "AvatarButton.h"
#include "Format.h"
#include "client/Client.h"
#include "client/AvatarRequest.h"
#include "graphics/Graphics.h"
#include "ContextMenu.h"
#include "Keys.h"
Expand All @@ -14,7 +13,6 @@ namespace ui {

AvatarButton::AvatarButton(Point position, Point size, ByteString username):
Component(position, size),
avatarRequest(nullptr),
name(username),
tried(false),
actionCallback(NULL)
Expand All @@ -27,20 +25,21 @@ AvatarButton::~AvatarButton()
delete actionCallback;
}

void AvatarButton::OnResponse(std::unique_ptr<VideoBuffer> Avatar)
{
avatar = std::move(Avatar);
}

void AvatarButton::Tick(float dt)
{
if(!avatar && !tried && name.size() > 0)
{
tried = true;
avatarRequest = new http::AvatarRequest(name, Size.X, Size.Y);
avatarRequest->Start();
RequestSetup(name, Size.X, Size.Y);
RequestStart();
}

if (avatarRequest && avatarRequest->CheckDone())
{
avatar = avatarRequest->Finish();
avatarRequest = nullptr;
}
RequestPoll();
}

void AvatarButton::Draw(const Point& screenPos)
Expand Down
11 changes: 5 additions & 6 deletions src/gui/interface/AvatarButton.h
Expand Up @@ -6,13 +6,11 @@
#include "Component.h"
#include "graphics/Graphics.h"
#include "gui/interface/Colour.h"
#include "client/AvatarRequest.h"
#include "client/RequestMonitor.h"

#include <memory>

namespace http
{
class AvatarRequest;
}
namespace ui
{
class AvatarButton;
Expand All @@ -23,10 +21,9 @@ class AvatarButtonAction
virtual ~AvatarButtonAction() {}
};

class AvatarButton : public Component
class AvatarButton : public Component, public http::RequestMonitor<http::AvatarRequest>
{
std::unique_ptr<VideoBuffer> avatar;
http::AvatarRequest *avatarRequest;
ByteString name;
bool tried;
public:
Expand All @@ -44,6 +41,8 @@ class AvatarButton : public Component
void Draw(const Point& screenPos) override;
void Tick(float dt) override;

void OnResponse(std::unique_ptr<VideoBuffer> avatar) override;

void DoAction();

void SetUsername(ByteString username) { name = username; }
Expand Down
25 changes: 9 additions & 16 deletions src/gui/interface/SaveButton.cpp
Expand Up @@ -8,11 +8,10 @@
#include "SaveButton.h"
#include "client/Client.h"
#include "client/SaveInfo.h"
#include "client/ThumbnailRequest.h"
#include "client/ThumbnailRenderer.h"
#include "graphics/Graphics.h"
#include "simulation/SaveRenderer.h"
#include "client/GameSave.h"
#include "simulation/SaveRenderer.h"

namespace ui {

Expand All @@ -24,7 +23,6 @@ SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
isMouseInsideAuthor(false),
isMouseInsideHistory(false),
showVotes(false),
thumbnailRequest(nullptr),
isButtonDown(false),
isMouseInside(false),
selected(false),
Expand Down Expand Up @@ -98,7 +96,6 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):
isMouseInsideAuthor(false),
isMouseInsideHistory(false),
showVotes(false),
thumbnailRequest(nullptr),
isButtonDown(false),
isMouseInside(false),
selected(false),
Expand All @@ -119,16 +116,16 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):

SaveButton::~SaveButton()
{
if (thumbnailRequest)
{
thumbnailRequest->Cancel();
}

delete actionCallback;
delete save;
delete file;
}

void SaveButton::OnResponse(std::unique_ptr<VideoBuffer> Thumbnail)
{
thumbnail = std::move(Thumbnail);
}

void SaveButton::Tick(float dt)
{
if (!thumbnail)
Expand All @@ -147,8 +144,8 @@ void SaveButton::Tick(float dt)
}
else if (save->GetID())
{
thumbnailRequest = new http::ThumbnailRequest(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y);
thumbnailRequest->Start();
RequestSetup(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y);
RequestStart();
triedThumbnail = true;
}
}
Expand All @@ -160,11 +157,7 @@ void SaveButton::Tick(float dt)
}
}

if (thumbnailRequest && thumbnailRequest->CheckDone())
{
thumbnail = thumbnailRequest->Finish();
thumbnailRequest = nullptr;
}
RequestPoll();

if (thumbnailRenderer)
{
Expand Down
12 changes: 6 additions & 6 deletions src/gui/interface/SaveButton.h
Expand Up @@ -8,14 +8,13 @@
#include "client/SaveInfo.h"
#include "graphics/Graphics.h"
#include "gui/interface/Colour.h"
#include "client/ThumbnailRequest.h"
#include "client/RequestMonitor.h"
#include "graphics/Graphics.h"

#include <memory>

class ThumbnailRendererTask;
namespace http
{
class ThumbnailRequest;
}
namespace ui
{
class SaveButton;
Expand All @@ -29,7 +28,7 @@ class SaveButtonAction
virtual ~SaveButtonAction() {}
};

class SaveButton : public Component
class SaveButton : public Component, public http::RequestMonitor<http::ThumbnailRequest>
{
SaveFile * file;
SaveInfo * save;
Expand All @@ -47,7 +46,6 @@ class SaveButton : public Component
bool isMouseInsideHistory;
bool showVotes;
std::unique_ptr<ThumbnailRendererTask> thumbnailRenderer;
http::ThumbnailRequest *thumbnailRequest;
public:
SaveButton(Point position, Point size, SaveInfo * save);
SaveButton(Point position, Point size, SaveFile * file);
Expand All @@ -67,6 +65,8 @@ class SaveButton : public Component
void Draw(const Point& screenPos) override;
void Tick(float dt) override;

void OnResponse(std::unique_ptr<VideoBuffer> thumbnail) override;

void SetSelected(bool selected_) { selected = selected_; }
bool GetSelected() { return selected; }
void SetSelectable(bool selectable_) { selectable = selectable_; }
Expand Down

0 comments on commit af4d022

Please sign in to comment.