Click here to Skip to main content
15,886,857 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 <string>

#include <RCF/test/TestMinimal.hpp>

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

namespace Test_MultipleClient {

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

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

    void clientThread(const RCF::I_ClientTransport &clientTransport)
    {
        try
        {
            RcfClient<I_Echo> echo(clientTransport.clone());
            std::string s0 = "something special";
            for (int i=0;i<10; i++)
            {
                try
                {
                    std::string s = echo.echo(s0);
                    BOOST_CHECK(s0 == s);
                }
                catch(const RCF::Exception &e)
                {
                    RCF_TRACE("")(e); // since we're overloading the server there will probably be some exceptions
                }
            }
        }
        catch(...)
        {
            BOOST_CHECK(1==0);
        }
    }

} // namespace Test_MultipleClient

int RCF_TEST_MAIN(int argc, char **argv)
{

    printTestHeader(__FILE__);

    using namespace Test_MultipleClient;

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

        Echo echo;
        RCF::RcfServer server(serverTransportPtr);
        server.bind( (I_Echo*) 0, echo);
        server.start();

        ThreadGroup clients;
        for (int i=0; i<100; ++i)
        {
            clients.push_back( ThreadPtr(new Thread(
                boost::bind(&clientThread, boost::ref(*clientTransportAutoPtr)))));
        }
        joinThreadGroup(clients);
    }
   
    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