Click here to Skip to main content
15,885,216 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 815.5K   4.6K   153  
User-friendly remote method invocation in C++.
#include <iostream>
#include <sstream>
#include <string>

#include <boost/test/minimal.hpp>
//#define BOOST_CHECK(arg)
//namespace boost { int exit_success = 0; }

#include <RCF/RCF.hpp>
#include <RCF/util/CommandLine.hpp>
#include <RCF/util/PortNumbers.hpp>

#include <SF/memory.hpp>

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

RCF_BEGIN(I_X, "I_X")
    RCF_METHOD_R1(std::string, echo, std::string);
    RCF_METHOD_V1(void, dummy, std::string);
RCF_END(I_X);

class X
{
public:
    std::string echo(const std::string &s)
    {
        // wait 2s and then echo the string
        Platform::OS::Sleep(2);
        return s;
    }
    void dummy(const std::string &s)
    {
        // wait 2s and then return
        Platform::OS::Sleep(2);
        return;
    }
};

// Note: these tests fail on Borland C++ because the scopeguards that are supposed
// to reset the client stub connections are not being triggered.

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

    std::string s;

    RCF::RcfServer server(port);
    server.bind<I_X,X>();
    server.start();

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

    // this call will timeout
    client.setRecvTimeoutMs(1000);
    try 
    {
        std::cout << "A\n";
        s = client.echo("abc");
        BOOST_CHECK(1==0);
    }
    catch (const RCF::Exception &e)
    {
        BOOST_CHECK(1==1);
        std::cout << e.what();
    }
    
    // these ones won't
    client.setRecvTimeoutMs(15000);
    std::cout << "B\n";
    s = client.echo("def");
    BOOST_CHECK(s == "def");

    std::cout << "C\n";
    client.dummy("abc");

    // this call will timeout
    client.setRecvTimeoutMs(1000);
    try 
    {
        std::cout << "D\n";
        client.dummy("abc");
        BOOST_CHECK(1==0);
    }
    catch (const RCF::Exception &e)
    {
        BOOST_CHECK(1==1);
        std::cout << e.what();
    }

    // these ones won't
    client.setRecvTimeoutMs(15000);
    
    std::cout << "E\n";
    s = client.echo("def");
    BOOST_CHECK(s == "def");

    std::cout << "F\n";
    client.dummy("abc");

    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