pica/server/session.cpp

66 lines
1.7 KiB
C++
Raw Normal View History

2023-12-30 22:42:11 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
2023-12-23 20:23:38 +00:00
#include "session.h"
2023-12-28 20:26:08 +00:00
#include "handler/poll.h"
2024-01-03 01:11:56 +00:00
Session::Session(
std::weak_ptr<TM::Scheduler> scheduler,
unsigned int id,
const std::string& access,
2024-01-09 17:02:56 +00:00
const std::string& renew,
unsigned int timeout
2024-01-03 01:11:56 +00:00
):
scheduler(scheduler),
2023-12-23 20:23:38 +00:00
id(id),
access(access),
2023-12-28 20:26:08 +00:00
renew(renew),
polling(nullptr),
2024-01-09 17:02:56 +00:00
timeoutId(TM::Scheduler::none),
timeout(timeout)
2023-12-23 20:23:38 +00:00
{}
Session::~Session () {
if (timeoutId != TM::Scheduler::none) {
if (std::shared_ptr<TM::Scheduler> sch = scheduler.lock())
sch->cancel(timeoutId);
}
}
2023-12-23 20:23:38 +00:00
std::string Session::getAccessToken() const {
return access;
}
std::string Session::getRenewToken() const {
return renew;
}
2023-12-28 20:26:08 +00:00
void Session::accept(std::unique_ptr<Request> request) {
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
2023-12-28 20:26:08 +00:00
if (polling) {
if (timeoutId != TM::Scheduler::none) {
if (sch)
sch->cancel(timeoutId);
timeoutId = TM::Scheduler::none;
}
2024-01-09 17:02:56 +00:00
Handler::Poll::error(*polling.get(), Handler::Poll::Result::replace, Response::Status::ok);
polling.reset();
2024-01-03 01:11:56 +00:00
}
2023-12-28 20:26:08 +00:00
2024-01-03 01:11:56 +00:00
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;
2023-12-28 20:26:08 +00:00
}
2024-01-09 17:02:56 +00:00
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), timeout);
2023-12-28 20:26:08 +00:00
polling = std::move(request);
2023-12-30 22:42:11 +00:00
}
void Session::onTimeout () {
timeoutId = TM::Scheduler::none;
Handler::Poll::error(*polling.get(), Handler::Poll::Result::timeout, Response::Status::ok);
polling.reset();
}