pica/server/session.cpp

65 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,
const std::string& renew
):
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),
timeoutId(TM::Scheduler::none)
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) {
2024-01-03 01:11:56 +00:00
Handler::Poll::error(*request.get(), Handler::Poll::Result::replace, Response::Status::ok);
if (timeoutId != TM::Scheduler::none) {
if (sch)
sch->cancel(timeoutId);
timeoutId = TM::Scheduler::none;
}
2024-01-03 01:11:56 +00:00
//TODO unschedule
}
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
}
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), TM::Scheduler::Delay(5000));
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();
}