The mapping of OMG IDL to the Erlang programming language when Erlang
generic server is the back-end of choice is similar to the one used in
the chapter 'OMG IDL Mapping'.
The only difference is in the generated code, a client stub and
server skeleton to an Erlang gen_server
. Orber's User's Guide
contain a more detailed description of IDL to Erlang mapping.
The ic:gen/2
function can be called from the command
line as follows:
shell> erlc "+{be, erl_genserv}" MyFile.idl
For each IDL interface <interface name>
defined in the IDL file :
_impl
.
For each function defined in the IDL interface :
In this example, a file random.idl
generates code for the erlang
gen_server back-end:
// Filename random.idl module rmod { interface random { // Generate a new random number double produce(); // Initialize random generator oneway void init(in long seed1, in long seed2, in long seed3); }; };
When the file "random.idl" is compiled (e.g., shell> erlc "+{be, erl_genserv}" random.idl
)
five files are produced; two for the top scope, two for the interface scope,
and one for the module scope. The header files for top scope and interface
are empty and not shown here. In this case, the stub/skeleton file
rmod_random.erl
is the most important. This module exports two kinds of
operations:
produce
and init
.
To create a new server instance, one of the following functions should be used:
Env
and RegName
, in that order, as parameters.
The former is passed uninterpreted to the initialization operation
of the call-back module, while the latter must be as the
gen_server
parameter ServerName
. If Env
is
left out, an empty list will be passed.
oe_create/0/1/2
,
but create a linked server.
"IDL:rmod/random:1.0"
.
Operations can either be synchronous or asynchronous
(i.e., oneway
). These are, respectively, mapped to
gen_server:call/2/3
and gen_server:cast/2
.
Consult the gen_server
documentation for valid return values.
The IDL dependent operations in this example are listed below. The first argument must be the whatever the create operation returned.
If the compile option timeout
is used a timeout must be added
(e.g., produce(ServerReference, 5000)
). For more information, see
the gen_server
documentation.
The implementation module shall, unless the compile option
impl
is used, be named rmod_random_impl.erl
.
and could look like this:
-module('rmod_random_impl'). %% Mandatory gen_server operations -export([init/1, terminate/2, code_change/3]). %% Add if 'handle_info' compile option used -export([handle_info/2]). %% API defined in IDL specification -export([produce/1,init/4]). %% Mandatory operations init(Env) -> {ok, []}. terminate(From, Reason) -> ok. code_change(OldVsn, State, Extra) -> {ok, State}. %% Optional handle_info(Info, State) -> {noreply, NewState}. %% IDL specification produce(State) -> case catch random:uniform() of {'EXIT',_} -> {stop, normal, "random:uniform/0 - EXIT", State}; RUnif -> {reply, RUnif, State} end. init(State, S1, S2, S3) -> case catch random:seed(S1, S2, S3) of {'EXIT',_} -> {stop, normal, State}; _ -> {noreply, State} end.
Compile the code and run the example:
1> make:all(). Recompile: rmod_random Recompile: oe_random Recompile: rmod_random_impl up_to_date 2> {ok,R} = rmod_random:oe_create(). {ok,<0.30.0>} 3> rmod_random:init(R, 1, 2, 3). ok 4> rmod_random:produce(R). 1.97963e-4 5>