Click here to Skip to main content
15,892,072 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 <RCF/test/TestMinimal.hpp>

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

#include <SF/AdlWorkaround.hpp>
#include <SF/memory.hpp>

#include <SF/ITextStream.hpp>
#include <SF/OTextStream.hpp>

#ifdef RCF_USE_BOOST_SERIALIZATION
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#endif

namespace Test_StubSerialization {

    RCF_BEGIN(I_X, "I_X")
        RCF_METHOD_V0(void, increment);
        RCF_METHOD_R0(int, getCount);
    RCF_END(I_X);

    class X
    {
    public:
        X() : count(RCF_DEFAULT_INIT)
        {}

        void increment()
        {
            count++;
        }

        int getCount()
        {
            return count;
        }

    private:
        int count;
    };

} // namespace Test_StubSerialization

RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(Test_StubSerialization::I_X::RcfClient)

// TODO: following belong in RCF headers
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(RCF::ClientStub)
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(RCF::RemoteCallSemantics)
//RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(RCF::I_Endpoint)
RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(RCF::EndpointPtr)

//SF_ADL_WORKAROUND(RCF, I_Endpoint)
SF_ADL_WORKAROUND(RCF, ByteBuffer)

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

    printTestHeader(__FILE__);

    using namespace Test_StubSerialization;

    util::CommandLine::getSingleton().parse(argc, argv);

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

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

        X x;
        RCF::RcfServer server(serverEndpoint);
        server.bind( (I_X*) 0, x);
        server.start();

        RcfClient<I_X> client(clientEndpoint);
        client.increment();

        std::ostringstream os1;
        SF::OTextStream(os1) << client;
        std::string s1 = os1.str();
        RCF_TRACE("")(s1);

        {
            RcfClient<I_X> client2;
            std::istringstream is(s1);
            SF::ITextStream(is) >> client2;
            client2.increment();
        }
        {
            RcfClient<I_X> client3;
            std::istringstream is(s1);
            SF::ITextStream(is) >> client3;
            client3.increment();
        }

        // Polymorphic serialization in B.Ser. is seriously broken on gcc.
        // On gcc 3.4, this test passes in debug, and crashes on release.
        // On gcc 3.2, it asserts.
#if defined(RCF_USE_BOOST_SERIALIZATION) && !defined(__GNUC__)

        {
            std::ostringstream os;
            boost::archive::text_oarchive(os) & client;
            std::string s = os.str();
            RCF_TRACE("")(s);

            RcfClient<I_X> client4;
            std::istringstream is(s);
            boost::archive::text_iarchive(is) & client4;
            client4.increment();
        }

        int count = client.getCount();
        BOOST_CHECK(count == 4);

#else

        int count = client.getCount();
        BOOST_CHECK(count == 3);

#endif

    }
   
    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