osrf / ovc

the Open Vision Computer
Apache License 2.0
198 stars 42 forks source link

Implement Dynamic Message Passing with jsoncpp #62

Closed gbalke closed 3 years ago

gbalke commented 3 years ago

Brought about by this suggestion: https://github.com/osrf/ovc/pull/61#issuecomment-898137828

I'm debating working on using JSON in #61 or another PR. Here's some sample code that I think I'll be basing the implementation off of:

#include <jsoncpp/json/json.h>
#include <iostream>
#include <sstream>

int main() {
  Json::Value root;

  root["name"] = "my_name";
  Json::StreamWriterBuilder builder;
  builder["indentation"] = "";
  std::string output = Json::writeString(builder, root);

  std::cout << output << std::endl;
  std::stringstream ss(output);

  Json::CharReaderBuilder rbuilder;
  Json::Value root_read;
  std::string errs;
  Json::parseFromStream(rbuilder, ss, &root_read, &errs);

  std::cout << errs << std::endl;
  std::cout << root_read["name"] << std::endl;
}

Output:

$ ./a.out
{"name":"my_name"}

"my_name"

Still not sure if I should change the serialization (struct vs an actual library). I could just replace the whole host -> client message with a length field followed by the JSON string. Something about it doesn't feel right but it would work... :shrug:

@luca-della-vedova thougths?

luca-della-vedova commented 3 years ago

Length + JSON string sounds simple enough that it would work, the alternative would be sending JSON straight away I guess? But not sure if the library has convenient functions for "read from this socket and return when a whole JSON message has been received", if it doesn't it might be too much work.