Boost logo

Boost Users :

From: Slawomir Lisznianski (slisznianski_at_[hidden])
Date: 2005-09-17 17:52:32


I've been thinking of a library that would bring RMI-like capabilities
to C++ (RMI stands for Remote Method Invocation).

Primary goal, and differentiating feature, of this library is not to
rely on IDL-like compiler, hence no cross-language support either -- if
you need that, check out CORBA (www.omg.org) or ICE (http://www.zeroc.com).

As an example, I'm going to "implement" client and server sides of a
trivial calculator using proposed library.

Step 1. Define calculator interface
-----------------------------------

//
// Operations: add, subtract, multiply and divide
//

struct add
{ typedef function2<double, double, double> function_type; };

struct subtract
{ typedef function2<double, double, double> function_type; };

struct multiply
{ typedef function2<double, double, double> function_type; };

struct divide
{ typedef function2<double, double, double> function_type; };

// Calculator interface
//
typedef interface< operation<sum>,
                    operation<subtract>,
                    operation<multiply>,
                    operation<divide> > calculator_i;

Step 2. Client invocation
-------------------------

// Construct a proxy to the remote calculator
// instance. Arguments to the constructor
// describe remote object's location (not shown).
//
proxy<calculator_i> calculator(...);

// Call `subtract' on remote object (call
// doesn't block). Returned object implements
// Asynchronous Completion Token (or Future)
// concept.
//
act<double> res = call<subtract>(calculator, 12, 43);

// Retrieve actual result, or get an exception...
//
double result = res();

Step 3. Server implementation
-----------------------------

// Implement actual calculator.
//
struct calculator_servant
{
   double sum(double a, double b) { return a + b; }
   ... // other operations here
};

// Instantiate calculator servant
//
calculator_servant servant;

// Create a TCP listener.
//
tcp_listener listener(...)

// Create a stub object that acts as an
// adapter between transport and a servant.
//
stub<calculator_i> calculator(listener);

// Install operation handlers.
//
calculator.register_function<add>(bind(&calculator_servant::sum,
ref(servant), _1, _2));

... // register remaining handlers

// Start accepting calls.
//
calculator.activate();

// Block until exiting.
//
calculator.wait_for_shutdown();

...

-- end of example]

I'm fairly satisfied with the client side, but I'm not so enthusiastic
about the server side though. For one, it feels less safe. For example,
what if a user forgets to register a "handler" for a particular
operation? Any way to enforce this at compile time? Typically, say with
CORBA, IDL compiler generates operations as pure virtual functions, so
at compile time, the user knows what's left out in her servant. On the
other side, it's quite powerful too (being able to "bind" functors to
operations, that is). Would that be a problem for you or you'd see it as
a good feature?

Let me know what you think.

Cheers,

Slawomir Lisznianski


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net