Inspirel banner

Programming Distributed Systems with YAMI4

6.2.1 Ada

In Ada the callback needs to be an instance of a user-defined tagged type that implements the Message_Handler interface. This interface defines only one primitive operation, Call, which is where the message processing is supposed to take place.

An example handler type can be defined the following way:

with YAMI.Incoming_Messages; use YAMI.Incoming_Messages;
--  ...

type My_Incoming_Message_Handler is
  new Message_Handler with null record;

overriding
procedure Call
  (H : in out My_Incoming_Message_Handler;
   Message : in out Incoming_Message'Class) is
begin
   --  code processing the incoming message
   --  ...
end Call;

My_Handler : aliased My_Incoming_Message_Handler;

and the registration of the handler can be done with:

My_Agent.Register_Object
  ("my_object", My_Handler'Unchecked_Access);

Above, the My_Handler object was declared as aliased, so that the access value can be obtained for it and given to the agent for placing it in the routing table, which is, essentially, a map of handlers keyed by object name.

After object registration the agent can properly route messages which have the given object name defined in their headers.

In all example programs that are provided with the YAMI4 distribution the access value to the handler is obtained as Unchecked_Access, because the expected access type is defined at the library level and the handler object is defined as a local variable. This is safe as long as the agent object itself does not refer to the handler longer than its lifetime, which is ensured in the example program by means of proper scoping. In production applications programmers might prefer to create the crucial components - agents and message handlers - at the library level as well, in which case the unchecked form would not be necessary.