-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathservice.cpp
85 lines (73 loc) · 2.27 KB
/
service.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include "telemetry/service.hpp"
#include <boost/assert.hpp>
namespace kagome::telemetry {
namespace {
class TelemetryInstanceProxy : public TelemetryService {
public:
void setGenesisBlockHash(const primitives::BlockHash &hash) override {
if (service_) {
service_->setGenesisBlockHash(hash);
}
genesis_hash_ = hash;
}
void notifyBlockImported(const primitives::BlockInfo &info,
BlockOrigin origin) override {
if (service_) {
service_->notifyBlockImported(info, origin);
}
}
void notifyBlockFinalized(const primitives::BlockInfo &info) override {
if (service_) {
service_->notifyBlockFinalized(info);
}
}
void pushBlockStats() override {
if (service_) {
service_->pushBlockStats();
}
}
void notifyWasSynchronized() override {
if (service_) {
service_->notifyWasSynchronized();
}
was_synchronized_ = true;
}
bool isEnabled() const override {
return false;
}
void setActualImplementation(Telemetry service) {
service_ = std::move(service);
if (not service_) {
return;
}
if (was_synchronized_) {
service_->notifyWasSynchronized();
}
service_->setGenesisBlockHash(genesis_hash_);
}
private:
bool was_synchronized_ = false;
primitives::BlockHash genesis_hash_{};
Telemetry service_;
};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::shared_ptr<TelemetryInstanceProxy> telemetry_service_;
} // namespace
void setTelemetryService(std::shared_ptr<TelemetryService> service) {
if (not telemetry_service_) {
telemetry_service_ = std::make_shared<TelemetryInstanceProxy>();
}
telemetry_service_->setActualImplementation(std::move(service));
}
std::shared_ptr<TelemetryService> createTelemetryService() {
if (not telemetry_service_) {
telemetry_service_ = std::make_shared<TelemetryInstanceProxy>();
}
return telemetry_service_;
}
} // namespace kagome::telemetry