-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Мамедов Кенан Эльчин Оглы
committed
Sep 29, 2024
1 parent
487b393
commit 63fc828
Showing
1 changed file
with
19 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,52 @@ | ||
// Copyright 2024 MamedovKenan | ||
#include "TimedDoor.h" | ||
#include "../include/TimedDoor.h" | ||
#include <stdexcept> | ||
#include <thread> | ||
#include <chrono> | ||
|
||
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)); | ||
} |