Gem #85:The Distributed Systems Annex 2 — Distributed Objects

by Thomas Quinot—AdaCore

Let's get started…

In the previous DSA Gem, we showed how subprograms in a package can be made remotely callable using a pragma Remote_Call_Interface (RCI for short). Each RCI unit is present in only one partition of a distributed application, and any call to a subprogram in such a unit made from another partition is transparently handled by the distribution run-time library.

This is sufficient to implement simple client/server communication, where a single partition is identified as the provider of a service (defined by an RCI package) and accepts requests from other partitions. Different services can be provided by different partitions, and services can be clients of one another. However this scheme is inflexible in that a given service can only ever be provided by a single server. Furthermore, the association between services and partitions is static.

In some contexts, however, more flexible interactions between application components are desired: multiple partitions may want to provide the same service, for performance or fault-tolerance reasons; servers may need to call back their clients; finally, direct (peer-to-peer) interactions between partitions may need to be established in a dynamic fashion, without determining in advance (prior to execution) who will interact with whom.

Such a flexible organization can be implemented using distributed objects. In nondistributed object-oriented programming, an object is an entity with an identity (you can reference, or designate it), internal state, and a set of methods that are common to all objects that belong to the same class, and which represent the ways any object of the class can interact with others. In a distributed world, this paradigm is naturally extended by allowing object references to designate objects that are located on another partition.

In the DSA, distributed objects are created using a specific pragma: Remote_Types. When this pragma is applied to a package, certain type declarations have additional semantics specific to distribution. If you declare a tagged limited private type in such a package, and a corresponding access-to-class-wide type, then that access type is a Remote Access to Class-Wide type (or RACW), and is allowed to designate objects that are located on partitions other than the current one.

These remote object references can be passed around as parameters in remote subprogram calls. For example, they can be sent to an RCI package, or retrieved from it, by passing them as parameters in remote subprogram calls.

Methods of remote objects can be called by just writing a regular dispatching call on any primitive operation. All underlying communication is handled transparently by the distribution run-time library.

So let's now assume that we want to allow users of our bulletin board application to exchange direct messages with one another. Each user will instantiate an object of a concrete type derived from the User type:

<strong>package</strong> Chat_Users <strong>is</strong>
   <strong>pragma</strong> Remote_Types;
   <em>--  This package declares a remote object type</em>
   <strong>type</strong> User <strong>is</strong> <strong>abstract</strong> <strong>tagged</strong> <strong>limited</strong> <strong>private</strong>;
   <em>--  Remote objects must be tagged, limited, and private</em>
   <strong>type</strong> User_Ref <strong>is</strong> <strong>access</strong> <strong>all</strong> User'Class;
   <em>--  This is a remote access-to-class-wide type</em>
   <strong>function</strong> Name (Who : User) <strong>return</strong> String;
   <strong>procedure</strong> Say
     (From : User_Ref;
      To   : User;
      What : String);
   <em>--  The controlling formal 'To' determines the object that calls are sent to.</em>
   <em>--  The recipient object may be remote. Formal parameter 'From' is a reference</em>
   <em>--  to the originating user, and can be used to call the user back at a later</em>
   <em>--  time.</em>
<strong>end</strong> Chat_Users;

Each message posted to the bulletin board can now include a reference to the message author:

<strong>with</strong> Chat_Users;
<strong>package</strong> Bulletin_Board <strong>is</strong>
  <strong>type</strong> News_Item (Message_Length : Natural) <strong>is</strong>
     Author  : Chat_Users.User_Ref;
     Message : String (1 .. Message_Length);
  <strong>end</strong> News_Item;
<strong>end</strong> Bulletin_Board;

Now each client can create an instance of a concrete type derived from Chat_Users.User, and pass a 'Access to that object to the bulletin board as it posts messages.

<strong>with</strong> Chat_Users;
<strong>package</strong> Client <strong>is</strong>
   <em>--  This is a regular package, no pragma needed</em>
   <strong>type</strong> Myself_Type <strong>is</strong> <strong>new</strong> Chat_Users.User <strong>with</strong> <strong>null</strong> <strong>record</strong>;
   <strong>function</strong> Name (Self : Myself_Type) <strong>return</strong> String;
   <strong>procedure</strong> Say
     (From : Chat_Users.User_Ref;
      To   : Myself_Type;
      What : String);
<strong>end</strong> Client;
<strong>with</strong> Ada.Text_IO; <strong>use</strong> Ada.Text_IO;
<strong>package</strong> <strong>body</strong> Client <strong>is</strong>
   <strong>function</strong> Name (Self : Myself_Type) <strong>return</strong> String <strong>is</strong>
      <strong>return</strong> "Jean-Pierre";
   <strong>end</strong> Name;
   <strong>procedure</strong> Say
     (From : Chat_Users.User_Ref;
      To   : Myself_Type;
      What : String)
      <strong>pragma</strong> Unreferenced (To);
      <em>--  Parameter 'To' is unused within the body. Its purpose is just to cause</em>
      <em>--  dispatching to the appropriate object instance.</em>
      Put_Line ("Got a message from " & From.Name);
      <em>--  Dispatching call to Name to retrieve user name of the sender 'From'</em>
      Put_Line (What);
      <em>--  Display received message.</em>
   <strong>end</strong> Say;
   Myself : <strong>aliased</strong> Myself_Type;
<strong>end</strong> Client;

Other clients can use the Author component retrieved from the bulletin board to directly contact other clients using the Say method:

     (From    => Myself'<strong>Access</strong>,
      To      => Some_Item.Author,
      Message => "I like it!");

Arbitrary partition-to-partition interactions can thus be established using distributed objects. More precisely, these are actually normal objects with the additional property that they can be designated from other partitions using special access types (RACWs). RCI units serve as switchboards to initially propagate references to remote objects across partition boundaries. Once these references are disseminated, partitions can interact directly without the mediation of RCIs.

In a future Gem, we will discuss the implementation of mailbox-based message passing using the Distributed Systems Annex.

About the Author

Thomas Quinot holds an engineering degree from Télécom Paris and a PhD from Université Paris VI. The main contribution of his research work is the definition of a flexible middleware architecture aiming at interoperability across distribution models. He joined AdaCore as a Senior Software Engineer in 2003, and is responsible for the distribution technologies. He also participates in the development, maintainance and support of the GNAT compiler.

Last Updated: 10/26/2017
Posted on: 5/3/2010