some ideas of updates delivery
This commit is contained in:
parent
4df8d4319e
commit
2e01fe8d67
6 changed files with 108 additions and 18 deletions
|
@ -20,7 +20,9 @@ Session::Session(
|
|||
renew(renew),
|
||||
polling(nullptr),
|
||||
timeoutId(TM::Scheduler::none),
|
||||
timeout(timeout)
|
||||
timeout(timeout),
|
||||
mtx(),
|
||||
cache()
|
||||
{}
|
||||
|
||||
Session::~Session () {
|
||||
|
@ -39,6 +41,7 @@ std::string Session::getRenewToken() const {
|
|||
}
|
||||
|
||||
void Session::accept(std::unique_ptr<Request> request) {
|
||||
std::lock_guard lock(mtx);
|
||||
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
|
||||
if (polling) {
|
||||
if (timeoutId != TM::Scheduler::none) {
|
||||
|
@ -51,17 +54,67 @@ void Session::accept(std::unique_ptr<Request> request) {
|
|||
polling.reset();
|
||||
}
|
||||
|
||||
std::map form = request->getForm();
|
||||
auto clear = form.find("clearCache");
|
||||
if (clear != form.end() && clear->second == "all")
|
||||
cache.clear();
|
||||
|
||||
if (!cache.empty())
|
||||
return sendUpdates(std::move(request));
|
||||
|
||||
if (!sch) {
|
||||
std::cerr << "Was unable to schedule polling timeout, replying with an error" << std::endl;
|
||||
Handler::Poll::error(*request.get(), Handler::Poll::Result::unknownError, Response::Status::internalError);
|
||||
return;
|
||||
}
|
||||
|
||||
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), timeout);
|
||||
polling = std::move(request);
|
||||
}
|
||||
|
||||
void Session::sendUpdates (std::unique_ptr<Request> request) {
|
||||
Response& res = request->createResponse(Response::Status::ok);
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
body["result"] = Handler::Poll::Result::success;
|
||||
nlohmann::json& data = body["data"] = nlohmann::json::object();
|
||||
|
||||
for (const auto& category : cache) {
|
||||
nlohmann::json& cat = data[category.first] = nlohmann::json::object();
|
||||
for (const auto& entry : category.second)
|
||||
cat[entry.first] = entry.second;
|
||||
}
|
||||
|
||||
res.setBody(body);
|
||||
res.send();
|
||||
}
|
||||
|
||||
void Session::onTimeout () {
|
||||
std::lock_guard lock(mtx);
|
||||
timeoutId = TM::Scheduler::none;
|
||||
Handler::Poll::error(*polling.get(), Handler::Poll::Result::timeout, Response::Status::ok);
|
||||
polling.reset();
|
||||
}
|
||||
|
||||
void Session::assetAdded (const DB::Asset& asset) {
|
||||
std::lock_guard lock(mtx);
|
||||
auto assets = cache["assets"];
|
||||
auto addedItr = assets.find("added");
|
||||
if (addedItr == assets.end())
|
||||
addedItr = assets.emplace("added", nlohmann::json::array()).first;
|
||||
|
||||
addedItr->second.push_back(asset.toJSON());
|
||||
checkUpdates();
|
||||
}
|
||||
|
||||
void Session::checkUpdates () {
|
||||
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
|
||||
if (polling) {
|
||||
if (timeoutId != TM::Scheduler::none) {
|
||||
if (sch)
|
||||
sch->cancel(timeoutId);
|
||||
|
||||
timeoutId = TM::Scheduler::none;
|
||||
}
|
||||
sendUpdates(std::move(polling));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,13 +4,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "request/accepting.h"
|
||||
#include "taskmanager/scheduler.h"
|
||||
|
||||
#include "database/schema/asset.h"
|
||||
|
||||
class Session : public Accepting {
|
||||
public:
|
||||
Session(
|
||||
Session (
|
||||
std::weak_ptr<TM::Scheduler> scheduler,
|
||||
unsigned int id,
|
||||
unsigned int owner,
|
||||
|
@ -18,21 +24,25 @@ public:
|
|||
const std::string& renew,
|
||||
unsigned int timeout
|
||||
);
|
||||
Session(const Session&) = delete;
|
||||
Session(Session&& other) = delete;
|
||||
~Session();
|
||||
Session (const Session&) = delete;
|
||||
Session (Session&& other) = delete;
|
||||
~Session ();
|
||||
Session& operator = (const Session&) = delete;
|
||||
Session& operator = (Session&& other) = delete;
|
||||
|
||||
std::string getAccessToken() const;
|
||||
std::string getRenewToken() const;
|
||||
void accept(std::unique_ptr<Request> request) override;
|
||||
std::string getAccessToken () const;
|
||||
std::string getRenewToken () const;
|
||||
void accept (std::unique_ptr<Request> request) override;
|
||||
|
||||
const unsigned int id;
|
||||
const unsigned int owner;
|
||||
|
||||
void assetAdded (const DB::Asset& asset);
|
||||
|
||||
private:
|
||||
void onTimeout();
|
||||
void onTimeout ();
|
||||
void sendUpdates (std::unique_ptr<Request> request);
|
||||
void checkUpdates ();
|
||||
|
||||
private:
|
||||
std::weak_ptr<TM::Scheduler> scheduler;
|
||||
|
@ -41,4 +51,7 @@ private:
|
|||
std::unique_ptr<Request> polling;
|
||||
TM::Record::ID timeoutId;
|
||||
TM::Scheduler::Delay timeout;
|
||||
std::mutex mtx;
|
||||
|
||||
std::map<std::string, std::map<std::string, nlohmann::json>> cache;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue