Skip to content

Commit

Permalink
Renamed websocket 'close' to 'stopped_connecting' for better ilustrat…
Browse files Browse the repository at this point in the history
…ion of what the function does

Signed-off-by: AssemblyJohn <[email protected]>
  • Loading branch information
AssemblyJohn committed Dec 9, 2024
1 parent 761c051 commit a0f4d44
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
4 changes: 2 additions & 2 deletions include/ocpp/common/websocket/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Websocket {
std::unique_ptr<WebsocketBase> websocket;
std::function<void(OcppProtocolVersion protocol)> connected_callback;
std::function<void()> disconnected_callback;
std::function<void(const WebsocketCloseReason reason)> closed_callback;
std::function<void(const WebsocketCloseReason reason)> stopped_connecting_callback;
std::function<void(const std::string& message)> message_callback;
std::shared_ptr<MessageLogging> logging;

Expand Down Expand Up @@ -52,7 +52,7 @@ class Websocket {

/// \brief register a \p callback that is called when the websocket connection has been closed and will not attempt
/// to reconnect
void register_closed_callback(const std::function<void(const WebsocketCloseReason)>& callback);
void register_stopped_connecting_callback(const std::function<void(const WebsocketCloseReason)>& callback);

/// \brief register a \p callback that is called when the websocket receives a message
void register_message_callback(const std::function<void(const std::string& message)>& callback);
Expand Down
4 changes: 2 additions & 2 deletions include/ocpp/common/websocket/websocket_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class WebsocketBase {
WebsocketConnectionOptions connection_options;
std::function<void(OcppProtocolVersion protocol)> connected_callback;
std::function<void()> disconnected_callback;
std::function<void(const WebsocketCloseReason reason)> closed_callback;
std::function<void(const WebsocketCloseReason reason)> stopped_connecting_callback;
std::function<void(const std::string& message)> message_callback;
std::function<void(ConnectionFailedReason)> connection_failed_callback;
std::shared_ptr<boost::asio::steady_timer> reconnect_timer;
Expand Down Expand Up @@ -119,7 +119,7 @@ class WebsocketBase {

/// \brief register a \p callback that is called when the websocket connection has been closed and will not attempt
/// to reconnect
void register_closed_callback(const std::function<void(const WebsocketCloseReason reason)>& callback);
void register_stopped_connecting_callback(const std::function<void(const WebsocketCloseReason reason)>& callback);

/// \brief register a \p callback that is called when the websocket receives a message
void register_message_callback(const std::function<void(const std::string& message)>& callback);
Expand Down
5 changes: 3 additions & 2 deletions include/ocpp/v201/connectivity_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ class ConnectivityManager {
///
void on_websocket_disconnected();

/// \brief Function invoked when the web socket closes
/// \brief Function invoked when the web socket stops connecting
/// and does not re-attempt to connect again
///
void on_websocket_closed(ocpp::WebsocketCloseReason reason);
void on_websocket_stopped_connecting(ocpp::WebsocketCloseReason reason);

///
/// \brief Get the active network configuration slot in use.
Expand Down
9 changes: 5 additions & 4 deletions lib/ocpp/common/websocket/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ void Websocket::register_disconnected_callback(const std::function<void()>& call
});
}

void Websocket::register_closed_callback(const std::function<void(const WebsocketCloseReason reason)>& callback) {
this->closed_callback = callback;
this->websocket->register_closed_callback(
[this](const WebsocketCloseReason reason) { this->closed_callback(reason); });
void Websocket::register_stopped_connecting_callback(
const std::function<void(const WebsocketCloseReason reason)>& callback) {
this->stopped_connecting_callback = callback;
this->websocket->register_stopped_connecting_callback(
[this](const WebsocketCloseReason reason) { this->stopped_connecting_callback(reason); });
}

void Websocket::register_message_callback(const std::function<void(const std::string& message)>& callback) {
Expand Down
9 changes: 5 additions & 4 deletions lib/ocpp/common/websocket/websocket_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ocpp {
WebsocketBase::WebsocketBase() :
m_is_connected(false),
connected_callback(nullptr),
closed_callback(nullptr),
stopped_connecting_callback(nullptr),
message_callback(nullptr),
reconnect_timer(nullptr),
connection_attempts(1),
Expand Down Expand Up @@ -44,8 +44,9 @@ void WebsocketBase::register_disconnected_callback(const std::function<void()>&
this->disconnected_callback = callback;
}

void WebsocketBase::register_closed_callback(const std::function<void(const WebsocketCloseReason reason)>& callback) {
this->closed_callback = callback;
void WebsocketBase::register_stopped_connecting_callback(
const std::function<void(const WebsocketCloseReason reason)>& callback) {
this->stopped_connecting_callback = callback;
}

void WebsocketBase::register_message_callback(const std::function<void(const std::string& message)>& callback) {
Expand All @@ -61,7 +62,7 @@ bool WebsocketBase::initialized() {
EVLOG_error << "Not properly initialized: please register connected callback.";
return false;
}
if (this->closed_callback == nullptr) {
if (this->stopped_connecting_callback == nullptr) {
EVLOG_error << "Not properly initialized: please closed_callback.";
return false;
}
Expand Down
35 changes: 18 additions & 17 deletions lib/ocpp/common/websocket/websocket_libwebsockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static constexpr int MESSAGE_SEND_TIMEOUT = 20;
/// \brief Current connection data, sets the internal state of the
struct ConnectionData {
explicit ConnectionData(WebsocketLibwebsockets* owner) :
wsi(nullptr), owner(owner), is_running(true), is_close_run(false), state(EConnectionState::INITIALIZE) {
wsi(nullptr), owner(owner), is_running(true), is_stopped_run(false), state(EConnectionState::INITIALIZE) {
}

~ConnectionData() {
Expand Down Expand Up @@ -157,14 +157,14 @@ struct ConnectionData {
return (is_running == false);
}

void mark_close_executed() {
void mark_stop_executed() {
std::lock_guard lock(this->mutex);
this->is_close_run = true;
this->is_stopped_run = true;
}

bool is_close_executed() {
bool is_stop_executed() {
std::lock_guard lock(this->mutex);
return this->is_close_run;
return this->is_stopped_run;
}

public:
Expand Down Expand Up @@ -199,7 +199,7 @@ struct ConnectionData {
}

// Reset the close status
is_close_run = false;
is_stopped_run = false;

// Causes a deadlock in callback_minimal if not reset
this->lws_ctx = std::unique_ptr<lws_context>(lws_ctx);
Expand Down Expand Up @@ -243,7 +243,7 @@ struct ConnectionData {
std::mutex mutex;

bool is_running;
bool is_close_run;
bool is_stopped_run;
EConnectionState state;

private:
Expand Down Expand Up @@ -365,7 +365,8 @@ WebsocketLibwebsockets::~WebsocketLibwebsockets() {
this->close_internal(WebsocketCloseReason::Normal, "websocket destructor");
}

// In the dtor we must make sure the deferred callback thread finishes
// In the dtor we must make sure the deferred callback thread
// finishes since the callbacks capture a reference to 'this'
if (this->deferred_callback_thread != nullptr && this->deferred_callback_thread->joinable()) {
this->stop_deferred_handler.store(true);
this->deferred_callback_queue.notify_waiting_thread();
Expand Down Expand Up @@ -850,12 +851,12 @@ void WebsocketLibwebsockets::thread_websocket_client_loop(std::shared_ptr<Connec
} while (try_reconnect); // End trying to connect

// Give back control to the application
if (false == local_data->is_close_executed()) {
if (false == local_data->is_stop_executed()) {
this->push_deferred_callback([this]() {
if (this->closed_callback) {
this->closed_callback(WebsocketCloseReason::Normal);
if (this->stopped_connecting_callback) {
this->stopped_connecting_callback(WebsocketCloseReason::Normal);
} else {
EVLOG_error << "Closed callback not registered!";
EVLOG_error << "Stopped connecting callback not registered!";
}

if (this->disconnected_callback) {
Expand Down Expand Up @@ -1448,10 +1449,10 @@ void WebsocketLibwebsockets::on_conn_close(ConnectionData* conn_data) {
message_queue.notify_waiting_thread();

this->push_deferred_callback([this]() {
if (this->closed_callback) {
this->closed_callback(WebsocketCloseReason::Normal);
if (this->stopped_connecting_callback) {
this->stopped_connecting_callback(WebsocketCloseReason::Normal);
} else {
EVLOG_error << "Closed callback not registered!";
EVLOG_error << "Stopped connecting callback not registered!";
}

if (this->disconnected_callback) {
Expand All @@ -1461,8 +1462,8 @@ void WebsocketLibwebsockets::on_conn_close(ConnectionData* conn_data) {
}
});

// We have polled the close
conn_data->mark_close_executed();
// We have polled the stopped connected
conn_data->mark_stop_executed();
}

void WebsocketLibwebsockets::on_conn_fail(ConnectionData* conn_data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void ChargePointImpl::init_websocket() {
this->signal_set_charging_profiles_callback();
}
});
this->websocket->register_closed_callback([this](const WebsocketCloseReason reason) {
this->websocket->register_stopped_connecting_callback([this](const WebsocketCloseReason reason) {
if (this->switch_security_profile_callback != nullptr) {
this->switch_security_profile_callback();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ocpp/v201/connectivity_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ void ConnectivityManager::try_connect_websocket() {
[this](OcppProtocolVersion protocol) { this->on_websocket_connected(protocol); });
this->websocket->register_disconnected_callback(
std::bind(&ConnectivityManager::on_websocket_disconnected, this));
this->websocket->register_closed_callback(
std::bind(&ConnectivityManager::on_websocket_closed, this, std::placeholders::_1));
this->websocket->register_stopped_connecting_callback(
std::bind(&ConnectivityManager::on_websocket_stopped_connecting, this, std::placeholders::_1));
} else {
this->websocket->set_connection_options(connection_options.value());
}
Expand Down Expand Up @@ -397,7 +397,7 @@ void ConnectivityManager::on_websocket_disconnected() {
}
}

void ConnectivityManager::on_websocket_closed(ocpp::WebsocketCloseReason reason) {
void ConnectivityManager::on_websocket_stopped_connecting(ocpp::WebsocketCloseReason reason) {
EVLOG_warning << "Closed websocket of NetworkConfigurationPriority: "
<< this->active_network_configuration_priority + 1 << " which is configurationSlot "
<< this->get_active_network_configuration_slot();
Expand Down

0 comments on commit a0f4d44

Please sign in to comment.