Created
February 19, 2012 15:05
-
-
Save garrensmith/1864199 to your computer and use it in GitHub Desktop.
Train Server
This file contains hidden or 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(train). | |
| -behaviour(gen_fsm). | |
| %public api | |
| -export([start_link/0, start_engine/0, move/0, stop/0, stop_server/0, slow/0, hit_the_break/0, open_doors/0, close_doors/0]). | |
| % gsm api | |
| -export([init/1, handle_event/3, handle_sync_event/4, handle_info/3, terminate/3, code_change/4]). | |
| % state api | |
| -export([stopped/2, started/2, full_speed/2, slow_down/2]). | |
| %start() -> gen_fsm:start(?MODULE, [], []). | |
| start_link() -> | |
| gen_fsm:start_link({local, train_server},?MODULE, [], []). | |
| start_engine() -> | |
| gen_fsm:send_event(train_server,{start_train}). | |
| move() -> | |
| gen_fsm:send_event(train_server,{accelerate}). | |
| slow() -> | |
| gen_fsm:send_event(train_server,{slow_down}). | |
| stop() -> | |
| gen_fsm:send_event(train_server,{stop}). | |
| open_doors() -> | |
| gen_fsm:send_all_state_event(train_server,open_doors). | |
| close_doors() -> | |
| gen_fsm:send_all_state_event(train_server,close_doors). | |
| hit_the_break() -> | |
| gen_fsm:send_all_state_event(train_server, emergency_break). | |
| stop_server() -> | |
| gen_fsm:send_all_state_event(train_server, stop). | |
| init(Args) -> | |
| io:format("init ~n"), | |
| {ok, stopped, Args}. | |
| stopped(timeout, State) -> | |
| io:format("we have stopped ~n"), | |
| {next_state, stopped, State}; | |
| stopped({start_train}, State) -> | |
| {next_state, started, State}; | |
| stopped(Event, State) -> | |
| unexpected(Event, State), | |
| {next_state, stopped, []}. | |
| started({accelerate}, State) -> | |
| io:format("starting engine ~n"), | |
| {next_state, full_speed, State, 1000}; | |
| started(Event, State) -> | |
| unexpected(Event, State), | |
| {next_state, started, []}. | |
| full_speed({slow_down}, State) -> | |
| io:format("slowing down ~n"), | |
| {next_state, slow_down, State}; | |
| full_speed(timeout, State) -> | |
| io:format("we have reached full speed ~n"), | |
| {next_state, full_speed, State}. | |
| slow_down({stop}, State) -> | |
| {next_state, stopped, State, 1000}; | |
| slow_down({accelerate}, State) -> | |
| {next_state, full_speed, State, 1000}; | |
| slow_down(_Event, State) -> | |
| io:format("slowing down ~n"), | |
| {next_state, slow_down, State}. | |
| handle_event(emergency_break, _StateName, StateData) -> | |
| {next_state, stopped, StateData, 1000}; | |
| handle_event(open_doors, StateName, StateData) -> | |
| io:format("doors opened ~n"), | |
| {next_state, StateName, StateData}; | |
| handle_event(close_doors, StateName, StateData) -> | |
| io:format("doors closed"), | |
| {next_state, StateName, StateData}; | |
| handle_event(stop, _StateName, StateData) -> | |
| {stop, normal, StateData}; | |
| handle_event(_Event, _StateName, StateData) -> | |
| io:format("event ~n"), | |
| {stop, unimplemented, StateData}. | |
| handle_sync_event(_Event, _From, _StName, StData) -> | |
| io:format("sync ~n"), | |
| {stop, unimplemented, StData}. | |
| handle_info(_Info, _StName, StData) -> | |
| io:format("info ~n"), | |
| {stop, unimplemented, StData}. | |
| terminate(_Reason, _StName, _StData) -> ok. | |
| code_change(_OldVsn, StName, StData, _Extra) -> {ok, StName, StData}. | |
| %% Unexpected allows to log unexpected messages | |
| unexpected(Msg, State) -> | |
| io:format("~p received unknown event ~p while in state ~p~n", | |
| [self(), Msg, State]). |
This file contains hidden or 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(train_supervisor). | |
| -behaviour(supervisor). | |
| -export([start_link/0]). | |
| -export([init/1]). | |
| start_link() -> | |
| supervisor:start_link({local,?MODULE}, ?MODULE, []). | |
| init(_Args) -> | |
| {ok, {{one_for_one, 3, 60}, | |
| [{server, | |
| {train, start_link, []}, permanent, 1000, worker, []} | |
| ]}}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment