1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Using the Erlang Generic Server Back-end</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="5"><!-- Empty --></A>
<H2>5 Using the Erlang Generic Server Back-end</H2>
<A NAME="5.1"><!-- Empty --></A>
<H3>5.1 Introduction</H3>
<P> 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 <CODE>gen_server</CODE>. Orber's User's Guide
contain a more detailed description of IDL to Erlang mapping.
<A NAME="5.2"><!-- Empty --></A>
<H3>5.2 Compiling the Code</H3>
<P>The <CODE>ic:gen/2</CODE> function can be called from the command
line as follows:
<P>
<PRE>
shell> erlc "+{be, erl_genserv}" MyFile.idl
</PRE>
<A NAME="5.3"><!-- Empty --></A>
<H3>5.3 Writing the Implementation File</H3>
<P> For each IDL interface <CODE><interface name></CODE> defined in the IDL file :
<P>
<UL>
<LI>
Create the coresponding Erlang file that will hold the
Erlang implementation of the IDL definitions.
</LI>
<LI>
Call the implementation file after the scope of the IDL interface,
followed by the suffix <CODE>_impl</CODE>.
</LI>
<LI>
Export the implementation functions.
</LI>
</UL>
<P> For each function defined in the IDL interface :
<P>
<UL>
<LI>
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.
</LI>
<LI>
When using the function, follow the mapping described in chapter 2.
</LI>
</UL>
<A NAME="5.4"><!-- Empty --></A>
<H3>5.4 An Example</H3>
<P>In this example, a file <CODE>random.idl</CODE> generates code for the erlang
gen_server back-end:
<PRE>
// 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);
};
};
</PRE>
<P> When the file "random.idl" is compiled (e.g., <CODE>shell> erlc "+{be, erl_genserv}" random.idl</CODE>)
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
<CODE>rmod_random.erl</CODE> is the most important. This module exports two kinds of
operations:
<P>
<UL>
<LI>
<STRONG>Administrative</STRONG> - used when, for example, creating and
terminating the server.
</LI>
<LI>
<STRONG>IDL dependent</STRONG> - operations defined in the IDL
specification. In this case, <CODE>produce</CODE> and <CODE>init</CODE>.
</LI>
</UL>
<A NAME="5.4.1"><!-- Empty --></A>
<H4>5.4.1 Administrative Operations</H4>
<P>To create a new server instance, one of the following functions should
be used:
<P>
<UL>
<LI>
<STRONG>oe_create/0/1/2</STRONG> - create a new instance of the object.
Accepts <CODE>Env</CODE> and <CODE>RegName</CODE>, 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
<CODE>gen_server</CODE> parameter <CODE>ServerName</CODE>. If <CODE>Env</CODE> is
left out, an empty list will be passed.
</LI>
<LI>
<STRONG>oe_create_link/0/1/2</STRONG> - similar to <CODE>oe_create/0/1/2</CODE>,
but create a linked server.
</LI>
<LI>
<STRONG>typeID/0</STRONG> - returns the scooped id compliant with the
OMG standard. In this case the string
<CODE>"IDL:rmod/random:1.0"</CODE>.
</LI>
<LI>
<STRONG>stop/1</STRONG> - asynchronously terminate the server. The required
argument is the return value from any of the start functions.
</LI>
</UL>
<A NAME="5.4.2"><!-- Empty --></A>
<H4>5.4.2 IDL Dependent Operations</H4>
<P>Operations can either be synchronous or asynchronous
(i.e., <CODE>oneway</CODE>). These are, respectively, mapped to
<CODE>gen_server:call/2/3</CODE> and <CODE>gen_server:cast/2</CODE>.
Consult the <CODE>gen_server</CODE> documentation for valid return values.
<P>The IDL dependent operations in this example are listed below.
The first argument must be the whatever the create operation returned.
<P>
<UL>
<LI>
<STRONG>init(ServerReference, Seed1, Seed2, Seed3)</STRONG> - initialize
the random number generator.
</LI>
<LI>
<STRONG>produce(ServerReference)</STRONG> - generate a new random number.
</LI>
</UL>
<P>If the compile option <CODE>timeout</CODE> is used a timeout must be added
(e.g., <CODE>produce(ServerReference, 5000)</CODE>). For more information, see
the <CODE>gen_server</CODE> documentation.<A NAME="5.4.3"><!-- Empty --></A>
<H4>5.4.3 Implementation Module</H4>
<P>The implementation module shall, unless the compile option
<CODE>impl</CODE> is used, be named <CODE>rmod_random_impl.erl</CODE>.
and could look like this:
<PRE>
-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.
</PRE>
<P>Compile the code and run the example:
<PRE>
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>
</PRE>
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|