[erlang-questions] Question about gen_fsm timeout

Bin Wang wbin00@REDACTED
Tue Oct 22 04:37:28 CEST 2013


There is an locked door example aboout gen_fsm in the Elrang Otp System
Documentation.  I have a question about timeout. I will copy the code here
first:

-module(code_lock).
-behaviour(gen_fsm).
-export([start_link/1]).
-export([button/1]).
-export([init/1, locked/2, open/2]).

start_link(Code) ->
    gen_fsm:start_link({local, code_lock}, code_lock, lists:reverse(Code,
[]).

button(Digit) ->
    gen_fsm:send_event(code_lock, {button, Digit}).

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

locked({button, Digit}, {SoFar, Code}) ->
    case [Digit|SoFar] of
    Code ->
        do_unlock(),
        {next_state, open, {[], Code}, 30000};
    Incomplete when length(Incomplete)<length(Code) ->
        {next_state, locked, {Incomplete, Code}};
    _Wrong ->
        {next_state, locked, {[], Code}}
    end.

open(timeout, State) ->
    do_lock(),
    {next_state, locked, State}.

Here is the question: when the door is opened, if I press the button, the
gen_fsm will have an {button, Digit} event at the state open. An error will
occurs. But if I add these code after open function:

open(_Event, State) ->
   {next_state, open, State}.

Then if I press the button in 30s, the timeout will not be occurs. The door
will open forever. What should I do?

Thanks.

---
Bin Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131022/cfa9b262/attachment.htm>


More information about the erlang-questions mailing list