Click here to Skip to main content
15,884,080 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 814.8K   4.6K   153  
User-friendly remote method invocation in C++.
#include <memory>
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/test/minimal.hpp>

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


class Echo
{
public:
    std::string echo(const std::string &s) { return s; }
};

RCF_BEGIN(I_Echo, "I_Echo")
    RCF_METHOD_R1(std::string, echo, const std::string &)
RCF_END(I_Echo)


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

    int port = util::Ports::getNext();

    std::string s0 = "whatever";

    {
        RCF::RcfServer server(port);
        server.bind<I_Echo, Echo>();
        server.start();

        RcfClient<I_Echo> echo("localhost", port);
        std::string s = echo.echo(s0);
        BOOST_CHECK(s == s0);
    }

    port = util::Ports::getNext();

    {   
        RCF::RcfServer server(port);
        server.bind<I_Echo, Echo>(true);
        server.start();

        RcfClient<I_Echo> echo("localhost", port);
        std::string s = echo.echo(s0);
        BOOST_CHECK(s == s0);
    }

    port = util::Ports::getNext();

    {
        Echo e;
        RCF::RcfServer server(port);
        server.bind<I_Echo>(e);
        server.start();

        RcfClient<I_Echo> echo("localhost", port);
        std::string s = echo.echo(s0);
        BOOST_CHECK(s == s0);
    }

    port = util::Ports::getNext();

    {
        std::auto_ptr<Echo> e( new Echo );
        RCF::RcfServer server(port);
        server.bind<I_Echo, Echo>(e);
        server.start();

        RcfClient<I_Echo> echo("localhost", port);
        std::string s = echo.echo(s0);
        BOOST_CHECK(s == s0);
    }

    port = util::Ports::getNext();

    {
        boost::shared_ptr<Echo> e( new Echo );
        RCF::RcfServer server(port);
        server.bind<I_Echo, Echo>(e);
        server.start();

        RcfClient<I_Echo> echo("localhost", port);
        std::string s = echo.echo(s0);
        BOOST_CHECK(s == s0);
    }

    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