Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call action handlers after action, in c++ server #850

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1267,9 +1267,11 @@ void Server<ServerConfiguration>::handleSubscribe(const nlohmann::json& payload,
continue;
}

{
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).subscriptionsByChannel.emplace(channelId, subId);
}
_handlers.subscribeHandler(channelId, hdl);
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).subscriptionsByChannel.emplace(channelId, subId);
}
}

Expand Down Expand Up @@ -1301,9 +1303,11 @@ void Server<ServerConfiguration>::handleUnsubscribe(const nlohmann::json& payloa
}

ChannelId chanId = sub->first;
{
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).subscriptionsByChannel.erase(chanId);
}
_handlers.unsubscribeHandler(chanId, hdl);
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).subscriptionsByChannel.erase(chanId);
}
}

Expand Down Expand Up @@ -1336,10 +1340,12 @@ void Server<ServerConfiguration>::handleAdvertise(const nlohmann::json& payload,
advertisement.encoding = chan.at("encoding").get<std::string>();
advertisement.schemaName = chan.at("schemaName").get<std::string>();

{
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).advertisedChannels.emplace(channelId);
clientPublications.emplace(channelId, advertisement);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this one needs to be under the lock, it's a local variable

}
jtbandes marked this conversation as resolved.
Show resolved Hide resolved
_handlers.clientAdvertiseHandler(advertisement, hdl);
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).advertisedChannels.emplace(channelId);
clientPublications.emplace(channelId, advertisement);
}
}

Expand All @@ -1361,14 +1367,16 @@ void Server<ServerConfiguration>::handleUnadvertise(const nlohmann::json& payloa
continue;
}

_handlers.clientUnadvertiseHandler(channelId, hdl);
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
auto& clientInfo = _clients.at(hdl);
clientPublications.erase(channelIt);
const auto advertisedChannelIt = clientInfo.advertisedChannels.find(channelId);
if (advertisedChannelIt != clientInfo.advertisedChannels.end()) {
clientInfo.advertisedChannels.erase(advertisedChannelIt);
{
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
auto& clientInfo = _clients.at(hdl);
clientPublications.erase(channelIt);
const auto advertisedChannelIt = clientInfo.advertisedChannels.find(channelId);
if (advertisedChannelIt != clientInfo.advertisedChannels.end()) {
clientInfo.advertisedChannels.erase(advertisedChannelIt);
}
}
_handlers.clientUnadvertiseHandler(channelId, hdl);
}
}

Expand Down Expand Up @@ -1443,9 +1451,11 @@ void Server<ServerConfiguration>::handleSubscribeConnectionGraph(ConnHandle hdl)
if (subscribeToConnnectionGraph) {
// First subscriber, let the handler know that we are interested in updates.
_server.get_alog().write(APP, "Subscribing to connection graph updates.");
{
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).subscribedToConnectionGraph = true;
}
_handlers.subscribeConnectionGraphHandler(true);
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
_clients.at(hdl).subscribedToConnectionGraph = true;
}

json::array_t publishedTopicsJson, subscribedTopicsJson, advertisedServicesJson;
Expand Down