Skip to content

Commit

Permalink
C++: add missing implementations for client advertised schema/encoding (
Browse files Browse the repository at this point in the history
#876)

### Changelog

C++ server/client: add missing implementations for client advertised
schema / schema encoding

### Docs

None

### Description

Although foxglove ws protocol allow client to advertise complete schema
definition string as well as schema name, current C++ implementation
lacks such fields. Particularly:
* `ClientAdvertisement` struct lacks `schemaEncoding` field,
* `ClientAdvertisement` struct has `schema` field, but it is never
populated (websocket server does not read schema or schema encoding to
json)
* websocket client is does not serialize schema or schema encoding to
json

This PR resolves above problems, and additionally since this is breaking
change from unimplemented to implemented, types are decided as
following:
* `schema` field type is changed from `std::vector<uint8_t>` to
`std::optional<std::string>`, as schema fields are passed in as json
quoted string, and it's optional
* `schemaEncoding` field type is determined as
`std::optional<std::string>`, the same reason for `schema`.


Fixes: #875
Fixes: FG-9447

---------

Co-authored-by: Hans-Joachim Krauch <achim-k@users.noreply.github.com>
  • Loading branch information
paulsohn and achim-k authored Dec 10, 2024
1 parent 005898f commit ce56725
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cpp/foxglove-websocket/include/foxglove/websocket/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ struct ClientAdvertisement {
std::string topic;
std::string encoding;
std::string schemaName;
std::vector<uint8_t> schema;
std::optional<std::string> schema;
std::optional<std::string> schemaEncoding;
};

struct ClientMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ inline void to_json(nlohmann::json& j, const ClientAdvertisement& p) {
{"topic", p.topic},
{"encoding", p.encoding},
{"schemaName", p.schemaName}};
if (p.schema) {
j["schema"] = p.schema.value();
}
if (p.schemaEncoding) {
j["schemaEncoding"] = p.schemaEncoding.value();
}
}

using TextMessageHandler = std::function<void(const std::string&)>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +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>();

if (const auto it = chan.find("schema"); it != chan.end()) {
advertisement.schema = it->get<std::string>();
}
if (const auto it = chan.find("schemaEncoding"); it != chan.end()) {
advertisement.schemaEncoding = it->get<std::string>();
}
clientPublications.emplace(channelId, advertisement);
{
std::unique_lock<std::shared_mutex> clientsLock(_clientsMutex);
Expand Down

0 comments on commit ce56725

Please sign in to comment.