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

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/IpServerTransport.hpp>

#include <RCF/test/TransportFactories.hpp>

#include <RCF/util/CommandLine.hpp>
#include <RCF/util/PortNumbers.hpp>
#include <RCF/util/Platform/OS/Sleep.hpp>

// Need to disable broken pipe signals for OS's such as Solaris
#ifdef SIGPIPE
#include <signal.h>
#include <RCF/util/InitDeinit.hpp>
static void setSigignore() { sigignore(SIGPIPE); }
UTIL_ON_INIT( sigignore(SIGPIPE) )
#endif

namespace Test_IpRestriction {

    RCF_BEGIN(I_X, "I_X")
        RCF_METHOD_V0(void, f);
    RCF_END(I_X);

    class X
    {
    public:
        void f() {}
    };

} // namespace Test_IpRestriction

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

    printTestHeader(__FILE__);

    using namespace Test_IpRestriction;

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

    std::string localIp = "localhost";
    int localPort = 0;

    for (unsigned int i=0; i<RCF::getIpTransportFactories().size(); ++i)
    {
        RCF::TransportFactoryPtr transportFactoryPtr = RCF::getTransportFactories()[i];
        std::pair<RCF::ServerTransportPtr, RCF::ClientTransportAutoPtrPtr> transports;
        RCF::ServerTransportPtr serverTransportPtr;
        RCF::ClientTransportAutoPtr clientTransportAutoPtr;

        transports = transportFactoryPtr->createTransports();
        serverTransportPtr = transports.first;
        clientTransportAutoPtr = *transports.second;

        RCF::I_IpServerTransport *ipTransport = dynamic_cast<RCF::I_IpServerTransport *>(serverTransportPtr.get());
        if (!ipTransport)
        {
            continue;
        }

        X x;

        RCF::writeTransportTypes(std::cout, *serverTransportPtr, *clientTransportAutoPtr);

        {
            // test client restriction

            transports = transportFactoryPtr->createTransports();
            serverTransportPtr = transports.first;
            clientTransportAutoPtr = *transports.second;

            RCF::RcfServer server(serverTransportPtr);
            server.bind( (I_X*) 0, x);

            std::vector<std::string> ips;
            ips.push_back("www.usatoday.com");
            ips.push_back("www.c-span.org");
            ips.push_back("www.google.com");

            RCF::I_IpServerTransport &ipTransport = dynamic_cast<RCF::I_IpServerTransport &>(server.getServerTransport());
            ipTransport.setAllowedClientIps(ips);

            server.start();

            Platform::OS::Sleep(1);

            try
            {
                RcfClient<I_X> client(clientTransportAutoPtr->clone());
                client.f();
                BOOST_CHECK(1==0);
            }
            catch(const RCF::Exception &e)
            {
                RCF_TRACE("")(e);
                BOOST_CHECK(1==1);
            }

            ips = ipTransport.getAllowedClientIps();
            ips.push_back("127.0.0.1");
            ipTransport.setAllowedClientIps(ips);

            RcfClient<I_X> client(clientTransportAutoPtr->clone());
            client.f();
        }



        {
            // test interface restriction: loopback interface

            transports = transportFactoryPtr->createTransports();
            serverTransportPtr = transports.first;
            clientTransportAutoPtr = *transports.second;

            std::vector<std::string> interfaces;
            interfaces.push_back("127.0.0.1");

            for (unsigned int i=0; i<interfaces.size(); i++)
            {
                RCF::RcfServer server(serverTransportPtr);
                server.bind( (I_X*) 0, x);

                RCF::I_IpServerTransport &ipTransport = dynamic_cast<RCF::I_IpServerTransport &>(server.getServerTransport());
                ipTransport.setNetworkInterface(interfaces[i]);

                server.start();

                RcfClient<I_X>(clientTransportAutoPtr->clone()).f();
                BOOST_CHECK(1==1);
            }
        }

        {
            // test interface restriction: erroneous interface

            transports = transportFactoryPtr->createTransports();
            serverTransportPtr = transports.first;
            clientTransportAutoPtr = *transports.second;

            RCF::RcfServer server(serverTransportPtr);
            server.bind( (I_X*) 0, x);

            RCF::I_IpServerTransport &ipTransport = dynamic_cast<RCF::I_IpServerTransport &>(server.getServerTransport());
            ipTransport.setNetworkInterface("1.2.3.4");

            try
            {
                server.start();
                // Asio 0.3.7 bug when compiled with Codewarrior; doesn't set error values properly, hence allows the server to start
#if defined(__MWERKS__) && defined(RCF_USE_BOOST_ASIO)
                typeid(*serverTransportPtr) == typeid(RCF::TcpAsioServerTransport) ?
                    BOOST_CHECK(1==1) :
                    BOOST_CHECK(1==0);
#else
                BOOST_CHECK(1==0);
#endif
            }
            catch (const std::exception &e)
            {
                // exception type depends on the server transport
                BOOST_CHECK(1==1);
                RCF_TRACE("")(e);
            }
        }

    }

    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