[erlang-questions] gen_fsm and keepalive timer

Konstantin Tcepliaev f355@REDACTED
Thu Dec 29 19:16:54 CET 2011


Hello Ivan,

Should the timer expire, it'll call the current state's callback function with {timeout, Ref, Msg} as its first argument.
So, basically, you need to write a clause for each of your states' callbacks which will match on that tuple, send a keepalive, and restart the timer.
You could also save the reference returned by start_timer/2 in your StateData and match on it in the tuple, to be sure that you receive the correct message.

Also, if you don't care in which state you are when the timeout occurs, you can use erlang:send_after/3 to send a message to yourself and deal with it in your handle_info/3 callback, sending keepalive and calling erlang:send_after/3 again. That's a bit hacky, but I still sometimes prefer that method over gen_fsm:start_timer/2 for simplicity reasons.

Hope this helps,
-- 
Konstantin

29.12.2011, 21:51, "Ivan Ostres" <iostres@REDACTED>:
> Hi all!
>
> I am trying to implement FSM of a protocol inside gen_fsm behavior. For
> a keepalive purpose I need a timer that would produce event when timer
> is expired so I could know when to send a keepalive packet and to know
> when holdtime is gone (I didn;t receive keepalive packet from other
> side). I was trying to get more info on gen_fsm:start_timer but
> documentation is a bit sparse - it's clear how to create timer but not
> how to catch it's event (well, at least to me since this is first time I
> am using gen_fsm).
>
> If someone could explain this in few sentences I would be very grateful.
> Also if there is a better way for keepalive mechanism inside gen_fsm, I
> would like to know it.
>
> Thanks and regards,
> Ivan Ostres
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list