diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp index 3546f99..fe4d4ae 100644 --- a/src/TimedDoor.cpp +++ b/src/TimedDoor.cpp @@ -1,5 +1,5 @@ // Copyright 2024 MamedovKenan -#include "TimedDoor.h" +#include "../include/TimedDoor.h" #include #include #include @@ -7,43 +7,46 @@ DoorTimerAdapter::DoorTimerAdapter(TimedDoor& d) : door(d) {} void DoorTimerAdapter::Timeout() { - if (door.isDoorOpened()) { - door.throwState(); - } + if (door.isDoorOpened()) { + door.throwState(); + } } TimedDoor::TimedDoor(int timeout) : iTimeout(timeout), isOpened(false) { - adapter = new DoorTimerAdapter(*this); + adapter = new DoorTimerAdapter(*this); } bool TimedDoor::isDoorOpened() { - return isOpened; + return isOpened; } void TimedDoor::unlock() { - isOpened = true; + isOpened = true; + Timer timer; + timer.tregister(iTimeout, adapter); } void TimedDoor::lock() { - isOpened = false; + isOpened = false; } int TimedDoor::getTimeOut() const { - return iTimeout; + return iTimeout; } void TimedDoor::throwState() { - throw std::runtime_error("Door is open for too long!"); + throw std::runtime_error("Door is open for too long!"); } void Timer::tregister(int seconds, TimerClient* client) { - this->client = client; - std::thread([this, seconds]() { - sleep(seconds); - client->Timeout(); - }).detach(); + this->client = client; + + std::thread([this, seconds]() { + sleep(seconds); + client->Timeout(); + }).detach(); } void Timer::sleep(int seconds) { - std::this_thread::sleep_for(std::chrono::seconds(seconds)); + std::this_thread::sleep_for(std::chrono::seconds(seconds)); }