./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 %%----------------------------------------------------------------------
12 %% Types
13 %%----------------------------------------------------------------------
14
15 -record(packet_handler, { module, extra }).
16
17 -type t() :: #packet_handler{
18 module :: module(),
19 extra :: map()
20 }.
21
22 -export_type([t/0]).
23
24 %%----------------------------------------------------------------------
25 %% Callback declarations
26 %%----------------------------------------------------------------------
27
28 -callback process_packet(Acc :: mongoose_acc:t(), From ::jid:jid(), To ::jid:jid(),
29 El :: exml:element(), Extra :: map()) -> mongoose_acc:t().
30
31 %%----------------------------------------------------------------------
32 %% API
33 %%----------------------------------------------------------------------
34
35 -export([new/1, new/2, process/5, add_extra/2]).
36 %% Getters
37 -export([module/1, extra/1]).
38
39 -ignore_xref([behaviour_info/1]).
40
41 -spec new(Module :: module()) -> t().
42 new(Module) ->
43 72 new(Module, #{}).
44
45 -spec new(Module :: module(), Extra :: map()) -> t().
46 new(Module, Extra) when is_atom(Module), is_map(Extra) ->
47 512 #packet_handler{ module = Module, extra = Extra }.
48
49 -spec process(Handler :: t(),
50 Acc :: mongoose_acc:t(),
51 From ::jid:jid(),
52 To ::jid:jid(),
53 El :: exml:element()) -> mongoose_acc:t().
54 process(#packet_handler{ module = Module, extra = Extra }, Acc, From, To, El) ->
55 17113 Module:process_packet(Acc, From, To, El, Extra).
56
57 module(#packet_handler{ module = Module }) ->
58 2 Module.
59
60 extra(#packet_handler{ extra = Extra }) ->
61 2 Extra.
62
63 add_extra(#packet_handler{ extra = OldExtra } = Handler, Extra) ->
64 %% KV pairs from the OldExtra map will remain unchanged, only
65 %% the new keys from Extra map will be added to the NewExtra map
66 422 NewExtra = maps:merge(Extra, OldExtra),
67 422 Handler#packet_handler{extra = NewExtra}.
Line Hits Source