CBOE Emulator  1.0
server.hpp
1 // A server for hosting a financial instrument exchange micro-service.
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_SERVER_HPP
19 #define ORDER_ENTRY_SERVER_HPP
20 
21 #include "exceptions.hpp"
22 #include "limit_order_book/limit_order_book.hpp"
23 #include "connection.hpp"
24 #include "authorizer.hpp"
25 #include <asio.hpp>
26 #include <nlohmann/json.hpp>
27 #include <vector>
28 
30 namespace OrderEntry {
31 
33 class Server {
34  private:
36  asio::io_context& context;
38  asio::ip::tcp::acceptor acceptor;
40  Authorizer<Connection> authorizer;
42  LOB::LimitOrderBook& book;
43 
45  void accept() {
46  // create a new connection with a shared pointer
47  auto connection = Connection::create(context, authorizer, book);
48  // asynchronously accept a new connection
49  acceptor.async_accept(
50  connection->get_socket(),
51  [this, connection](const std::error_code& error) {
52  // throw an error if there is an error code
53  if (error) throw Exception(error.message());
54  // start the connection and accept another connection
55  connection->start();
56  accept();
57  }
58  );
59  }
60 
61  public:
70  asio::io_context& context_,
71  LOB::LimitOrderBook& book_,
72  uint16_t port,
73  std::vector<nlohmann::json> accounts
74  ) :
75  context(context_),
76  acceptor(context, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)),
77  authorizer(),
78  book(book_) {
79  for (auto user : accounts) { // iterate over the accounts
80  auto username = user["username"].get<std::string>();
81  auto password = user["password"].get<std::string>();
82  // create a new user
83  authorizer.new_user(username, password);
84  }
85  // start accepting connections
86  accept();
87  }
88 
93  inline asio::ip::address address() const {
94  return acceptor.local_endpoint().address();
95  }
96 
101  inline uint16_t port() const { return acceptor.local_endpoint().port(); }
102 
107  inline const Authorizer<Connection>& get_authorizer() { return authorizer; }
108 
113  inline const LOB::LimitOrderBook& get_book() { return book; }
114 };
115 
116 } // namespace OrderEntry
117 
118 #endif // ORDER_ENTRY_SERVER_HPP
OrderEntry::Connection::create
static Pointer create(asio::io_context &context, Authorizer< Connection > &authorizer, LOB::LimitOrderBook &book)
Create a shared pointer to a new connection.
Definition: connection.hpp:326
OrderEntry
Logic for sending/receiving application messages in a financial market.
Definition: authorizer.hpp:26
OrderEntry::Server::Server
Server(asio::io_context &context_, LOB::LimitOrderBook &book_, uint16_t port, std::vector< nlohmann::json > accounts)
Initialize a new order entry server.
Definition: server.hpp:69
OrderEntry::Authorizer
Logic for looking up and validating credentials for new connections.
Definition: authorizer.hpp:32
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
OrderEntry::LOB::LimitOrderBook
An order book for managing Limit / Order objects in a continuous double auction.
Definition: limit_order_book.hpp:40
OrderEntry::Authorizer::new_user
void new_user(std::string username, std::string password)
Create a new user in the account manager.
Definition: authorizer.hpp:46
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