From 1d6b704c15ee289037447fb566e7583962496650 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Sun, 26 May 2024 12:31:12 -0500 Subject: Refactor to separate interface from implementation --- src/client/websocketpp_client.cpp | 193 ++++++++++++++++---------------------- 1 file changed, 83 insertions(+), 110 deletions(-) (limited to 'src/client') diff --git a/src/client/websocketpp_client.cpp b/src/client/websocketpp_client.cpp index 9967d74..3cc6c99 100644 --- a/src/client/websocketpp_client.cpp +++ b/src/client/websocketpp_client.cpp @@ -1,132 +1,105 @@ -#include "web_socket_client.hpp" - -using std::error_code; -using std::function; -using std::lock_guard; -using std::make_tuple; -using std::mutex; -using std::string; -using std::tuple; -using std::unordered_map; - -namespace nostr -{ -namespace client -{ -/** - * @brief An implementation of the `IWebSocketClient` interface that uses the WebSocket++ library. - */ -class WebsocketppClient : public IWebSocketClient -{ -public: - void start() override - { - this->_client.init_asio(); - this->_client.start_perpetual(); - }; - - void stop() override - { - this->_client.stop_perpetual(); - this->_client.stop(); - }; +#include "websocketpp_client.hpp" - void openConnection(string uri) override - { - error_code error; - websocketpp_client::connection_ptr connection = this->_client.get_connection(uri, error); - - if (error.value() == -1) - { - // PLOG_ERROR << "Error connecting to relay " << relay << ": " << error.message(); - } +using namespace std; - // Configure the connection here via the connection pointer. - connection->set_fail_handler([this, uri](auto handle) { - // PLOG_ERROR << "Error connecting to relay " << relay << ": Handshake failed."; - lock_guard lock(this->_propertyMutex); - if (this->isConnected(uri)) - { - this->_connectionHandles.erase(uri); - } - }); +void nostr::client::WebsocketppClient::start() +{ + this->_client.init_asio(); + this->_client.start_perpetual(); +}; - lock_guard lock(this->_propertyMutex); - this->_connectionHandles[uri] = connection->get_handle(); - this->_client.connect(connection); - }; +void nostr::client::WebsocketppClient::stop() +{ + this->_client.stop_perpetual(); + this->_client.stop(); +}; - bool isConnected(string uri) override - { - lock_guard lock(this->_propertyMutex); - return this->_connectionHandles.find(uri) != this->_connectionHandles.end(); - }; +void nostr::client::WebsocketppClient::openConnection(string uri) +{ + error_code error; + websocketpp_client::connection_ptr connection = this->_client.get_connection(uri, error); - tuple send(string message, string uri) override + if (error.value() == -1) { - error_code error; + // PLOG_ERROR << "Error connecting to relay " << relay << ": " << error.message(); + } - // Make sure the connection isn't closed from under us. + // Configure the connection here via the connection pointer. + connection->set_fail_handler([this, uri](auto handle) { + // PLOG_ERROR << "Error connecting to relay " << relay << ": Handshake failed."; lock_guard lock(this->_propertyMutex); - this->_client.send( - this->_connectionHandles[uri], - message, - websocketpp::frame::opcode::text, - error); - - if (error.value() == -1) + if (this->isConnected(uri)) { - return make_tuple(uri, false); + this->_connectionHandles.erase(uri); } + }); - return make_tuple(uri, true); - }; + lock_guard lock(this->_propertyMutex); + this->_connectionHandles[uri] = connection->get_handle(); + this->_client.connect(connection); +}; - tuple send(string message, string uri, function messageHandler) override - { - auto successes = this->send(message, uri); - this->receive(uri, messageHandler); - return successes; - }; +bool nostr::client::WebsocketppClient::isConnected(string uri) +{ + lock_guard lock(this->_propertyMutex); + return this->_connectionHandles.find(uri) != this->_connectionHandles.end(); +}; - void receive(string uri, function messageHandler) override - { - lock_guard lock(this->_propertyMutex); - auto connectionHandle = this->_connectionHandles[uri]; - auto connection = this->_client.get_con_from_hdl(connectionHandle); +tuple nostr::client::WebsocketppClient::send(string message, string uri) +{ + error_code error; - connection->set_message_handler([messageHandler]( - websocketpp::connection_hdl connectionHandle, - websocketpp_client::message_ptr message) - { - messageHandler(message->get_payload()); - }); - }; + // Make sure the connection isn't closed from under us. + lock_guard lock(this->_propertyMutex); + this->_client.send( + this->_connectionHandles[uri], + message, + websocketpp::frame::opcode::text, + error); - void closeConnection(string uri) override + if (error.value() == -1) { - lock_guard lock(this->_propertyMutex); + return make_tuple(uri, false); + } - websocketpp::connection_hdl handle = this->_connectionHandles[uri]; - this->_client.close( - handle, - websocketpp::close::status::going_away, - "_client requested close."); - - this->_connectionHandles.erase(uri); - }; + return make_tuple(uri, true); +}; -private: - typedef websocketpp::client websocketpp_client; - typedef unordered_map::iterator connection_hdl_iterator; +tuple nostr::client::WebsocketppClient::send( + string message, + string uri, + function messageHandler) +{ + auto successes = this->send(message, uri); + this->receive(uri, messageHandler); + return successes; +}; - websocketpp_client _client; - unordered_map _connectionHandles; - mutex _propertyMutex; +void nostr::client::WebsocketppClient::receive( + string uri, + function messageHandler) +{ + lock_guard lock(this->_propertyMutex); + auto connectionHandle = this->_connectionHandles[uri]; + auto connection = this->_client.get_con_from_hdl(connectionHandle); - void onMessage(websocketpp::connection_hdl handle, websocketpp_client::message_ptr message) + connection->set_message_handler([messageHandler]( + websocketpp::connection_hdl connectionHandle, + websocketpp_client::message_ptr message) { - }; + messageHandler(message->get_payload()); + }); +}; + +void nostr::client::WebsocketppClient::closeConnection(string uri) +{ + lock_guard lock(this->_propertyMutex); + + websocketpp::connection_hdl handle = this->_connectionHandles[uri]; + this->_client.close( + handle, + websocketpp::close::status::going_away, + "_client requested close."); + + this->_connectionHandles.erase(uri); }; -} // namespace client -} // namespace nostr -- cgit