Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Мамедов Кенан Эльчин Оглы committed Sep 29, 2024
1 parent 487b393 commit 63fc828
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/TimedDoor.cpp
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));
}

0 comments on commit 63fc828

Please sign in to comment.