Click here to Skip to main content
15,895,538 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.
#include <iostream>
#include <sstream>
#include <string>

#include <boost/test/minimal.hpp>
#include <boost/thread/xtime.hpp>

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

#include <SF/memory.hpp>

// Need to disable broken pipe signals for OS's like Solaris
#ifdef SIGPIPE
#include <signal.h>
#include <RCF/util/InitDeinit.hpp>
UTIL_ON_INIT( sigignore(SIGPIPE) )
#endif

RCF_BEGIN(I_X, "I_X")
    RCF_METHOD_R1(std::string, echo, std::string);
RCF_END(I_X);

class X
{
public:
    std::string echo(const std::string &s)
    {
        // wait 2s and then echo the string
        boost::xtime xt;
        boost::xtime_get(&xt, boost::TIME_UTC);
        xt.sec += 2;
        boost::thread::sleep(xt);

        return s;
    }
};

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

    for (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);

        X x;
        RCF::RcfServer server(serverTransportPtr);
        server.bind<I_X>(x);
        server.start();

        RcfClient<I_X> client(clientTransportAutoPtr);

        // this call will timeout
        client.getClientStub().setRemoteCallTimeoutMs(1000);
        try 
        {
            std::string s = client.echo("abc");
            BOOST_CHECK(1==0);
        }
        catch (const RCF::Exception &e)
        {
            BOOST_CHECK(1==1);
            std::cout << e.what();
        }
        
        // this one won't
        client.getClientStub().setRemoteCallTimeoutMs(15000);
        std::string s = client.echo("def");
        // whether or not this check succeeds depends on the client transport implementation
        //BOOST_CHECK(s == "def");
    }
    
    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