Open-Source Project C ++ remote method calling framework RMI for C ++

Source: Internet
Author: User

RMI for C ++ is a remote method calling framework designed for the C ++ language. Unlike CORBA, RMI is suitable for interoperability between different programming languages, RMI for C ++ is designed for C ++, so it is more efficient, faster, and more convenient to develop. Engineering

The project requires third-party library boost (which has been put into the source code package). The Boost library is a C ++ library that has been tested, transplanted, and provided with source code, and serves as a backup for the standard library, it is one of the engines of the C ++ standardization process.
The Boost library was initiated by members of the C ++ Standards Committee working group. It has a significant impact in the C ++ community and has nearly 2000 members. The Boost Library provides us with the latest, coolest, and most practical technologies. It is an uncompromising standard library.
The program calls the C ++ program using the CORBA specification. It directly uses the pre-processor to specify the interface and encapsulate the process parameters in the source code. Of course, the Boost library must be used to serialize the local C ++ program framework.
Echo server
Instance source code:
[Cpp]
# Include <RCF/RCF. hpp>
 
 
RCF_BEGIN (I _Echo, "I _Echo ")
RCF_METHOD_R1 (std: string, echo, const std: string &);
RCF_END (I _Echo );
 
Class Echo
{
Public:
Std: string echo (const std: string & msg) {return msg ;}
};
 
Int main ()
{
Int port = 50001;
RCF: RcfServer server (port );
Server. bind <I _Echo, Echo> ();
Server. start ();
Return 0;
}
Echo Client
Instance source code:
[Cpp]
# Include <RCF/RCF. hpp>
 
 
RCF_BEGIN (I _Echo, "I _Echo ")
RCF_METHOD_R1 (std: string, echo, const std: string &);
RCF_END (I _Echo );
 
Int main ()
{
Std: cout <RcfClient <I _Echo> ("localhost ",
50001). echo ("my message ");
Return 0;
}
Boost. Serialization library, used for Serialization parameters and return values. It has standard types and containers automatically, and can be easily extended to user-defined classes. It also enables us to serialize pointers, multi-state pointers, and properly process multiple pointers of a single object.
Basic usage
There are three basic steps to use this framework:
Use the RCF_xxx macro to define the interface.
The exposed RcfServer class used to implement this interface.
Call the method on the server of the object exposed by the RcfClient <> class.
The usage of the interface definition macro is as follows:
[Cpp]
RCF_BEGIN (type, type_id)
//...
 
RCF_METHOD_xx (return_type, name ,....):
//...
 
RCF_END (type)
Type is the identifier of the interface, and TYPE_ID is a string that gives the description of a runtime interface.
Once we define an interface using the RCF_xxx macro, we can start the server and bind it to the interface of a specific object:
[Cpp]
{
// Create the server and tell it which port to listen on
 
RCF: RcfServer server (port );
 
// Interface is the identifer of the interface we're re exporting,
 
// Object is a type that implements that interface
 

// One object for each client
 
Server. bind <Interface, Object> ();
 
//... Or one object shared by all clients
 
Object object;
Server. bind <Interface> (object );
 
// Tell the server to start listening for connections
 
Server. start ();
 
//...
 
 
// The server will shut down automatically as it goes out of scope
 
}
This object is statically bound to the corresponding interface, and does not need to be derived from an interface class for the traditional dynamic polymorphism. Instead, the compiler not only effectively solves interfaces during compilation, but also allows more flexible semantics.
The server can process multiple clients simultaneously, even in single-threaded mode and can be stopped at any time. The life cycle of the object exposed by the server to determine the number of objects currently connected to a given object. Once there are more live object connections, timeout is set and when it expires, this object is deleted.
To make the client call, We instantiate the corresponding RcfClient <> template and use the server's IP address and port number constructor. When the first remote method is called, the client tries to connect to the server, specify the object query, call the member function of the remote object request, and then return the remote return value.
[Cpp]
// Define the interface
 
RCF_BEGIN (Interface, "Interface ")
RCF_METHOD_R2 (int, add, int, int );
RCF_END (Interface );
 
//...
 
 
{
Std: string ip = "localhost ";
Int port = 50001;
RcfClient <Interface> client (ip, port );

// Connect and call the add function
 
Int sum = client. add (1, 1 );

// Connection closed as we exit scope
 
}

If any exception occurs, when the server calls the requested object, an exception type RCF: RemoteException is propagated back to the client and thrown. If any exception occurs, such as serialized parameters on the server side, in other places, the client throws an exception when the server forcibly closes the connection.
RCF automatically processes parameter types, including int, double, std: string, STL container, pointer, reference, boost: shared_ptr, std: auto_ptr, and so on.
In CORBA, the marked parameters include in, out, And inout. Based on these parameters, the parameters are sent for processing. Follow the following conventions:
[Cpp]
Value: in
Pointer: in
Const reference: in
Nonconst reference: inout
 
Nonconst reference to pointer: out

To use a user-defined type as a parameter or return value, some additional Serialization code (Boost. Serialization) is required, as shown below.
[Cpp]
Struct MyStruct
{
Int;
Int B;
Int c;
Double d;
Std: string s;
Std: map <std: string, std: vector <std: string> m;

Template <typename Archive>
Void serialize (Archive & archive, unsigned int version)
{
Ar & a & B & c & d & s & m;
}

};
 
RCF_BEGIN (MyInterface, "MyInterface ")
RCF_METHOD_R1 (MyStruct, myfunc, const MyStruct &);
RCF_END (MyInterface );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.