Click here to Skip to main content
15,881,812 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.
#ifndef INCLUDE_TEST_PERFORMANCE_HPP
#define INCLUDE_TEST_PERFORMANCE_HPP

#include <string>

#include <boost/lexical_cast.hpp>

#include <RCF/ClientTransport.hpp>
#include <RCF/FilterService.hpp>
#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/ServerTransport.hpp>
#include <RCF/util/Profile.hpp>

class X 
{
public:
    void f0() {}
    int f1(int) { return 0; }
    std::string f2(const std::string &) { return ""; }
    std::string echo(const std::string &s) { return s; }
};

RCF_BEGIN( I_X, "I_X" )
    RCF_METHOD_V0(void, f0)
    RCF_METHOD_R1(int, f1, int)
    RCF_METHOD_R1(std::string, f2, const std::string &)
    RCF_METHOD_R1(std::string, echo, const std::string &)
RCF_END( I_X )

void runClientTests(const RCF::I_Endpoint &clientEndpoint, int calls, const std::string &title)
{
    RcfClient<I_X> client(clientEndpoint);
    client.f0();
    client.f1(1);
    client.f2("");

    for (int protocol=1; protocol<=10; protocol++)
    {
        if (RCF::isSerializationProtocolSupported(protocol))
        {
            client.getClientStub().setSerializationProtocol(protocol);
            std::string protocolName = RCF::getSerializationProtocolName(protocol);
            std::string titleHeader = 
                "Protocol: " + protocolName + 
                ", Calls: " + boost::lexical_cast<std::string>(calls);
            {
                std::string desc = title + titleHeader + ", void f()";
                util::Profile profile(desc);
                for (int i=0; i<calls; i++)
                {
                    client.f0();
                }
            }
            {
                std::string desc = title + titleHeader + ", int f(int)";
                util::Profile profile(desc);
                for (int i=0; i<calls; i++)
                {
                    client.f1(0);
                }
            }
            {
                std::string desc = title + titleHeader + ", std::string f(std::string)";
                util::Profile profile(desc);
                for (int i=0; i<calls; i++)
                {
                    client.f2("");
                }
            }
        }
    }
}

void runPerformanceTest(
                        const std::string &title,
                        RCF::ClientTransportAutoPtr clientTransportAutoPtr,
                        RCF::ServerTransportPtr serverTransportPtr,
                        RCF::RemoteCallSemantics rcs,
                        const std::string &s0,
                        int serializationProtocol,
                        const std::vector<RCF::FilterFactoryPtr> &filterFactories,
                        const std::vector<RCF::FilterPtr> &payloadFilters,
                        const std::vector<RCF::FilterPtr> &transportFilters,
                        int calls)
{
    std::string serverTransportDesc = typeid(*serverTransportPtr).name();
    std::string clientTransportDesc = typeid(*clientTransportAutoPtr).name();
    std::string transportDesc = "(" + serverTransportDesc + ", " + clientTransportDesc + ")";

    RCF::FilterServicePtr filterServicePtr( new RCF::FilterService() );
    for (unsigned int i=0; i<filterFactories.size(); ++i)
    {
        filterServicePtr->addFilterFactory( filterFactories[i] );
    }

    std::string payloadFiltersDesc = "<empty>";
    if (!payloadFilters.empty())
    {
        payloadFiltersDesc.clear();
        for (unsigned int i=0; i<payloadFilters.size(); ++i)
        {
            payloadFiltersDesc += "<" + payloadFilters[i]->getFilterDescription().getName() + ">";
        }
    }

    std::string transportFiltersDesc = "<empty>";
    if (!transportFilters.empty())
    {
        transportFiltersDesc.clear();
        for (unsigned int i=0; i<transportFilters.size(); ++i)
        {
            transportFiltersDesc += "<" + transportFilters[i]->getFilterDescription().getName() + ">";
        }
    }

    std::string serializationProtocolDesc = 
        boost::lexical_cast<std::string>(serializationProtocol) 
        + " (" + RCF::getSerializationProtocolName(serializationProtocol) + ")";

    RCF_ASSERT(rcs == RCF::Oneway || rcs == RCF::Twoway);
    std::string directionDesc = (rcs == RCF::Oneway) ? "one-way" : "two-way";

    std::string profileDesc = title
        + " Transport: " + transportDesc
        + ", Semantics: " + "std::string echo(const std::string &)"
        + ", Direction: " + directionDesc
        + ", String length: " + boost::lexical_cast<std::string>(s0.length())
        + ", Ser. protocol: " + serializationProtocolDesc
        + ", Payload filters: " + payloadFiltersDesc
        + ", Transport filters: " + transportFiltersDesc
        + ", Calls: " + boost::lexical_cast<std::string>(calls);

    RCF::RcfServer server(serverTransportPtr);
    server.bind<I_X>( boost::shared_ptr<X>(new X) );
    server.addService(filterServicePtr);
    server.start();

    // TODO: wait a little bit here?

    // all calls on the same connection
    RcfClient<I_X> client(clientTransportAutoPtr);
    client.getClientStub().setSerializationProtocol(serializationProtocol);
    client.getClientStub().setPayloadFilters(payloadFilters);
    client.getClientStub().setTransportFilters(transportFilters);
    std::string s = client.echo(s0);
    util::Profile profile(profileDesc);
    for (int i=0; i<calls; ++i)
    {
        std::string s = client.echo(rcs, s0);
    }
}


#endif // ! INCLUDE_TEST_PERFORMANCE_HPP

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