testing and debugging
This commit is contained in:
parent
5d765958e5
commit
a1ab1339e3
10 changed files with 38 additions and 47 deletions
|
@ -62,19 +62,16 @@ void Router::handleNotFound(std::unique_ptr<Request> request) {
|
|||
std::string path = request->getPath();
|
||||
notFound.setBody(std::string("Path \"") + path + "\" was not found");
|
||||
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) {
|
||||
Response& error = request->createResponse(Response::Status::internalError);
|
||||
error.setBody(std::string(exception.what()));
|
||||
error.send();
|
||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << request->getPath() << std::endl;
|
||||
}
|
||||
|
||||
void Router::handleMethodNotAllowed(std::unique_ptr<Request> request) {
|
||||
Response& error = request->createResponse(Response::Status::methodNotAllowed);
|
||||
error.setBody(std::string("Method not allowed"));
|
||||
error.send();
|
||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << request->getPath() << std::endl;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include "handler/env.h"
|
||||
#include "handler/register.h"
|
||||
#include "handler/login.h"
|
||||
#include "handler/poll.h"
|
||||
|
||||
#include "taskmanager/route.h"
|
||||
|
||||
|
@ -20,6 +21,7 @@ constexpr const char* dbPassword = "pica";
|
|||
constexpr const char* dbName = "pica";
|
||||
constexpr const char* dbPath = "/run/mysqld/mysqld.sock";
|
||||
constexpr uint8_t dbConnectionsCount = 4;
|
||||
constexpr uint32_t pollTimout = 10000;
|
||||
|
||||
constexpr const char* randomChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
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::Register>(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();
|
||||
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) {
|
||||
std::unique_ptr<Request> request = std::make_unique<Request>();
|
||||
bool result = request->wait(socketDescriptor);
|
||||
|
@ -192,7 +174,8 @@ Session& Server::openSession(const std::string& login) {
|
|||
if (sessionId == 0)
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -207,7 +190,8 @@ Session& Server::getSession (const std::string& accessToken) {
|
|||
scheduler,
|
||||
s.id,
|
||||
s.accessToken,
|
||||
s.renewToken
|
||||
s.renewToken,
|
||||
pollTimout
|
||||
);
|
||||
return *session.get();
|
||||
}
|
||||
|
|
|
@ -9,14 +9,16 @@ Session::Session(
|
|||
std::weak_ptr<TM::Scheduler> scheduler,
|
||||
unsigned int id,
|
||||
const std::string& access,
|
||||
const std::string& renew
|
||||
const std::string& renew,
|
||||
unsigned int timeout
|
||||
):
|
||||
scheduler(scheduler),
|
||||
id(id),
|
||||
access(access),
|
||||
renew(renew),
|
||||
polling(nullptr),
|
||||
timeoutId(TM::Scheduler::none)
|
||||
timeoutId(TM::Scheduler::none),
|
||||
timeout(timeout)
|
||||
{}
|
||||
|
||||
Session::~Session () {
|
||||
|
@ -37,14 +39,14 @@ std::string Session::getRenewToken() const {
|
|||
void Session::accept(std::unique_ptr<Request> request) {
|
||||
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
|
||||
if (polling) {
|
||||
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;
|
||||
}
|
||||
//TODO unschedule
|
||||
Handler::Poll::error(*polling.get(), Handler::Poll::Result::replace, Response::Status::ok);
|
||||
polling.reset();
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,8 @@ public:
|
|||
std::weak_ptr<TM::Scheduler> scheduler,
|
||||
unsigned int id,
|
||||
const std::string& access,
|
||||
const std::string& renew
|
||||
const std::string& renew,
|
||||
unsigned int timeout
|
||||
);
|
||||
Session(const Session&) = delete;
|
||||
Session(Session&& other);
|
||||
|
@ -36,4 +37,5 @@ private:
|
|||
std::string renew;
|
||||
std::unique_ptr<Request> polling;
|
||||
TM::Record::ID timeoutId;
|
||||
TM::Scheduler::Delay timeout;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue