Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C++

RMI for C++

Rate me:
Please Sign up or sign in to vote.
4.87/5 (113 votes)
6 Aug 2009CPOL8 min read 837K   4.6K   153  
User-friendly remote method invocation in C++.
#include <boost/test/minimal.hpp>

#include <RCF/RCF.hpp>
#include <RCF/util/CommandLine.hpp>
#include <RCF/util/PortNumbers.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


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

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

int test_main(int argc, char **argv)
{
    //util::CommandLineOption<int> port("port", util::Ports::getNext(), "port number");
    util::CommandLine::getSingleton().parse(argc, argv);

    int port = 0;

    std::vector<std::string> ips;
    ips.push_back("www.usatoday.com");
    ips.push_back("www.sunet.se");
    ips.push_back("www.google.com");

    X x;
    {
        port = util::Ports::getNext();
        RCF::RcfServer server(port);
        server.setAllowedClientIps(ips);
        server.bind<I_X>(x);
        server.start();

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

        ips = server.getAllowedClientIps();
        ips.push_back("localhost");
        server.setAllowedClientIps(ips);

        RcfClient<I_X> client("localhost", port);
        client.f();
    }

    // test interface restriction

    // loopback interface
    std::vector<std::string> interfaces;
    interfaces.push_back("127.0.0.1");
    interfaces.push_back("localhost");
    
    for (unsigned int i=0; i<interfaces.size(); i++)
    {
        port = util::Ports::getNext();
        RCF::RcfServer server(port);
        server.bind<I_X>(x);
        server.setNetworkInterface(interfaces[i]);
        server.start();

        RcfClient<I_X>("localhost", port).f();
        BOOST_CHECK(1==1);
    }
    
    // erroneous interface
    {
        port = util::Ports::getNext();
        RCF::RcfServer server(port);
        server.bind<I_X>(x);
        server.setNetworkInterface("1.2.3.4");
        try 
        {
            server.start();
            BOOST_CHECK(1==0);
        }
        catch (const RCF::Exception &e)
        {
            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