OTP behaviours: gen_fsm; gen_event. Practical examples?
Asked Answered
M

3

6

I have used both the supervisor and gen_server behaviours, and I can understand the practical uses for both of them. However, I don't really understand the use of the gen_fsm and the gen_event behaviours. Can someone clarify with practical examples?

Thanks in advance

Mathura answered 4/1, 2014 at 4:24 Comment(0)
X
4

One classic example for FSM would be lock with timeout which is mentioned in manual,

Another example which I implemented in my experience would be telephone lines, because phone have states, like ringing, connected, disconnected etc and some operations are allowed and some are not allowed during this states.

An example for event is logging used in https://github.com/basho/lager

Xuthus answered 4/1, 2014 at 4:44 Comment(2)
Thanks. Why would the lager team decide to use a gen_event instead of a gen_server?Mathura
From Manual, Since each event handler is one callback module, an event manager will have several callback modules which are added and deleted dynamically. Therefore gen_event is more tolerant of callback module errors than the other behaviours. If a callback function for an installed event handler fails with Reason, or returns a bad value Term, the event manager will not fail. It will delete the event handler by calling the callback function Module:terminate/2 (see below), giving as argument {error,{'EXIT',Reason}} or {error,Term}, respectively. No other event handler will be affected.Xuthus
G
3

gen_fsm is a neat implementation of finite state machine, you cane do roughly the same thing that you do with a gen_server, and in addition manage easily the different states of your application (for example in a game server select a level, a table, modify player attribute, play, save, restore...).

gen-event is an easy way to dispach event, your application send all event to the gen_event knowing nothing about potential usage, and you dynamically add and delete handlers, with different behavior (log in file, in a database, display information in graphical interface...). I have used this to have a graphical view of the processes state and communication of my application, and file log for performance analysis.

Goudy answered 4/1, 2014 at 12:12 Comment(0)
R
3

Some good examples you can find them here:

"Event handlers" and "Finite State Machines"

gen_fsm:

The gen_fsm behaviour is somewhat similar to gen_server in that it is a specialised version of it. The biggest difference is that rather than handling calls and casts, we're handling synchronous and asynchronous events. Much like our dog and cat examples, each state is represented by a function. Again, we'll go through the callbacks our modules need to implement in order to work.

gen_event:

The gen_event behaviour differs quite a bit from the gen_server and gen_fsm behaviours in that you are never really starting a process. The gen_event behaviour basically runs the process that accepts and calls functions, and you only provide a module with these functions. This is to say, you have nothing to do with regards to event manipulation except give your callback functions in a format that pleases the event manager. All managing is done for free; you only provide what's specific to your application. This is not really surprising given OTP is, again, all about separating what's generic from specific.

Richmond answered 6/1, 2014 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.