Permalink
Browse files

Proof of concept implementation of handle_state/2

  • Loading branch information...
1 parent bcc9b5e commit 29e490960fb96a2e79d9789e223b5a39336e0ddf @yrashk committed Feb 3, 2011
Showing with 19 additions and 3 deletions.
  1. +19 −3 src/gen_fsm2.erl
View
@@ -131,7 +131,7 @@
-spec behaviour_info(atom()) -> 'undefined' | [{atom(), arity()}].
behaviour_info(callbacks) ->
- [{init,1},{handle_event,3},{handle_sync_event,4},{handle_info,3},
+ [{init,1},{handle_state, 2},{handle_event,3},{handle_sync_event,4},{handle_info,3},
{terminate,3},{code_change,4}];
behaviour_info(_Other) ->
undefined.
@@ -347,17 +347,19 @@ name(Pid) when is_pid(Pid) -> Pid.
%% The MAIN loop
%%-----------------------------------------------------------------
loop(Parent, Name, StateName, StateData, Mod, hibernate, Debug) ->
+ StateData1 = handle_state_handler(Mod, StateName, StateData),
proc_lib:hibernate(?MODULE,wake_hib,
- [Parent, Name, StateName, StateData, Mod,
+ [Parent, Name, StateName, StateData1, Mod,
Debug]);
loop(Parent, Name, StateName, StateData, Mod, Time, Debug) ->
+ StateData1 = handle_state_handler(Mod, StateName, StateData),
Msg = receive
Input ->
Input
after Time ->
{'$gen_event', timeout}
end,
- decode_msg(Msg,Parent, Name, StateName, StateData, Mod, Time, Debug, false).
+ decode_msg(Msg,Parent, Name, StateName, StateData1, Mod, Time, Debug, false).
wake_hib(Parent, Name, StateName, StateData, Mod, Debug) ->
Msg = receive
@@ -383,6 +385,20 @@ decode_msg(Msg,Parent, Name, StateName, StateData, Mod, Time, Debug, Hib) ->
end.
%%-----------------------------------------------------------------
+%% Handiling handle_state
+%%-----------------------------------------------------------------
+handle_state_handler(Mod, StateName, StateData) ->
+ case Mod:handle_state(StateName, StateData) of
+ ignore ->
+ StateData;
+ {ok, NStateData} ->
+ NStateData;
+ {stop, Reason, NStateData} ->
+ terminate(Reason, handle_state, undefined, Mod, StateName, NStateData, [])
+ end.
+
+
+%%-----------------------------------------------------------------
%% Callback functions for system messages handling.
%%-----------------------------------------------------------------
system_continue(Parent, Debug, [Name, StateName, StateData, Mod, Time]) ->

0 comments on commit 29e4909

Please sign in to comment.