Click here to Skip to main content
15,894,017 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/test/minimal.hpp>

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

void exhaustTokenSpace(int numberOfTokens, RCF::I_RcfClient &client)
{
    int count = 0;
    for (int j=0; j<numberOfTokens+1;++j)
    {
        if (RCF::createRemoteObject<I_Echo>(client))
        {
            ++count;
        }
        std::cout << client.getClientStub().getToken() << std::endl;
    }
    BOOST_CHECK(count == numberOfTokens);
}

int test_main(int argc, char **argv)
{
    util::CommandLineOption<int> clNumberOfTokens("tokens", 5, "number of tokens in object factory");
    util::CommandLineOption<int> clClientStubTimeoutS("timeout", 2, "server object timeout in seconds");
    util::CommandLineOption<int> clWaitIntervalS("wait", 5, "wait interval in seconds, for object factory cleanup");
    util::CommandLine::getSingleton().parse(argc, argv);

    {

        std::string s0 = "something special";

        std::string ip = "localhost";
        int port = util::Ports::getNext();
        RCF::TcpEndpoint serverEndpoint(port);
        RCF::TcpEndpoint clientEndpoint(ip, port);

        RCF::RcfServer server( serverEndpoint );

        unsigned int waitIntervalS = clWaitIntervalS;
        unsigned int numberOfTokens = clNumberOfTokens;
        unsigned int clientStubTimeoutS = clClientStubTimeoutS;

        RCF::ObjectFactoryServicePtr objectFactoryServicePtr( new RCF::ObjectFactoryService(numberOfTokens, clientStubTimeoutS) );
        objectFactoryServicePtr->bind<I_Echo, Echo>();
        server.addService( objectFactoryServicePtr );

        server.start();

        RcfClient<I_Echo> client( clientEndpoint );
        bool ok = RCF::createRemoteObject<I_Echo>(client);
        BOOST_CHECK(ok);
        std::string s = client.echo(s0);
        RCF::Token token = client.getClientStub().getToken();
        BOOST_CHECK( s == s0 );
        BOOST_CHECK(token != RCF::Token());

        for (int j=0; j<numberOfTokens-1;++j)
        {
            ok = RCF::createRemoteObject<I_Echo>(client);
            BOOST_CHECK(ok);
        }

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

        std::cout << "Waiting for " << waitIntervalS << " seconds...\n";
        Platform::OS::Sleep(waitIntervalS);
        exhaustTokenSpace(numberOfTokens, client);

        server.removeService(objectFactoryServicePtr);
        server.addService(objectFactoryServicePtr);
        
        std::cout << "Waiting for " << waitIntervalS << " seconds...\n";
        Platform::OS::Sleep(waitIntervalS);
        exhaustTokenSpace(numberOfTokens, client);
        
        server.removeService(objectFactoryServicePtr);
        objectFactoryServicePtr.reset( new RCF::ObjectFactoryService(numberOfTokens, clientStubTimeoutS) );
        objectFactoryServicePtr->bind<I_Echo, Echo>();
        server.addService(objectFactoryServicePtr);

        std::cout << "Waiting for " << waitIntervalS << " seconds...\n";
        Platform::OS::Sleep(waitIntervalS);
        exhaustTokenSpace(numberOfTokens, client);
    }

    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