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

#include <RCF/test/TestMinimal.hpp>

#include <SF/utility.hpp> // serialization for std::pair

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/CurrentSession.hpp>
#include <RCF/RcfSession.hpp>
#include <RCF/test/TransportFactories.hpp>
#include <RCF/util/CommandLine.hpp>

namespace Test_ClientInfo {

    class GetMyAddress
    {
    public:
        std::pair<std::string, int> getMyAddress()
        {
            const RCF::I_RemoteAddress &address = RCF::getCurrentRcfSessionPtr()->getRemoteAddress();
            const RCF::IpAddress &ipAddress = dynamic_cast<const RCF::IpAddress &>(address);
            return std::make_pair(ipAddress.getIp(), ipAddress.getPort());
        }
    };

    typedef std::pair<std::string, int> StringIntPair;

    RCF_BEGIN(I_GetMyAddress, "I_GetMyAddress")
        RCF_METHOD_R0(StringIntPair, getMyAddress)
    RCF_END(I_GetMyAddress)

} // namespace Test_ClientInfo

RCF_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(Test_ClientInfo::StringIntPair)

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

    printTestHeader(__FILE__);

    using namespace Test_ClientInfo;

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

    for (int i=0; i<RCF::getIpTransportFactories().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);

        GetMyAddress getMyAddress;
        RCF::RcfServer server(serverTransportPtr);
        server.bind( (I_GetMyAddress*) 0, getMyAddress);
        server.start();

        std::pair<std::string, int> myAddress = RcfClient<I_GetMyAddress>(clientTransportAutoPtr).getMyAddress(RCF::Twoway);
        BOOST_CHECK(myAddress.first == "127.0.0.1");
        BOOST_CHECK(myAddress.second > 0);
    }
   
    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