File: gen_fsm.html

package info (click to toggle)
erlang-doc-html 1%3A8.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 18,028 kB
  • ctags: 7,419
  • sloc: perl: 1,841; ansic: 323; erlang: 155
file content (457 lines) | stat: -rw-r--r-- 26,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<HTML>
<HEAD>
<!-- refpage -->
<TITLE>gen_fsm</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>


<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Erlang Systems]" SRC="min_head.gif"></A>
<H1>gen_fsm</H1>
</CENTER>
<H3>MODULE</H3>
<UL>
gen_fsm</UL>
<H3>MODULE SUMMARY</H3>
<UL>
Generic Finite State Machine Behaviour</UL>
<H3>DESCRIPTION</H3>
<UL>
<P>A behaviour module for implementing a finite state machine.
A generic finite state machine process (gen_fsm) implemented
using this module will have a standard set of interface functions
and include functionality for tracing and error reporting. It will
also fit into an OTP supervision tree. Refer to <STRONG>OTP Design
        Principles</STRONG> for more information.<P>A gen_fsm assumes all specific parts to be located in a callback
module exporting a pre-defined set of functions. The relationship
between the behaviour functions and the callback functions can be
illustrated as follows:<PRE>gen_fsm module                    Callback module
--------------                    ---------------
gen_fsm:start_link                -----&#62; Module:init/1

gen_fsm:send_event                -----&#62; Module:StateName/2

gen_fsm:send_all_state_event      -----&#62; Module:handle_event/3

gen_fsm:sync_send_event           -----&#62; Module:StateName/3

gen_fsm:sync_send_all_state_event -----&#62; Module:handle_sync_event/4

-                                 -----&#62; Module:handle_info/3

-                                 -----&#62; Module:terminate/3

-                                 -----&#62; Module:code_change/4
    </PRE>
