Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C++

RCF - Interprocess Communication for C++

Rate me:
Please Sign up or sign in to vote.
4.94/5 (147 votes)
25 Oct 2011CPOL20 min read 4.6M   8.4K   331  
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
// uncomment to enable VLD leak detection - will automatically link to required libs
//#include "vld.h"
//#include "vldapi.h"

#include <string>

#include <boost/test/minimal.hpp>

#include <RCF/Idl.hpp>
#include <RCF/InitDeinit.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/test/EndpointFactories.hpp>
#include <RCF/test/TransportFactories.hpp>
#include <RCF/util/CommandLine.hpp>

class Echo
{
public:
    std::string echo(const std::string &s) 
    {
        sLog = s;
        return s; 
    }
    static std::string sLog;
};

std::string Echo::sLog;

RCF_BEGIN(I_Echo, "I_Echo")
    RCF_METHOD_R1(std::string, echo, const std::string &)
RCF_END(I_Echo)

int test_main(int argc, char **argv)
{
    util::CommandLine::getSingleton().parse(argc, argv);

    for (unsigned int i=0; i<RCF::getTransportFactories().size(); ++i)
    {

        RCF::TransportFactoryPtr transportFactoryPtr = RCF::getTransportFactories()[i];
        std::pair<RCF::ServerTransportPtr, RCF::ClientTransportAutoPtrPtr> transports = transportFactoryPtr->createTransports();
        RCF::ServerTransportPtr serverTransportPtr( transports.first );
        RCF::ClientTransportAutoPtr clientTransportAutoPtr( *transports.second );
    
        RCF::writeTransportTypes(std::cout, *serverTransportPtr, *clientTransportAutoPtr);

        serverTransportPtr->setMaxMessageLength(1000*10);
        clientTransportAutoPtr->setMaxMessageLength(1000*10);

        std::string s0 = "something special";

        Echo echo;
        RCF::RcfServer server( boost::dynamic_pointer_cast<RCF::I_Service>(serverTransportPtr) );
        server.start();
        server.bind<I_Echo>(echo);

        // all calls on the same connection
        RcfClient<I_Echo> client( clientTransportAutoPtr->clone() );

        for (int j=0; j<100; ++j)
        {
            std::string s = client.echo(s0); 
            BOOST_CHECK(s0 == s);
        }

        // new connection for each call
        for (int j=0; j<100; ++j)
        {
            std::string s = RcfClient<I_Echo>( clientTransportAutoPtr->clone() ).echo(s0); 
            BOOST_CHECK(s0 == s);
        }

    }
  
    for (unsigned int i=0; i<RCF::getEndpointPairFactories().size(); ++i)
    {

        std::string s0 = "something special";

        RCF::EndpointPair endpointPair = RCF::getEndpointPairFactories()[i]->createEndpointPair();
        const RCF::I_Endpoint &serverEndpoint = *endpointPair.first;
        const RCF::I_Endpoint &clientEndpoint = *endpointPair.second;

        RCF::writeEndpointTypes(std::cout, serverEndpoint, clientEndpoint);

        Echo echo;
        RCF::RcfServer server(serverEndpoint);
        server.bind<I_Echo>(echo);
        server.start();

        // all calls on the same connection
        RcfClient<I_Echo> client( clientEndpoint );

        for (int j=0; j<100; ++j)
        {
            RCF_TRACE("")(j);
            std::string s = client.echo(s0); 
            BOOST_CHECK(s0 == s);
        }

        // new connection for each call
        for (int j=0; j<100; ++j)
        {
            std::string s = RcfClient<I_Echo>( clientEndpoint ).echo(s0); 
            BOOST_CHECK(s0 == s);
        }

    }

    return boost::exit_success;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
Software developer, from Sweden and now living in Canberra, Australia, working on distributed C++ applications. When he is not programming, Jarl enjoys skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

Comments and Discussions