diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-05-22 23:43:16 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-05-22 23:43:16 +0300 |
commit | a2e133a11fc7e91f18bf23723a975bf69fb218db (patch) | |
tree | 127e52b610d5e4106d69f692e7444a537b1e61fd /src/server | |
parent | 1c6fa70ab65e4b267b5163b18dd8743d3fdc8e8d (diff) |
server: add --device-error-limit
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/server.cc | 12 | ||||
-rw-r--r-- | src/server/server.h | 6 |
2 files changed, 17 insertions, 1 deletions
diff --git a/src/server/server.cc b/src/server/server.cc index c464b13..a025a7d 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -25,8 +25,10 @@ Server::Server(std::shared_ptr<voltronic::Device> device) : sock_(0) , port_(0) , cacheTimeout_(CACHE_TIMEOUT) - , delay_(0) + , delay_(DELAY) , endExecutionTime_(0) + , deviceErrorLimit_(DEVICE_ERROR_LIMIT) + , deviceErrorCounter_(0) , verbose_(false) , device_(std::move(device)) { client_.setDevice(device_); @@ -45,6 +47,10 @@ void Server::setDelay(u64 delay) { delay_ = delay; } +void Server::setDeviceErrorLimit(u32 deviceErrorLimit) { + deviceErrorLimit_ = deviceErrorLimit; +} + Server::~Server() { if (sock_ > 0) close(sock_); @@ -146,9 +152,13 @@ std::shared_ptr<p18::response_type::BaseResponse> Server::executeCommand(p18::Co }; cache_[commandType] = cr; + deviceErrorCounter_ = 0; return response; } catch (voltronic::DeviceError& e) { + deviceErrorCounter_++; + if (!shutdownCaught && deviceErrorCounter_ >= deviceErrorLimit_) + shutdownCaught = true; throw std::runtime_error("device error: " + std::string(e.what())); } catch (voltronic::TimeoutError& e) { diff --git a/src/server/server.h b/src/server/server.h index dc24ee8..705503f 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -44,6 +44,8 @@ private: u64 cacheTimeout_; u64 delay_; u64 endExecutionTime_; + u32 deviceErrorLimit_; + u32 deviceErrorCounter_; std::map<p18::CommandType, CachedResponse> cache_; std::mutex threads_mutex_; @@ -53,6 +55,8 @@ private: public: static const u64 CACHE_TIMEOUT = 1000; + static const u32 DEVICE_ERROR_LIMIT = 10; + static const u64 DELAY = 0; volatile std::atomic<bool> sigCaught = 0; @@ -62,6 +66,8 @@ public: void setVerbose(bool verbose); void setCacheTimeout(u64 timeout); void setDelay(u64 delay); + void setDeviceErrorLimit(u32 deviceErrorLimit); + void start(std::string& host, int port); bool verbose() const { return verbose_; } |