<P>If a callback function fails or returns a bad value, the gen_fsm
will terminate.<P>The <CODE>sys</CODE> module can be used for debugging a gen_fsm.<P>Note that a gen_fsm does not trap exit signals automatically,
this must be explicitly initiated in the callback module.<P>Unless otherwise stated, all functions in this module fail if
the specified gen_fsm does not exist or if bad arguments are given.</UL>
<H3>EXPORTS</H3>
<P><A NAME="start%3"><STRONG><CODE>start(Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="start%4"><STRONG><CODE>start(FsmName, Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="start_link%3"><STRONG><CODE>start_link(Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="start_link%4"><STRONG><CODE>start_link(FsmName, Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FsmName = {local,Name} | {global,Name}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = atom()</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = term()</CODE></STRONG><BR>
<STRONG><CODE>Options = [Option]</CODE></STRONG><BR>
<STRONG><CODE>&#160;Option = {debug,Dbgs} | {timeout,Time}</CODE></STRONG><BR>
<STRONG><CODE>&#160;&#160;Dbgs = [Dbg]</CODE></STRONG><BR>
<STRONG><CODE>&#160;&#160;&#160;Dbg = trace | log | statistics</CODE></STRONG><BR>
<STRONG><CODE>&#160;&#160;&#160;&#160;| {log_to_file,FileName}
         | {install,{Func,FuncState}}</CODE></STRONG><BR>
<STRONG><CODE>Result = {ok,Pid} | ignore | {error,Error}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Pid = pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Error = {already_started,Pid} | term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Creates a gen_fsm process which calls <CODE>Module:init/1</CODE> to
         initialize. To ensure a synchronized start-up procedure, this
         function does not return until <CODE>Module:init/1</CODE> has returned.
        <P>A gen_fsm started using <CODE>start_link</CODE> is linked to
         the calling process, this function must be used if the gen_fsm is
         included in a supervision tree. A gen_fsm started using
         <CODE>start</CODE> is not linked to the calling process.<P>If <CODE>FsmName={local,Name}</CODE>, the gen_fsm is registered
         locally as <CODE>Name</CODE> using <CODE>register/2</CODE>.
         If <CODE>FsmName={global,Name}</CODE>, the gen_fsm is registered
         globally as <CODE>Name</CODE> using <CODE>global:register_name/2</CODE>.
         If no name is provided, the gen_fsm is not registered.<P><CODE>Module</CODE> is the name of the callback module.<P><CODE>Args</CODE> is an arbitrary term which is passed as the argument
         to <CODE>Module:init/1</CODE>.<P>If the option <CODE>{timeout,Time}</CODE> is present, the gen_fsm
         is allowed to spend <CODE>Time</CODE> milliseconds initializing
         or it will be terminated and the start function will return
         <CODE>{error,timeout}</CODE>.<P>If the option <CODE>{debug,Dbgs}</CODE> is present, the corresponding
         <CODE>sys</CODE> function will be called for each item in <CODE>Dbgs</CODE>.
         Refer to <CODE>sys(3)</CODE> for more information.<P>If the gen_fsm is successfully created and initialized
         the function returns <CODE>{ok,Pid}</CODE>, where <CODE>Pid</CODE> is
         the pid of the gen_fsm. If there already exists a process with
         the specified <CODE>FsmName</CODE>, the function returns
         <CODE>{error,{already_started,Pid}}</CODE> where <CODE>Pid</CODE> is the pid
         of that process.<P>If <CODE>Module:init/1</CODE> fails with <CODE>Reason</CODE>, the function
         returns <CODE>{error,Reason}</CODE>. If <CODE>Module:init/1</CODE> returns
         <CODE>{stop,Reason}</CODE> or <CODE>ignore</CODE>, the process is terminated
         and the function returns <CODE>{error,Reason}</CODE> or <CODE>ignore</CODE>,
         respectively.</UL>
<P><A NAME="send_event%2"><STRONG><CODE>send_event(FsmRef, Event) -&#62; ok</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FsmRef = Name | {Name,Node} | {global,Name} | pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sends an event asynchronously to the gen_fsm <CODE>FsmRef</CODE>
         and returns <CODE>ok</CODE> immediately. The gen_fsm will call
         <CODE>Module:StateName/2</CODE> to handle the event, where
         <CODE>StateName</CODE> is the name of the current state of the gen_fsm.
        <P><CODE>FsmRef</CODE> can be:<P><UL>
<LI>the pid,</LI><BR>
<LI><CODE>Name</CODE>, if the gen_fsm is locally registered,</LI><BR>
<LI><CODE>{Name,Node}</CODE>, if the gen_fsm is locally registered at
         another node, or</LI><BR>
<LI><CODE>{global,Name}</CODE>, if the gen_fsm is globally registered.
         </LI><BR>
</UL>
<P><CODE>Event</CODE> is an arbitrary term which is passed as one of
         the arguments to <CODE>Module:StateName/2</CODE>.</UL>
<P><A NAME="send_all_state_event%2"><STRONG><CODE>send_all_state_event(FsmRef, Event) -&#62; ok</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FsmRef = Name | {Name,Node} | {global,Name} | pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sends an event asynchronously to the gen_fsm <CODE>FsmRef</CODE>
         and returns <CODE>ok</CODE> immediately. The gen_fsm will call
         <CODE>Module:handle_event/3</CODE> to handle the event.<P>See <CODE>send_event/2</CODE> for a description of the arguments.<P>The difference between <CODE>send_event</CODE> and
         <CODE>send_all_state_event</CODE> 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
         <CODE>handle_event</CODE> clause is needed to handle the event instead
         of one clause in each state name function.</UL>
<P><A NAME="sync_send_event%2"><STRONG><CODE>sync_send_event(FsmRef, Event) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="sync_send_event%3"><STRONG><CODE>sync_send_event(FsmRef, Event, Timeout) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FsmRef = Name | {Name,Node} | {global,Name} | pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
<STRONG><CODE>Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>Reply = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sends an event to the gen_fsm <CODE>FsmRef</CODE> and waits until a
         reply arrives or a timeout occurs. The gen_fsm will call
         <CODE>Module:StateName/3</CODE> to handle the event, where
         <CODE>StateName</CODE> is the name of the current state of the gen_fsm.
        <P>See <CODE>send_event/2</CODE> for a description of <CODE>FsmRef</CODE> and
         <CODE>Event</CODE>.<P><CODE>Timeout</CODE> is an integer greater than zero which specifies
         how many milliseconds to wait for a reply, or the atom
         <CODE>infinity</CODE> to wait indefinitely. Default value is 5000.
         If no reply is received within the specified time, the function
         call fails.<P>The return value <CODE>Reply</CODE> is defined in the return value of
         <CODE>Module:StateName/3</CODE>.<P>In the case where the gen_fsm terminates during the handling of
         the event and the caller is linked to the gen_fsm and trapping
         exits, the exit message is removed from the caller's receive
         queue before the function call fails.<BR>

         This behaviour is retained for backwards compatibility only and may
         change in the future. Note that if the gen_fsm crashes in between
         calls, a linked process must take care of the exit message anyway.
         <BR>

         Warning: Under certain circumstances
         (e.g. <CODE>FsmRef = {Name,Node}</CODE>, and <CODE>Node</CODE> goes down)
         the exit message cannot be removed.</UL>
<P><A NAME="sync_send_all_state_event%2"><STRONG><CODE>sync_send_all_state_event(FsmRef, Event) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="sync_send_all_state_event%3"><STRONG><CODE>sync_send_all_state_event(FsmRef, Event, Timeout) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FsmRef = Name | {Name,Node} | {global,Name} | pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
<STRONG><CODE>Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>Reply = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sends an event to the gen_fsm <CODE>FsmRef</CODE> and waits until a
         reply arrives or a timeout occurs. The gen_fsm will call
         <CODE>Module:handle_event/3</CODE> to handle the event.<P>See <CODE>send_event/2</CODE> for a description of <CODE>FsmRef</CODE> and
         <CODE>Event</CODE>. See <CODE>sync_send_event/3</CODE> for a description of
         <CODE>Timeout</CODE> and <CODE>Reply</CODE>.<P>See <CODE>send_all_state_event/2</CODE> for a discussion about
         the difference between <CODE>sync_send_event</CODE> and
         <CODE>sync_send_all_state_event</CODE>.</UL>
<P><A NAME="reply%2"><STRONG><CODE>reply(Caller, Reply) -&#62; true</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Caller - see below</CODE></STRONG><BR>
<STRONG><CODE>Reply = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function can be used by a gen_fsm to explicitly send a reply
         to a client process that called <CODE>sync_send_event</CODE> or
         <CODE>sync_send_all_state_event</CODE>, when the reply cannot be defined
         in the return value of <CODE>Module:State/3</CODE> or
         <CODE>Module:handle_sync_event/4</CODE>.<P><CODE>Caller</CODE> must be the <CODE>From</CODE> argument provided to
         the callback function. <CODE>Reply</CODE> is an arbitrary term, which
         will be given back to the client as the return value of
         <CODE>sync_send_event</CODE> or <CODE>sync_send_all_state_event</CODE>.</UL>
<H3>CALLBACK FUNCTIONS</H3>
<UL>
<P>The following functions should be exported from a <CODE>gen_fsm</CODE>
callback module.<P>In the description, the expression <STRONG>state name</STRONG> is used to
denote a state of the state machine. <STRONG>state data</STRONG> is used to
denote the internal state of the Erlang process which implements
the state machine.<P> </UL>
<H3>EXPORTS</H3>
<P><A NAME="Module:init%1"><STRONG><CODE>Module:init(Args) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Args = term()</CODE></STRONG><BR>
<STRONG><CODE>Return = {ok,StateName,StateData}
         | {ok,StateName,StateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160;| {stop,Reason} | ignore</CODE></STRONG><BR>
<STRONG><CODE>&#160;StateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>&#160;StateData = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Whenever a gen_fsm is started using <CODE>gen_fsm:start/3,4</CODE> or
         <CODE>gen_fsm:start_link/3,4</CODE>, this function is called by the new
         process to initialize.<P><CODE>Args</CODE> is the <CODE>Args</CODE> argument provided to the start
         function.<P>If initialization is successful, the function should return
         <CODE>{ok,StateName,StateData}</CODE> or
         <CODE>{ok,StateName,StateData,Timout}</CODE>, where <CODE>StateName</CODE>
         is the initial state name and <CODE>StateData</CODE> the initial
         state data of the gen_fsm.<P>If an integer timout value is provided, a timout will occur
         unless an event or a message is received within <CODE>Timeout</CODE>
         milliseconds. A timout is represented by the atom <CODE>timeout</CODE>
         and should be handled by the <CODE>Module:StateName/2</CODE> callback
         functions. The atom <CODE>inifinity</CODE> can be used to wait
         indefinitely, this is the default value.<P>If something goes wrong during the initialization the function
         should return <CODE>{stop,Reason}</CODE>, where <CODE>Reason</CODE> is any
         term, or <CODE>ignore</CODE>.</UL>
<P><A NAME="Module:StateName%2"><STRONG><CODE>Module:StateName(Event, StateData) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Event = timeout | term()</CODE></STRONG><BR>
<STRONG><CODE>StateData = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {next_state,NextStateName,NewStateData} |
         {next_state,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {stop,Reason,NewStateData}</CODE></STRONG><BR>
<STRONG><CODE>&#160;NextStateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewStateData = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>There should be one instance of this function for each possible
         state name. Whenever a gen_fsm receives an event sent using
         <CODE>gen_fsm:send_event/2</CODE>, the instance of this function with
         the same name as the current state name <CODE>StateName</CODE> is called
         to handle the event. It is also called if a timeout occurs.<P><CODE>Event</CODE> is either the atom <CODE>timeout</CODE>, if a timeout has
         occured, or the <CODE>Event</CODE> argument provided to
         <CODE>send_event</CODE>.<P><CODE>StateData</CODE> is the state data of the gen_fsm.<P>If the function returns
         <CODE>{next_state,NextStateName,NewStateData}</CODE> or
         <CODE>{next_state,NextStateName,NewStateData,Timeout}</CODE>,
         the gen_fsm will continue executing with the current state name
         set to <CODE>NextStateName</CODE> and with the possibly updated state
         data <CODE>NewStateData</CODE>.
         See <CODE>Module:init/1</CODE> for a description of <CODE>Timeout</CODE>.<P>If the function returns <CODE>{stop,Reason,NewStateData}</CODE>,
         the gen_fsm will call <CODE>Module:terminate(Reason,NewStateData)</CODE>
         and terminate.</UL>
<P><A NAME="Module:handle_event%3"><STRONG><CODE>Module:handle_event(Event, StateName, StateData) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
<STRONG><CODE>StateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>StateData = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {next_state,NextStateName,NewStateData} |
         {next_state,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {stop,Reason,NewStateData}</CODE></STRONG><BR>
<STRONG><CODE>&#160;NextStateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewStateData = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Whenever a gen_fsm receives an event sent using
         <CODE>gen_fsm:send_all_state_event/2</CODE>, this function is called
         to handle the event.<P><CODE>StateName</CODE> is the current state name of the gen_fsm.<P>See <CODE>Module:StateName/2</CODE> for a description of the other
         arguments and possible return values.</UL>
<P><A NAME="Module:StateName%3"><STRONG><CODE>Module:StateName(Event, From, StateData) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
<STRONG><CODE>From = {pid(),Tag}</CODE></STRONG><BR>
<STRONG><CODE>StateData = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {reply,Reply,NextStateName,NewStateData} |
         {reply,Reply,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {next_state,NextStateName,NewStateData} |
         {next_state,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {stop,Reason,Reply,NewStateData} |
         {stop,Reason,NewStateData}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reply = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NextStateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewStateData = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = normal | term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>There should be one instance of this function for each possible
         state name. Whenever a gen_fsm receives an event sent using
         <CODE>gen_fsm:sync_send_event/2,3</CODE>, the instance of this function
         with the same name as the current state name <CODE>StateName</CODE> is
         called to handle the event.<P><CODE>Event</CODE> is the <CODE>Event</CODE> argument provided to
         <CODE>sync_send_event</CODE>.<P><CODE>From</CODE> is a tuple <CODE>{Pid,Tag}</CODE> where <CODE>Pid</CODE> is
         the pid of the process which called <CODE>sync_send_event</CODE> and
         <CODE>Tag</CODE> is a unique tag.<P><CODE>StateData</CODE> is the state data of the gen_fsm.<P>If the function returns
         <CODE>{reply,Reply,NextStateName,NewStateData}</CODE> or
         <CODE>{reply,Reply,NextStateName,NewStateData,Timeout}</CODE>,
         <CODE>Reply</CODE> will be given back to <CODE>From</CODE> as the return value
         of <CODE>sync_send_event</CODE>. The gen_fsm then continues executing
         with the current state name set to <CODE>NextStateName</CODE> and with
         the possibly updated state data <CODE>NewStateData</CODE>.
         See <CODE>Module:init/1</CODE> for a description of <CODE>Timeout</CODE>.<P>If the function returns
         <CODE>{next_state,NextStateName,NewStateData}</CODE> or
         <CODE>{next_state,NextStateName,NewStateData,Timeout}</CODE>,
         the gen_fsm will continue executing in <CODE>NextStateName</CODE> with
         <CODE>NewStateData</CODE>. Any reply to <CODE>From</CODE> must be given
         explicitly using <CODE>gen_fsm:reply/2</CODE>.<P>If the function returns <CODE>{stop,Reason,Reply,NewStateData}</CODE>,
         <CODE>Reply</CODE> will be given back to <CODE>From</CODE>. If the function
         returns <CODE>{stop,Reason,NewStateData}</CODE>, any reply to <CODE>From</CODE>
         must be given explicitly using <CODE>gen_fsm:reply/2</CODE>. The gen_fsm
         will then call <CODE>Module:terminate(Reason,NewStateData)</CODE> and
         terminate.</UL>
<P><A NAME="Module:handle_sync_event%4"><STRONG><CODE>Module:handle_sync_event(Event, From, StateName, StateData)
        -&#62; Result
</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Event = term()</CODE></STRONG><BR>
<STRONG><CODE>From = {pid(),Tag}</CODE></STRONG><BR>
<STRONG><CODE>StateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>StateData = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {reply,Reply,NextStateName,NewStateData} |
         {reply,Reply,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {next_state,NextStateName,NewStateData} |
         {next_state,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {stop,Reason,Reply,NewStateData} |
         {stop,Reason,NewStateData}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reply = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NextStateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewStateData = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Whenever a gen_fsm receives an event sent using
         <CODE>gen_fsm:sync_send_all_state_event/2,3</CODE>, this function is
         called to handle the event.<P><CODE>StateName</CODE> is the current state name of the gen_fsm.<P>See <CODE>Module:StateName/3</CODE> for a description of the other
         arguments and possible return values.</UL>
<P><A NAME="Module:handle_info%3"><STRONG><CODE>Module:handle_info(Info, StateName, StateData) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Info = term()</CODE></STRONG><BR>
<STRONG><CODE>StateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>StateData = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {next_state,NextStateName,NewStateData} |
         {next_state,NextStateName,NewStateData,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160; | {stop,Reason,NewStateData}</CODE></STRONG><BR>
<STRONG><CODE>&#160;NextStateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewStateData = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = normal | term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function is called by a gen_fsm when it receives any other
         message than a synchronous or asynchronous event (or a system
         message).<P><CODE>Info</CODE> is the received message.<P>See <CODE>Module:StateName/2</CODE> for a description of the other
         arguments and possible return values.</UL>
<P><A NAME="Module:terminate%3"><STRONG><CODE>Module:terminate(Reason, StateName, StateData)</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reason = normal | shutdown | term()</CODE></STRONG><BR>
<STRONG><CODE>StateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>StateData = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function is called by a gen_fsm when it is about to
         terminate. It should be the opposite of <CODE>Module:init/1</CODE> and
         do any necessary cleaning up. When it returns, the gen_fsm
         terminates with <CODE>Reason</CODE>. The return value is ignored.<P><CODE>Reason</CODE> is a term denoting the stop reason, <CODE>StateName</CODE>
         is the current state name, and <CODE>StateData</CODE> is the state data
         of the gen_fsm.<P><CODE>Reason</CODE> depends on why the gen_fsm is terminating. If it is
         because another callback function has returned a stop tuple
         <CODE>{stop,..}</CODE>, <CODE>Reason</CODE> will have the value specified in
         that tuple. If it is due to a failure, <CODE>Reason</CODE> is the error
         reason.<P>If the gen_fsm is part of a supervision tree and is ordered
         by its superviser to terminate, this function will be called with
         <CODE>Reason=shutdown</CODE> if the following conditions apply:<P><UL>
<LI>the gen_fsm has been set to trap exit signals, and</LI><BR>
<LI>the shutdown strategy as defined in the supervisor's child
         specification is an integer timeout value, not
         <CODE>brutal_kill</CODE>.
         </LI><BR>
</UL>
<P>Otherwise, the gen_fsm will be immediately terminated.<P>Note that for any other reason than <CODE>normal</CODE> or
         <CODE>shutdown</CODE>, the gen_fsm is assumed to terminate due to an
         error and an error report is issued using
         <CODE>error_logger:format/2</CODE>.</UL>
<P><A NAME="Module:code_change%4"><STRONG><CODE>Module:code_change(OldVsn, StateName, StateData, Extra) -&#62;
        {ok, NextStateName, NewStateData}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>OldVsn = undefined | term()</CODE></STRONG><BR>
<STRONG><CODE>StateName = NextStateName = atom()</CODE></STRONG><BR>
<STRONG><CODE>StateData = NewStateData = term()</CODE></STRONG><BR>
<STRONG><CODE>Extra = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function is called by a gen_fsm when it should update its
         state data due to a code replacement, i.e. when the instruction
         <CODE>{update,Module,Change,PrePurge,PostPurge,Modules}</CODE> where
         <CODE>Change={advanced,Extra}</CODE> has been given to the release
         handler. See <STRONG>SASL User's Guide</STRONG> for more information.<P><CODE>OldVsn</CODE> is the <CODE>vsn</CODE> attribute of the old version of
         the callback module <CODE>Module</CODE>, or <CODE>undefined</CODE> if no
         such attribute is defined.<P><CODE>StateName</CODE> is the current state name and <CODE>StateData</CODE>
         the state data of the gen_fsm.<P><CODE>Extra</CODE> is the same as in the <CODE>{advanced,Extra}</CODE> part
         of the update instruction.<P>The function should return the new current state name and updated
         state data.</UL>
<H3>SEE ALSO</H3>
<UL>
<P>supervisor(3), sys(3)</UL>
<H3>AUTHORS</H3>
<UL>
Gunilla Hugosson - support@erlang.ericsson.se<BR>
</UL>
<CENTER>
<HR>
<FONT SIZE=-1>stdlib 1.10<BR>
Copyright &copy; 1991-2001
<A HREF="http://www.erlang.se">Ericsson Utvecklings AB</A><BR>
<!--#include virtual="/ssi/otp_footer.html"-->
</FONT>
</CENTER>
</BODY>
</HTML>