Last active
November 29, 2015 14:03
-
-
Save ryuone/c720143b10cdf0984925 to your computer and use it in GitHub Desktop.
gen_fsm sample(すごいErlangゆかいに学ぼう! 15章)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(stupid_dog_fsm). | |
-author('ryuone<[email protected]>'). | |
-behaviour(gen_fsm). | |
-export([start_link/0]). | |
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3, terminate/3, code_change/4]). | |
-export([sit/3, bark/3, wag_tail/3]). | |
-export([get_sync_state/1]). | |
-export([test/0]). | |
-record(state, {name}). | |
start_link() -> | |
{ok, proc_lib:spawn_link(?MODULE, init, [self()])}. | |
init(_Args) -> | |
process_flag(trap_exit, true), | |
State = #state{name = ryuone}, | |
gen_fsm:enter_loop(?MODULE, [], sit, State). | |
handle_event(stop, _StateName, State) -> | |
{stop, normal, State}. | |
handle_sync_event(get_state, _From, StateName, State) -> | |
{reply, StateName, StateName, State}; | |
handle_sync_event(stop, _From, _StateName, State) -> | |
{stop, normal, error, State}; | |
handle_sync_event(Event, _From, StateName, State) -> | |
io:format("Event : ~p~n", [Event]), | |
{reply, StateName, StateName, State}. | |
handle_info(_Info, StateName, State) -> | |
{next_state, StateName, State}. | |
code_change(iOldVsn, StateName, State, _) -> | |
{ok, StateName, State}. | |
terminate(Reason, _StateName, _State) -> | |
io:format("terminate called with ~p~n", [Reason]), | |
ok. | |
%% | |
%% State | |
%% | |
sit(Event=see_squirrel, From, State) -> | |
io:format("Current state is sit. Event is ~p~n", [Event]), | |
gen_fsm:reply(From, " State will change to bark."), | |
{next_state, bark, State}. | |
bark(Event=get_petted, From, State) -> | |
io:format("Current state is bark. Event is ~p~n", [Event]), | |
gen_fsm:reply(From, " State will change to get_petted."), | |
{next_state, wag_tail, State}. | |
wag_tail(Event=wait, From, State) -> | |
io:format("Current state is wag_tail. Event is ~p~n", [Event]), | |
gen_fsm:reply(From, " State will change to bark."), | |
{next_state, bark, State}; | |
wag_tail(Event=get_petted, From, State) -> | |
io:format("Current state is wag_tail. Event is ~p~n", [Event]), | |
gen_fsm:reply(From, " State will change to sit."), | |
{next_state, sit, State}. | |
%% | |
%% stupid_dog_fsm functins. | |
%% | |
get_sync_state(Pid) -> | |
gen_fsm:sync_send_all_state_event(Pid, get_state). | |
%% | |
%% Test function. | |
%% | |
test() -> | |
{ok, Pid} = start_link(), | |
Res = gen_fsm:sync_send_all_state_event(Pid, get_state), | |
% * Current state : sit | |
io:format("* Current state : ~p~n", [Res]), | |
Ret1 = gen_fsm:sync_send_event(Pid, see_squirrel), | |
% Current state is sit. Event is see_squirrel | |
% State will change to bark. | |
io:format("~s~n", [Ret1]), | |
Ret2 = gen_fsm:sync_send_event(Pid, get_petted), | |
% Current state is bark. Event is get_petted | |
% State will change to get_petted. | |
io:format("~s~n", [Ret2]), | |
Ret3 = gen_fsm:sync_send_event(Pid, get_petted), | |
% Current state is wag_tail. Event is get_petted | |
% State will change to sit. | |
io:format("~s~n", [Ret3]), | |
ok = gen_fsm:stop(Pid), | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment