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
The
shell> erlc "+{be, erl_genserv}" MyFile.idl
For each IDL interface
For each function defined in the IDL interface :
In this example, a file
// 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.,
To create a new server instance, one of the following functions should be used:
Operations can either be synchronous or asynchronous
(i.e.,
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
The implementation module shall, unless the compile option
-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:
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>
]]>