[Ericsson AB]

5 Using the Erlang Generic Server Back-end

5.1 Introduction

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.

5.2 Compiling the Code

The ic:gen/2 function can be called from the command line as follows:

shell> erlc "+{be, erl_genserv}" MyFile.idl
    

5.3 Writing the Implementation File

For each IDL interface <interface name> defined in the IDL file :

For each function defined in the IDL interface :

5.4 An Example

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:

5.4.1 Administrative Operations

To create a new server instance, one of the following functions should be used:

5.4.2 IDL Dependent Operations

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.

5.4.3 Implementation Module

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> 
      

Copyright © 1991-2006 Ericsson AB