Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

RMI for C++

, 6 Aug 2009
User-friendly remote method invocation in C++.
rcf.zip
RCF
demo
vs2003
RCF
Client
Server
doc
include
RCF
Connection.inl
Marshal.inl
Multiplexer.inl
RCF.inl
RcfServer.inl
ServerStub.inl
Protocol
util
Platform
Compiler
bcc55
cw80
gcc32
icl70
msvc71
Library
bcc55
cw80
gcc32
msvc60
msvc71
Machine
SPARC
x86
OS
Cygwin
Unix
Windows
System
NonWindows
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
Jamfile
Jamrules
borland
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
#include <iostream>
#include <sstream>
#include <string>

#include <boost/test/minimal.hpp>

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

#include <SF/memory.hpp>

RCF_BEGIN(I_X, "I_X")
    RCF_METHOD_V0(void, increment)
    RCF_METHOD_R0(int, getCount)
RCF_END(I_X);

// gcc < 3.4 doesn't support boost::is_abstract so we have to be explicit
//#if (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4))
namespace boost {
    template<>
    struct is_abstract< RcfClient<I_X> >
    {
        enum { value = false };
        typedef boost::mpl::false_ type;
    };
    template<>
    struct is_abstract< const RcfClient<I_X> >
    {
        enum { value = false };
        typedef boost::mpl::false_ type;
    };
}
//#endif

RCF_BEGIN(I_Y, "I_Y")
    RCF_METHOD_R1( RcfClient<I_X>, pass, const RcfClient<I_X> & )
RCF_END(I_Y)



class X
{
public:
    X() : count()
    {}

    void increment()
    {
        count++;
    }

    int getCount()
    {
        return count;
    }

private:
    int count;
};

class Y
{
public:
    RcfClient<I_X> pass(RcfClient<I_X> stub)
    {
        stub.increment();
        return stub;
    }
};

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

    {
        // test serialize and deserialize a stub between invocations

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

        RcfClient<I_X> stub1("localhost", port);
        stub1.increment();
        std::ostringstream os;
        SF::OTextStream(os) << stub1;
        std::string s = os.str();

        {
            RcfClient<I_X> stub2;
            std::istringstream is(s);
            SF::ITextStream(is) >> stub2;
            stub2.increment();
        }
        {
            RcfClient<I_X> stub3;
            std::istringstream is(s);
            SF::ITextStream(is) >> stub3;
            stub3.increment();
        }

        int count = stub1.getCount();
        BOOST_CHECK(count == 3);
    }

    {
        // distributed test, passing client stubs from server to server
        int portA = util::Ports::getNext();
        int portB = util::Ports::getNext();
        
        RCF::RcfServer serverA(portA);
        serverA.bind<I_X, X>();
        serverA.start();

        RCF::RcfServer serverB(portB);
        serverB.bind<I_Y, Y>();
        serverB.start();

        RcfClient<I_X> stub1("localhost", portA);
        stub1.increment();
        RcfClient<I_Y> stub2("localhost", portB);
        RcfClient<I_X> stub3 = stub2.pass(stub1);
        stub1.increment();
        stub3.increment();

        int count = stub3.getCount();
        BOOST_CHECK(count == 4);
    }

    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)

Share

About the Author

Jarl Lindrud

Australia Australia
Software developer, ex-resident of Sweden and now living in Canberra, Australia, working on distributed C++ applications. Jarl enjoys programming, but prefers skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 6 Aug 2009
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid