Skip to content

Commit

Permalink
Simplify Socket interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MPogotsky committed Oct 5, 2024
1 parent 5ff16b7 commit f24c50f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 55 deletions.
18 changes: 3 additions & 15 deletions test/TestSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,23 @@ class SocketTest : public testing::Test
boost::asio::io_context m_context;
};

TEST_F(SocketTest, initSession_exception)
{
Socket socket(getIoContext());
EXPECT_THROW(runAwaitableVoid(socket.initSession("invalid")), exception::ConnectionClosed);
}

TEST_F(SocketTest, closeSession_exception)
{
Socket socket(getIoContext());
EXPECT_NO_THROW(runAwaitableVoid(socket.closeSession()));
}

TEST_F(SocketTest, login_exception)
{
Socket socket(getIoContext());
std::string accountId = "Invalid";
std::string password = "Invalid123";
std::string accountType = "demo";

std::string streamId;
EXPECT_THROW(streamId = runAwaitable(socket.login(accountId, password)), exception::ConnectionClosed);
EXPECT_THROW(streamId = runAwaitable(socket.login(accountId, password, accountType)), exception::LoginFailed);
EXPECT_TRUE(streamId.empty());
}

TEST_F(SocketTest, logout_exception)
{
Socket socket(getIoContext());
boost::json::object result;
EXPECT_THROW(result = runAwaitable(socket.logout()), exception::ConnectionClosed);
EXPECT_TRUE(result.empty());
EXPECT_THROW(runAwaitableVoid(socket.logout()), exception::ConnectionClosed);
}

TEST_F(SocketTest, getAllSymbols_exception)
Expand Down
21 changes: 7 additions & 14 deletions xapi/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,19 @@ Socket::Socket(boost::asio::io_context &ioContext) : Connection(ioContext), safe
{
}

boost::asio::awaitable<void> Socket::initSession(const std::string &accountType)
boost::asio::awaitable<std::string> Socket::login(const std::string &accountId, const std::string &password, const std::string &accountType)
{
validateAccountType(accountType);
const boost::url socketUrl = boost::urls::format("wss://ws.xtb.com/{}", accountType);
co_await connect(socketUrl);
}

boost::asio::awaitable<void> Socket::closeSession()
{
co_await disconnect();
}

boost::asio::awaitable<std::string> Socket::login(const std::string &accountId, const std::string &password)
{
boost::json::object command = {
{"command", "login"},
{"arguments", {
{"userId", accountId},
{"password", password}
}}
};

auto result = co_await request(command);

if (result["status"].as_bool() != true)
{
throw exception::LoginFailed(boost::json::serialize(result));
Expand All @@ -40,13 +29,17 @@ boost::asio::awaitable<std::string> Socket::login(const std::string &accountId,
co_return result["streamSessionId"].as_string();
}

boost::asio::awaitable<boost::json::object> Socket::logout()
boost::asio::awaitable<void> Socket::logout()
{
boost::json::object command = {
{"command", "logout"}
};
auto result = co_await request(command);
co_return result;
if (result["status"].as_bool() != true)
{
// If logout fails and server is not closed the connection gracefully, close it from client side.
co_await disconnect();
}
}

boost::asio::awaitable<boost::json::object> Socket::getAllSymbols()
Expand Down
27 changes: 9 additions & 18 deletions xapi/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,25 @@ class Socket final : protected internals::Connection
bool safeMode;

/**
* @brief Initializes a session with the specified account type.
* @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.
* @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);
boost::asio::awaitable<std::string> login(const std::string &accountId, const std::string &password, const std::string &accountType);

/**
* @brief Logs out of the server.
* @brief Logs out of the server. If the logout fails, the connection is closed from the client side.
* @return An awaitable boost::json::object containing the logout status.
*/
boost::asio::awaitable<boost::json::object> logout();
boost::asio::awaitable<void> logout();

// Other methods omitted for brevity.
// Description of the omitted methods: http://developers.xstore.pro/documentation/2.5.0#retrieving-trading-data
Expand Down
10 changes: 2 additions & 8 deletions xapi/XStationClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ XStationClient::XStationClient(boost::asio::io_context &ioContext, const boost::
boost::asio::awaitable<void> XStationClient::setupSocketConnection()
{
socket = std::make_unique<xapi::Socket>(m_ioContext);
co_await socket->initSession(m_accountType);
m_streamSessionId = co_await socket->login(m_accountId, m_password);
m_streamSessionId = co_await socket->login(m_accountId, m_password, m_accountType);
}

boost::asio::awaitable<void> XStationClient::setupStreamConnection()
Expand All @@ -42,12 +41,7 @@ boost::asio::awaitable<void> XStationClient::closeSocketConnection()
{
co_return;
}
auto result = co_await socket->logout();
if (result["status"].as_bool() != true)
{
// If logout fails and server is not closed the connection gracefully, close it from client side.
co_await socket->closeSession();
}
co_await socket->logout();
}

boost::asio::awaitable<void> XStationClient::closeStreamConnection()
Expand Down

0 comments on commit f24c50f

Please sign in to comment.