UnixStreamSocket::UnixStreamSocket(int fd) :
		::UnixStreamSocket::UnixStreamSocket(fd, NS_MAX_SIZE)
	{
		MS_TRACE_STD();

		// Create the JSON reader.
		{
			Json::CharReaderBuilder builder;
			Json::Value settings = Json::nullValue;
			Json::Value invalid_settings;

			builder.strictMode(&settings);

			MS_ASSERT(builder.validate(&invalid_settings), "invalid Json::CharReaderBuilder");

			this->jsonReader = builder.newCharReader();
		}

		// Create the JSON writer.
		{
			Json::StreamWriterBuilder builder;
			Json::Value invalid_settings;

			builder["commentStyle"] = "None";
			builder["indentation"] = "";
			builder["enableYAMLCompatibility"] = false;
			builder["dropNullPlaceholders"] = false;

			MS_ASSERT(builder.validate(&invalid_settings), "invalid Json::StreamWriterBuilder");

			this->jsonWriter = builder.newStreamWriter();
		}
	}
Example #2
0
 std::string JsontoString(Json::Value v) {
   std::stringstream sstream;
   Json::StreamWriterBuilder builder;
   std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
   writer->write(v, &sstream);
   std::string result = sstream.str();
   return result;
 }
Example #3
0
int			main() {
  char			buffer[1024] = {0};
  std::streamsize	ssize;
  std::ostringstream	osstr;

  while (std::cin.good()) {
    osstr << buffer;
    std::cin.getline(buffer, sizeof(buffer) - 1);
    ssize = std::cin.gcount();
    if (ssize < (sizeof(buffer) - 1)) {
      buffer[ssize - 1] = '\n';
      buffer[ssize] = 0;
    }
  }

  boost::asio::io_service	io;
  tcp::resolver			resolver(io);
  tcp::resolver::query		query("0.0.0.0", "4242");
  tcp::resolver::iterator	endpoint_it = resolver.resolve(query);
  tcp::socket			socket(io);

  boost::asio::connect(socket, endpoint_it);

  std::string					str = osstr.str();
  unsigned int					size = str.size();

  std::cout << "Will write " << size;
  size = swapBytes(size);
  std::cout << ": raw(" << size << ")" << std::endl;
  std::vector<boost::asio::const_buffer>	buffers = {
    boost::asio::buffer(&size, sizeof(size)),
    boost::asio::buffer(str)
  };
  boost::asio::write(socket, buffers);

  //========
  
  boost::asio::read(socket, boost::asio::buffer(&size, sizeof(size)));
  std::cout << "Will read " << size;
  size = swapBytes(size);
  std::cout << ": raw(" << size << ")" << std::endl;
  char				*readBuf = new char[size];
  boost::asio::read(socket, boost::asio::buffer(readBuf, size));

  Json::Reader			reader;
  Json::Value			response;

  reader.parse(readBuf, readBuf + size - 1, response);

  Json::StreamWriterBuilder	builder;
  std::shared_ptr<Json::StreamWriter>	writer(builder.newStreamWriter());

  writer->write(response, &std::cout);
  std::cout << std::endl;
  return (0);
}
Example #4
0
  Json::Value Connection::exec_call(const std::string& methodname, Json::Value params) {
    Json::Value call;
    call["method"]=methodname;
    call["params"]=params;
    call["id"]=0;
    std::stringstream json_call;
    Json::StreamWriterBuilder builder;
    std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
    writer->write(call, &json_call);
    std::string json_call_str = json_call.str();

    boost::asio::streambuf request;
    std::ostream request_stream(&request);
    request_stream << "POST /" << link << " HTTP/1.1\r\n";
    request_stream << "Host: " << hostname << "\r\n";
    request_stream << "Content-Type: application/json-rpc\r\n";
    request_stream << "Content-Length: " << json_call_str.size() << "\r\n";
    request_stream << "Connection: keep-alive\r\n\r\n";
    request_stream << json_call_str << "\r\n\r\n";

    // Send the request.
    boost::system::error_code ec;
    boost::asio::write(socket, request, ec);
    if(ec) {
      std::cout << ec.message() << std::endl;
      throw ConnectionError(ec.message());
    }

    boost::asio::streambuf response;
    boost::asio::read_until(socket, response, "\r\n");
    // Check that response is OK.
    std::istream response_stream(&response);
    std::string http_version;
    response_stream >> http_version;
    unsigned int status_code;
    response_stream >> status_code;
    std::string status_message;
    std::getline(response_stream, status_message);
    if (!response_stream || http_version.substr(0, 5) != "HTTP/")
      {
        std::cout << "Invalid response\n";
        throw ConnectionError("Invalid response");
      }
    if (status_code != 200)
      {
        std::cout << "Response returned with status code " << status_code << "\n";
        std::stringstream error;
        error << "Response returned with status code " << status_code;
        throw ConnectionError(error.str());
      }

    // Read the response headers, which are terminated by a blank line.
    boost::asio::read_until(socket, response, "\r\n\r\n");
    // Process the response headers.
    std::string header;
    std::stringstream header_stream;
    while (std::getline(response_stream, header) && header != "\r")
      header_stream << header << "\n";
    header_stream << "\n";

    std::stringstream json_result;
    // Write whatever content we already have to output.
    if (response.size() > 0)
      json_result << &response;

    Json::CharReaderBuilder reader_builder;
    reader_builder["collectComments"] = false;
    Json::Value value;
    std::string errs;
    bool ok = Json::parseFromStream(reader_builder, json_result, &value, &errs);
    if(!ok) {
      throw ConnectionError(errs);
    }

    return process_result(value);
  }
