Callback AMI¶
With callback AMI (Asynchronous Method Invocation, from CORBA
Messaging), a client calls sendc_<op> instead of <op>. The call returns
at once; when the reply comes, the ORB calls the operation of the same name
on a reply handler, an object the client implements. The server does
not take part: to it, the request is an ordinary one, so AMI clients work
with any server.
rustorb has callback AMI in Python, C++, Java, C#, C and Ruby, with code
generated by rustorb-idl -Wbami (rustorb-idl 0.3.0 or later; 0.5.0 for C#,
C and Ruby). Rust has
async fn <op>_async
instead.
What -Wbami generates¶
For each interface I, a handler interface AMI_IHandler, and in the
stub of I, a sendc_ operation for each operation and attribute. For
Demo::Calc:
// Implied IDL: what -Wbami adds for Calc.
interface AMI_CalcHandler : Messaging::ReplyHandler {
void add(in long ami_return_val);
void add_excep(in Messaging::ExceptionHolder excep_holder);
void echo_pair(in Pair ami_return_val);
void echo_pair_excep(in Messaging::ExceptionHolder excep_holder);
// echo_any likewise; nothing for the oneway post
};
// In the stub of Calc:
// void sendc_add(in AMI_CalcHandler ami_handler, in long a, in long b);
// void sendc_echo_pair(in AMI_CalcHandler ami_handler, in Pair p);
sendc_<op>takes the handler, then theinandinoutarguments.- The handler's
<op>takes the return value (ami_return_val), then theinoutandoutarguments. <op>_excepreceives every exception, user or system, in anExceptionHolder;raise_exception()raises it again.- Attributes give
get_<attr>andset_<attr>.
Example¶
import sys
import threading
from rustorb import CORBA
import Demo, Demo__POA
from calc_impl import CalcImpl
class AddHandler(Demo__POA.AMI_CalcHandler):
def __init__(self):
self.done = threading.Event()
def add(self, ami_return_val):
print("result:", ami_return_val)
self.done.set()
def add_excep(self, excep_holder):
try:
excep_holder.raise_exception()
except Demo.Overflow as e:
print("overflow at", e.limit)
except CORBA.SystemException as e:
print("failed:", e)
self.done.set()
orb = CORBA.ORB_init(sys.argv)
poa = orb.resolve_initial_references("RootPOA")
poa._get_the_POAManager().activate()
calc = CalcImpl()._this()
handler = AddHandler()
calc.sendc_add(handler._this(), 20, 22) # returns at once
handler.done.wait() # result: 42
handler = AddHandler()
calc.sendc_add(handler._this(), 999, 2)
handler.done.wait() # overflow at 1000
orb.destroy()
How replies are delivered¶
- The handler runs on a thread of the ORB's reply pool, even if the client
never calls
orb.run(). A handler in a POA withMAIN_THREAD_MODELwaits for the main thread to callrunorperform_work. - Replies come in the order they arrive, possibly on several threads at once.
- Everything that goes wrong after
sendc_returns (no connection,TRANSIENT, a timeout) reaches<op>_excep.sendc_itself raises only what it knows before sending, such as a destroyed ORB. - A nil handler is allowed: the request is sent and the reply dropped.
- The handler may live in another process: it is an ordinary object.
In other languages¶
| C++ | Java | |
|---|---|---|
| Generate | rustorb-idl -bc++11 -Wbami |
rustorb-idl -bjava -Wbami |
| Handler servant | derive from CORBA::servant_traits<Demo::AMI_CalcHandler>::base_type |
extend Demo.AMI_CalcHandlerPOA |
| Call | calc->sendc_add(handler, 20, 22) |
calc.sendc_add(handler, 20, 22) |
| Exception | Messaging::ExceptionHolder (<rustorb/messaging.h>) |
org.omg.Messaging.ExceptionHolder |
Not supported: the polling model (sendp_ and pollers) and
time-independent invocation (routers).
More: ami.md.