release handler and behaviour

Wiger Ulf <>
Sat Dec 28 21:05:43 CET 2002


The part of the OTP documentation that describes behaviours is:

http://www.erlang.org/doc/r9b/doc/design_principles/des_princ.html#1.1

If you want to make a fault tolerant application, the supervisor behaviour
is an essential component. The above document also talks about building
supervision trees. The reference manual for the 'supervisor' module (part of
stdlib) is also important.

One of the most common behaviours is gen_server, the generic client-server
behaviour.
Here's a slightly simplified version of gen_server. Perhaps it will clarify
how the callbacks are used. It lacks a few of the gen_server features (see
erl -man gen_server), but behaves essentially like gen_server does. The most
notable omission is the support for code change.

-module(simple_server).

-export([start_link/3, call/2, call/3, cast/2]).

start_link({local, Name}, Module, Argument) ->
   Parent = self(),
   proc_lib:start_link(fun() ->
                           init(Name, Module, Argument, Parent)
                       end).

call(Server, Request) ->
   call(Server, Request, 5000).

call(Server, Request, Timeout) ->
   Ref = erlang:monitor(process, Server),
   Server ! {'$gen_call', {self(), Ref}, Request),
   receive
      {Ref, Reply} ->
         erlang:demonitor(Ref),
         Reply;
      {'DOWN', process, Ref, Reason} ->
         exit(timeout)   % If memory serves me - too lazy to check
   after Timeout ->
      exit(timeout)
   end.

cast(Server, Msg) ->
   Server ! {'$gen_cast', Msg},
   ok.

init(Name, Module, Argument, Parent) ->
   register(Name, self()),
   case catch Module:init(Argument) of
      {'EXIT', Reason} ->
         terminate(Module, Reason, []);
      {ok, State} ->
         main_loop(Module, Parent, State);
      Other ->
         terminate(Module, {bad_return, Other}, [])
   end.

main_loop(Module, Parent, State) ->
   receive
      {'EXIT', Parent, Reason} ->     % Note: the *Parent* exited
         terminate(Module, Reason, State);
      {'$gen_call', {Pid, Ref}=From, Request} ->
         case catch Module:handle_call(Request, From, State) of
            {'EXIT', Reason} ->
               terminate(Module, Reason, State);
            {reply, Reply, NewState} ->
               Pid ! {Ref, Reply},
               main_loop(Module, Parent, NewState);
            {noreply, NewState} ->
               main_loop(Module, Parent, NewState);
            {stop, Reason, NewState} ->
               terminate(Module, Reason, NewState};
            {stop, Reply, Reason, NewState} ->
               Pid ! {Ref, Reply},
               terminate(Module, Reason, NewState)
         end;
      {'$gen_cast', Msg} ->
         case catch Module:handle_cast(Msg, State) of
            {'EXIT', Reason} ->
               terminate(Module, Reason, State);
            {noreply, NewState} ->
               main_loop(Module, Parent, NewState);
            {stop, Reason, NewState} ->
               terminate(Module, Reason, NewState)
         end;
      Msg ->
         case catch Module:handle_info(Msg, State) of
            {'EXIT', Reason} ->
               terminate(Module, Reason, State);
            {noreply, NewState} ->
               main_loop(Module, Parent, NewState);
            {stop, Reason, NewState} ->
               terminate(Module, Reason, NewState)
         end
   end.

terminate(Module, Reason, State) ->
   catch Module:terminate(Reason, State),
   exit(Reason).




----- Original Message -----
From: "Suresh S" <>
To: "Wiger Ulf" <>
Cc: <>
Sent: den 28 december 2002 16:04
Subject: release handler and behaviour


> Hi Uffe Wiger,
>
> I have not used any behaviour in my application,
> i am getting confused to make user of behaviour
> and all the stuff related to call_back
>
> Where do i get more info on behaviours and is
> supervisor module is necessery for a release ?
> i really want to include this module in my
> application,
> because i want to make a fault taularent applicatin
>
> very first statement of my .app file is throughing a
> parse error
>
> Thanking u
>
> with regards
> s suresh
>
>
> > Hi Suresh,
> >
> > The parser most likely stumbles on the 'modules'
> > list in your app file. The
> > modules are given as authentication.erl, which is
> > not a valid atom. You
> > should remove the ".erl" part from the module names.
> > You can check your .app
> > file from an erlang shell by calling
> > file:consult("roamapp.app").
> >
> > /Uffe
> >
> > ----- Original Message -----
> > From: "Suresh S" <>
> > To: <>
> > Cc: "Ulf Wiger" <>
> > Sent: den 24 december 2002 06:55
> >
> >
> > > Hi Ulf Wiger,
> > > After Going Through ur work "OTP Release Handling
> > > Tutorial" i tried to work on packaging of my
> > > application,
> > >
> > > while making  script it is giving the error for
> > very
> > > first erlang term in the .app file,
> > >
> > > what may be the problem .
> > >
> > > Eshell V5.0.1.1  (abort with ^G)
> > > 1> Dir = "/home/suresh/InRoam-1.0/RoamApp".
> > > "/home/suresh/InRoam-1.0/RoamApp"
> > > 2> Path = [Dir ++ "/lib/*/ebin"].
> > > ["/home/suresh/InRoam-1.0/RoamApp/lib/*/ebin"]
> > > 3>  Var = {"RoamApp", Dir}.
> > > {"RoamApp","/home/suresh/InRoam-1.0/RoamApp"}
> > > 4>
> > >
> >
> systools:make_script("start_roamapp",[{path,Path},{variables,[Var]}]).
> > > roamapp: Parse error in file:
> > >
> >
> "/home/suresh/InRoam-1.0/RoamApp/lib/roamapp-1.0.1/ebin/roamapp.app".
> > >  Line: 1  Error: "bad term";
> > >
> > > error
> > > 5>
> > >
> > > This is my app file
> > > -------------------
> > >
> > > {application, roamapp,
> > >  [{description, "Application for Call Processing
> > of a
> > > Roamer"},
> > >   {vsn, "1.0.1"},
> > >   {modules,   [ authentication,
> > >                 inroam_trigger,
> > >                 timefun,
> > >                 importdata,
>
> > >                 roamapp]},
> > >   {registered,[ transceiver,
> > >                 keepaliveagent,
> > >                 keepalivemanager,
> > >                 supervise_process,
> > >                 Table_name,
> > >                 manager,
> > >                 agent,
> > >                 recTrigMsg,
> > >                 trap_msg]},
> > >   {applications, [kernel,stdlib]},
> > >   {mod, {roamapp,[]}}]}.
> > >
> > > Thanking u
> > >
> > > suresh s
> > >
> > >
> > >
> > >
> >
> ________________________________________________________________________
> > > Missed your favourite TV serial last night? Try
> > the new, Yahoo! TV.
> > >        visit http://in.tv.yahoo.com
> >
>
> ________________________________________________________________________
> Missed your favourite TV serial last night? Try the new, Yahoo! TV.
>        visit http://in.tv.yahoo.com




More information about the erlang-questions mailing list