Example #5
0
void ResultIO::writeResultContainerToJSON(ResultContainer result, const std::string & filename)
{
	auto root = Json::Value();

	root["fasta"] = result.fasta;
	root["id"] = result.id;

	root["fastaLabels"] = Json::Value(Json::arrayValue);
	for (auto & lbl : result.fastaLabels)
	{
		root["fastaLabels"].append(lbl);
	}

	root["oneshot"] = clusterAnalysisResultToJSON(result, result.oneshot, false);

	root["bootstraps"] = Json::Value(Json::arrayValue);
	for (auto & bs : result.bootstraps)
	{
		root["bootstraps"].append(clusterAnalysisResultToJSON(result, bs, true));
	}

    if (krakenEnabled)
    {
    	root["krakenLabels"] = Json::Value(Json::arrayValue);
        for (auto & lbl : result.fastaLabels)
        {
            std::string krakenLbl = "unknown";
            if (result.kraken.classification.find(lbl) != result.kraken.classification.end())
            {
                krakenLbl = result.kraken.classification.at(lbl);
            }
            root["krakenLabels"].append(krakenLbl);
        }
        root["krakenBacterialBackground"] = result.kraken.bacterialBackground;
    }

    root["contains16S"] = Json::Value(Json::arrayValue);
    for (const std::string & s : result._16S)
    {
        root["contains16S"].append(!s.empty());
    }

    root["stats"] = Json::Value();
    root["stats"]["numBasepairs"] = (unsigned)result.stats.numBasepairs;
    root["stats"]["gcContent"] = result.stats.gcContent;
    root["stats"]["contigGcContent"] = Json::Value();
    for (const auto & entry : result.stats.contigGcContent)
    {
        root["stats"]["contigGcContent"][entry.first] = entry.second;
    }
    root["stats"]["contigLength"] = Json::Value();
    for (const auto & entry : result.stats.contigLength)
    {
        root["stats"]["contigLength"][entry.first] = (unsigned)entry.second;
    }

	Json::StreamWriterBuilder builder;
	std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
	std::ofstream ofs(filename, std::ofstream::out | std::ofstream::app);
	ofs << "results['" << result.fasta << "'] = ";
	writer->write(root, &ofs);
	ofs << ";" << std::endl;
	ofs.close();
}