Skip to content

Commit

Permalink
feat: implement ftp::observer::on_connected() event
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskovalchuk committed Sep 23, 2023
1 parent bc75ad3 commit ae41fbe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/ftp/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class client

static std::string make_type_command(transfer_type type);

void notify_connected(std::string_view hostname, std::uint16_t port);

void notify_request(std::string_view command);

void notify_reply(const reply & reply);
Expand Down
2 changes: 2 additions & 0 deletions include/ftp/observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace ftp
class observer
{
public:
virtual void on_connected(std::string_view hostname, std::uint16_t port) { };

virtual void on_request(std::string_view command) { };

virtual void on_reply(const reply & reply) { };
Expand Down
10 changes: 10 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ replies client::connect(std::string_view hostname,
{
control_connection_.open(hostname, port);

notify_connected(hostname, port);

replies replies;
reply reply = recv(replies);

Expand Down Expand Up @@ -726,6 +728,14 @@ std::string client::make_type_command(transfer_type type)
}
}

void client::notify_connected(std::string_view hostname, std::uint16_t port)
{
for (const std::shared_ptr<observer> & observer : observers_)
{
observer->on_connected(hostname, port);
}
}

void client::notify_request(std::string_view command)
{
for (const std::shared_ptr<observer> & observer : observers_)
Expand Down
30 changes: 24 additions & 6 deletions test/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ class test_observer : public ftp::observer
: mark_(mark)
{}

void on_connected(std::string_view hostname, std::uint16_t port) override
{
status_strings_.append(mark_);
status_strings_.append(": -- ");
status_strings_.append("Connected to ");
status_strings_.append(hostname);
status_strings_.append(" on port ");
status_strings_.append(std::to_string(port));
status_strings_.append(".");
}

void on_request(std::string_view command) override
{
status_strings_.append(mark_);
Expand Down Expand Up @@ -674,15 +685,17 @@ TEST_F(client, event_observer)

check_reply(client.connect("localhost", 2121), "220 FTP server is ready.");
check_reply(client.disconnect(), "221 Goodbye.");
ASSERT_EQ("observer1: <- 220 FTP server is ready."
ASSERT_EQ("observer1: -- Connected to localhost on port 2121."
"observer1: <- 220 FTP server is ready."
"observer1: -> QUIT"
"observer1: <- 221 Goodbye.", obs1->get_status_strings());

client.remove_observer(obs1);

check_reply(client.connect("localhost", 2121), "220 FTP server is ready.");
check_reply(client.disconnect(), "221 Goodbye.");
ASSERT_EQ("observer1: <- 220 FTP server is ready."
ASSERT_EQ("observer1: -- Connected to localhost on port 2121."
"observer1: <- 220 FTP server is ready."
"observer1: -> QUIT"
"observer1: <- 221 Goodbye.", obs1->get_status_strings());
}
Expand All @@ -697,23 +710,28 @@ TEST_F(client, event_observer)

check_reply(client.connect("localhost", 2121), "220 FTP server is ready.");
check_reply(client.disconnect(), "221 Goodbye.");
ASSERT_EQ("observer1: <- 220 FTP server is ready."
ASSERT_EQ("observer1: -- Connected to localhost on port 2121."
"observer1: <- 220 FTP server is ready."
"observer1: -> QUIT"
"observer1: <- 221 Goodbye.", obs1->get_status_strings());
ASSERT_EQ("observer2: <- 220 FTP server is ready."
ASSERT_EQ("observer2: -- Connected to localhost on port 2121."
"observer2: <- 220 FTP server is ready."
"observer2: -> QUIT"
"observer2: <- 221 Goodbye.", obs2->get_status_strings());

client.remove_observer(obs1);

check_reply(client.connect("localhost", 2121), "220 FTP server is ready.");
check_reply(client.disconnect(), "221 Goodbye.");
ASSERT_EQ("observer1: <- 220 FTP server is ready."
ASSERT_EQ("observer1: -- Connected to localhost on port 2121."
"observer1: <- 220 FTP server is ready."
"observer1: -> QUIT"
"observer1: <- 221 Goodbye.", obs1->get_status_strings());
ASSERT_EQ("observer2: <- 220 FTP server is ready."
ASSERT_EQ("observer2: -- Connected to localhost on port 2121."
"observer2: <- 220 FTP server is ready."
"observer2: -> QUIT"
"observer2: <- 221 Goodbye."
"observer2: -- Connected to localhost on port 2121."
"observer2: <- 220 FTP server is ready."
"observer2: -> QUIT"
"observer2: <- 221 Goodbye.", obs2->get_status_strings());
Expand Down

0 comments on commit ae41fbe

Please sign in to comment.