...
The name
operation returns a value of type string
. Given a proxy to an object of type Node
, the client can invoke the operation as follows:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
node = ...; % Initialize proxy
name = node.name(); % Get name via RPC
|
...
The proxy interface for this is:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef ExamplePrx < Ice.ObjectPrx
function r = op1(obj, varargin)
function r = op2(obj, varargin)
end |
...
The Slice compiler generates the following proxy for these definitions:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef ClientToServerPrx < Ice.ObjectPrx
methods
function op1(obj, i, f, b, s, varargin)
...
end
function op2(obj, ns, ss, st, varargin)
...
end
function op3(obj, proxy, varargin)
...
end
end
end |
Given a proxy to a ClientToServer
interface, the client code can pass parameters as in the following example:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
p = ...; % Get ClientToServerPrx proxy...
p.op1(42, 3.14f, true, 'Hello world!'); % Pass simple literals
i = 42;
f = 3.14;
b = true;
s = 'Hello world!';
p.op1(i, f, b, s); % Pass simple variables
ns = NumberAndString();
ns.x = 42;
ns.str = 'The Answer';
ss = { 'Hello world!' };
st = StringTable.new();
st(0) = ns;
p.op2(ns, ss, st); % Pass complex variables
p.op3(p); % Pass proxy
|
...
The Slice compiler generates the following code for these definitions:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef ServerToClientPrx < Ice.ObjectPrx
methods
function [i, f, b, s] = op1(obj, varargin)
...
end
function [ns, ss, st] = op2(obj, varargin)
...
end
function proxy = op3(obj, varargin)
...
end
end
end |
...
A client can invoke this operation as shown below:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
[i, v] = proxy.execute('--file log.txt');
[i, v] = proxy.execute(Ice.Unset);
if v ~= Ice.Unset
fprintf('value = %f\n"', v); % v is set to a value
end |
A well-behaved program must always test an optional parameter prior to using its value. Keep in mind that the Ice.Unset
marker value has different semantics than an empty array. Since an empty array is a legal value for certain Slice types, the Ice run time requires a separate marker value so that it can determine whether an optional parameter is set. An optional parameter set to an empty array is considered to be set. If you need to distinguish between an unset parameter and a parameter set to an empty array, you can do so as follows:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
if optionalParam == Ice.Unset
fprintf('optionalParam is unset\n');
elseif isempty(optionalParam)
fprintf('optionalParam is empty\n');
else
fprintf('optionalParam = %s\n', optionalParam);
end |
...
Slice exceptions are thrown as MATLAB exceptions, so you can simply enclose one or more operation invocations in a try
-catch
block:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
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 |
Typically, you will catch only a few exceptions of specific interest around an operation invocation; other exceptions, such as unexpected run-time errors, will typically be handled by exception handlers higher in the hierarchy. For example:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef Client
methods(Static)
function run()
child = ...; % Get child proxy...
try
child.askToCleanUp();
catch ex
if isa(ex, 'Tantrum')
fprintf('The child says: %s\n', ex.reason);
child.scold(); % Recover from error...
else
rethrow(ex);
end
end
child.praise(); % Give positive feedback...
end
end
function main(args)
try
% ...
Client.run();
% ...
catch ex
if isa(ex, 'Ice.UserException')
% handle uncaught user exception
elseif isa(ex, 'Ice.LocalException')
% handle uncaught local exception
else
% other exception type
rethrow(ex);
end
end
end
end |
...