Skip to content

Commit

Permalink
Add doxygen documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MPogotsky committed Sep 5, 2024
1 parent 0eadde5 commit c35acc0
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 14 deletions.
76 changes: 69 additions & 7 deletions xapi/Connection.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#pragma once

/**
* @file Connection.hpp
* @brief Defines the Connection class for managing connection.
*
* This file contains the definition of the Connection class, which is used for
* establishing and managing connections, making requests, and handling responses.
*/

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/url.hpp>
#include <chrono>
#include <coroutine>
#include <jsoncpp/json/json.h>
#include <optional>
#include <json/json.h>
#include <string>
#include <unordered_set>

Expand All @@ -18,6 +22,13 @@ namespace xapi
namespace internals
{

/**
* @class Connection
* @brief Manages connection and communication with a server using secure WebSocket.
*
* The Connection class encapsulates the functionality for establishing an SSL connection,
* sending requests, and receiving responses.
*/
class Connection
{
public:
Expand All @@ -27,35 +38,86 @@ class Connection
Connection &operator=(const Connection &) = delete;

Connection(Connection &&other) = default;
// Move assignment operatopr not supported because of boost::beast::websocket::stream
// Move assignment operator is not supported because of boost::beast::websocket::stream
Connection &operator=(Connection &&other) = delete;

/**
* @brief Constructs a new Connection object.
* @param ioContext The IO context for asynchronous operations.
*/
explicit Connection(boost::asio::io_context &ioContext);

virtual ~Connection() = default;

/**
* @brief Asynchronously establishes secure WebSocket connection to the server.
* @param url The URL to connect to.
* @return An awaitable void.
* @throw xapi::exception::ConnectionClosed if the connection fails.
*/
boost::asio::awaitable<void> connect(const boost::url &url);

/**
* @brief Asynchronously disconnects from the server.
* @return An awaitable void.
*/
boost::asio::awaitable<void> disconnect();

/**
* @brief Validates the account type.
* @param accountType The account type to validate.
* @throw xapi::exception::ConnectionClosed if the account type is not known.
*/
void validateAccountType(const std::string &accountType) const;

/**
* @brief Makes an asynchronous request to the server.
* @param command The command to send as a JSON value.
* @return An awaitable void.
* @throw xapi::exception::ConnectionClosed if the request fails.
*/
boost::asio::awaitable<void> makeRequest(const Json::Value &command);

/**
* @brief Waits for a response from the server.
* @return An awaitable Json::Value with response from the server.
* @throw xapi::exception::ConnectionClosed if the response fails.
*/
boost::asio::awaitable<Json::Value> waitResponse();

private:
/**
* @brief Establishes an SSL connection asynchronously.
* @param results The resolved endpoints to attempt to connect to.
* @param host The host name.
* @return An awaitable void.
* @throw xapi::exception::ConnectionClosed if the SSL connection fails.
*/
boost::asio::awaitable<void> establishSSLConnection(boost::asio::ip::tcp::resolver::results_type results,
const char *host);

// The IO context for asynchronous operations.
boost::asio::io_context &m_ioContext;
// SSL context, stores certificates

// SSL context, stores certificates.
boost::asio::ssl::context m_sslContext;

// The WebSocket stream.
boost::beast::websocket::stream<boost::asio::ssl::stream<boost::beast::tcp_stream>> m_websocket;

// Time of the last request.
std::chrono::time_point<std::chrono::system_clock> m_lastRequestTime;

// Flag indicating if the connection is established.
bool m_connectionEstablished;

// Timeout for requests.
const std::chrono::milliseconds m_requestTimeout;

// Default port for WebSocket connections.
const std::string m_websocketDefaultPort;

// Set of known account types.
const std::unordered_set<std::string> m_knownAccountTypes;
};

Expand Down
27 changes: 25 additions & 2 deletions xapi/Enums.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#pragma once

namespace xapi {
/**
* @file Enums.hpp
* @brief Defines the enumeration classes for the xAPI.
*
* This file contains the definition of the enumeration classes used in the XAPI.
*/

// Enum classes
namespace xapi
{

/**
* @enum TradeCmd
* @brief Represents trading commands in the xAPI.
*/
enum class TradeCmd
{
BUY = 0, // buy
Expand All @@ -16,6 +27,10 @@ enum class TradeCmd
CREDIT = 7 // Read only
};

/**
* @enum TradeType
* @brief Represents the type of a trade in the xAPI.
*/
enum class TradeType
{
OPEN = 0, // order open, used for opening orders
Expand All @@ -25,6 +40,10 @@ enum class TradeType
DELETE = 4 // order delete, only used in the tradeTransaction command
};

/**
* @enum TradeStatus
* @brief Represents the status of a trade in the xAPI.
*/
enum class TradeStatus
{
ERROR = 0, // error
Expand All @@ -33,6 +52,10 @@ enum class TradeStatus
REJECTED = 4 // The transaction has been rejected
};

/**
* @enum PeriodCode
* @brief Represents the period code for a time period in the xAPI.
*/
enum class PeriodCode
{
PERIOD_M1 = 1, // 1 minute
Expand Down
21 changes: 20 additions & 1 deletion xapi/Exceptions.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#pragma once

/**
* @file Exceptions.hpp
* @brief Defines the exception classes for the XAPI.
*
* This file contains the definition of the exception classes used in the XAPI.
*/

#include <exception>
#include <iostream>
#include <string>

namespace xapi
{
namespace exception
{

/**
* @class LoginFailed
* @brief Exception class to indicate login failure.
*
* This exception is thrown when a login attempt fails.
*/
class LoginFailed final : public std::exception
{
public:
Expand All @@ -24,6 +37,12 @@ class LoginFailed final : public std::exception
std::string m_message;
};

/**
* @class ConnectionClosed
* @brief Exception class to indicate that the connection has been closed.
*
* This exception is thrown when connection fails.
*/
class ConnectionClosed final : public std::exception
{
public:
Expand Down
53 changes: 53 additions & 0 deletions xapi/Socket.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
#pragma once

/**
* @file Socket.hpp
* @brief Defines the Socket class for retrieving trading data.
*
* This file contains the definition of the Socket class, which encapsulates
* operations for retrieving trading data from xAPI.
*/

#include "Connection.hpp"
#include "Enums.hpp"

namespace xapi
{

/**
* @brief Encapsulates operations for retrieving trading data from xAPI.
*
* The Socket class provides a high-level interface for retrieving trading data
* from xAPI. It is built on top of the Connection class, which handles the
* low-level details of establishing and maintaining a connection.
*/
class Socket final : protected internals::Connection
{
public:
Expand All @@ -17,19 +32,52 @@ class Socket final : protected internals::Connection
Socket(Socket &&other) = default;
Socket &operator=(Socket &&other) = delete;

/**
* @brief Constructs a new Socket object.
* @param ioContext The IO context for asynchronous operations.
*/
explicit Socket(boost::asio::io_context &ioContext);
~Socket() override = default;

/**
* Flag to indicate if the socket operates in safe mode. Safe mode means that you can not send any trade commands.
* Only read-only commands are allowed.
*/
bool safeMode;

/**
* @brief Initializes a session with the specified account type.
* @param accountType The type of account to initialize the session for.
* Possible values are:
* - "demo" for a demo account,
* - "real" for a real money account.
* @return An awaitable void.
*/
boost::asio::awaitable<void> initSession(const std::string &accountType);

/**
* @brief Closes the current session.
* @return An awaitable void.
*/
boost::asio::awaitable<void> closeSession();

/**
* @brief Logs in to the server with the specified account ID and password.
* @param accountId The account ID to log in with.
* @param password The password for the account.
* @return An awaitable string containing the stream session ID.
*/
boost::asio::awaitable<std::string> login(const std::string &accountId, const std::string &password);

/**
* @brief Logs out of the server.
* @return An awaitable Json::Value containing the logout status.
*/
boost::asio::awaitable<Json::Value> logout();

// Other methods omitted for brevity.
// Description of the omitted methods: http://developers.xstore.pro/documentation/2.5.0#retrieving-trading-data

boost::asio::awaitable<Json::Value> getAllSymbols();

boost::asio::awaitable<Json::Value> getCalendar();
Expand Down Expand Up @@ -83,6 +131,11 @@ class Socket final : protected internals::Connection
boost::asio::awaitable<Json::Value> tradeTransactionStatus(int order);

private:
/**
* @brief Sends a request to the server and waits for response.
* @param command The command to send as a Json::Value.
* @return An awaitable Json::Value with the response from the server.
*/
boost::asio::awaitable<Json::Value> request(const Json::Value &command);
};

Expand Down
45 changes: 41 additions & 4 deletions xapi/Stream.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
#pragma once

/**
* @file Stream.hpp
* @brief Defines the Stream class for managing streaming commands.
*
* This file contains the definition of the Stream class, which encapsulates
* streaming operations for real-time streaming data.
*/

#include "Connection.hpp"
#include "Enums.hpp"

namespace xapi
{

/**
* @brief Encapsulates operations for streaming real-time data from xAPI.
*
* The Stream class provides a high-level interface for streaming real-time data
* from xAPI.
*/
class Stream final : protected internals::Connection
{
public:
Expand All @@ -17,16 +30,39 @@ class Stream final : protected internals::Connection
Stream(Stream &&other) = default;
Stream &operator=(Stream &&other) = delete;

/**
* @brief Constructs a new Stream object.
* @param ioContext The IO context for asynchronous operations.
*/
explicit Stream(boost::asio::io_context &ioContext);
~Stream() override = default;

boost::asio::awaitable<void> initSession(const std::string &accountType,
const std::string &streamSessionId);

/**
* @brief Initializes a streaming session with the specified account type and stream session ID.
* @param accountType The type of account to initialize the session for.
* Possible values are:
* - "demo" for a demo account,
* - "real" for a real money account.
* @param streamSessionId The stream session ID returned by Socket login call.
* @return An awaitable void.
*/
boost::asio::awaitable<void> initSession(const std::string &accountType, const std::string &streamSessionId);

/**
* @brief Closes the current streaming session.
* @return An awaitable void.
*/
boost::asio::awaitable<void> closeSession();

/**
* @brief Starts listening for streaming data.
* @return An awaitable Json::Value with streaming data.
*/
boost::asio::awaitable<Json::Value> listen();

// Other methods omitted for brevity.
// Description of the omitted methods: http://developers.xstore.pro/documentation/2.5.0#retrieving-trading-data

boost::asio::awaitable<void> getBalance();

boost::asio::awaitable<void> stopBalance();
Expand Down Expand Up @@ -62,6 +98,7 @@ class Stream final : protected internals::Connection
boost::asio::awaitable<void> ping();

private:
// The stream session ID.
std::string m_streamSessionId;
};

Expand Down

0 comments on commit c35acc0

Please sign in to comment.