-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathts_config_mqtt.erl
124 lines (112 loc) · 5.4 KB
/
ts_config_mqtt.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
%%% This code was developed by Zhihui Jiao(jzhihui521@gmail.com).
%%%
%%% Copyright (C) 2013 Zhihui Jiao
%%%
%%% This program is free software; you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation; either version 2 of the License, or
%%% (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with this program; if not, write to the Free Software
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
%%%
%%% In addition, as a special exception, you have the permission to
%%% link the code of this program with any library released under
%%% the EPL license and distribute linked combinations including
%%% the two; the MPL (Mozilla Public License), which EPL (Erlang
%%% Public License) is based on, is included in this exception.
-module(ts_config_mqtt).
-vc('$Id$ ').
-author('jzhihui521@gmail.com').
-export([parse_config/2]).
-include("ts_profile.hrl").
-include("ts_config.hrl").
-include("ts_mqtt.hrl").
-include("xmerl.hrl").
%%----------------------------------------------------------------------
%% Func: parse_config/2
%% Args: Element, Config
%% Returns: List
%% Purpose: parse a request defined in the XML config file
%%----------------------------------------------------------------------
%% Parsing other elements
parse_config(Element = #xmlElement{name = dyn_variable}, Conf = #config{}) ->
ts_config:parse(Element, Conf);
parse_config(Element = #xmlElement{name = mqtt},
Config = #config{curid = Id, session_tab = Tab,
sessions = [CurS | _], dynvar = DynVar,
subst = SubstFlag, match = MatchRegExp}) ->
Type = ts_config:getAttr(atom, Element#xmlElement.attributes, type),
CleanStart = ts_config:getAttr(atom, Element#xmlElement.attributes,
clean_start, true),
UserName = ts_config:getAttr(string, Element#xmlElement.attributes,
username, undefined),
Password = ts_config:getAttr(string, Element#xmlElement.attributes,
password, undefined),
ClientId = ts_config:getAttr(string, Element#xmlElement.attributes,
client_id, undefined),
KeepAlive = ts_config:getAttr(float_or_integer, Element#xmlElement.attributes,
keepalive, 10),
WillTopic = ts_config:getAttr(string, Element#xmlElement.attributes,
will_topic, ""),
WillQos = ts_config:getAttr(float_or_integer, Element#xmlElement.attributes,
will_qos, 0),
WillMsg = ts_config:getAttr(string, Element#xmlElement.attributes,
will_msg, ""),
WillRetain = ts_config:getAttr(atom, Element#xmlElement.attributes,
will_retain, false),
Topic = ts_config:getAttr(string, Element#xmlElement.attributes, topic, ""),
Qos = ts_config:getAttr(float_or_integer, Element#xmlElement.attributes,
qos, 0),
Retained = ts_config:getAttr(atom, Element#xmlElement.attributes,
retained, false),
Stamped = ts_config:getAttr(atom, Element#xmlElement.attributes,
stamped, false),
RetainValue = case Retained of
true -> 1;
false -> 0
end,
Timeout = ts_config:getAttr(float_or_integer, Element#xmlElement.attributes,
timeout, 1),
Payload = ts_config:getText(Element#xmlElement.content),
Request = #mqtt_request{type = Type, clean_start = CleanStart,
keepalive = KeepAlive, will_topic = WillTopic,
will_qos = WillQos, will_msg = WillMsg,
will_retain = WillRetain, topic = Topic, qos = Qos,
retained = RetainValue, payload = Payload, username = UserName,
password = Password, stamped = Stamped, client_id = ClientId},
Ack = case {Type, Qos} of
{publish, 0} -> no_ack;
{disconnect, _} -> no_ack;
_ -> parse
end,
Msg = #ts_request{ack = Ack,
endpage = true,
dynvar_specs = DynVar,
subst = SubstFlag,
match = MatchRegExp,
param = Request},
ts_config:mark_prev_req(Id-1, Tab, CurS),
case Type of
waitForMessages ->
ets:insert(Tab, {{CurS#session.id, Id},
{thinktime, Timeout * 1000}});
_ ->
ets:insert(Tab, {{CurS#session.id, Id}, Msg })
end,
?LOGF("request tab: ~p~n", [ets:match(Tab, '$1')], ?INFO),
lists:foldl( fun(A, B)->ts_config:parse(A, B) end,
Config#config{dynvar = []},
Element#xmlElement.content);
%% Parsing other elements
parse_config(Element = #xmlElement{}, Conf = #config{}) ->
ts_config:parse(Element,Conf);
%% Parsing non #xmlElement elements
parse_config(_, Conf = #config{}) ->
Conf.