User's Guide
Reference Manual
Release Notes
PDF
Top

ic
User's Guide
Version 4.4

Expand All
Contract All

Chapters

- Using the IC Compiler
  - Top of chapter
  - Introduction
  - Compilation of IDL Files
- OMG IDL
  - Top of chapter
  - OMG IDL - Overview
- IC Protocol
  - Top of chapter
  - Introduction
  - Language mappings and IDL operations
  - IC Protocol
  - Gen_server Protocol
  - Erlang Distribution Protocol
- Using the Plain Erlang Back-end
  - Top of chapter
  - Introduction
  - Compiling the Code
  - Writing the Implementation File
  - An Example
- Using the Erlang Generic Server Back-end
  - Top of chapter
  - Introduction
  - Compiling the Code
  - Writing the Implementation File
  - An Example
- IDL to C mapping
  - Top of chapter
  - Introduction
  - C Mapping Characteristics
  - Basic OMG IDL Types
  - Constructed OMG IDL Types
  - OMG IDL Constants
  - OMG IDL Operations
  - Exceptions
  - Access to Attributes
  - Summary of Argument/Result Passing for the C-client
  - Supported Memory Allocation Functions
  - Special Memory Deallocation Functions
  - Exception Access Functions
  - Special Types
  - A Mapping Example
- The C Client Back-end
  - Top of chapter
  - Introduction
  - Generated Stub Files
  - C Interface Functions
  - Generating, Compiling and Linking
  - An Example
- The C Server Back-end
  - Top of chapter
  - Introduction
  - Generated Stub Files
  - C Skeleton Functions
  - The Server Loop
  - Generating, Compiling and Linking
  - An Example
- CORBA_Environment C Structure
  - Top of chapter
  - C Structure
  - The CORBA Compatibility Part
  - The External Part
  - The Internal Part
  - Creating and Initiating the CORBA_Environment Structure
  - Setting System Exceptions
- IDL to Java language Mapping
  - Top of chapter
  - Introduction
  - Specialties in the Mapping
  - Basic OMG IDL Types
  - Constructed OMG IDL Types
  - Mapping for Constants
  - Invocations of Operations
  - Exceptions
  - Access to Attributes
  - Summary of Argument/Result Passing for Java
  - Communication Toolbox
  - The Package com.ericsson.otp.ic
  - The Term Class
  - Stub File Types
  - Client Stub Initialization, Methods Exported
  - Server Skeleton Initialization, Server Stub Implementation, Methods Exported
  - A Mapping Example
  - Running the Compiled Code

# 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 :

- Create the corresponding Erlang file that will hold the Erlang implementation of the IDL definitions.
- Call the implementation file after the scope of the IDL interface, followed by the suffix _impl.
- Export the implementation functions.

For each function defined in the IDL interface :

- Implement an Erlang function that uses as arguments in the same order, as the input arguments described in the IDL file, and returns the value described in the interface.
- When using the function, follow the mapping described in chapter 2.

### 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:

- Administrative - used when, for example, creating and terminating the server.
- IDL dependent - operations defined in the IDL specification. In this case, produce and init.

#### Administrative Operations

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

- oe_create/0/1/2 - create a new instance of the object. Accepts 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_link/0/1/2 - similar to oe_create/0/1/2, but create a linked server.
- typeID/0 - returns the scooped id compliant with the OMG standard. In this case the string "IDL:rmod/random:1.0".
- stop/1 - asynchronously terminate the server. The required argument is the return value from any of the start functions.

#### 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.

- init(ServerReference, Seed1, Seed2, Seed3) - initialize the random number generator.
- produce(ServerReference) - generate a new random number.

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.

#### 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>