./ct_report/coverage/mongoose_iq.COVER.html

1 %% @doc Functions to work with iq record.
2 %%
3 %%==============================================================================
4 %% Copyright 2015 Erlang Solutions Ltd.
5 %%
6 %% Licensed under the Apache License, Version 2.0 (the "License");
7 %% you may not use this file except in compliance with the License.
8 %% You may obtain a copy of the License at
9 %%
10 %% http://www.apache.org/licenses/LICENSE-2.0
11 %%
12 %% Unless required by applicable law or agreed to in writing, software
13 %% distributed under the License is distributed on an "AS IS" BASIS,
14 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 %% See the License for the specific language governing permissions and
16 %% limitations under the License.
17 %%==============================================================================
18 %%
19 -module(mongoose_iq).
20 -export([iq_to_sub_el/1,
21 empty_result_iq/1]).
22 -export([update_acc_info/1]).
23 -export([info/1, xmlns/1, command/1]).
24
25 -ignore_xref([command/1, empty_result_iq/1, iq_to_sub_el/1, update_acc_info/1]).
26
27 -include("jlib.hrl").
28
29 %% ---------------------------------------------------------
30 %% API
31 %% ---------------------------------------------------------
32
33 iq_to_sub_el(#iq{sub_el = SubEl}) ->
34 2 SubEl.
35
36 empty_result_iq(IQ) ->
37 2 IQ#iq{type = result, sub_el = []}.
38
39 %% Fills mongoose_acc with useful IQ information
40 %% If the info cache is already present, then it is updated only if
41 %% IQ data ref() doesn't match stanza ref() inside acc.
42 -spec update_acc_info(mongoose_acc:t()) -> mongoose_acc:t().
43 update_acc_info(Acc0) ->
44 5581 IQRef = mongoose_acc:get(iq, ref, not_exists, Acc0),
45 5581 case mongoose_acc:stanza_ref(Acc0) of
46 IQRef ->
47 % Up to date
48 424 Acc0;
49 CurrentRef ->
50 5157 El = mongoose_acc:element(Acc0),
51 5157 IQ = jlib:iq_query_or_response_info(El),
52 5157 Acc1 = mongoose_acc:set(iq, record, IQ, Acc0),
53 5157 {XMLNS, Command} = case IQ of
54 #iq{ xmlns = XMLNS0, sub_el = SubEl } ->
55 5145 {XMLNS0, sub_el_to_command(SubEl)};
56 _ ->
57 12 {undefined, undefined}
58 end,
59 5157 Acc2 = mongoose_acc:set(iq, xmlns, XMLNS, Acc1),
60 5157 Acc3 = mongoose_acc:set(iq, command, Command, Acc2),
61 5157 mongoose_acc:set(iq, ref, CurrentRef, Acc3)
62 end.
63
64 %% update_and_get updates only when it is actually necessary
65 %% and adds IQ data if it's missing
66
67 -spec info(mongoose_acc:t()) -> {invalid | not_iq | jlib:iq(), UpdatedAcc :: mongoose_acc:t()}.
68 3619 info(Acc) -> update_and_get(record, Acc).
69
70 -spec xmlns(mongoose_acc:t()) -> {binary() | undefined, UpdatedAcc :: mongoose_acc:t()}.
71 1962 xmlns(Acc) -> update_and_get(xmlns, Acc).
72
73 -spec command(mongoose_acc:t()) -> {binary() | undefined, UpdatedAcc :: mongoose_acc:t()}.
74
:-(
command(Acc) -> update_and_get(command, Acc).
75
76 %% ---------------------------------------------------------
77 %% Internal functions
78 %% ---------------------------------------------------------
79
80 -spec sub_el_to_command([exml:element()] | exml:element()) -> binary() | undefined.
81 967 sub_el_to_command([]) -> undefined;
82 1208 sub_el_to_command([SubEl | _]) -> SubEl#xmlel.name;
83 2970 sub_el_to_command(#xmlel{ name = Name }) -> Name.
84
85 -type iq_acc_field() :: record | xmlns | command.
86
87 -spec update_and_get(iq_acc_field(), mongoose_acc:t()) ->
88 {FieldValue :: any(), mongoose_acc:t()}.
89 update_and_get(Field, Acc0) ->
90 5581 Acc1 = update_acc_info(Acc0),
91 5581 {mongoose_acc:get(iq, Field, Acc1), Acc1}.
Line Hits Source