danicoin/tests/IntegrationTestLib/NodeObserver.h

112 lines
2.3 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-05-27 12:08:46 +00:00
#pragma once
#include "INode.h"
#include <mutex>
#include <condition_variable>
namespace CryptoNote {
template <typename T>
class ObservableValue {
public:
ObservableValue(const T defaultValue = 0) :
m_prev(defaultValue), m_value(defaultValue) {
}
void init(T value) {
std::lock_guard<std::mutex> lk(m_mutex);
m_value = m_prev = value;
}
void set(T value) {
std::lock_guard<std::mutex> lk(m_mutex);
m_value = value;
m_cv.notify_all();
}
T get() {
std::lock_guard<std::mutex> lk(m_mutex);
return m_value;
}
bool waitFor(std::chrono::milliseconds ms, T& value) {
std::unique_lock<std::mutex> lk(m_mutex);
if (m_cv.wait_for(lk, ms, [this] { return m_prev != m_value; })) {
value = m_prev = m_value;
return true;
}
return false;
}
T wait() {
std::unique_lock<std::mutex> lk(m_mutex);
m_cv.wait(lk, [this] { return m_prev != m_value; });
m_prev = m_value;
return m_value;
}
private:
std::mutex m_mutex;
std::condition_variable m_cv;
T m_prev;
T m_value;
};
class NodeObserver: public INodeObserver {
public:
NodeObserver(INode& node) : m_node(node) {
m_knownHeight.init(node.getLastKnownBlockHeight());
node.addObserver(this);
}
~NodeObserver() {
m_node.removeObserver(this);
}
2015-07-30 15:22:07 +00:00
virtual void lastKnownBlockHeightUpdated(uint32_t height) override {
2015-05-27 12:08:46 +00:00
m_knownHeight.set(height);
}
2015-07-30 15:22:07 +00:00
virtual void localBlockchainUpdated(uint32_t height) override {
2015-05-27 12:08:46 +00:00
m_localHeight.set(height);
}
virtual void peerCountUpdated(size_t count) override {
m_peerCount.set(count);
}
2015-07-30 15:22:07 +00:00
bool waitLastKnownBlockHeightUpdated(std::chrono::milliseconds ms, uint32_t& value) {
2015-05-27 12:08:46 +00:00
return m_knownHeight.waitFor(ms, value);
}
2015-07-30 15:22:07 +00:00
bool waitLocalBlockchainUpdated(std::chrono::milliseconds ms, uint32_t& value) {
2015-05-27 12:08:46 +00:00
return m_localHeight.waitFor(ms, value);
}
2015-07-30 15:22:07 +00:00
uint32_t waitLastKnownBlockHeightUpdated() {
2015-05-27 12:08:46 +00:00
return m_knownHeight.wait();
}
2015-07-30 15:22:07 +00:00
ObservableValue<uint32_t> m_knownHeight;
ObservableValue<uint32_t> m_localHeight;
2015-05-27 12:08:46 +00:00
ObservableValue<size_t> m_peerCount;
private:
INode& m_node;
};
}