[Erlang Systems]

4 Using the Erlang Generic Server Back-end

4.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 on the generated code, a client stub and server skeleton to an Erlang gen_server.

4.2 Compiling the Code

In the Erlang shell type :

ic:gen(<filename>, [{be, erl_genserv}]).

4.3 Writing the Implementation File

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

For each function defined in the IDL interface :

4.4 An Example

In this example, a file "random.idl" generates code for the plain erlang back-end :

Compile the file :

      Erlang BEAM) emulator version 4.9
      
      Eshell V4.9  (abort with ^G)
      1> ic:gen(random,[{be, erl_genserv}]).
      Erlang IDL compiler version 2.5.1
      ok
      2> 
    

When the file "random.idl" is compiled it produces five files: 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, only the file for the interface rmod_random.erl is important :.

The implementation file should be called rmod_random_impl.erl and could look like this :

-module('rmod_random_impl').
-export([init/1, terminate/2]).
-export([produce/1,init/4]).


init(Env) ->
    {ok, []}.

terminate(From, Reason) ->
    ok.


produce(_Random)  ->
    case catch random:uniform() of
        {'EXIT',_} ->
            true;
        RUnif ->
            {reply,RUnif,[]}
     end.


init(_Random,S1,S2,S3)  ->
    case catch random:seed(S1,S2,S3) of
        {'EXIT',_} ->
            true;
        _ ->
            {noreply,[]}
    end.

    

Compiling the code :

2> make:all().
Recompile: rmod_random
Recompile: oe_random
Recompile: rmod_random_impl
up_to_date
      

Running the example :

3> {ok,R} =  rmod_random:oe_create().
{ok,<0.30.0>
4> rmod_random:init(R,1,2,3).
ok
5> rmod_random:produce(R).
1.97963e-4
6> 

      

Copyright © 1991-2002 Ericsson Utvecklings AB