From 99633fb815832666597ba5bda7cd37e2c4fd91ea Mon Sep 17 00:00:00 2001 From: Ery Lee Date: Wed, 15 Apr 2015 17:21:02 +0800 Subject: [PATCH] emqtt.hrl --- apps/emqtt/include/emqtt.hrl | 69 ++++++++++++++++++++++ apps/emqtt/include/emqtt_packet.hrl | 27 --------- apps/emqtt/src/emqtt.app.src | 4 +- apps/emqtt/src/emqtt.erl | 18 ------ apps/emqtt/src/emqtt_packet.erl | 2 + apps/emqtt/src/emqtt_parser.erl | 2 + apps/emqtt/src/emqtt_serialiser.erl | 2 + apps/emqtt/test/emqtt_parser_tests.erl | 15 ++--- apps/emqtt/test/emqtt_serialiser_tests.erl | 1 + apps/emqttd/src/emqttd_retained.erl | 4 +- rel/reltool.config | 2 +- 11 files changed, 88 insertions(+), 58 deletions(-) create mode 100644 apps/emqtt/include/emqtt.hrl delete mode 100644 apps/emqtt/src/emqtt.erl diff --git a/apps/emqtt/include/emqtt.hrl b/apps/emqtt/include/emqtt.hrl new file mode 100644 index 000000000..1184aab01 --- /dev/null +++ b/apps/emqtt/include/emqtt.hrl @@ -0,0 +1,69 @@ +%%%----------------------------------------------------------------------------- +%%% @Copyright (C) 2012-2015, Feng Lee +%%% +%%% Permission is hereby granted, free of charge, to any person obtaining a copy +%%% of this software and associated documentation files (the "Software"), to deal +%%% in the Software without restriction, including without limitation the rights +%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%%% copies of the Software, and to permit persons to whom the Software is +%%% furnished to do so, subject to the following conditions: +%%% +%%% The above copyright notice and this permission notice shall be included in all +%%% copies or substantial portions of the Software. +%%% +%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +%%% SOFTWARE. +%%%----------------------------------------------------------------------------- +%%% @doc +%%% MQTT Common Header. +%%% +%%% @end +%%%----------------------------------------------------------------------------- + +%%------------------------------------------------------------------------------ +%% MQTT Protocol Version and Levels +%%------------------------------------------------------------------------------ +-define(MQTT_PROTO_V31, 3). +-define(MQTT_PROTO_V311, 4). + +-define(PROTOCOL_NAMES, [ + {?MQTT_PROTO_V31, <<"MQIsdp">>}, + {?MQTT_PROTO_V311, <<"MQTT">>}]). + +-type mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311. + +%%------------------------------------------------------------------------------ +%% QoS Levels +%%------------------------------------------------------------------------------ + +-define(QOS_0, 0). +-define(QOS_1, 1). +-define(QOS_2, 2). + +-define(IS_QOS(I), (I >= ?QOS_0 andalso I =< ?QOS_2)). + +-type mqtt_qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2. + +%%------------------------------------------------------------------------------ +%% MQTT Message +%%------------------------------------------------------------------------------ + +-type mqtt_msgid() :: undefined | 1..16#ffff. + +-record(mqtt_message, { + %% topic is first for message may be retained + topic :: binary(), + qos = ?QOS_0 :: mqtt_qos(), + retain = false :: boolean(), + dup = false :: boolean(), + msgid :: mqtt_msgid(), + payload :: binary() +}). + +-type mqtt_message() :: #mqtt_message{}. + diff --git a/apps/emqtt/include/emqtt_packet.hrl b/apps/emqtt/include/emqtt_packet.hrl index 729eafc65..36442a923 100644 --- a/apps/emqtt/include/emqtt_packet.hrl +++ b/apps/emqtt/include/emqtt_packet.hrl @@ -25,33 +25,6 @@ %%% @end %%%----------------------------------------------------------------------------- --author("feng@emqtt.io"). - -%%------------------------------------------------------------------------------ -%% MQTT Protocol Version and Levels -%%------------------------------------------------------------------------------ --define(MQTT_PROTO_V31, 3). --define(MQTT_PROTO_V311, 4). - --define(PROTOCOL_NAMES, [ - {?MQTT_PROTO_V31, <<"MQIsdp">>}, - {?MQTT_PROTO_V311, <<"MQTT">>}]). - --type mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311. - -%%------------------------------------------------------------------------------ -%% QoS Levels -%%------------------------------------------------------------------------------ - --define(QOS_0, 0). --define(QOS_1, 1). --define(QOS_2, 2). --define(QOS_ERR, 128). - --define(IS_QOS(I), (I >= ?QOS_0 andalso I =< ?QOS_2)). - --type mqtt_qos() :: ?QOS_0 | ?QOS_1 | ?QOS_2. - %%------------------------------------------------------------------------------ %% Max ClientId Length. Why 1024? NiDongDe! %%------------------------------------------------------------------------------ diff --git a/apps/emqtt/src/emqtt.app.src b/apps/emqtt/src/emqtt.app.src index e19a3f14a..ff295a8a3 100644 --- a/apps/emqtt/src/emqtt.app.src +++ b/apps/emqtt/src/emqtt.app.src @@ -2,9 +2,7 @@ [ {description, "Erlang MQTT Common Library"}, {vsn, "0.6.0"}, - {modules, [ - emqtt_parser - ]}, + {modules, []}, {registered, []}, {applications, [ kernel, diff --git a/apps/emqtt/src/emqtt.erl b/apps/emqtt/src/emqtt.erl deleted file mode 100644 index bd733119e..000000000 --- a/apps/emqtt/src/emqtt.erl +++ /dev/null @@ -1,18 +0,0 @@ --module(emqtt). - -%% emqtt: emqtt library's entry point. - --export([my_func/0]). - - -%% API - -my_func() -> - ok(). - -%% Internals - -ok() -> - ok. - -%% End of Module. diff --git a/apps/emqtt/src/emqtt_packet.erl b/apps/emqtt/src/emqtt_packet.erl index 64ead9f8e..460ada30c 100644 --- a/apps/emqtt/src/emqtt_packet.erl +++ b/apps/emqtt/src/emqtt_packet.erl @@ -28,6 +28,8 @@ -author("feng@emqtt.io"). +-include("emqtt.hrl"). + -include("emqtt_packet.hrl"). %% API diff --git a/apps/emqtt/src/emqtt_parser.erl b/apps/emqtt/src/emqtt_parser.erl index defef9e6b..40969c05b 100644 --- a/apps/emqtt/src/emqtt_parser.erl +++ b/apps/emqtt/src/emqtt_parser.erl @@ -28,6 +28,8 @@ -author("feng@emqtt.io"). +-include("emqtt.hrl"). + -include("emqtt_packet.hrl"). %% API diff --git a/apps/emqtt/src/emqtt_serialiser.erl b/apps/emqtt/src/emqtt_serialiser.erl index db729aee2..402b6cba3 100644 --- a/apps/emqtt/src/emqtt_serialiser.erl +++ b/apps/emqtt/src/emqtt_serialiser.erl @@ -28,6 +28,8 @@ -author("feng@emqtt.io"). +-include("emqtt.hrl"). + -include("emqtt_packet.hrl"). %% API diff --git a/apps/emqtt/test/emqtt_parser_tests.erl b/apps/emqtt/test/emqtt_parser_tests.erl index 149fba546..6cb3c9286 100644 --- a/apps/emqtt/test/emqtt_parser_tests.erl +++ b/apps/emqtt/test/emqtt_parser_tests.erl @@ -26,6 +26,7 @@ %%%----------------------------------------------------------------------------- -module(emqtt_parser_tests). +-include("emqtt.hrl"). -include("emqtt_packet.hrl"). -ifdef(TEST). @@ -41,9 +42,9 @@ parse_connect_test() -> dup = false, qos = 0, retain = false}, - variable = #mqtt_packet_connect{proto_ver = 3, + variable = #mqtt_packet_connect{proto_ver = 3, proto_name = <<"MQIsdp">>, - client_id = <<"mosqpub/10451-iMac.loca">>, + clientid = <<"mosqpub/10451-iMac.loca">>, clean_sess = true, keep_alive = 60}}, <<>>}, emqtt_parser:parse(V31ConnBin, State)), %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined) @@ -53,11 +54,11 @@ parse_connect_test() -> dup = false, qos = 0, retain = false}, - variable = #mqtt_packet_connect{proto_ver = 4, + variable = #mqtt_packet_connect{proto_ver = 4, proto_name = <<"MQTT">>, - client_id = <<"mosqpub/10451-iMac.loca">>, + clientid = <<"mosqpub/10451-iMac.loca">>, clean_sess = true, - keep_alive = 60 } }, <<>>}, emqtt_parser:parse(V311ConnBin, State)), + keep_alive = 60 } }, <<>>}, emqtt_parser:parse(V311ConnBin, State)), %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60) V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>, @@ -68,7 +69,7 @@ parse_connect_test() -> retain = false}, variable = #mqtt_packet_connect{proto_ver = 4, proto_name = <<"MQTT">>, - client_id = <<>>, + clientid = <<>>, clean_sess = true, keep_alive = 60 } }, <<>>}, emqtt_parser:parse(V311ConnWithoutClientId, State)), %%CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10452-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=test, Password=******, Will(Qos=1, Retain=false, Topic=/will, Msg=willmsg)) @@ -80,7 +81,7 @@ parse_connect_test() -> retain = false}, variable = #mqtt_packet_connect{proto_ver = 3, proto_name = <<"MQIsdp">>, - client_id = <<"mosqpub/10452-iMac.loca">>, + clientid = <<"mosqpub/10452-iMac.loca">>, clean_sess = true, keep_alive = 60, will_retain = false, diff --git a/apps/emqtt/test/emqtt_serialiser_tests.erl b/apps/emqtt/test/emqtt_serialiser_tests.erl index 03f38833d..5cec36a18 100644 --- a/apps/emqtt/test/emqtt_serialiser_tests.erl +++ b/apps/emqtt/test/emqtt_serialiser_tests.erl @@ -26,6 +26,7 @@ %%%----------------------------------------------------------------------------- -module(emqtt_serialiser_tests). +-include("emqtt.hrl"). -include("emqtt_packet.hrl"). -ifdef(TEST). diff --git a/apps/emqttd/src/emqttd_retained.erl b/apps/emqttd/src/emqttd_retained.erl index c34bd0dde..f4797db82 100644 --- a/apps/emqttd/src/emqttd_retained.erl +++ b/apps/emqttd/src/emqttd_retained.erl @@ -42,13 +42,13 @@ -export([retain/1, redeliver/2]). mnesia(create) -> - ok = emqtt_mnesia:create_table(message, [ + ok = emqttd_mnesia:create_table(message, [ {type, ordered_set}, {ram_copies, [node()]}, {record_name, mqtt_message}, {attributes, record_info(fields, mqtt_message)}]); mnesia(replicate) -> - ok = emqtt_mnesia:copy_table(message). + ok = emqttd_mnesia:copy_table(message). diff --git a/rel/reltool.config b/rel/reltool.config index 9c19a7a65..9d913f6d7 100644 --- a/rel/reltool.config +++ b/rel/reltool.config @@ -47,7 +47,7 @@ {app, lager, [{mod_cond, app}, {incl_cond, include}]}, {app, esockd, [{mod_cond, app}, {incl_cond, include}]}, {app, mochiweb, [{mod_cond, app}, {incl_cond, include}]}, - {app, emqtt, [{mod_cond, app}, {incl_cond, include}]} + {app, emqtt, [{mod_cond, app}, {incl_cond, include}]}, {app, emqttd, [{mod_cond, app}, {incl_cond, include}]} ]}.