testing and debugging
This commit is contained in:
parent
5d765958e5
commit
a1ab1339e3
@ -305,6 +305,7 @@ DB::Session DB::MySQL::findSession(const std::string& accessToken) {
|
|||||||
|
|
||||||
Statement session(con, selectSession);
|
Statement session(con, selectSession);
|
||||||
session.bind(a.data(), MYSQL_TYPE_STRING);
|
session.bind(a.data(), MYSQL_TYPE_STRING);
|
||||||
|
session.execute();
|
||||||
|
|
||||||
std::vector<std::vector<std::any>> result = session.fetchResult();
|
std::vector<std::vector<std::any>> result = session.fetchResult();
|
||||||
if (result.empty())
|
if (result.empty())
|
||||||
|
@ -94,19 +94,19 @@ std::vector<std::vector<std::any>> DB::MySQL::Statement::fetchResult() {
|
|||||||
} break;
|
} break;
|
||||||
case MYSQL_TYPE_TINY:
|
case MYSQL_TYPE_TINY:
|
||||||
line[i] = uint8_t{0};
|
line[i] = uint8_t{0};
|
||||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
bind[i].buffer = &std::any_cast<uint8_t&>(line[i]);
|
||||||
break;
|
break;
|
||||||
case MYSQL_TYPE_SHORT:
|
case MYSQL_TYPE_SHORT:
|
||||||
line[i] = uint16_t{0};
|
line[i] = uint16_t{0};
|
||||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
bind[i].buffer = &std::any_cast<uint16_t&>(line[i]);
|
||||||
break;
|
break;
|
||||||
case MYSQL_TYPE_LONG:
|
case MYSQL_TYPE_LONG:
|
||||||
line[i] = uint32_t{0};
|
line[i] = uint32_t{0};
|
||||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
bind[i].buffer = &std::any_cast<uint32_t&>(line[i]);
|
||||||
break;
|
break;
|
||||||
case MYSQL_TYPE_LONGLONG:
|
case MYSQL_TYPE_LONGLONG:
|
||||||
line[i] = uint64_t{0};
|
line[i] = uint64_t{0};
|
||||||
bind[i].buffer = &std::any_cast<std::string&>(line[i]);
|
bind[i].buffer = &std::any_cast<uint64_t&>(line[i]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("Unsupported data fetching statement result " + std::to_string(field->type));
|
throw std::runtime_error("Unsupported data fetching statement result " + std::to_string(field->type));
|
||||||
|
@ -8,8 +8,8 @@
|
|||||||
#include "request/redirect.h"
|
#include "request/redirect.h"
|
||||||
#include "database/exceptions.h"
|
#include "database/exceptions.h"
|
||||||
|
|
||||||
Handler::Poll::Poll (Server* server):
|
Handler::Poll::Poll (std::weak_ptr<Server> server):
|
||||||
Handler("login", Request::Method::get),
|
Handler("poll", Request::Method::get),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -20,9 +20,13 @@ void Handler::Poll::handle (Request& request) {
|
|||||||
|
|
||||||
if (access.size() != 32)
|
if (access.size() != 32)
|
||||||
return error(request, Result::tokenProblem, Response::Status::badRequest);
|
return error(request, Result::tokenProblem, Response::Status::badRequest);
|
||||||
|
|
||||||
|
std::shared_ptr<Server> srv = server.lock();
|
||||||
|
if (!srv)
|
||||||
|
return error(request, Result::unknownError, Response::Status::internalError);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Session& session = server->getSession(access);
|
Session& session = srv->getSession(access);
|
||||||
throw Redirect(&session);
|
throw Redirect(&session);
|
||||||
} catch (const Redirect& r) {
|
} catch (const Redirect& r) {
|
||||||
throw r;
|
throw r;
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "handler.h"
|
#include "handler.h"
|
||||||
#include "request/request.h"
|
#include "request/request.h"
|
||||||
#include "response/response.h"
|
#include "response/response.h"
|
||||||
@ -12,7 +14,7 @@ namespace Handler {
|
|||||||
|
|
||||||
class Poll : public Handler {
|
class Poll : public Handler {
|
||||||
public:
|
public:
|
||||||
Poll(Server* server);
|
Poll(std::weak_ptr<Server> server);
|
||||||
void handle(Request& request) override;
|
void handle(Request& request) override;
|
||||||
|
|
||||||
enum class Result {
|
enum class Result {
|
||||||
@ -25,7 +27,7 @@ public:
|
|||||||
|
|
||||||
static void error(Request& request, Result result, Response::Status status);
|
static void error(Request& request, Result result, Response::Status status);
|
||||||
private:
|
private:
|
||||||
Server* server;
|
std::weak_ptr<Server> server;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -188,7 +188,7 @@ std::string Request::getServerName() const {
|
|||||||
return FCGX_GetParam(SERVER_NAME, raw.envp);;
|
return FCGX_GetParam(SERVER_NAME, raw.envp);;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::printEnvironment(std::ostream& out) {
|
void Request::printEnvironment(std::ostream& out) const {
|
||||||
if (!active())
|
if (!active())
|
||||||
throw std::runtime_error("An attempt to print environment of a request in a wrong state");
|
throw std::runtime_error("An attempt to print environment of a request in a wrong state");
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ void Request::printEnvironment(std::ostream& out) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::printEnvironment(nlohmann::json& out) {
|
void Request::printEnvironment(nlohmann::json& out) const {
|
||||||
if (!out.is_object())
|
if (!out.is_object())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -61,8 +61,8 @@ public:
|
|||||||
std::string getPath() const;
|
std::string getPath() const;
|
||||||
std::string getServerName() const;
|
std::string getServerName() const;
|
||||||
std::string getAuthorizationToken() const;
|
std::string getAuthorizationToken() const;
|
||||||
void printEnvironment(std::ostream& out);
|
void printEnvironment(std::ostream& out) const;
|
||||||
void printEnvironment(nlohmann::json& out);
|
void printEnvironment(nlohmann::json& out) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OStream getOutputStream();
|
OStream getOutputStream();
|
||||||
|
@ -62,19 +62,16 @@ void Router::handleNotFound(std::unique_ptr<Request> request) {
|
|||||||
std::string path = request->getPath();
|
std::string path = request->getPath();
|
||||||
notFound.setBody(std::string("Path \"") + path + "\" was not found");
|
notFound.setBody(std::string("Path \"") + path + "\" was not found");
|
||||||
notFound.send();
|
notFound.send();
|
||||||
std::cerr << notFound.statusCode() << '\t' << request->methodName() << '\t' << path << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Router::handleInternalError(const std::exception& exception, std::unique_ptr<Request> request) {
|
void Router::handleInternalError(const std::exception& exception, std::unique_ptr<Request> request) {
|
||||||
Response& error = request->createResponse(Response::Status::internalError);
|
Response& error = request->createResponse(Response::Status::internalError);
|
||||||
error.setBody(std::string(exception.what()));
|
error.setBody(std::string(exception.what()));
|
||||||
error.send();
|
error.send();
|
||||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << request->getPath() << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Router::handleMethodNotAllowed(std::unique_ptr<Request> request) {
|
void Router::handleMethodNotAllowed(std::unique_ptr<Request> request) {
|
||||||
Response& error = request->createResponse(Response::Status::methodNotAllowed);
|
Response& error = request->createResponse(Response::Status::methodNotAllowed);
|
||||||
error.setBody(std::string("Method not allowed"));
|
error.setBody(std::string("Method not allowed"));
|
||||||
error.send();
|
error.send();
|
||||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << request->getPath() << std::endl;
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "handler/env.h"
|
#include "handler/env.h"
|
||||||
#include "handler/register.h"
|
#include "handler/register.h"
|
||||||
#include "handler/login.h"
|
#include "handler/login.h"
|
||||||
|
#include "handler/poll.h"
|
||||||
|
|
||||||
#include "taskmanager/route.h"
|
#include "taskmanager/route.h"
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ constexpr const char* dbPassword = "pica";
|
|||||||
constexpr const char* dbName = "pica";
|
constexpr const char* dbName = "pica";
|
||||||
constexpr const char* dbPath = "/run/mysqld/mysqld.sock";
|
constexpr const char* dbPath = "/run/mysqld/mysqld.sock";
|
||||||
constexpr uint8_t dbConnectionsCount = 4;
|
constexpr uint8_t dbConnectionsCount = 4;
|
||||||
|
constexpr uint32_t pollTimout = 10000;
|
||||||
|
|
||||||
constexpr const char* randomChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
constexpr const char* randomChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
constexpr uint8_t saltSize = 16;
|
constexpr uint8_t saltSize = 16;
|
||||||
@ -64,31 +66,11 @@ void Server::run(int socketDescriptor) {
|
|||||||
router->addRoute(std::make_unique<Handler::Env>());
|
router->addRoute(std::make_unique<Handler::Env>());
|
||||||
router->addRoute(std::make_unique<Handler::Register>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::Register>(shared_from_this()));
|
||||||
router->addRoute(std::make_unique<Handler::Login>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::Login>(shared_from_this()));
|
||||||
|
router->addRoute(std::make_unique<Handler::Poll>(shared_from_this()));
|
||||||
|
|
||||||
taskManager->start();
|
taskManager->start();
|
||||||
scheduler->start();
|
scheduler->start();
|
||||||
|
|
||||||
scheduler->schedule([]() {
|
|
||||||
std::cout << "5000" << std::endl;
|
|
||||||
}, TM::Scheduler::Delay(5000));
|
|
||||||
scheduler->schedule([]() {
|
|
||||||
std::cout << "2000" << std::endl;
|
|
||||||
}, TM::Scheduler::Delay(2000));
|
|
||||||
scheduler->schedule([]() {
|
|
||||||
std::cout << "6000" << std::endl;
|
|
||||||
}, TM::Scheduler::Delay(6000));
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
||||||
|
|
||||||
scheduler->schedule([]() {
|
|
||||||
std::cout << "2000 + 500" << std::endl;
|
|
||||||
}, TM::Scheduler::Delay(2000));
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
||||||
|
|
||||||
scheduler->schedule([]() {
|
|
||||||
std::cout << "1000 + 600" << std::endl;
|
|
||||||
}, TM::Scheduler::Delay(1000));
|
|
||||||
|
|
||||||
while (!terminating) {
|
while (!terminating) {
|
||||||
std::unique_ptr<Request> request = std::make_unique<Request>();
|
std::unique_ptr<Request> request = std::make_unique<Request>();
|
||||||
bool result = request->wait(socketDescriptor);
|
bool result = request->wait(socketDescriptor);
|
||||||
@ -192,7 +174,8 @@ Session& Server::openSession(const std::string& login) {
|
|||||||
if (sessionId == 0)
|
if (sessionId == 0)
|
||||||
throw std::runtime_error("Couldn't create session, ran out of attempts");
|
throw std::runtime_error("Couldn't create session, ran out of attempts");
|
||||||
|
|
||||||
std::unique_ptr<Session>& session = sessions[accessToken] = std::make_unique<Session>(scheduler, sessionId, accessToken, renewToken);
|
std::unique_ptr<Session>& session = sessions[accessToken]
|
||||||
|
= std::make_unique<Session>(scheduler, sessionId, accessToken, renewToken, pollTimout);
|
||||||
return *session.get();
|
return *session.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,7 +190,8 @@ Session& Server::getSession (const std::string& accessToken) {
|
|||||||
scheduler,
|
scheduler,
|
||||||
s.id,
|
s.id,
|
||||||
s.accessToken,
|
s.accessToken,
|
||||||
s.renewToken
|
s.renewToken,
|
||||||
|
pollTimout
|
||||||
);
|
);
|
||||||
return *session.get();
|
return *session.get();
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,16 @@ Session::Session(
|
|||||||
std::weak_ptr<TM::Scheduler> scheduler,
|
std::weak_ptr<TM::Scheduler> scheduler,
|
||||||
unsigned int id,
|
unsigned int id,
|
||||||
const std::string& access,
|
const std::string& access,
|
||||||
const std::string& renew
|
const std::string& renew,
|
||||||
|
unsigned int timeout
|
||||||
):
|
):
|
||||||
scheduler(scheduler),
|
scheduler(scheduler),
|
||||||
id(id),
|
id(id),
|
||||||
access(access),
|
access(access),
|
||||||
renew(renew),
|
renew(renew),
|
||||||
polling(nullptr),
|
polling(nullptr),
|
||||||
timeoutId(TM::Scheduler::none)
|
timeoutId(TM::Scheduler::none),
|
||||||
|
timeout(timeout)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Session::~Session () {
|
Session::~Session () {
|
||||||
@ -37,14 +39,14 @@ std::string Session::getRenewToken() const {
|
|||||||
void Session::accept(std::unique_ptr<Request> request) {
|
void Session::accept(std::unique_ptr<Request> request) {
|
||||||
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
|
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
|
||||||
if (polling) {
|
if (polling) {
|
||||||
Handler::Poll::error(*request.get(), Handler::Poll::Result::replace, Response::Status::ok);
|
|
||||||
if (timeoutId != TM::Scheduler::none) {
|
if (timeoutId != TM::Scheduler::none) {
|
||||||
if (sch)
|
if (sch)
|
||||||
sch->cancel(timeoutId);
|
sch->cancel(timeoutId);
|
||||||
|
|
||||||
timeoutId = TM::Scheduler::none;
|
timeoutId = TM::Scheduler::none;
|
||||||
}
|
}
|
||||||
//TODO unschedule
|
Handler::Poll::error(*polling.get(), Handler::Poll::Result::replace, Response::Status::ok);
|
||||||
|
polling.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sch) {
|
if (!sch) {
|
||||||
@ -52,8 +54,7 @@ void Session::accept(std::unique_ptr<Request> request) {
|
|||||||
Handler::Poll::error(*request.get(), Handler::Poll::Result::unknownError, Response::Status::internalError);
|
Handler::Poll::error(*request.get(), Handler::Poll::Result::unknownError, Response::Status::internalError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), TM::Scheduler::Delay(5000));
|
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), timeout);
|
||||||
|
|
||||||
polling = std::move(request);
|
polling = std::move(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,8 @@ public:
|
|||||||
std::weak_ptr<TM::Scheduler> scheduler,
|
std::weak_ptr<TM::Scheduler> scheduler,
|
||||||
unsigned int id,
|
unsigned int id,
|
||||||
const std::string& access,
|
const std::string& access,
|
||||||
const std::string& renew
|
const std::string& renew,
|
||||||
|
unsigned int timeout
|
||||||
);
|
);
|
||||||
Session(const Session&) = delete;
|
Session(const Session&) = delete;
|
||||||
Session(Session&& other);
|
Session(Session&& other);
|
||||||
@ -36,4 +37,5 @@ private:
|
|||||||
std::string renew;
|
std::string renew;
|
||||||
std::unique_ptr<Request> polling;
|
std::unique_ptr<Request> polling;
|
||||||
TM::Record::ID timeoutId;
|
TM::Record::ID timeoutId;
|
||||||
|
TM::Scheduler::Delay timeout;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user