pica/server/session.h

67 lines
1.8 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
#pragma once
#include <string>
2024-01-13 23:57:42 +00:00
#include <map>
#include <mutex>
#include <nlohmann/json.hpp>
2023-12-23 20:23:38 +00:00
2023-12-28 20:26:08 +00:00
#include "request/accepting.h"
2024-01-03 01:11:56 +00:00
#include "taskmanager/scheduler.h"
2023-12-28 20:26:08 +00:00
2024-01-13 23:57:42 +00:00
#include "database/schema/asset.h"
#include "database/schema/transaction.h"
2024-01-13 23:57:42 +00:00
2023-12-28 20:26:08 +00:00
class Session : public Accepting {
2023-12-23 20:23:38 +00:00
public:
2024-01-13 23:57:42 +00:00
Session (
2024-01-03 01:11:56 +00:00
std::weak_ptr<TM::Scheduler> scheduler,
unsigned int id,
unsigned int owner,
2024-01-03 01:11:56 +00:00
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
);
2024-01-13 23:57:42 +00:00
Session (const Session&) = delete;
Session (Session&& other) = delete;
~Session ();
2023-12-29 17:40:00 +00:00
Session& operator = (const Session&) = delete;
Session& operator = (Session&& other) = delete;
2023-12-23 20:23:38 +00:00
2024-01-13 23:57:42 +00:00
std::string getAccessToken () const;
std::string getRenewToken () const;
void accept (std::unique_ptr<Request> request) override;
2023-12-23 20:23:38 +00:00
const unsigned int id;
const unsigned int owner;
2024-01-13 23:57:42 +00:00
void assetAdded (const DB::Asset& asset);
2024-04-07 23:03:10 +00:00
void assetChanged (const DB::Asset& asset);
2024-01-21 19:23:48 +00:00
void assetRemoved (unsigned int assetId);
2024-01-13 23:57:42 +00:00
void transactionAdded(const DB::Transaction& txn);
void transactionChanged(const DB::Transaction& txn);
void transactionRemoved(unsigned int txnId);
2023-12-23 20:23:38 +00:00
private:
2024-01-13 23:57:42 +00:00
void onTimeout ();
void sendUpdates (std::unique_ptr<Request> request);
void checkUpdates ();
2024-01-03 01:11:56 +00:00
2024-04-07 23:03:10 +00:00
void static removeByID (nlohmann::json& array, unsigned int id);
2024-01-03 01:11:56 +00:00
private:
std::weak_ptr<TM::Scheduler> scheduler;
2023-12-23 20:23:38 +00:00
std::string access;
std::string renew;
2023-12-28 20:26:08 +00:00
std::unique_ptr<Request> polling;
TM::Record::ID timeoutId;
2024-01-09 17:02:56 +00:00
TM::Scheduler::Delay timeout;
2024-01-13 23:57:42 +00:00
std::mutex mtx;
std::map<std::string, std::map<std::string, nlohmann::json>> cache;
2023-12-23 20:23:38 +00:00
};