Пример #1
0
void Agent::processRequest(const PCPClient::ParsedChunks& parsed_chunks) {
    auto request_id = parsed_chunks.envelope.get<std::string>("id");
    auto requester_endpoint = parsed_chunks.envelope.get<std::string>("sender");

    std::cout << "Received message " << request_id
              << " from " << requester_endpoint << ":\n"
              << parsed_chunks.toString() << "\n";

    // Prepare data chunk
    // The request message has been validated as conformed to the
    // 'request message schema'; we know that it contains a JSON
    // 'request' entry (mandatory) and possibly a string 'details'.
    std::string response_txt;
    auto& d = parsed_chunks.data;
    auto request = d.get<leatherman::json_container::JsonContainer>("request");

    if (request.includes("artist")) {
        auto artist = request.get<std::string>("artist");
        if (artist == "Captain Beefheart") {
            response_txt = "as requested: http://goo.gl/7RWu4G";
        } else {
            response_txt = "unknown artist - get some of: http://goo.gl/0DWhp6";
        }
    }

    if (parsed_chunks.data.includes("details")) {
        auto details_txt = parsed_chunks.data.get<std::string>("details");
        response_txt += std::string { " (details: \"" + details_txt + "\")" };
    }

    leatherman::json_container::JsonContainer response_data {};
    response_data.set<std::string>("response", response_txt);

    // Prepare debug chunk
    std::vector<leatherman::json_container::JsonContainer> response_debug {};
    for (auto& debug_txt : parsed_chunks.debug) {
        leatherman::json_container::JsonContainer debug_entry {};
        response_debug.push_back(debug_entry);
    }

    // Send the message back to the requester
    std::vector<std::string> endpoints { requester_endpoint };

    // Connector::send()

    try {
        connector_ptr_->send(endpoints,
                             RESPONSE_SCHEMA_NAME,
                             MSG_TIMEOUT_S,
                             response_data,
                             response_debug);
        std::cout << "Response message sent to " << requester_endpoint << "\n";
    } catch (PCPClient::connection_processing_error& e) {
        std::cout << "Failed to send the response message: "
                  << e.what() << "\n";

        // NB: we don't want to throw anything here; we must not stop
        // the event handler thread for sending error
    }
}
Пример #2
0
void Agent::processRequest(const PCPClient::ParsedChunks& parsed_chunks) {
    auto request_id = parsed_chunks.envelope.get<std::string>("id");
    auto requester_endpoint = parsed_chunks.envelope.get<std::string>("sender");

    std::cout << "Received message " << request_id
              << " from " << requester_endpoint << ":\n"
              << parsed_chunks.toString() << "\n";
}
Пример #3
0
void Agent::processRequest(const PCPClient::ParsedChunks& parsed_chunks) {
    auto request_id = parsed_chunks.envelope.get<std::string>("id");
    auto requester_endpoint = parsed_chunks.envelope.get<std::string>("sender");
    auto& d = parsed_chunks.data;
    auto request = d.get<leatherman::json_container::JsonContainer>("request");

    std::cout << "Received message " << request_id
              << " from " << requester_endpoint << ":\n"
              << parsed_chunks.toString() << "\n";

    // Validator::validate()

    try {
        validator_.validate(request, SONG_SCHEMA);
    } catch (PCPClient::validation_error& e) {
        std::string err { "Failed to validate request: " };
        err += e.what();
        std::cout << err << "\n";
        leatherman::json_container::JsonContainer err_data {};
        err_data.set<std::string>("description", err);
        err_data.set<std::string>("id", request_id);

        try {
            connector_ptr_->send(std::vector<std::string> { requester_endpoint },
                                 PCPClient::Protocol::ERROR_MSG_TYPE,
                                 MSG_TIMEOUT_S,
                                 err_data);
            std::cout << "Error message sent to " << requester_endpoint << "\n";
        } catch (PCPClient::connection_processing_error& e) {
            std::cout << "Failed to send the error message: "
                      << e.what() << "\n";
            // NB: as below, we don't want to throw anything here; we
            // must not stop the event handler thread
        }

        return;
    }

    // Prepare data chunk

    std::string response_txt;

    if (request.includes("artist")) {
        auto artist = request.get<std::string>("artist");
        if (artist == "Captain Beefheart") {
            response_txt = "as requested: http://goo.gl/7RWu4G";
        } else {
            response_txt = "unknown artist - get some of: http://goo.gl/0DWhp6";
        }
    }

    if (parsed_chunks.data.includes("details")) {
        auto details_txt = parsed_chunks.data.get<std::string>("details");
        response_txt += std::string { " (details: \"" + details_txt + "\")" };
    }

    leatherman::json_container::JsonContainer response_data {};
    response_data.set<std::string>("response", response_txt);

    // Prepare debug chunk
    std::vector<leatherman::json_container::JsonContainer> response_debug {};
    for (auto& debug_txt : parsed_chunks.debug) {
        leatherman::json_container::JsonContainer debug_entry {};
        response_debug.push_back(debug_entry);
    }

    // Connector::send()

    try {
        connector_ptr_->send(std::vector<std::string> { requester_endpoint },
                             RESPONSE_SCHEMA_NAME,
                             MSG_TIMEOUT_S,
                             response_data,
                             response_debug);
        std::cout << "Response message sent to " << requester_endpoint << "\n";
    } catch (PCPClient::connection_processing_error& e) {
        std::cout << "Failed to send the response message: "
                  << e.what() << "\n";

        // NB: as above, we don't want to throw anything here
    }
}