PreviousNext

Creating Remote-Dynamic Objects

In C++ we create new objects dynamically by calling the new operator for the class. This works to creates local objects, but how do clients create remote dynamic objects? In order for a client to create dynamic objects, at least one static object creator operation must be defined in an interface to create its objects. Remember that a static member function does not have to be invoked by an existing object, and thus it is appropriate as a way to create new objects. The operation can be declared to return a new object as a return value or an output parameter. Object creator functions are declared as static in either of two ways:

· In the IDL file explicitly by using the static keyword

· In an ACF by using the cxx_static (or cxx_new) attribute

The following example is an ACF that specifies object creator member functions for the Matrix interface:

/* FILE NAME: matrix.acf */
/* This file defines some attributes for a simple client of the */
/* Matrix interface. */

interface Matrix
{
/* createMatrix needs to be mapped as a creator function */
[cxx_static] createMatrix();

/* newMatrix needs to be mapped as a static member function */
[cxx_static] newMatrix();
}

When the interface is compiled with this ACF, the IDL compiler generates a proxy class for the client in which these operations are declared as static member functions. The proxy class is our client's interface to DCE, and it is DCE's mechanisms that let clients interact with objects in the distributed environment.

In some ACFs, static object creator functions may be specified with the cxx_new attribute instead of cxx_static, and both the attributes may include an argument. These differences do not affect the client stub and are significant only for server stubs.

The following example shows how a client calls the Matrix interface's static member functions:

#include "matrix.h" // IDL generated header file
#include printmatrix.h

void
main()
{
Matrix *m1;

cout << "Creating dynamic objects:" << endl;

// Create a remote Matrix object on a server using an
// object creator function.
m1 = Matrix::createMatrix(1, 2, 3, 4);
cout << "m1 created by an object creator function:" << endl;
print(m1);
delete m1;

// Create a remote Matrix object on a server using
// a static member function.
idl_boolean result = Matrix::newMatrix(2, 2, &m1);
if(result)
{
print(m1);
delete m1;
}
.
.
.

#include "matrix.h"
The interface class and proxy class are defined in the matrix.h header file generated by the IDL compiler.

Matrix *m1;
Object references are declared as pointers to an interface class. The interface class is an abstract class, and C++ does not allow you to create instances of it. However, pointers to abstract classes are allowed. When a remote object is created for one of these object references, the client stub actually creates a proxy class object on the client.

m1 = Matrix::createMatrix(...)
Object creator functions are invoked using the interface class name (Matrix) with the standard C++ scope operator (::). This function creates a remote Matrix object on a server and returns a reference to the remote object.

print(m1);
After a dynamic object is created, the application uses it just like any local object. This function is an application-specific inline function to display a Matrix. It is defined in the printmatrix.h header file and uses the Matrix interface's get( ) function.

delete m1;
Remote dynamic objects are deleted with the standard C++ delete operator, just like local objects. However, for the remote object, an RPC is sent to it to decrement the reference count. If no other clients have a reference to it, the object is also deleted from the server's address space. Client applications should take care to delete all dynamic objects prior to exiting. Otherwise, the object remains in the server's address space wasting resources. Dynamic objects are created for the use of the invoking client. This means that servers cannot give a different client a reference to a dynamic object. However, the client could behave as a server and give a copy of the object reference to another client. For this reason, a reference count is maintained on the server for objects.

... = Matrix::newMatrix(2, 2, &m1)
Static member functions are invoked using the interface class name (Matrix) with the standard C++ scope operator (::). This function is also an object creator function that creates a remote Matrix object on a server.