[erlang-questions] gen_fsm - timeout ?

Serge Aleynikov <>
Tue Sep 19 20:29:28 CEST 2006


Sanjaya,

Aside from calling erlang:send_after/3, and gen_fsm:send_event_after/2, 
as mentioned by Chandru and Sean in prior replies, there's an 
undocumented trick for this already built into the OTP:

% inet:start_timer/1 creates a new timer based on TimeOut interval
Timer = inet:start_timer(TimeOut),

% inet:timeout/1 returns the remaining time left given the
% timer reference
TimeOut = inet:timeout(Timer),

% inet:stop_timer/1 cancels the timer
inet:stop_timer(Timer),


Example:

'open_state'(OtherEvent, #state{timer = Timer} = StateData) ->
     TimeOut = inet:timeout (Timer),
     ....
     {next_state, 'open', StateData, TimeOut}.

Regards,

Serge

Sanjaya Vitharana wrote:
> Hi ... ALL,
>  
> Behavior: Gen FSM
>  
> Scenario:
> Want to be in state "open" (see below) for limited time (assume 5 sec) 
> until some EXPECTED event (as_E) comes, have to move to some other state 
> if the expected event not received in that time. All other events should 
> be ignored in state "open".
>  
> Problem: Below works fine if no events other than expected comes. But 
> what happend if SOME un-expected events received before timeout? It 
> always keep updating the timeout value to 5000. Removing the timeout 
> value from {next_state, open, State,5000} for "OtherEvents" will cause 
> to stuck in the "open" forever if the expected event not received.
>  
> Question: How the time out value should be use in this case ? Are there 
> any way to set the timeout value ONCE for all "OtherEvents".
>  
> -------------------------------------------
> Asume this as beginning point
>  
>    as(blah,blah), %%Calling Async Func
> {next_state, open, State,5000};
> -------------------------------------------
>  
> open(timeout, State) ->
>     do_something,
>     {next_state, Any_Appropriat, State}.
>  
> open(as_E, State) ->
>     do_something,
>     {next_state, Any_Appropriat, State}.
>  
> open(OtherEvents, State) ->
>      {next_state, open, State,5000}.
>  
> Thanks in advance
>  
> Sanjaya Vitharana
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions


-- 
Serge Aleynikov
R&D Telecom, MIS, IDT Corp
Tel: +1 (973) 438-3436
Fax: +1 (973) 438-1464



More information about the erlang-questions mailing list