From f1a2006b4bf35f0dda9214f8830ec510b6c5302f Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 30 Dec 2023 19:42:11 -0300 Subject: [PATCH] task manager, license formatting --- CMakeLists.txt | 4 ++ cmake/FindArgon2.cmake | 3 ++ cmake/FindFCGI.cmake | 3 ++ cmake/FindMariaDB.cmake | 3 ++ config.h.in | 3 ++ database/CMakeLists.txt | 3 ++ database/exceptions.cpp | 4 +- database/exceptions.h | 4 +- database/interface.cpp | 4 +- database/migrations/CMakeLists.txt | 3 ++ database/mysql/CMakeLists.txt | 3 ++ database/mysql/mysql.cpp | 4 +- database/mysql/mysql.h | 4 +- database/mysql/statement.cpp | 4 +- database/mysql/statement.h | 4 +- database/mysql/transaction.cpp | 4 +- database/mysql/transaction.h | 4 +- database/pool.cpp | 4 +- database/pool.h | 4 +- database/resource.cpp | 4 +- database/resource.h | 4 +- handler/CMakeLists.txt | 3 ++ handler/env.cpp | 4 +- handler/env.h | 4 +- handler/handler.cpp | 4 +- handler/handler.h | 4 +- handler/info.cpp | 4 +- handler/info.h | 4 +- handler/login.cpp | 14 +++--- handler/login.h | 10 +++-- handler/poll.cpp | 6 +-- handler/poll.h | 6 +-- handler/register.cpp | 12 ++++-- handler/register.h | 10 +++-- main.cpp | 9 ++-- request/CMakeLists.txt | 3 ++ request/accepting.h | 4 +- request/redirect.cpp | 6 +-- request/redirect.h | 6 +-- request/request.cpp | 8 ++-- request/request.h | 4 +- response/CMakeLists.txt | 3 ++ response/response.cpp | 4 +- response/response.h | 4 +- run.sh.in | 5 ++- server/CMakeLists.txt | 3 ++ server/router.cpp | 4 +- server/router.h | 4 +- server/server.cpp | 26 +++++++---- server/server.h | 10 +++-- server/session.cpp | 6 +-- server/session.h | 4 +- stream/CMakeLists.txt | 3 ++ stream/ostream.cpp | 4 +- stream/ostream.h | 4 +- stream/stream.cpp | 4 +- stream/stream.h | 4 +- taskmanager/CMakeLists.txt | 16 +++++++ taskmanager/job.cpp | 9 ++++ taskmanager/job.h | 19 ++++++++ taskmanager/manager.cpp | 69 ++++++++++++++++++++++++++++++ taskmanager/manager.h | 40 +++++++++++++++++ taskmanager/route.cpp | 13 ++++++ taskmanager/route.h | 23 ++++++++++ utils/CMakeLists.txt | 3 ++ utils/formdecode.cpp | 4 +- utils/formdecode.h | 4 +- utils/helpers.cpp | 4 +- utils/helpers.h | 4 +- 69 files changed, 380 insertions(+), 122 deletions(-) create mode 100644 taskmanager/CMakeLists.txt create mode 100644 taskmanager/job.cpp create mode 100644 taskmanager/job.h create mode 100644 taskmanager/manager.cpp create mode 100644 taskmanager/manager.h create mode 100644 taskmanager/route.cpp create mode 100644 taskmanager/route.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eac80e..4d8e5a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + cmake_minimum_required(VERSION 3.5) project(pica VERSION 0.0.1 @@ -54,6 +57,7 @@ add_subdirectory(response) add_subdirectory(stream) add_subdirectory(database) add_subdirectory(utils) +add_subdirectory(taskmanager) configure_file(config.h.in config.h @ONLY) configure_file(run.sh.in run.sh @ONLY) diff --git a/cmake/FindArgon2.cmake b/cmake/FindArgon2.cmake index 75d70a6..0f1fc81 100644 --- a/cmake/FindArgon2.cmake +++ b/cmake/FindArgon2.cmake @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + find_library(Argon2_LIBRARIES argon2) find_path(Argon2_INCLUDE_DIR argon2.h) diff --git a/cmake/FindFCGI.cmake b/cmake/FindFCGI.cmake index e3a3e88..77a04df 100644 --- a/cmake/FindFCGI.cmake +++ b/cmake/FindFCGI.cmake @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + find_library(FCGI_LIBRARIES fcgi NAMES FCGI libfcgi) find_library(FCGI++_LIBRARIES fcgi++ NAMES FCGI++ libfcgi++) diff --git a/cmake/FindMariaDB.cmake b/cmake/FindMariaDB.cmake index 623de5f..f32d521 100644 --- a/cmake/FindMariaDB.cmake +++ b/cmake/FindMariaDB.cmake @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-laterlater + find_library(MariaDB_CLIENT_LIBRARIES mysqlclient NAMES mariadbclient) find_path(MariaDB_INCLUDE_DIR mysql/mysql.h) diff --git a/config.h.in b/config.h.in index 07ee1ea..624a6d9 100644 --- a/config.h.in +++ b/config.h.in @@ -1,3 +1,6 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + #pragma once #define FULL_DATA_DIR "@CMAKE_INSTALL_FULL_DATADIR@" diff --git a/database/CMakeLists.txt b/database/CMakeLists.txt index d903715..42e02ef 100644 --- a/database/CMakeLists.txt +++ b/database/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADERS interface.h exceptions.h diff --git a/database/exceptions.cpp b/database/exceptions.cpp index d76ff93..5ba8058 100644 --- a/database/exceptions.cpp +++ b/database/exceptions.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "exceptions.h" diff --git a/database/exceptions.h b/database/exceptions.h index 8b677d8..19da281 100644 --- a/database/exceptions.h +++ b/database/exceptions.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/database/interface.cpp b/database/interface.cpp index 563296c..ecf195e 100644 --- a/database/interface.cpp +++ b/database/interface.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "interface.h" diff --git a/database/migrations/CMakeLists.txt b/database/migrations/CMakeLists.txt index 9091bba..d0842f0 100644 --- a/database/migrations/CMakeLists.txt +++ b/database/migrations/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-laterater + set(MIGRATIONS migrations) configure_file(m0.sql ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_DATADIR}/${MIGRATIONS}/m0.sql COPYONLY) diff --git a/database/mysql/CMakeLists.txt b/database/mysql/CMakeLists.txt index b1cec73..7a77c4b 100644 --- a/database/mysql/CMakeLists.txt +++ b/database/mysql/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-laterr + set(HEADERS mysql.h statement.h diff --git a/database/mysql/mysql.cpp b/database/mysql/mysql.cpp index b436b03..151fa9b 100644 --- a/database/mysql/mysql.cpp +++ b/database/mysql/mysql.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "mysql.h" diff --git a/database/mysql/mysql.h b/database/mysql/mysql.h index 7edc2ae..7bd9775 100644 --- a/database/mysql/mysql.h +++ b/database/mysql/mysql.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/database/mysql/statement.cpp b/database/mysql/statement.cpp index 3e8a841..edd90bd 100644 --- a/database/mysql/statement.cpp +++ b/database/mysql/statement.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "statement.h" diff --git a/database/mysql/statement.h b/database/mysql/statement.h index 68d9563..4f013f5 100644 --- a/database/mysql/statement.h +++ b/database/mysql/statement.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/database/mysql/transaction.cpp b/database/mysql/transaction.cpp index 24cda9f..e280fe6 100644 --- a/database/mysql/transaction.cpp +++ b/database/mysql/transaction.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "transaction.h" diff --git a/database/mysql/transaction.h b/database/mysql/transaction.h index b4b97d3..aa972e6 100644 --- a/database/mysql/transaction.h +++ b/database/mysql/transaction.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/database/pool.cpp b/database/pool.cpp index 53c08f7..644fee1 100644 --- a/database/pool.cpp +++ b/database/pool.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "pool.h" diff --git a/database/pool.h b/database/pool.h index 1040781..8874f1f 100644 --- a/database/pool.h +++ b/database/pool.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/database/resource.cpp b/database/resource.cpp index 4021a51..bfccae7 100644 --- a/database/resource.cpp +++ b/database/resource.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "resource.h" diff --git a/database/resource.h b/database/resource.h index d437efd..6e9d4fa 100644 --- a/database/resource.h +++ b/database/resource.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/handler/CMakeLists.txt b/handler/CMakeLists.txt index 07b134a..536d1d0 100644 --- a/handler/CMakeLists.txt +++ b/handler/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADERS handler.h info.h diff --git a/handler/env.cpp b/handler/env.cpp index 08910c2..5d4630c 100644 --- a/handler/env.cpp +++ b/handler/env.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "env.h" diff --git a/handler/env.h b/handler/env.h index cb361f0..d452af1 100644 --- a/handler/env.h +++ b/handler/env.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/handler/handler.cpp b/handler/handler.cpp index e0c39c1..49b8572 100644 --- a/handler/handler.cpp +++ b/handler/handler.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "handler.h" diff --git a/handler/handler.h b/handler/handler.h index 4e682c4..1ba643b 100644 --- a/handler/handler.h +++ b/handler/handler.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/handler/info.cpp b/handler/info.cpp index 3a65cd7..408476c 100644 --- a/handler/info.cpp +++ b/handler/info.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "info.h" diff --git a/handler/info.h b/handler/info.h index b1adc12..da162b3 100644 --- a/handler/info.h +++ b/handler/info.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/handler/login.cpp b/handler/login.cpp index 4cd1ef9..e8c32a1 100644 --- a/handler/login.cpp +++ b/handler/login.cpp @@ -1,12 +1,12 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "login.h" #include "server/server.h" #include "database/exceptions.h" -Handler::Login::Login(Server* server): +Handler::Login::Login(std::weak_ptr server): Handler("login", Request::Method::post), server(server) {} @@ -29,9 +29,13 @@ void Handler::Login::handle(Request& request) { if (password.empty()) return error(request, Result::emptyPassword, Response::Status::badRequest); + std::shared_ptr srv = server.lock(); + if (!srv) + return error(request, Result::unknownError, Response::Status::internalError); + bool success = false; try { - success = server->validatePassword(login, password); + success = srv->validatePassword(login, password); } catch (const DB::NoLogin& e) { std::cerr << "Exception on logging in:\n\t" << e.what() << std::endl; return error(request, Result::wrongCredentials, Response::Status::badRequest); @@ -49,7 +53,7 @@ void Handler::Login::handle(Request& request) { body["result"] = Result::success; try { - Session& session = server->openSession(login); + Session& session = srv->openSession(login); body["accessToken"] = session.getAccessToken(); body["renewToken"] = session.getRenewToken(); } catch (const std::exception& e) { diff --git a/handler/login.h b/handler/login.h index d128f40..47f0456 100644 --- a/handler/login.h +++ b/handler/login.h @@ -1,8 +1,10 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once +#include + #include "handler.h" class Server; @@ -10,7 +12,7 @@ namespace Handler { class Login : public Handler { public: - Login(Server* server); + Login(std::weak_ptr server); void handle(Request& request) override; enum class Result { @@ -27,7 +29,7 @@ private: void error(Request& request, Result result, Response::Status code); private: - Server* server; + std::weak_ptr server; }; } diff --git a/handler/poll.cpp b/handler/poll.cpp index a6a2e0c..5fb2c3b 100644 --- a/handler/poll.cpp +++ b/handler/poll.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "handler/poll.h" @@ -41,4 +41,4 @@ void Handler::Poll::error(Request& request, Result result, Response::Status stat res.setBody(body); res.send(); -} \ No newline at end of file +} diff --git a/handler/poll.h b/handler/poll.h index a7b4362..656a159 100644 --- a/handler/poll.h +++ b/handler/poll.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once @@ -29,4 +29,4 @@ private: }; -} \ No newline at end of file +} diff --git a/handler/register.cpp b/handler/register.cpp index ee485c8..203e376 100644 --- a/handler/register.cpp +++ b/handler/register.cpp @@ -1,12 +1,12 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "register.h" #include "server/server.h" #include "database/exceptions.h" -Handler::Register::Register(Server* server): +Handler::Register::Register(std::weak_ptr server): Handler("register", Request::Method::post), server(server) {} @@ -33,8 +33,12 @@ void Handler::Register::handle(Request& request) { //TODO password policies checkup + std::shared_ptr srv = server.lock(); + if (!srv) + return error(request, Result::unknownError, Response::Status::internalError); + try { - server->registerAccount(login, password); + srv->registerAccount(login, password); } catch (const DB::DuplicateLogin& e) { std::cerr << "Exception on registration:\n\t" << e.what() << std::endl; return error(request, Result::loginExists, Response::Status::conflict); diff --git a/handler/register.h b/handler/register.h index 6e36655..39fe000 100644 --- a/handler/register.h +++ b/handler/register.h @@ -1,8 +1,10 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once +#include + #include "handler.h" class Server; @@ -10,7 +12,7 @@ namespace Handler { class Register : public Handler { public: - Register(Server* server); + Register(std::weak_ptr server); void handle(Request& request) override; enum class Result { @@ -29,6 +31,6 @@ private: void error(Request& request, Result result, Response::Status code); private: - Server* server; + std::weak_ptr server; }; } diff --git a/main.cpp b/main.cpp index 8c37269..c336bf6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include @@ -7,6 +7,7 @@ #include #include +#include #include "config.h" //autogenereted by cmake in the root of bindir #include "utils/helpers.h" @@ -34,6 +35,6 @@ int main(int argc, char** argv) { FCGX_Init(); - Server server; - server.run(sockfd); + auto server = std::make_shared(); + server->run(sockfd); } diff --git a/request/CMakeLists.txt b/request/CMakeLists.txt index 19e567c..87e818d 100644 --- a/request/CMakeLists.txt +++ b/request/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADERS request.h redirect.h diff --git a/request/accepting.h b/request/accepting.h index de7b626..f5d731c 100644 --- a/request/accepting.h +++ b/request/accepting.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/request/redirect.cpp b/request/redirect.cpp index 04dac58..e7fc326 100644 --- a/request/redirect.cpp +++ b/request/redirect.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "redirect.h" @@ -9,4 +9,4 @@ Redirect::Redirect(Accepting* destination): const char* Redirect::what() const noexcept { return "This is a redirect, should have beeh handled in router, but if you see it - something went terrebly wrong"; -} \ No newline at end of file +} diff --git a/request/redirect.h b/request/redirect.h index ce54f79..e31d722 100644 --- a/request/redirect.h +++ b/request/redirect.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once @@ -13,4 +13,4 @@ public: Accepting* destination; const char* what() const noexcept override; -}; \ No newline at end of file +}; diff --git a/request/request.cpp b/request/request.cpp index 4b239c9..3ee662f 100644 --- a/request/request.cpp +++ b/request/request.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "request.h" @@ -140,8 +140,8 @@ void Request::responseIsComplete() { throw std::runtime_error("An attempt to mark the request as complete, but it wasn't responded"); break; case State::responding: - std::cout << responseCode() << '\t' << methodName() << '\t' << path << std::endl; state = State::responded; + std::cout << responseCode() << '\t' << methodName() << '\t' << path << std::endl; break; case State::responded: throw std::runtime_error("An attempt to mark the request as a complete for the second time"); @@ -265,4 +265,4 @@ std::string Request::getAuthorizationToken() const { trim(result); return result; -} \ No newline at end of file +} diff --git a/request/request.h b/request/request.h index d4bacd2..dae582f 100644 --- a/request/request.h +++ b/request/request.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/response/CMakeLists.txt b/response/CMakeLists.txt index e4c5093..3df4e32 100644 --- a/response/CMakeLists.txt +++ b/response/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADERS response.h ) diff --git a/response/response.cpp b/response/response.cpp index e9a483a..7a4e4cb 100644 --- a/response/response.cpp +++ b/response/response.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "response.h" diff --git a/response/response.h b/response/response.h index bb18c5a..6fbce08 100644 --- a/response/response.h +++ b/response/response.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/run.sh.in b/run.sh.in index ab3070f..919bcad 100644 --- a/run.sh.in +++ b/run.sh.in @@ -1,5 +1,8 @@ #!/bin/bash +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + start_service() { if systemctl is-active --quiet $1 then @@ -18,4 +21,4 @@ fi start_service "mariadb" start_service "httpd" -./@PROJECT_NAME@ \ No newline at end of file +./@PROJECT_NAME@ diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 9a1b2d8..3ac7309 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADERS server.h router.h diff --git a/server/router.cpp b/server/router.cpp index 2a1a63c..d64b875 100644 --- a/server/router.cpp +++ b/server/router.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "router.h" diff --git a/server/router.h b/server/router.h index 83c2fea..1368075 100644 --- a/server/router.h +++ b/server/router.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/server/server.cpp b/server/server.cpp index 4bf05d4..dbb99f1 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "server.h" @@ -12,6 +12,8 @@ #include "handler/register.h" #include "handler/login.h" +#include "taskmanager/route.h" + constexpr const char* pepper = "well, not much of a secret, huh?"; constexpr const char* dbLogin = "pica"; constexpr const char* dbPassword = "pica"; @@ -29,11 +31,13 @@ constexpr uint32_t hashMemoryCost = 65536; constexpr uint8_t currentDbVesion = 1; Server::Server(): + std::enable_shared_from_this(), terminating(false), requestCount(0), serverName(std::nullopt), - router(), + router(std::make_shared()), pool(DB::Pool::create()), + taskManager(std::make_shared()), sessions() { std::cout << "Startig pica..." << std::endl; @@ -50,16 +54,18 @@ Server::Server(): DB::Resource db = pool->request(); db->migrate(currentDbVesion); - - router.addRoute(std::make_unique()); - router.addRoute(std::make_unique()); - router.addRoute(std::make_unique(this)); - router.addRoute(std::make_unique(this)); } Server::~Server() {} void Server::run(int socketDescriptor) { + router->addRoute(std::make_unique()); + router->addRoute(std::make_unique()); + router->addRoute(std::make_unique(shared_from_this())); + router->addRoute(std::make_unique(shared_from_this())); + + taskManager->start(); + while (!terminating) { std::unique_ptr request = std::make_unique(); bool result = request->wait(socketDescriptor); @@ -86,7 +92,9 @@ void Server::handleRequest(std::unique_ptr request) { } request->readPath(serverName.value()); - router.route(std::move(request)); + + auto route = std::make_unique(router, std::move(request)); + taskManager->schedule(std::move(route)); } std::string Server::generateRandomString(std::size_t length) { diff --git a/server/server.h b/server/server.h index 9d3319c..72f7fe6 100644 --- a/server/server.h +++ b/server/server.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once @@ -25,8 +25,9 @@ #include "database/pool.h" #include "utils/helpers.h" #include "config.h" +#include "taskmanager/manager.h" -class Server { +class Server : public std::enable_shared_from_this { public: Server(); ~Server(); @@ -48,7 +49,8 @@ private: bool terminating; uint64_t requestCount; std::optional serverName; - Router router; + std::shared_ptr router; std::shared_ptr pool; + std::shared_ptr taskManager; Sessions sessions; }; diff --git a/server/session.cpp b/server/session.cpp index 43de593..fb7d91b 100644 --- a/server/session.cpp +++ b/server/session.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "session.h" @@ -31,4 +31,4 @@ void Session::accept(std::unique_ptr request) { } polling = std::move(request); -} \ No newline at end of file +} diff --git a/server/session.h b/server/session.h index 910070f..0027562 100644 --- a/server/session.h +++ b/server/session.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/stream/CMakeLists.txt b/stream/CMakeLists.txt index 3fef9b0..0d75e92 100644 --- a/stream/CMakeLists.txt +++ b/stream/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADERS stream.h ostream.h diff --git a/stream/ostream.cpp b/stream/ostream.cpp index 8674355..31adf8a 100644 --- a/stream/ostream.cpp +++ b/stream/ostream.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "ostream.h" diff --git a/stream/ostream.h b/stream/ostream.h index e08a936..e25fd4a 100644 --- a/stream/ostream.h +++ b/stream/ostream.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/stream/stream.cpp b/stream/stream.cpp index 022dc07..f65203a 100644 --- a/stream/stream.cpp +++ b/stream/stream.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "stream.h" diff --git a/stream/stream.h b/stream/stream.h index 07a263d..697942a 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/taskmanager/CMakeLists.txt b/taskmanager/CMakeLists.txt new file mode 100644 index 0000000..2149fd4 --- /dev/null +++ b/taskmanager/CMakeLists.txt @@ -0,0 +1,16 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + +set(HEADERS + manager.h + job.h + route.h +) + +set(SOURCES + manager.cpp + job.cpp + route.cpp +) + +target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/taskmanager/job.cpp b/taskmanager/job.cpp new file mode 100644 index 0000000..5c74920 --- /dev/null +++ b/taskmanager/job.cpp @@ -0,0 +1,9 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#include "job.h" + +TM::Job::Job () +{} + +TM::Job::~Job () {} diff --git a/taskmanager/job.h b/taskmanager/job.h new file mode 100644 index 0000000..4895ced --- /dev/null +++ b/taskmanager/job.h @@ -0,0 +1,19 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +namespace TM { +class Job { +public: + Job(); + Job(const Job& other) = delete; + Job(Job&& other) = delete; + virtual ~Job(); + + Job& operator = (const Job& other) = delete; + Job& operator = (Job&& other) = delete; + + virtual void execute() = 0; +}; +} diff --git a/taskmanager/manager.cpp b/taskmanager/manager.cpp new file mode 100644 index 0000000..1f86661 --- /dev/null +++ b/taskmanager/manager.cpp @@ -0,0 +1,69 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#include "manager.h" + +TM::Manager::Manager (): + terminating(false), + threads(), + queue(), + mtx(), + cond() +{} + +TM::Manager::~Manager () { + std::unique_lock lock(mtx); + if (threads.empty()) + return; + + lock.unlock(); + stop(); +} + +void TM::Manager::start () { + std::lock_guard lock(mtx); + + std::size_t amount = std::thread::hardware_concurrency(); + for (std::size_t i = 0; i < amount; ++i) + threads.emplace_back(std::thread(&Manager::loop, this)); +} + +void TM::Manager::stop () { + std::unique_lock lock(mtx); + + terminating = true; + + lock.unlock(); + cond.notify_all(); + for (std::thread& thread : threads) + thread.join(); + + lock.lock(); + threads.clear(); + terminating = false; +} + +void TM::Manager::loop () { + while (true) { + std::unique_lock lock(mtx); + while (!terminating && queue.empty()) + cond.wait(lock); + + if (terminating) + return; + + std::unique_ptr job = std::move(queue.front()); + queue.pop(); + lock.unlock(); + + job->execute(); + } +} + +void TM::Manager::schedule (std::unique_ptr job) { + std::unique_lock lock(mtx); + queue.emplace(std::move(job)); + + lock.unlock(); + cond.notify_one(); +} diff --git a/taskmanager/manager.h b/taskmanager/manager.h new file mode 100644 index 0000000..1f1aa3b --- /dev/null +++ b/taskmanager/manager.h @@ -0,0 +1,40 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "job.h" + +namespace TM { +class Manager { +public: + Manager(); + Manager(const Manager&) = delete; + Manager(Manager&&) = delete; + ~Manager(); + + Manager& operator = (const Manager&) = delete; + Manager& operator = (Manager&&) = delete; + + void start(); + void stop(); + void schedule(std::unique_ptr job); + +private: + void loop(); + +private: + bool terminating; + std::vector threads; + std::queue> queue; + std::mutex mtx; + std::condition_variable cond; +}; +} diff --git a/taskmanager/route.cpp b/taskmanager/route.cpp new file mode 100644 index 0000000..f2d5af0 --- /dev/null +++ b/taskmanager/route.cpp @@ -0,0 +1,13 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#include "route.h" + +TM::Route::Route (std::shared_ptr router, std::unique_ptr request): + router(router), + request(std::move(request)) +{} + +void TM::Route::execute () { + router->route(std::move(request)); +} diff --git a/taskmanager/route.h b/taskmanager/route.h new file mode 100644 index 0000000..49d7c69 --- /dev/null +++ b/taskmanager/route.h @@ -0,0 +1,23 @@ +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include + +#include "job.h" +#include "server/router.h" +#include "request/request.h" + +namespace TM { +class Route : public Job { +public: + Route(std::shared_ptr router, std::unique_ptr request); + + void execute () override; + +private: + std::shared_ptr router; + std::unique_ptr request; +}; +} diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 4834150..ac09c13 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,3 +1,6 @@ +#SPDX-FileCopyrightText: 2023 Yury Gubich +#SPDX-License-Identifier: GPL-3.0-or-later + set(HEADER helpers.h formdecode.h diff --git a/utils/formdecode.cpp b/utils/formdecode.cpp index 8dbd14d..ed33203 100644 --- a/utils/formdecode.cpp +++ b/utils/formdecode.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "formdecode.h" diff --git a/utils/formdecode.h b/utils/formdecode.h index 5f4ec61..ffbad3d 100644 --- a/utils/formdecode.h +++ b/utils/formdecode.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once diff --git a/utils/helpers.cpp b/utils/helpers.cpp index c1b93e4..5e2aed2 100644 --- a/utils/helpers.cpp +++ b/utils/helpers.cpp @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #include "helpers.h" diff --git a/utils/helpers.h b/utils/helpers.h index 34fcd03..2c00151 100644 --- a/utils/helpers.h +++ b/utils/helpers.h @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Yury Gubich -// SPDX-License-Identifier: GPL-3.0-or-later +//SPDX-FileCopyrightText: 2023 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later #pragma once