diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/server.cc | 19 | ||||
-rw-r--r-- | src/server/server.h | 3 |
2 files changed, 21 insertions, 1 deletions
diff --git a/src/server/server.cc b/src/server/server.cc index 2c2ff33..8361b19 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -25,6 +25,8 @@ Server::Server(std::shared_ptr<voltronic::Device> device) : sock_(0) , port_(0) , cacheTimeout_(CACHE_TIMEOUT) + , delay_(0) + , endExecutionTime_(0) , verbose_(false) , device_(std::move(device)) { client_.setDevice(device_); @@ -39,6 +41,10 @@ void Server::setCacheTimeout(u64 timeout) { cacheTimeout_ = timeout; } +void Server::setDelay(u64 delay) { + delay_ = delay; +} + Server::~Server() { if (sock_ > 0) close(sock_); @@ -121,14 +127,25 @@ std::shared_ptr<p18::response_type::BaseResponse> Server::executeCommand(p18::Co cache_.erase(it); } + if (delay_ != 0 && endExecutionTime_ != 0) { + u64 now = voltronic::timestamp(); + u64 diff = now - endExecutionTime_; + + if (diff < delay_) + usleep(delay_ - diff); + } + try { auto response = client_.execute(commandType, arguments); + endExecutionTime_ = voltronic::timestamp(); + CachedResponse cr { - .time = voltronic::timestamp(), + .time = endExecutionTime_, .arguments = arguments, .response = response }; cache_[commandType] = cr; + return response; } catch (voltronic::DeviceError& e) { diff --git a/src/server/server.h b/src/server/server.h index e083c7e..dc24ee8 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -42,6 +42,8 @@ private: std::shared_ptr<voltronic::Device> device_; u64 cacheTimeout_; + u64 delay_; + u64 endExecutionTime_; std::map<p18::CommandType, CachedResponse> cache_; std::mutex threads_mutex_; @@ -59,6 +61,7 @@ public: void setVerbose(bool verbose); void setCacheTimeout(u64 timeout); + void setDelay(u64 delay); void start(std::string& host, int port); bool verbose() const { return verbose_; } |