Skip to content
Snippets Groups Projects
Commit 5608cb3b authored by Konstantinos Sideris's avatar Konstantinos Sideris
Browse files

Allow the port number along with the domain in set_server

parent f2b4bffc
No related branches found
No related tags found
No related merge requests found
......@@ -73,7 +73,7 @@ public:
//! Wait for the client to close.
void close();
//! Set the homeserver domain name.
void set_server(const std::string &server) { server_ = server; };
void set_server(const std::string &server);
//! Retrieve the homeserver domain name.
std::string server() { return server_; };
//! Set the homeserver port.
......
......@@ -10,6 +10,10 @@ namespace mtx {
namespace client {
namespace utils {
//! Check if the given string represents a number.
bool
is_number(const std::string &s);
//! Generates a random string of the given size.
std::string
random_token(uint8_t len = 12, bool with_symbols = true) noexcept;
......
......@@ -22,6 +22,21 @@ Client::Client(const std::string &server, uint16_t port)
thread_group_.add_thread(new boost::thread([this]() { ios_.run(); }));
}
void
Client::set_server(const std::string &server)
{
// Check if the input also contains the port.
std::vector<std::string> parts;
boost::split(parts, server, [](char c) { return c == ':'; });
if (parts.size() == 2 && mtx::client::utils::is_number(parts.at(1))) {
server_ = parts.at(0);
port_ = std::stoi(parts.at(1));
} else {
server_ = server;
}
};
void
Client::close()
{
......
......@@ -14,6 +14,13 @@
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
bool
mtx::client::utils::is_number(const std::string &s)
{
return !s.empty() &&
std::find_if(s.begin(), s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
}
std::string
mtx::client::utils::random_token(uint8_t len, bool with_symbols) noexcept
{
......
......@@ -25,6 +25,19 @@ TEST(Basic, Connection)
alice->close();
}
TEST(Basic, ServerWithPort)
{
auto alice = std::make_shared<Client>("matrix.org");
alice->set_server("localhost:8448");
EXPECT_EQ(alice->server(), "localhost");
EXPECT_EQ(alice->port(), 8448);
alice->versions(
[](const mtx::responses::Versions &, RequestErr err) { ASSERT_FALSE(err); });
alice->close();
}
TEST(Basic, Failure)
{
auto alice = std::make_shared<Client>("not-resolvable-example-domain.wrong");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment