Client-Side MATLAB Mapping for Operations
Mapping for Operations
As we saw in the Client-Side MATLAB Mapping for Interfaces, for each operation on an interface, the generated proxy class contains 2 methods for this operation. To invoke an operation, you call one of these methods on the proxy. For example, let’s take the generated code from the greeter example:
["matlab:identifier:visitorcenter"]
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}
The proxy class generated from the Greeter interface, after removing extra details, is as follows:
classdef GreeterPrx < Ice.ObjectPrx
methods
function returnValue = greet(obj, name, context)
% ...
end
function future = greetAsync(obj, name, context)
% ...
end
end
end
Given a proxy to an object of type Greeter, the client can invoke the greetoperation as follows:
greeter = visitorcenter.GreeterPrx(
communicator, 'greeter:tcp -h localhost -p 4061');
greeting = greeter.greet('Alice'); % Get name via RPC
Sync and Async Methods
For each operation, the Slice compiler generates 2 methods on the proxy class:
a “sync” method with the same name as the operation. When you call this method, your program waits synchronously until the invocation completes. A successful invocation completes with a return value (which can be void), while an unsuccessful invocation completes with an exception.
an “async” method, named
<operation-name>Async. When you call this method, your program marshals the arguments to the method synchronously, but the remainder of this invocation is asynchronous, and the method returns a future immediately. These async methods are described in more detail in Asynchronous Method Invocation (AMI) in MATLAB.
Async invocations allow you to perform other work while the server is processing the request. Sync invocations are more convenient to call. You decide what’s more important for your application.
Exception Handling
Any operation invocation may throw a runtime exception and, if the operation has an exception specification, may also throw user exceptions. Suppose we have the following simple interface:
exception Tantrum
{
string reason;
}
interface Child
{
void askToCleanUp() throws Tantrum;
}
Slice exceptions are thrown as MATLAB exceptions, so you can simply enclose one or more operation invocations in a try-catch block:
child = ...; % Get child proxy...
try
child.askToCleanUp();
catch ex
if isa(ex, 'Tantrum')
fprintf('The child says: %s\n', ex.reason);
else
rethrow(ex);
end
end