[erlang-questions] Question about gen_fsm timeout

Shian Wu shian5@REDACTED
Tue Oct 22 06:28:32 CEST 2013


Hi

Your return of open(timeout, State) will clean the timeout value.
You may change

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

to

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

Another note, timeout will occur unless a request or a message is received.
If user keep to click button, it will not able to lock door ;p


---
Shian Wu



On Tue, Oct 22, 2013 at 10:37 AM, Bin Wang <wbin00@REDACTED> wrote:

> 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
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131022/b32a4e21/attachment.htm>


More information about the erlang-questions mailing list