Modern C++ Request-Reply code example

 

The following is a simple example that shows how to use a Requester and a Replier in the RTI Connext Modern C++ Request-Reply API. 

The types used in this example have the following IDL definition:

  
            module directory_service {
                    
                    struct DirectoryRequest {
                        string path;
                    };
                
                    enum Result {
                        OK, PERMISSION_ERROR, DOESNT_EXIST
                    };
                
                    struct DirectoryReply {
                        Result result;
                        sequence<string> files;
                    };
                
            };

 

That can be generated with 

 

rtiddsgen -language C++11 -stl -unboundedSupport -example <your architecture> DirectoryService.idl

(You'll need to modify the makefile or Visual Studio project to link with one additional library: rticonnextmsgcpp2)

The following code shows how to create a Requester, send a request, and receive a reply:

  
void requester_main(int domain_id)
{
    using namespace directory_service;
    
    dds::domain::DomainParticipant participant(domain_id);
    
    rti::request::Requester<DirectoryRequest, DirectoryReply> requester(
        participant,
        "DirectoryService");
        
    rti::util::sleep(dds::core::Duration::from_secs(2));
      
    DirectoryRequest request("/usr/bin/");
    requester.send_request(request);
    
    auto replies = requester.receive_replies(dds::core::Duration::from_secs(10));
    for (const auto& reply_sample : replies) {
        if (reply_sample.info().valid()) {
            if (reply_sample.data().result() == Result::OK) {
                std::cout << "Received reply: " << reply_sample.data() 
                          << std::endl;
            } else {
                std::cout << "Request failed: " << reply_sample.data().result()
                          << std::endl;
            }
        }
    }
}                  

The following code shows how to create a Replier that will receive a request from the previous Requester and send a reply to it:

                void replier_main(int domain_id)
                {
                    using namespace directory_service;
                    
                    // Create a DomainParticipant with default Qos
                    dds::domain::DomainParticipant participant(domain_id);
                
                    rti::request::Replier<DirectoryRequest, DirectoryReply> replier(
                        participant,
                        "DirectoryService");
                        
                    bool keep_running = true;
                    while (keep_running) {
                        std::cout << "Waiting for requests..." << std::endl;
                        auto requests = replier.receive_requests(dds::core::Duration::from_secs(10));
                        
                        for (const auto& request_sample : requests) {
                            if (request_sample.info().valid()) {
                                std::cout << "Received request for directory '" 
                                          << request_sample.data().path() << "'" << std::endl;
                                          
                                DirectoryReply reply;
                                reply.files() = get_files_in_directory(request_sample.data().path());
                                reply.result(Result::OK);
                                
                                replier.send_reply(
                                    reply, 
                                    request_sample.info()->publication_virtual_sample_identity());
                            }
                        }
                    }
                }

                std::vector<std::string> get_files_in_directory(const std::string& path)
                {
                     return std::vector<std::string>({path + "foo.txt", path + "bar.jpg"});
                }

 

Comments

which version of the DDS works with this example?

whats the header files required?