pica/server/session.cpp

45 lines
1.2 KiB
C++

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "session.h"
#include "handler/poll.h"
Session::Session(
std::weak_ptr<TM::Scheduler> scheduler,
unsigned int id,
const std::string& access,
const std::string& renew
):
scheduler(scheduler),
id(id),
access(access),
renew(renew),
polling(nullptr)
{}
std::string Session::getAccessToken() const {
return access;
}
std::string Session::getRenewToken() const {
return renew;
}
void Session::accept(std::unique_ptr<Request> request) {
if (polling) {
Handler::Poll::error(*request.get(), Handler::Poll::Result::replace, Response::Status::ok);
//TODO unschedule
}
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
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;
}
sch->schedule(std::bind(&Session::onTimeout, this), TM::Scheduler::Delay(5000));
polling = std::move(request);
}