FSM advantages?

Vance Shipley vances@REDACTED
Wed Jan 22 20:54:06 CET 2003


Eduardo,

The function gen_fsm:sync_send_event will fail when the timeout
is reached.  Take this example:

-module(test_fsm).
-behaviour(gen_fsm).
-export([start/0, init/1, one/3, two/3]).


start() ->
   gen_fsm:start_link(test_fsm, [], []).

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

one(Event, From, State) ->
   {reply, one_two, two, State}.

two(Event, From, State) ->
   {next_state, one, State}.


Here the state handler for state "one" will return a reply.  The second 
state will not.  This is because the return for state one is {reply,..}
while the return for state two is {next_state,...} which does not return
a reply but simply transitions to the next state.


1> {ok, Pid} = test_fsm:start().
{ok,<0.30.0>}
2> gen_fsm:sync_send_event(Pid, foo).
one_two
3> gen_fsm:sync_send_event(Pid, foo).
** exited: {timeout,{gen_fsm,sync_send_event,[<0.30.0>,foo]}} **


In the second case the default timeout (5000) took place and then the
function failed.  The exit value was {timeout,...}.

To use this you either let the crash occur and use the system supervision
principles or you wrap the call to gen_fsm:sync_send_event with a catch:

case catch gen_fsm:sync_send_event(Pid, foo) of
	{'EXIT',{timeout, _} ->
		...
	Other ->
		...
end,


	-Vance



	-Vance




More information about the erlang-questions mailing list