./ct_report/coverage/mongoose_packet_handler.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% File : mongoose_packet_handler.erl
3 %%% Author : Piotr Nosek <piotr.nosek@erlang-solutions.com>
4 %%% Purpose : Packet handler behaviour
5 %%% Created : 24 Jan 2017
6 %%%----------------------------------------------------------------------
7
8 -module(mongoose_packet_handler).
9 -author('piotr.nosek@erlang-solutions.com').
10
11 -record(packet_handler, {
12 handler :: process_packet(),
13 extra :: extra()
14 }).
15 -type extra() :: map().
16 -type t() :: #packet_handler{}.
17 -export_type([t/0]).
18
19 -define(ARGS, Acc :: mongoose_acc:t(),
20 From :: jid:jid(),
21 To :: jid:jid(),
22 El :: exml:element(),
23 Extra :: extra()).
24 -type process_packet() :: fun((?ARGS) -> mongoose_acc:t()).
25 -callback process_packet(?ARGS) -> mongoose_acc:t().
26
27 %%----------------------------------------------------------------------
28 %% API
29 %%----------------------------------------------------------------------
30
31 -export([new/1, new/2, process/5, add_extra/2]).
32 %% Getters
33 -export([module/1, extra/1]).
34
35 -spec new(Module :: module()) -> t().
36 new(Module) ->
37 134 new(Module, #{}).
38
39 -spec new(Module :: module(), Extra :: extra()) -> t().
40 new(Module, Extra) when is_atom(Module), is_map(Extra) ->
41 692 #packet_handler{handler = fun Module:process_packet/5, extra = Extra }.
42
43 -spec process(Handler :: t(),
44 Acc :: mongoose_acc:t(),
45 From ::jid:jid(),
46 To ::jid:jid(),
47 El :: exml:element()) -> mongoose_acc:t().
48 process(#packet_handler{handler = ProcessPacket, extra = Extra }, Acc, From, To, El) ->
49 49968 ProcessPacket(Acc, From, To, El, Extra).
50
51 module(#packet_handler{handler = ProcessPacket }) ->
52 1 {_, Module} = erlang:fun_info(ProcessPacket, module),
53 1 Module.
54
55 extra(#packet_handler{ extra = Extra }) ->
56 1 Extra.
57
58 add_extra(#packet_handler{ extra = OldExtra } = Handler, Extra) ->
59 %% KV pairs from the OldExtra map will remain unchanged, only
60 %% the new keys from Extra map will be added to the NewExtra map
61 601 NewExtra = maps:merge(Extra, OldExtra),
62 601 Handler#packet_handler{extra = NewExtra}.
Line Hits Source