CBOE Emulator  1.0
heartbeat.hpp
1 // A context extension for printing data from a feed using a heartbeat timer.
2 // Copyright 2020 Christian Kauten
3 //
4 // Author: Christian Kauten (kautenja@auburn.edu)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef DATA_FEED_EXTENSIONS_HEARTBEAT_HPP
19 #define DATA_FEED_EXTENSIONS_HEARTBEAT_HPP
20 
21 #include "data_feed/receiver.hpp"
22 #include <asio.hpp>
23 #include <cstdint>
24 #include <chrono>
25 #include <ostream>
26 
28 namespace DataFeed {
29 
31 namespace Extensions {
32 
34 template<typename T>
35 class Heartbeat {
36  private:
38  Receiver<T>& receiver;
40  asio::steady_timer timer;
42  uint16_t time;
44  std::ostream& stream;
45 
47  void start() {
48  // clear the terminal
49  stream << "\033[2J" << "\033[1;1H" << std::endl;
50  // print the top of book information
51  stream << receiver.get_book() << std::endl;
52  // set a timer to start the strategy again
53  timer.expires_after(std::chrono::milliseconds(time));
54  timer.async_wait([this](const std::error_code& error) {
55  if (error) throw error;
56  start();
57  });
58  }
59 
60  public:
69  asio::io_context& context,
70  Receiver<T>& receiver_,
71  uint16_t time_ = 300,
72  std::ostream& stream_ = std::cout
73  ) : receiver(receiver_), timer(context), time(time_), stream(stream_) {
74  start();
75  }
76 };
77 
78 } // namespace Extensions
79 
80 } // namespace DataFeed
81 
82 #endif // DATA_FEED_EXTENSIONS_HEARTBEAT_HPP
DataFeed::Extensions::Heartbeat
A Receiver context extension for printing data using a heartbeat timer.
Definition: heartbeat.hpp:35
DataFeed::Receiver< T >
DataFeed::Extensions::Heartbeat::Heartbeat
Heartbeat(asio::io_context &context, Receiver< T > &receiver_, uint16_t time_=300, std::ostream &stream_=std::cout)
Initialize a new receiver heartbeat.
Definition: heartbeat.hpp:68
DataFeed::Receiver::get_book
const LOB::LimitOrderBook & get_book()
Return the limit order book for this receiver.
Definition: receiver.hpp:214
DataFeed
Logic for sending and receiving messages on a financial data feed.
Definition: heartbeat.hpp:28