CBOE Emulator  1.0
server_heartbeat.hpp
1 // A context extension for printing data from an order entry server.
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 ORDER_ENTRY_EXTENSIONS_SERVER_HEARTBEAT_HPP
19 #define ORDER_ENTRY_EXTENSIONS_SERVER_HEARTBEAT_HPP
20 
21 #include "order_entry/server.hpp"
22 #include "data_feed/sender.hpp"
23 #include <asio.hpp>
24 #include <cstdint>
25 #include <chrono>
26 #include <ostream>
27 
29 namespace OrderEntry {
30 
32 namespace Extensions {
33 
36  private:
38  Server& server;
40  DataFeed::Sender& feed;
42  asio::steady_timer timer;
44  uint16_t time;
46  std::ostream& stream;
47 
49  void start() {
50  timer.expires_after(std::chrono::milliseconds(time));
51  timer.async_wait([this](const std::error_code& error) {
52  if (error) throw error;
53  // clear the terminal
54  stream << "\033[2J" << "\033[1;1H" << std::endl;
55  // print the server information
56  stream << "Order Entry: ";
57  stream << server.address() << ":" << server.port() << std::endl;
58  // print the feed information
59  stream << "Data Feed: ";
60  stream << feed.group() << ":" << feed.port() << std::endl;
61  stream << "\tqueued: " << feed.get_output_buffer_size() << std::endl;
62  // print the authorizer information
63  stream << std::endl;
64  stream << server.get_authorizer() << std::endl;
65  // print the top of book information
66  stream << server.get_book() << std::endl;
67  // start the timer again
68  start();
69  });
70  }
71 
72  public:
82  asio::io_context& context,
83  Server& server_,
84  DataFeed::Sender& feed_,
85  uint16_t time_ = 300,
86  std::ostream& stream_ = std::cout
87  ) :
88  server(server_),
89  feed(feed_),
90  timer(context),
91  time(time_),
92  stream(stream_) { start(); }
93 };
94 
95 } // namespace Extensions
96 
97 } // namespace OrderEntry
98 
99 #endif // ORDER_ENTRY_EXTENSIONS_SERVER_HEARTBEAT_HPP
OrderEntry
Logic for sending/receiving application messages in a financial market.
Definition: authorizer.hpp:26
DataFeed::Sender::port
uint16_t port() const
Return the port this sender is running at.
Definition: sender.hpp:111
DataFeed::Sender
A class for multi-casting depth of book messages from a LOB::LimitOrderBook.
Definition: sender.hpp:31
OrderEntry::Extensions::ServerHeartbeat
A Server context extension for printing data using a heartbeat timer.
Definition: server_heartbeat.hpp:35
OrderEntry::Server::get_authorizer
const Authorizer< Connection > & get_authorizer()
Return the authorizer associated with this server.
Definition: server.hpp:107
OrderEntry::Server::address
asio::ip::address address() const
Return the address the server is running at.
Definition: server.hpp:93
DataFeed::Sender::get_output_buffer_size
uint32_t get_output_buffer_size() const
Return the current size of the output buffer (number of messages).
Definition: sender.hpp:117
OrderEntry::Extensions::ServerHeartbeat::ServerHeartbeat
ServerHeartbeat(asio::io_context &context, Server &server_, DataFeed::Sender &feed_, uint16_t time_=300, std::ostream &stream_=std::cout)
Initialize a new receiver heartbeat.
Definition: server_heartbeat.hpp:81
DataFeed::Sender::group
asio::ip::address group() const
Return the group that this sender is sending to.
Definition: sender.hpp:105
OrderEntry::Server
A server that manages multiple client connections for direct market access.
Definition: server.hpp:33
OrderEntry::Server::port
uint16_t port() const
Return the port the server is running at.
Definition: server.hpp:101
OrderEntry::Server::get_book
const LOB::LimitOrderBook & get_book()
Return the limit order book for this server.
Definition: server.hpp:113