Monero
zmq_pub.h
Go to the documentation of this file.
1 // Copyright (c) 2020, The Monero Project
2 //
3 // All rights reserved.
4  //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <array>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/utility/string_ref.hpp>
34 #include <cstdint>
35 #include <deque>
36 #include <memory>
37 #include <vector>
38 
39 #include "cryptonote_basic/fwd.h"
40 #include "net/zmq.h"
41 #include "span.h"
42 
43 namespace cryptonote { namespace listener
44 {
53 class zmq_pub
54 {
55  /* Each socket has its own internal queue. So we can only use one socket, else
56  the messages being published are not guaranteed to be in the same order
57  pushed. */
58 
60  std::deque<std::vector<txpool_event>> txes_;
61  std::array<std::size_t, 2> chain_subs_;
62  std::array<std::size_t, 2> txpool_subs_;
63  boost::mutex sync_;
64 
65  public:
67  static constexpr const char* relay_endpoint() noexcept { return "inproc://pub_relay"; }
68 
69  explicit zmq_pub(void* context);
70 
71  zmq_pub(const zmq_pub&) = delete;
72  zmq_pub(zmq_pub&&) = delete;
73 
74  ~zmq_pub();
75 
76  zmq_pub& operator=(const zmq_pub&) = delete;
77  zmq_pub& operator=(zmq_pub&&) = delete;
78 
80  bool sub_request(const boost::string_ref message);
81 
84  bool relay_to_pub(void* relay, void* pub);
85 
89  std::size_t send_chain_main(std::uint64_t height, epee::span<const cryptonote::block> blocks);
90 
94  std::size_t send_txpool_add(std::vector<cryptonote::txpool_event> txes);
95 
97  struct chain_main
98  {
99  std::weak_ptr<zmq_pub> self_;
100  void operator()(std::uint64_t height, epee::span<const cryptonote::block> blocks) const;
101  };
102 
104  struct txpool_add
105  {
106  std::weak_ptr<zmq_pub> self_;
107  void operator()(std::vector<cryptonote::txpool_event> txes) const;
108  };
109  };
110 }}
Sends ZMQ PUB messages on cryptonote events.
Definition: zmq_pub.h:54
std::deque< std::vector< txpool_event > > txes_
Definition: zmq_pub.h:60
zmq_pub(const zmq_pub &)=delete
boost::mutex sync_
Synchronizes counts in *_subs_ arrays.
Definition: zmq_pub.h:63
zmq_pub(zmq_pub &&)=delete
std::array< std::size_t, 2 > chain_subs_
Definition: zmq_pub.h:61
~zmq_pub()
Definition: zmq_pub.cpp:340
static constexpr const char * relay_endpoint() noexcept
Definition: zmq_pub.h:67
zmq_pub & operator=(zmq_pub &&)=delete
std::array< std::size_t, 2 > txpool_subs_
Definition: zmq_pub.h:62
bool relay_to_pub(void *relay, void *pub)
Definition: zmq_pub.cpp:378
zmq_pub & operator=(const zmq_pub &)=delete
std::size_t send_txpool_add(std::vector< cryptonote::txpool_event > txes)
Definition: zmq_pub.cpp:439
zmq_pub(void *context)
Definition: zmq_pub.cpp:321
net::zmq::socket relay_
Definition: zmq_pub.h:59
bool sub_request(const boost::string_ref message)
Process a client subscription request (from XPUB sockets). Thread-safe.
Definition: zmq_pub.cpp:343
std::size_t send_chain_main(std::uint64_t height, epee::span< const cryptonote::block > blocks)
Definition: zmq_pub.cpp:410
Definition: blocks.cpp:13
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:108
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:105
Callable for send_chain_main with weak ownership to zmq_pub object.
Definition: zmq_pub.h:98
void operator()(std::uint64_t height, epee::span< const cryptonote::block > blocks) const
Definition: zmq_pub.cpp:460
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:99
Callable for send_txpool_add with weak ownership to zmq_pub object.
Definition: zmq_pub.h:105
void operator()(std::vector< cryptonote::txpool_event > txes) const
Definition: zmq_pub.cpp:469
std::weak_ptr< zmq_pub > self_
Definition: zmq_pub.h:106