.TH gen_fsm 3erl "stdlib 3.2" "Ericsson AB" "Erlang Module Definition" .SH NAME gen_fsm \- Generic finite state machine behavior. .SH DESCRIPTION .LP .RS -4 .B Note: .RE There is a new behaviour \fB\fIgen_statem\fR\&\fR\& that is intended to replace \fIgen_fsm\fR\& for new code\&. \fIgen_fsm\fR\& will not be removed for the foreseeable future to keep old state machine implementations running\&. .LP This behavior module provides a finite state machine\&. A generic finite state machine process (\fIgen_fsm\fR\&) implemented using this module has a standard set of interface functions and includes functionality for tracing and error reporting\&. It also fits into an OTP supervision tree\&. For more information, see \fBOTP Design Principles\fR\&\&. .LP A \fIgen_fsm\fR\& process assumes all specific parts to be located in a callback module exporting a predefined set of functions\&. The relationship between the behavior functions and the callback functions is as follows: .LP .nf gen_fsm module Callback module -------------- --------------- gen_fsm:start gen_fsm:start_link -----> Module:init/1 gen_fsm:stop -----> Module:terminate/3 gen_fsm:send_event -----> Module:StateName/2 gen_fsm:send_all_state_event -----> Module:handle_event/3 gen_fsm:sync_send_event -----> Module:StateName/3 gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 - -----> Module:handle_info/3 - -----> Module:terminate/3 - -----> Module:code_change/4 .fi .LP If a callback function fails or returns a bad value, the \fIgen_fsm\fR\& process terminates\&. .LP A \fIgen_fsm\fR\& process handles system messages as described in \fB\fIsys(3erl)\fR\&\fR\&\&. The \fIsys\fR\& module can be used for debugging a \fIgen_fsm\fR\& process\&. .LP Notice that a \fIgen_fsm\fR\& process does not trap exit signals automatically, this must be explicitly initiated in the callback module\&. .LP Unless otherwise stated, all functions in this module fail if the specified \fIgen_fsm\fR\& process does not exist or if bad arguments are specified\&. .LP The \fIgen_fsm\fR\& process can go into hibernation (see \fB\fIerlang:hibernate/3\fR\&\fR\&) if a callback function specifies \fI\&'hibernate\&'\fR\& instead of a time-out value\&. This can be useful if the server is expected to be idle for a long time\&. However, use this feature with care, as hibernation implies at least two garbage collections (when hibernating and shortly after waking up) and is not something you want to do between each call to a busy state machine\&. .SH EXPORTS .LP .B cancel_timer(Ref) -> RemainingTime | false .br .RS .LP Types: .RS 3 Ref = reference() .br RemainingTime = integer() .br .RE .RE .RS .LP Cancels an internal timer referred by \fIRef\fR\& in the \fIgen_fsm\fR\& process that calls this function\&. .LP \fIRef\fR\& is a reference returned from \fB\fIsend_event_after/2\fR\&\fR\& or \fB\fIstart_timer/2\fR\&\fR\&\&. .LP If the timer has already timed out, but the event not yet been delivered, it is cancelled as if it had \fInot\fR\& timed out, so there is no false timer event after returning from this function\&. .LP Returns the remaining time in milliseconds until the timer would have expired if \fIRef\fR\& referred to an active timer, otherwise \fIfalse\fR\&\&. .RE .LP .B enter_loop(Module, Options, StateName, StateData) .br .B enter_loop(Module, Options, StateName, StateData, FsmName) .br .B enter_loop(Module, Options, StateName, StateData, Timeout) .br .B enter_loop(Module, Options, StateName, StateData, FsmName, Timeout) .br .RS .LP Types: .RS 3 Module = atom() .br Options = [Option] .br Option = {debug,Dbgs} .br Dbgs = [Dbg] .br Dbg = trace | log | statistics .br | {log_to_file,FileName} | {install,{Func,FuncState}} .br StateName = atom() .br StateData = term() .br FsmName = {local,Name} | {global,GlobalName} .br | {via,Module,ViaName} .br Name = atom() .br GlobalName = ViaName = term() .br Timeout = int() | infinity .br .RE .RE .RS .LP Makes an existing process into a \fIgen_fsm\fR\& process\&. Does not return, instead the calling process enters the \fIgen_fsm\fR\& receive loop and becomes a \fIgen_fsm\fR\& process\&. The process \fImust\fR\& have been started using one of the start functions in \fB\fIproc_lib(3erl)\fR\&\fR\&\&. The user is responsible for any initialization of the process, including registering a name for it\&. .LP This function is useful when a more complex initialization procedure is needed than the \fIgen_fsm\fR\& behavior provides\&. .LP \fIModule\fR\&, \fIOptions\fR\&, and \fIFsmName\fR\& have the same meanings as when calling \fB\fIstart[_link]/3,4\fR\&\fR\&\&. However, if \fIFsmName\fR\& is specified, the process must have been registered accordingly \fIbefore\fR\& this function is called\&. .LP \fIStateName\fR\&, \fIStateData\fR\&, and \fITimeout\fR\& have the same meanings as in the return value of \fB\fIModule:init/1\fR\&\fR\&\&. The callback module \fIModule\fR\& does not need to export an \fIinit/1\fR\& function\&. .LP The function fails if the calling process was not started by a \fIproc_lib\fR\& start function, or if it is not registered according to \fIFsmName\fR\&\&. .RE .LP .B reply(Caller, Reply) -> Result .br .RS .LP Types: .RS 3 Caller - see below .br Reply = term() .br Result = term() .br .RE .RE .RS .LP This function can be used by a \fIgen_fsm\fR\& process to explicitly send a reply to a client process that called \fB\fIsync_send_event/2,3\fR\&\fR\& or \fB\fIsync_send_all_state_event/2,3\fR\&\fR\& when the reply cannot be defined in the return value of \fB\fIModule:StateName/3\fR\&\fR\& or \fB\fIModule:handle_sync_event/4\fR\&\fR\&\&. .LP \fICaller\fR\& must be the \fIFrom\fR\& argument provided to the callback function\&. \fIReply\fR\& is any term given back to the client as the return value of \fIsync_send_event/2,3\fR\& or \fIsync_send_all_state_event/2,3\fR\&\&. .LP Return value \fIResult\fR\& is not further defined, and is always to be ignored\&. .RE .LP .B send_all_state_event(FsmRef, Event) -> ok .br .RS .LP Types: .RS 3 FsmRef = Name | {Name,Node} | {global,GlobalName} .br | {via,Module,ViaName} | pid() .br Name = Node = atom() .br GlobalName = ViaName = term() .br Event = term() .br .RE .RE .RS .LP Sends an event asynchronously to the \fIFsmRef\fR\& of the \fIgen_fsm\fR\& process and returns \fIok\fR\& immediately\&. The \fIgen_fsm\fR\& process calls \fB\fIModule:handle_event/3\fR\&\fR\& to handle the event\&. .LP For a description of the arguments, see \fB\fIsend_event/2\fR\&\fR\&\&. .LP The difference between \fIsend_event/2\fR\& and \fIsend_all_state_event/2\fR\& is which callback function is used to handle the event\&. This function is useful when sending events that are handled the same way in every state, as only one \fIhandle_event\fR\& clause is needed to handle the event instead of one clause in each state name function\&. .RE .LP .B send_event(FsmRef, Event) -> ok .br .RS .LP Types: .RS 3 FsmRef = Name | {Name,Node} | {global,GlobalName} .br | {via,Module,ViaName} | pid() .br Name = Node = atom() .br GlobalName = ViaName = term() .br Event = term() .br .RE .RE .RS .LP Sends an event asynchronously to the \fIFsmRef\fR\& of the \fIgen_fsm\fR\& process and returns \fIok\fR\& immediately\&. The \fIgen_fsm\fR\& process calls \fB\fIModule:StateName/2\fR\&\fR\& to handle the event, where \fIStateName\fR\& is the name of the current state of the \fIgen_fsm\fR\& process\&. .LP \fIFsmRef\fR\& can be any of the following: .RS 2 .TP 2 * The pid .LP .TP 2 * \fIName\fR\&, if the \fIgen_fsm\fR\& process is locally registered .LP .TP 2 * \fI{Name,Node}\fR\&, if the \fIgen_fsm\fR\& process is locally registered at another node .LP .TP 2 * \fI{global,GlobalName}\fR\&, if the \fIgen_fsm\fR\& process is globally registered .LP .TP 2 * \fI{via,Module,ViaName}\fR\&, if the \fIgen_fsm\fR\& process is registered through an alternative process registry .LP .RE .LP \fIEvent\fR\& is any term that is passed as one of the arguments to \fIModule:StateName/2\fR\&\&. .RE .LP .B send_event_after(Time, Event) -> Ref .br .RS .LP Types: .RS 3 Time = integer() .br Event = term() .br Ref = reference() .br .RE .RE .RS .LP Sends a delayed event internally in the \fIgen_fsm\fR\& process that calls this function after \fITime\fR\& milliseconds\&. Returns immediately a reference that can be used to cancel the delayed send using \fB\fIcancel_timer/1\fR\&\fR\&\&. .LP The \fIgen_fsm\fR\& process calls \fB\fIModule:StateName/2\fR\&\fR\& to handle the event, where \fIStateName\fR\& is the name of the current state of the \fIgen_fsm\fR\& process at the time the delayed event is delivered\&. .LP \fIEvent\fR\& is any term that is passed as one of the arguments to \fIModule:StateName/2\fR\&\&. .RE .LP .B start(Module, Args, Options) -> Result .br .B start(FsmName, Module, Args, Options) -> Result .br .RS .LP Types: .RS 3 FsmName = {local,Name} | {global,GlobalName} .br | {via,Module,ViaName} .br Name = atom() .br GlobalName = ViaName = term() .br Module = atom() .br Args = term() .br Options = [Option] .br Option = {debug,Dbgs} | {timeout,Time} | {spawn_opt,SOpts} .br Dbgs = [Dbg] .br Dbg = trace | log | statistics .br | {log_to_file,FileName} | {install,{Func,FuncState}} .br SOpts = [term()] .br Result = {ok,Pid} | ignore | {error,Error} .br Pid = pid() .br Error = {already_started,Pid} | term() .br .RE .RE .RS .LP Creates a standalone \fIgen_fsm\fR\& process, that is, a process that is not part of a supervision tree and thus has no supervisor\&. .LP For a description of arguments and return values, see \fB\fIstart_link/3,4\fR\&\fR\&\&. .RE .LP .B start_link(Module, Args, Options) -> Result .br .B start_link(FsmName, Module, Args, Options) -> Result .br .RS .LP Types: .RS 3 FsmName = {local,Name} | {global,GlobalName} .br | {via,Module,ViaName} .br Name = atom() .br GlobalName = ViaName = term() .br Module = atom() .br Args = term() .br Options = [Option] .br Option = {debug,Dbgs} | {timeout,Time} | {spawn_opt,SOpts} .br Dbgs = [Dbg] .br Dbg = trace | log | statistics .br | {log_to_file,FileName} | {install,{Func,FuncState}} .br SOpts = [SOpt] .br SOpt - see erlang:spawn_opt/2,3,4,5 .br Result = {ok,Pid} | ignore | {error,Error} .br Pid = pid() .br Error = {already_started,Pid} | term() .br .RE .RE .RS .LP Creates a \fIgen_fsm\fR\& process as part of a supervision tree\&. The function is to be called, directly or indirectly, by the supervisor\&. For example, it ensures that the \fIgen_fsm\fR\& process is linked to the supervisor\&. .LP The \fIgen_fsm\fR\& process calls \fB\fIModule:init/1\fR\&\fR\& to initialize\&. To ensure a synchronized startup procedure, \fIstart_link/3,4\fR\& does not return until \fIModule:init/1\fR\& has returned\&. .RS 2 .TP 2 * If \fIFsmName={local,Name}\fR\&, the \fIgen_fsm\fR\& process is registered locally as \fIName\fR\& using \fIregister/2\fR\&\&. .LP .TP 2 * If \fIFsmName={global,GlobalName}\fR\&, the \fIgen_fsm\fR\& process is registered globally as \fIGlobalName\fR\& using \fB\fIglobal:register_name/2\fR\&\fR\&\&. .LP .TP 2 * If \fIFsmName={via,Module,ViaName}\fR\&, the \fIgen_fsm\fR\& process registers with the registry represented by \fIModule\fR\&\&. The \fIModule\fR\& callback is to export the functions \fIregister_name/2\fR\&, \fIunregister_name/1\fR\&, \fIwhereis_name/1\fR\&, and \fIsend/2\fR\&, which are to behave like the corresponding functions in \fB\fIglobal\fR\&\fR\&\&. Thus, \fI{via,global,GlobalName}\fR\& is a valid reference\&. .LP .RE .LP If no name is provided, the \fIgen_fsm\fR\& process is not registered\&. .LP \fIModule\fR\& is the name of the callback module\&. .LP \fIArgs\fR\& is any term that is passed as the argument to \fIModule:init/1\fR\&\&. .LP If option \fI{timeout,Time}\fR\& is present, the \fIgen_fsm\fR\& process is allowed to spend \fITime\fR\& milliseconds initializing or it terminates and the start function returns \fI{error,timeout}\fR\&\&. .LP If option \fI{debug,Dbgs}\fR\& is present, the corresponding \fIsys\fR\& function is called for each item in \fIDbgs\fR\&; see \fB\fIsys(3erl)\fR\&\fR\&\&. .LP If option \fI{spawn_opt,SOpts}\fR\& is present, \fISOpts\fR\& is passed as option list to the \fIspawn_opt\fR\& BIF that is used to spawn the \fIgen_fsm\fR\& process; see \fB\fIspawn_opt/2\fR\&\fR\&\&. .LP .RS -4 .B Note: .RE Using spawn option \fImonitor\fR\& is not allowed, it causes the function to fail with reason \fIbadarg\fR\&\&. .LP If the \fIgen_fsm\fR\& process is successfully created and initialized, the function returns \fI{ok,Pid}\fR\&, where \fIPid\fR\& is the pid of the \fIgen_fsm\fR\& process\&. If a process with the specified \fIFsmName\fR\& exists already, the function returns \fI{error,{already_started,Pid}}\fR\&, where \fIPid\fR\& is the pid of that process\&. .LP If \fIModule:init/1\fR\& fails with \fIReason\fR\&, the function returns \fI{error,Reason}\fR\&\&. If \fIModule:init/1\fR\& returns \fI{stop,Reason}\fR\& or \fIignore\fR\&, the process is terminated and the function returns \fI{error,Reason}\fR\& or \fIignore\fR\&, respectively\&. .RE .LP .B start_timer(Time, Msg) -> Ref .br .RS .LP Types: .RS 3 Time = integer() .br Msg = term() .br Ref = reference() .br .RE .RE .RS .LP Sends a time-out event internally in the \fIgen_fsm\fR\& process that calls this function after \fITime\fR\& milliseconds\&. Returns immediately a reference that can be used to cancel the timer using \fB\fIcancel_timer/1\fR\&\fR\&\&. .LP The \fIgen_fsm\fR\& process calls \fB\fIModule:StateName/2\fR\&\fR\& to handle the event, where \fIStateName\fR\& is the name of the current state of the \fIgen_fsm\fR\& process at the time the time-out message is delivered\&. .LP \fIMsg\fR\& is any term that is passed in the time-out message, \fI{timeout, Ref, Msg}\fR\&, as one of the arguments to \fIModule:StateName/2\fR\&\&. .RE .LP .B stop(FsmRef) -> ok .br .B stop(FsmRef, Reason, Timeout) -> ok .br .RS .LP Types: .RS 3 FsmRef = Name | {Name,Node} | {global,GlobalName} .br | {via,Module,ViaName} | pid() .br Node = atom() .br GlobalName = ViaName = term() .br Reason = term() .br Timeout = int()>0 | infinity .br .RE .RE .RS .LP Orders a generic finite state machine to exit with the specified \fIReason\fR\& and waits for it to terminate\&. The \fIgen_fsm\fR\& process calls \fB\fIModule:terminate/3\fR\&\fR\& before exiting\&. .LP The function returns \fIok\fR\& if the generic finite state machine terminates with the expected reason\&. Any other reason than \fInormal\fR\&, \fIshutdown\fR\&, or \fI{shutdown,Term}\fR\& causes an error report to be issued using \fB\fIerror_logger:format/2\fR\&\fR\&\&. The default \fIReason\fR\& is \fInormal\fR\&\&. .LP \fITimeout\fR\& is an integer greater than zero that specifies how many milliseconds to wait for the generic FSM to terminate, or the atom \fIinfinity\fR\& to wait indefinitely\&. The default value is \fIinfinity\fR\&\&. If the generic finite state machine has not terminated within the specified time, a \fItimeout\fR\& exception is raised\&. .LP If the process does not exist, a \fInoproc\fR\& exception is raised\&. .RE .LP .B sync_send_all_state_event(FsmRef, Event) -> Reply .br .B sync_send_all_state_event(FsmRef, Event, Timeout) -> Reply .br .RS .LP Types: .RS 3 FsmRef = Name | {Name,Node} | {global,GlobalName} .br | {via,Module,ViaName} | pid() .br Name = Node = atom() .br GlobalName = ViaName = term() .br Event = term() .br Timeout = int()>0 | infinity .br Reply = term() .br .RE .RE .RS .LP Sends an event to the \fIFsmRef\fR\& of the \fIgen_fsm\fR\& process and waits until a reply arrives or a time-out occurs\&. The \fIgen_fsm\fR\& process calls \fB\fIModule:handle_sync_event/4\fR\&\fR\& to handle the event\&. .LP For a description of \fIFsmRef\fR\& and \fIEvent\fR\&, see \fBsend_event/2\fR\&\&. For a description of \fITimeout\fR\& and \fIReply\fR\&, see \fB\fIsync_send_event/3\fR\&\fR\&\&. .LP For a discussion about the difference between \fIsync_send_event\fR\& and \fIsync_send_all_state_event\fR\&, see \fB\fIsend_all_state_event/2\fR\&\fR\&\&. .RE .LP .B sync_send_event(FsmRef, Event) -> Reply .br .B sync_send_event(FsmRef, Event, Timeout) -> Reply .br .RS .LP Types: .RS 3 FsmRef = Name | {Name,Node} | {global,GlobalName} .br | {via,Module,ViaName} | pid() .br Name = Node = atom() .br GlobalName = ViaName = term() .br Event = term() .br Timeout = int()>0 | infinity .br Reply = term() .br .RE .RE .RS .LP Sends an event to the \fIFsmRef\fR\& of the \fIgen_fsm\fR\& process and waits until a reply arrives or a time-out occurs\&. \fIThe gen_fsm\fR\& process calls \fB\fIModule:StateName/3\fR\&\fR\& to handle the event, where \fIStateName\fR\& is the name of the current state of the \fIgen_fsm\fR\& process\&. .LP For a description of \fIFsmRef\fR\& and \fIEvent\fR\&, see \fB\fIsend_event/2\fR\&\fR\&\&. .LP \fITimeout\fR\& is an integer greater than zero that specifies how many milliseconds to wait for a reply, or the atom \fIinfinity\fR\& to wait indefinitely\&. Defaults to 5000\&. If no reply is received within the specified time, the function call fails\&. .LP Return value \fIReply\fR\& is defined in the return value of \fIModule:StateName/3\fR\&\&. .LP .RS -4 .B Note: .RE The ancient behavior of sometimes consuming the server exit message if the server died during the call while linked to the client was removed in Erlang 5\&.6/OTP R12B\&. .RE .SH "CALLBACK FUNCTIONS" .LP The following functions are to be exported from a \fIgen_fsm\fR\& callback module\&. .LP \fIstate name\fR\& denotes a state of the state machine\&. .LP \fIstate data\fR\& denotes the internal state of the Erlang process that implements the state machine\&. .SH EXPORTS .LP .B Module:code_change(OldVsn, StateName, StateData, Extra) -> {ok, NextStateName, NewStateData} .br .RS .LP Types: .RS 3 OldVsn = Vsn | {down, Vsn} .br Vsn = term() .br StateName = NextStateName = atom() .br StateData = NewStateData = term() .br Extra = term() .br .RE .RE .RS .LP This function is called by a \fIgen_fsm\fR\& process when it is to update its internal state data during a release upgrade/downgrade, that is, when instruction \fI{update,Module,Change,\&.\&.\&.}\fR\&, where \fIChange={advanced,Extra}\fR\&, is given in the \fIappup\fR\& file; see section \fB Release Handling Instructions\fR\& in OTP Design Principles\&. .LP For an upgrade, \fIOldVsn\fR\& is \fIVsn\fR\&, and for a downgrade, \fIOldVsn\fR\& is \fI{down,Vsn}\fR\&\&. \fIVsn\fR\& is defined by the \fIvsn\fR\& attribute(s) of the old version of the callback module \fIModule\fR\&\&. If no such attribute is defined, the version is the checksum of the Beam file\&. .LP \fIStateName\fR\& is the current state name and \fIStateData\fR\& the internal state data of the \fIgen_fsm\fR\& process\&. .LP \fIExtra\fR\& is passed "as is" from the \fI{advanced,Extra}\fR\& part of the update instruction\&. .LP The function is to return the new current state name and updated internal data\&. .RE .LP .B Module:format_status(Opt, [PDict, StateData]) -> Status .br .RS .LP Types: .RS 3 Opt = normal | terminate .br PDict = [{Key, Value}] .br StateData = term() .br Status = term() .br .RE .RE .RS .LP .RS -4 .B Note: .RE This callback is optional, so callback modules need not export it\&. The \fIgen_fsm\fR\& module provides a default implementation of this function that returns the callback module state data\&. .LP This function is called by a \fIgen_fsm\fR\& process in the following situations: .RS 2 .TP 2 * One of \fB\fIsys:get_status/1,2\fR\&\fR\& is invoked to get the \fIgen_fsm\fR\& status\&. \fIOpt\fR\& is set to the atom \fInormal\fR\& for this case\&. .LP .TP 2 * The \fIgen_fsm\fR\& process terminates abnormally and logs an error\&. \fIOpt\fR\& is set to the atom \fIterminate\fR\& for this case\&. .LP .RE .LP This function is useful for changing the form and appearance of the \fIgen_fsm\fR\& status for these cases\&. A callback module wishing to change the \fIsys:get_status/1,2\fR\& return value as well as how its status appears in termination error logs, exports an instance of \fIformat_status/2\fR\& that returns a term describing the current status of the \fIgen_fsm\fR\& process\&. .LP \fIPDict\fR\& is the current value of the process dictionary of the \fIgen_fsm\fR\& process\&. .LP \fIStateData\fR\& is the internal state data of the \fIgen_fsm\fR\& process\&. .LP The function is to return \fIStatus\fR\&, a term that change the details of the current state and status of the \fIgen_fsm\fR\& process\&. There are no restrictions on the form \fIStatus\fR\& can take, but for the \fIsys:get_status/1,2\fR\& case (when \fIOpt\fR\& is \fInormal\fR\&), the recommended form for the \fIStatus\fR\& value is \fI[{data, [{"StateData", Term}]}]\fR\&, where \fITerm\fR\& provides relevant details of the \fIgen_fsm\fR\& state data\&. Following this recommendation is not required, but it makes the callback module status consistent with the rest of the \fIsys:get_status/1,2\fR\& return value\&. .LP One use for this function is to return compact alternative state data representations to avoid that large state terms are printed in log files\&. .RE .LP .B Module:handle_event(Event, StateName, StateData) -> Result .br .RS .LP Types: .RS 3 Event = term() .br StateName = atom() .br StateData = term() .br Result = {next_state,NextStateName,NewStateData} .br | {next_state,NextStateName,NewStateData,Timeout} .br | {next_state,NextStateName,NewStateData,hibernate} .br | {stop,Reason,NewStateData} .br NextStateName = atom() .br NewStateData = term() .br Timeout = int()>0 | infinity .br Reason = term() .br .RE .RE .RS .LP Whenever a \fIgen_fsm\fR\& process receives an event sent using \fB\fIsend_all_state_event/2\fR\&\fR\&, this function is called to handle the event\&. .LP \fIStateName\fR\& is the current state name of the \fIgen_fsm\fR\& process\&. .LP For a description of the other arguments and possible return values, see \fB\fIModule:StateName/2\fR\&\fR\&\&. .RE .LP .B Module:handle_info(Info, StateName, StateData) -> Result .br .RS .LP Types: .RS 3 Info = term() .br StateName = atom() .br StateData = term() .br Result = {next_state,NextStateName,NewStateData} .br | {next_state,NextStateName,NewStateData,Timeout} .br | {next_state,NextStateName,NewStateData,hibernate} .br | {stop,Reason,NewStateData} .br NextStateName = atom() .br NewStateData = term() .br Timeout = int()>0 | infinity .br Reason = normal | term() .br .RE .RE .RS .LP This function is called by a \fIgen_fsm\fR\& process when it receives any other message than a synchronous or asynchronous event (or a system message)\&. .LP \fIInfo\fR\& is the received message\&. .LP For a description of the other arguments and possible return values, see \fB\fIModule:StateName/2\fR\&\fR\&\&. .RE .LP .B Module:handle_sync_event(Event, From, StateName, StateData) -> Result .br .RS .LP Types: .RS 3 Event = term() .br From = {pid(),Tag} .br StateName = atom() .br StateData = term() .br Result = {reply,Reply,NextStateName,NewStateData} .br | {reply,Reply,NextStateName,NewStateData,Timeout} .br | {reply,Reply,NextStateName,NewStateData,hibernate} .br | {next_state,NextStateName,NewStateData} .br | {next_state,NextStateName,NewStateData,Timeout} .br | {next_state,NextStateName,NewStateData,hibernate} .br | {stop,Reason,Reply,NewStateData} | {stop,Reason,NewStateData} .br Reply = term() .br NextStateName = atom() .br NewStateData = term() .br Timeout = int()>0 | infinity .br Reason = term() .br .RE .RE .RS .LP Whenever a \fIgen_fsm\fR\& process receives an event sent using \fB\fIsync_send_all_state_event/2,3\fR\&\fR\&, this function is called to handle the event\&. .LP \fIStateName\fR\& is the current state name of the \fIgen_fsm\fR\& process\&. .LP For a description of the other arguments and possible return values, see \fB\fIModule:StateName/3\fR\&\fR\&\&. .RE .LP .B Module:init(Args) -> Result .br .RS .LP Types: .RS 3 Args = term() .br Result = {ok,StateName,StateData} | {ok,StateName,StateData,Timeout} .br | {ok,StateName,StateData,hibernate} .br | {stop,Reason} | ignore .br StateName = atom() .br StateData = term() .br Timeout = int()>0 | infinity .br Reason = term() .br .RE .RE .RS .LP Whenever a \fIgen_fsm\fR\& process is started using \fB\fIstart/3,4\fR\&\fR\& or \fB\fIstart_link/3,4\fR\&\fR\&, this function is called by the new process to initialize\&. .LP \fIArgs\fR\& is the \fIArgs\fR\& argument provided to the start function\&. .LP If initialization is successful, the function is to return \fI{ok,StateName,StateData}\fR\&, \fI{ok,StateName,StateData,Timeout}\fR\&, or \fI{ok,StateName,StateData,hibernate}\fR\&, where \fIStateName\fR\& is the initial state name and \fIStateData\fR\& the initial state data of the \fIgen_fsm\fR\& process\&. .LP If an integer time-out value is provided, a time-out occurs unless an event or a message is received within \fITimeout\fR\& milliseconds\&. A time-out is represented by the atom \fItimeout\fR\& and is to be handled by the \fB\fIModule:StateName/2\fR\&\fR\& callback functions\&. The atom \fIinfinity\fR\& can be used to wait indefinitely, this is the default value\&. .LP If \fIhibernate\fR\& is specified instead of a time-out value, the process goes into hibernation when waiting for the next message to arrive (by calling \fB\fIproc_lib:hibernate/3\fR\&\fR\&)\&. .LP If the initialization fails, the function returns \fI{stop,Reason}\fR\&, where \fIReason\fR\& is any term, or \fIignore\fR\&\&. .RE .LP .B Module:StateName(Event, StateData) -> Result .br .RS .LP Types: .RS 3 Event = timeout | term() .br StateData = term() .br Result = {next_state,NextStateName,NewStateData} .br | {next_state,NextStateName,NewStateData,Timeout} .br | {next_state,NextStateName,NewStateData,hibernate} .br | {stop,Reason,NewStateData} .br NextStateName = atom() .br NewStateData = term() .br Timeout = int()>0 | infinity .br Reason = term() .br .RE .RE .RS .LP There is to be one instance of this function for each possible state name\&. Whenever a \fIgen_fsm\fR\& process receives an event sent using \fB\fIsend_event/2\fR\&\fR\&, the instance of this function with the same name as the current state name \fIStateName\fR\& is called to handle the event\&. It is also called if a time-out occurs\&. .LP \fIEvent\fR\& is either the atom \fItimeout\fR\&, if a time-out has occurred, or the \fIEvent\fR\& argument provided to \fIsend_event/2\fR\&\&. .LP \fIStateData\fR\& is the state data of the \fIgen_fsm\fR\& process\&. .LP If the function returns \fI{next_state,NextStateName,NewStateData}\fR\&, \fI{next_state,NextStateName,NewStateData,Timeout}\fR\&, or \fI{next_state,NextStateName,NewStateData,hibernate}\fR\&, the \fIgen_fsm\fR\& process continues executing with the current state name set to \fINextStateName\fR\& and with the possibly updated state data \fINewStateData\fR\&\&. For a description of \fITimeout\fR\& and \fIhibernate\fR\&, see \fB\fIModule:init/1\fR\&\fR\&\&. .LP If the function returns \fI{stop,Reason,NewStateData}\fR\&, the \fIgen_fsm\fR\& process calls \fIModule:terminate(Reason,StateName,NewStateData)\fR\& and terminates\&. .RE .LP .B Module:StateName(Event, From, StateData) -> Result .br .RS .LP Types: .RS 3 Event = term() .br From = {pid(),Tag} .br StateData = term() .br Result = {reply,Reply,NextStateName,NewStateData} .br | {reply,Reply,NextStateName,NewStateData,Timeout} .br | {reply,Reply,NextStateName,NewStateData,hibernate} .br | {next_state,NextStateName,NewStateData} .br | {next_state,NextStateName,NewStateData,Timeout} .br | {next_state,NextStateName,NewStateData,hibernate} .br | {stop,Reason,Reply,NewStateData} | {stop,Reason,NewStateData} .br Reply = term() .br NextStateName = atom() .br NewStateData = term() .br Timeout = int()>0 | infinity .br Reason = normal | term() .br .RE .RE .RS .LP There is to be one instance of this function for each possible state name\&. Whenever a \fIgen_fsm\fR\& process receives an event sent using \fB\fIsync_send_event/2,3\fR\&\fR\&, the instance of this function with the same name as the current state name \fIStateName\fR\& is called to handle the event\&. .LP \fIEvent\fR\& is the \fIEvent\fR\& argument provided to \fIsync_send_event/2,3\fR\&\&. .LP \fIFrom\fR\& is a tuple \fI{Pid,Tag}\fR\& where \fIPid\fR\& is the pid of the process that called \fIsync_send_event/2,3\fR\& and \fITag\fR\& is a unique tag\&. .LP \fIStateData\fR\& is the state data of the \fIgen_fsm\fR\& process\&. .RS 2 .TP 2 * If \fI{reply,Reply,NextStateName,NewStateData}\fR\&, \fI{reply,Reply,NextStateName,NewStateData,Timeout}\fR\&, or \fI{reply,Reply,NextStateName,NewStateData,hibernate}\fR\& is returned, \fIReply\fR\& is given back to \fIFrom\fR\& as the return value of \fIsync_send_event/2,3\fR\&\&. The \fIgen_fsm\fR\& process then continues executing with the current state name set to \fINextStateName\fR\& and with the possibly updated state data \fINewStateData\fR\&\&. For a description of \fITimeout\fR\& and \fIhibernate\fR\&, see \fB\fIModule:init/1\fR\&\fR\&\&. .LP .TP 2 * If \fI{next_state,NextStateName,NewStateData}\fR\&, \fI{next_state,NextStateName,NewStateData,Timeout}\fR\&, or \fI{next_state,NextStateName,NewStateData,hibernate}\fR\& is returned, the \fIgen_fsm\fR\& process continues executing in \fINextStateName\fR\& with \fINewStateData\fR\&\&. Any reply to \fIFrom\fR\& must be specified explicitly using \fB\fIreply/2\fR\&\fR\&\&. .LP .TP 2 * If the function returns \fI{stop,Reason,Reply,NewStateData}\fR\&, \fIReply\fR\& is given back to \fIFrom\fR\&\&. If the function returns \fI{stop,Reason,NewStateData}\fR\&, any reply to \fIFrom\fR\& must be specified explicitly using \fIreply/2\fR\&\&. The \fIgen_fsm\fR\& process then calls \fIModule:terminate(Reason,StateName,NewStateData)\fR\& and terminates\&. .LP .RE .RE .LP .B Module:terminate(Reason, StateName, StateData) .br .RS .LP Types: .RS 3 Reason = normal | shutdown | {shutdown,term()} | term() .br StateName = atom() .br StateData = term() .br .RE .RE .RS .LP This function is called by a \fIgen_fsm\fR\& process when it is about to terminate\&. It is to be the opposite of \fB\fIModule:init/1\fR\&\fR\& and do any necessary cleaning up\&. When it returns, the \fIgen_fsm\fR\& process terminates with \fIReason\fR\&\&. The return value is ignored\&. .LP \fIReason\fR\& is a term denoting the stop reason, \fIStateName\fR\& is the current state name, and \fIStateData\fR\& is the state data of the \fIgen_fsm\fR\& process\&. .LP \fIReason\fR\& depends on why the \fIgen_fsm\fR\& process is terminating\&. If it is because another callback function has returned a stop tuple \fI{stop,\&.\&.}\fR\&, \fIReason\fR\& has the value specified in that tuple\&. If it is because of a failure, \fIReason\fR\& is the error reason\&. .LP If the \fIgen_fsm\fR\& process is part of a supervision tree and is ordered by its supervisor to terminate, this function is called with \fIReason=shutdown\fR\& if the following conditions apply: .RS 2 .TP 2 * The \fIgen_fsm\fR\& process has been set to trap exit signals\&. .LP .TP 2 * The shutdown strategy as defined in the child specification of the supervisor is an integer time-out value, not \fIbrutal_kill\fR\&\&. .LP .RE .LP Even if the \fIgen_fsm\fR\& process is \fInot\fR\& part of a supervision tree, this function is called if it receives an \fI\&'EXIT\&'\fR\& message from its parent\&. \fIReason\fR\& is the same as in the \fI\&'EXIT\&'\fR\& message\&. .LP Otherwise, the \fIgen_fsm\fR\& process terminates immediately\&. .LP Notice that for any other reason than \fInormal\fR\&, \fIshutdown\fR\&, or \fI{shutdown,Term}\fR\& the \fIgen_fsm\fR\& process is assumed to terminate because of an error and an error report is issued using \fB\fIerror_logger:format/2\fR\&\fR\&\&. .RE .SH "SEE ALSO" .LP \fB\fIgen_event(3erl)\fR\&\fR\&, \fB\fIgen_server(3erl)\fR\&\fR\&, \fB\fIgen_statem(3erl)\fR\&\fR\&, \fB\fIproc_lib(3erl)\fR\&\fR\&, \fB\fIsupervisor(3erl)\fR\&\fR\&, \fB\fIsys(3erl)\fR\&\fR\&