ledger-core
AbstractBlockchainObserver.h
1 /*
2  *
3  * AbstractBlockchainObserver
4  *
5  * Created by El Khalil Bellakrid on 29/11/2018.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2018 Ledger
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  */
30 
31 
32 #ifndef LEDGER_CORE_ABSTRACTBLOCKCHAINOBSERVER_H
33 #define LEDGER_CORE_ABSTRACTBLOCKCHAINOBSERVER_H
34 
35 #include <async/DedicatedContext.hpp>
36 #include <api/Currency.hpp>
37 #include <api/ExecutionContext.hpp>
38 #include <api/DynamicObject.hpp>
39 #include <debug/logger.hpp>
40 #include <soci.h>
41 
42 namespace ledger {
43  namespace core {
44  template<typename Account, typename BlockchainExplorerTransaction, typename BlockchainExplorerBlock>
46 
47  public:
48  virtual bool registerAccount(const std::shared_ptr<Account>& account) {
49  std::lock_guard<std::mutex> lock(_lock);
50  if (!_isRegistered(lock, account)) {
51  bool needsStart = _accounts.empty();
52  _accounts.push_front(account);
53  if (needsStart)
54  onStart();
55  return true;
56  }
57  return false;
58  };
59  virtual bool unregisterAccount(const std::shared_ptr<Account>& account) {
60  std::lock_guard<std::mutex> lock(_lock);
61  if (_isRegistered(lock, account)) {
62  bool needsStop = _accounts.size() == 1;
63  _accounts.remove(account);
64  if (needsStop)
65  onStop();
66  return true;
67  }
68  return false;
69  };
70 
71  virtual bool isRegistered(const std::shared_ptr<Account>& account) {
72  std::lock_guard<std::mutex> lock(_lock);
73  return _isRegistered(lock, account);
74  };
75 
76  std::shared_ptr<spdlog::logger> logger() const {
77  return _logger;
78  };
79 
80  bool isObserving() const {
81  std::lock_guard<std::mutex> lock(_lock);
82  return _accounts.size() > 0;
83  };
84 
85  protected:
86  virtual void onStart() = 0;
87  virtual void onStop() = 0;
88 
89  virtual void putTransaction(const BlockchainExplorerTransaction& tx) = 0;
90  virtual void putBlock(const BlockchainExplorerBlock& block) = 0;
91 
92 
93  void setLogger(const std::shared_ptr<spdlog::logger>& logger) {
94  _logger = logger;
95  }
96 
97  private:
98  bool _isRegistered(std::lock_guard<std::mutex>& lock, const std::shared_ptr<Account>& account) {
99  for (auto& acc : _accounts) {
100  if (acc.get() == account.get())
101  return true;
102  }
103  return false;
104  };
105 
106  protected:
107  std::list<std::shared_ptr<Account>> _accounts;
108  mutable std::mutex _lock;
109  std::shared_ptr<spdlog::logger> _logger;
110  };
111  }
112 }
113 
114 #endif //LEDGER_CORE_ABSTRACTBLOCKCHAINOBSERVER_H
Definition: AbstractBlockchainObserver.h:45
Definition: Account.cpp:8
Definition: logger.hpp:44