ledger-core
RippleLikeBlockchainObserver.h
1 /*
2  *
3  * RippleLikeBlockchainObserver
4  *
5  * Created by El Khalil Bellakrid on 06/01/2019.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2019 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_RIPPLELIKEBLOCKCHAINOBSERVER_H
33 #define LEDGER_CORE_RIPPLELIKEBLOCKCHAINOBSERVER_H
34 
35 
36 #include <string>
37 #include <list>
38 
39 #include <wallet/ripple/explorers/RippleLikeBlockchainExplorer.h>
40 
41 #include <utils/ConfigurationMatchable.h>
42 #include <async/DedicatedContext.hpp>
43 
44 #include <api/ExecutionContext.hpp>
45 #include <api/Currency.hpp>
46 #include <api/DynamicObject.hpp>
47 
48 #include <net/WebSocketClient.h>
49 #include <net/WebSocketConnection.h>
50 
51 #include <spdlog/logger.h>
52 #include <wallet/common/observers/AbstractBlockchainObserver.h>
53 #include <wallet/common/observers/AbstractLedgerApiBlockchainObserver.h>
54 
55 namespace ledger {
56  namespace core {
57  class RippleLikeAccount;
58 
59  using RippleBlockchainObserver = AbstractBlockchainObserver<RippleLikeAccount, RippleLikeBlockchainExplorerTransaction, RippleLikeBlockchainExplorer::Block>;
60 
63  public DedicatedContext,
65  public std::enable_shared_from_this<RippleLikeBlockchainObserver> {
66  public:
67  RippleLikeBlockchainObserver(const std::shared_ptr<api::ExecutionContext> &context,
68  const std::shared_ptr<api::DynamicObject> &configuration,
69  const std::shared_ptr<spdlog::logger> &logger,
70  const api::Currency &currency,
71  const std::vector<std::string> &matchableKeys);
72 
73  RippleLikeBlockchainObserver(const std::shared_ptr<api::ExecutionContext> &context,
74  const std::shared_ptr<WebSocketClient> &client,
75  const std::shared_ptr<api::DynamicObject> &configuration,
76  const std::shared_ptr<spdlog::logger> &logger,
77  const api::Currency &currency);
78 
79 
80  protected:
81  void putTransaction(const RippleLikeBlockchainExplorerTransaction &tx) override;
82 
83  void putBlock(const RippleLikeBlockchainExplorer::Block &block) override;
84 
85  const api::Currency &getCurrency() const {
86  return _currency;
87  };
88 
89  std::shared_ptr<api::DynamicObject> getConfiguration() const {
90  return _configuration;
91  };
92 
93  private:
94  api::Currency _currency;
95  std::shared_ptr<api::DynamicObject> _configuration;
96 
97 
98  protected:
99  void onStart() override;
100 
101  void onStop() override;
102 
103  private:
104  void connect() override;
105 
106  void reconnect() override;
107 
108  void onMessage(const std::string &message) override;
109 
110  private:
111  std::shared_ptr<spdlog::logger> logger() const override ;
112  std::shared_ptr<WebSocketClient> _client;
113  WebSocketEventHandler _handler;
114  };
115  }
116 }
117 
118 
119 #endif //LEDGER_CORE_RIPPLELIKEBLOCKCHAINOBSERVER_H
Definition: AbstractLedgerApiBlockchainObserver.h:40
Definition: AbstractBlockchainObserver.h:45
Definition: ConfigurationMatchable.h:38
Definition: RippleLikeBlockchainExplorer.h:55
Definition: Currency.hpp:23
Definition: RippleLikeBlockchainObserver.h:61
Definition: Account.cpp:8
Definition: DedicatedContext.hpp:39
Definition: Block.h:40
Definition: logger.hpp:44