Click here to Skip to main content
15,892,768 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 <boost/bind.hpp>
#include <boost/test/minimal.hpp>
#include <boost/thread.hpp>

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/ObjectFactoryService.hpp>
#include <RCF/test/TransportFactories.hpp>
#include <RCF/test/ThreadGroup.hpp>
#include <RCF/util/CommandLine.hpp>
#include <RCF/util/Platform/OS/Sleep.hpp>
#include <RCF/util/PortNumbers.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)

boost::shared_ptr<boost::thread> serverThread;

void serverThreadTask(RCF::RcfServer &server)
{
    while (server.cycle() == false);
}
void joinServerThread()
{
    serverThread->join();
}

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

        Echo echo;
        RCF::RcfServer server(serverTransportPtr);
        
        unsigned int numberOfTokens = 5;
        unsigned int objectTimeoutS = 2;
        RCF::ObjectFactoryServicePtr objectFactoryServicePtr( new RCF::ObjectFactoryService(numberOfTokens, objectTimeoutS) );
        objectFactoryServicePtr->bind<I_Echo, Echo>();
        server.addService(objectFactoryServicePtr);
        
        server.bind<I_Echo>(echo);
        server.start(false);
        
        static const int Immediate = 0;
        static const int Infinite = -1;
        
        RcfClient<I_Echo> myClient(clientTransportAutoPtr);

        std::string s0;
        
        s0 = "bingo1";
        myClient.echo(RCF::Oneway, s0);
        Echo::sLog = "";
        while (Echo::sLog == "")
        {
            server.cycle(200); // max wait = 200 ms
        }
        BOOST_CHECK(Echo::sLog == s0);

        s0 = "bingo2";
        myClient.echo(RCF::Oneway, s0);
        Echo::sLog = "";
        while (Echo::sLog == "")
        {
            server.cycle(Infinite); // max wait = infinite
        }
        BOOST_CHECK(Echo::sLog == s0);

        s0 = "bingo3";
        myClient.echo(RCF::Oneway, s0);
        Echo::sLog = "";
        while (Echo::sLog == "")
        {
            server.cycle(Immediate); // max wait = immediate
        }
        BOOST_CHECK(Echo::sLog == s0);

        // check that the object factory service cleanup task is running properly
        {
            serverThread.reset( new boost::thread( boost::bind(serverThreadTask, boost::ref(server)) ) );
            server.addJoinFunctor(joinServerThread);

            for (int i=0; i<numberOfTokens; ++i)
            {
                bool ok = RCF::createRemoteObject<I_Echo>(myClient);
                BOOST_CHECK(ok);
            }

            bool ok = RCF::createRemoteObject<I_Echo>(myClient);
            BOOST_CHECK(!ok);

            Platform::OS::Sleep(5);

            for (int i=0; i<numberOfTokens; ++i)
            {
                bool ok = RCF::createRemoteObject<I_Echo>(myClient);
                BOOST_CHECK(ok);
            }

            server.stop();
        }
        
    }

    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