A behaviour module for implementing event handling functionality. The OTP event handling model consists of a generic event manager process with an arbitrary number of event handlers which are added and deleted dynamically.
An event manager 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 OTP Design Principles for more information.
Each event handler is implemented as 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:
gen_event module Callback module ---------------- --------------- gen_event:start_link -----> - gen_event:add_handler gen_event:add_sup_handler -----> Module:init/1 gen_event:notify gen_event:sync_notify -----> Module:handle_event/2 gen_event:call -----> Module:handle_call/2 - -----> Module:handle_info/2 gen_event:delete_handler -----> Module:terminate/2 gen_event:swap_handler gen_event:swap_sup_handler -----> Module1:terminate/2 Module2:init/1 gen_event:which_handlers -----> - gen_event:stop -----> Module:terminate/2 - -----> Module:code_change/3
Since each event handler is one callback module, an event manager
will have several callback modules which are added and deleted
dynamically. Therefore
A gen_event process handles system messages as documented in
Note that an event manager does trap exit signals automatically.
The gen_event process can go into hibernation
(see
It's also worth noting that when multiple event handlers are
invoked, it's sufficient that one single event handler returns a
Unless otherwise stated, all functions in this module fail if the specified event manager does not exist or if bad arguments are given.
Creates an event manager process as part of a supervision tree. The function should be called, directly or indirectly, by the supervisor. It will, among other things, ensure that the event manager is linked to the supervisor.
If
If the event manager is successfully created the function
returns
Creates a stand-alone event manager process, i.e. an event manager which is not part of a supervision tree and thus has no supervisor.
See
Adds a new event handler to the event manager
If
Adds a new event handler in the same way as
If the event handler later is deleted, the event manager
sends a message
See
Sends an event notification to the event manager
See
Makes a synchronous call to the event handler
See
The return value
Deletes an event handler from the event manager
See
The return value is the return value of
Replaces an old event handler with a new event handler in
the event manager
See
First the old event handler
Then the new event handler
The new handler will be added even if the the specified old event
handler is not installed in which case
If there was a supervised connection between
If
Replaces an event handler in the event manager
See
Returns a list of all event handlers installed in the event
manager
See
Terminates the event manager
See
The following functions should be exported from a
Whenever a new event handler is added to an event manager, this function is called to initialize the event handler.
If the event handler is added due to a call to
If the event handler is replacing another event handler due to
a call to
The function should return
If
Whenever an event manager receives an event sent using
If the function returns
If
If the function returns
If the function returns
Whenever an event manager receives a request sent using
The return values are the same as for
This function is called for each installed event handler when an event manager receives any other message than an event or a synchronous request (or a system message).
See
Whenever an event handler is deleted from an event manager,
this function is called. It should be the opposite of
If the event handler is deleted due to a call to
The event manager will terminate if it is part of a supervision
tree and it is ordered by its supervisor to terminate.
Even if it is not part of a supervision tree, it will
terminate if it receives an
The function may return any term. If the event handler is
deleted due to a call to
This function is called for an installed event handler which
should update its internal state during a release
upgrade/downgrade, i.e. when the instruction
In the case of an upgrade,
The function should return the updated internal state.
This callback is optional, so event handler modules need not export it. If a handler does not export this function, the gen_event module uses the handler state directly for the purposes described below.
This function is called by a gen_event process when:
This function is useful for customising the form and
appearance of the event handler state for these cases. An
event handler callback module wishing to customise
the
The function should return
One use for this function is to return compact alternative state representations to avoid having large state terms printed in logfiles.