Merge branch 'emq22' into proxy-protocol

This commit is contained in:
Feng Lee 2017-03-22 10:18:44 +08:00 committed by GitHub
commit 942edad3c5
96 changed files with 2771 additions and 1671 deletions

3
.gitignore vendored
View File

@ -28,3 +28,6 @@ ct.coverdata
emqttd.iml emqttd.iml
_rel/ _rel/
data/ data/
_build
.rebar3
rebar3.crashdump

View File

@ -1,10 +1,9 @@
language: erlang language: erlang
otp_release: otp_release:
- 18.0 - 19.0
- 18.1 - 19.1
- 18.2.1 - 19.2
- 18.3
script: script:
- make - make

View File

@ -1,23 +1,26 @@
PROJECT = emqttd PROJECT = emqttd
PROJECT_DESCRIPTION = Erlang MQTT Broker PROJECT_DESCRIPTION = Erlang MQTT Broker
PROJECT_VERSION = 2.0.7 PROJECT_VERSION = 2.1.0
DEPS = gproc lager gen_logger esockd mochiweb NO_AUTOPATCH = cuttlefish
dep_gproc = git https://github.com/uwiger/gproc DEPS = gproc lager esockd mochiweb lager_syslog pbkdf2
dep_getopt = git https://github.com/jcomellas/getopt v0.8.2
dep_lager = git https://github.com/basho/lager master dep_gproc = git https://github.com/uwiger/gproc
dep_gen_logger = git https://github.com/emqtt/gen_logger dep_getopt = git https://github.com/jcomellas/getopt v0.8.2
dep_esockd = git https://github.com/emqtt/esockd proxy-protocol dep_lager = git https://github.com/basho/lager master
dep_mochiweb = git https://github.com/emqtt/mochiweb dep_esockd = git https://github.com/emqtt/esockd emq22
dep_mochiweb = git https://github.com/emqtt/mochiweb emq22
dep_lager_syslog = git https://github.com/basho/lager_syslog
dep_pbkdf2 = git https://github.com/comtihon/erlang-pbkdf2.git 2.0.0
ERLC_OPTS += +'{parse_transform, lager_transform}' ERLC_OPTS += +'{parse_transform, lager_transform}'
TEST_DEPS = cuttlefish emqttc BUILD_DEPS = cuttlefish
dep_cuttlefish = git https://github.com/emqtt/cuttlefish dep_cuttlefish = git https://github.com/emqtt/cuttlefish
dep_emqttc = git https://github.com/emqtt/emqttc
NO_AUTOPATCH = cuttlefish TEST_DEPS = emqttc
dep_emqttc = git https://github.com/emqtt/emqttc
TEST_ERLC_OPTS += +debug_info TEST_ERLC_OPTS += +debug_info
TEST_ERLC_OPTS += +'{parse_transform, lager_transform}' TEST_ERLC_OPTS += +'{parse_transform, lager_transform}'
@ -25,9 +28,9 @@ TEST_ERLC_OPTS += +'{parse_transform, lager_transform}'
EUNIT_OPTS = verbose EUNIT_OPTS = verbose
# EUNIT_ERL_OPTS = # EUNIT_ERL_OPTS =
CT_SUITES = emqttd emqttd_access emqttd_lib emqttd_mod emqttd_net \ CT_SUITES = emqttd emqttd_access emqttd_lib emqttd_inflight emqttd_mod \
emqttd_mqueue emqttd_protocol emqttd_topic emqttd_trie \ emqttd_net emqttd_mqueue emqttd_protocol emqttd_topic \
emqttd_vm emqttd_trie emqttd_vm
CT_OPTS = -cover test/ct.cover.spec -erl_args -name emqttd_ct@127.0.0.1 CT_OPTS = -cover test/ct.cover.spec -erl_args -name emqttd_ct@127.0.0.1
@ -38,5 +41,5 @@ include erlang.mk
app:: rebar.config app:: rebar.config
app.config:: app.config::
cuttlefish -l info -e etc/ -c etc/emq.conf -i priv/emq.schema -d data/ ./deps/cuttlefish/cuttlefish -l info -e etc/ -c etc/emq.conf -i priv/emq.schema -d data/

View File

@ -53,7 +53,7 @@ Documentation on [emqtt.io/docs/v2/](http://emqtt.io/docs/v2/install.html), [doc
## Build From Source ## Build From Source
The *EMQ* broker requires Erlang/OTP R18+ to build. The *EMQ* broker requires Erlang/OTP R19+ to build since 2.1 release.
``` ```
git clone https://github.com/emqtt/emq-relx.git git clone https://github.com/emqtt/emq-relx.git
@ -71,6 +71,8 @@ Plugin | Descrip
-----------------------------------------------------------------------|-------------------------------------- -----------------------------------------------------------------------|--------------------------------------
[emq_plugin_template](https://github.com/emqtt/emq_plugin_template) | Plugin template and demo [emq_plugin_template](https://github.com/emqtt/emq_plugin_template) | Plugin template and demo
[emq_dashboard](https://github.com/emqtt/emq_dashboard) | Web Dashboard [emq_dashboard](https://github.com/emqtt/emq_dashboard) | Web Dashboard
[emq_retainer](https://github.com/emqtt/emq_retainer) | Store MQTT Retained Messages
[emq_modules](https://github.com/emqtt/emq_modules) | Presence, Subscription and Rewrite Modules
[emq_auth_username](https://github.com/emqtt/emq_auth_username) | Username/Password Authentication Plugin [emq_auth_username](https://github.com/emqtt/emq_auth_username) | Username/Password Authentication Plugin
[emq_auth_clientid](https://github.com/emqtt/emq_auth_clientid) | ClientId Authentication Plugin [emq_auth_clientid](https://github.com/emqtt/emq_auth_clientid) | ClientId Authentication Plugin
[emq_auth_mysql](https://github.com/emqtt/emq_auth_mysql) | MySQL Authentication/ACL Plugin [emq_auth_mysql](https://github.com/emqtt/emq_auth_mysql) | MySQL Authentication/ACL Plugin
@ -79,10 +81,6 @@ Plugin | Descrip
[emq_auth_mongo](https://github.com/emqtt/emq_auth_mongo) | MongoDB Authentication/ACL Plugin [emq_auth_mongo](https://github.com/emqtt/emq_auth_mongo) | MongoDB Authentication/ACL Plugin
[emq_auth_http](https://github.com/emqtt/emq_auth_http) | Authentication/ACL by HTTP API [emq_auth_http](https://github.com/emqtt/emq_auth_http) | Authentication/ACL by HTTP API
[emq_auth_ldap](https://github.com/emqtt/emq_auth_ldap) | LDAP Authentication Plugin [emq_auth_ldap](https://github.com/emqtt/emq_auth_ldap) | LDAP Authentication Plugin
[emq_mod_presence](https://github.com/emqtt/emq_mod_presence) | Presence Module
[emq_mod_rewrite](https://github.com/emqtt/emq_mod_rewrite) | Rewrite Module
[emq_mod_retainer](https://github.com/emqtt/emq_mod_retainer) | Store MQTT Retained Messages
[emq_mod_subscription](https://github.com/emqtt/emq_mod_subscription) | Subscribe topics when client connected
[emq_sn](https://github.com/emqtt/emq_sn) | MQTT-SN Protocol Plugin [emq_sn](https://github.com/emqtt/emq_sn) | MQTT-SN Protocol Plugin
[emq_coap](https://github.com/emqtt/emq_coap) | CoAP Protocol Plugin [emq_coap](https://github.com/emqtt/emq_coap) | CoAP Protocol Plugin
[emq_stomp](https://github.com/emqtt/emq_stomp) | Stomp Protocol Plugin [emq_stomp](https://github.com/emqtt/emq_stomp) | Stomp Protocol Plugin

View File

@ -1,3 +1,8 @@
##===================================================================
## EMQ Configuration R2.1
##===================================================================
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Node Args ## Node Args
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
@ -39,35 +44,57 @@ node.max_ets_tables = 256000
node.fullsweep_after = 1000 node.fullsweep_after = 1000
## Crash dump ## Crash dump
node.crash_dump = log/crash.dump node.crash_dump = {{ platform_log_dir }}/crash.dump
## Distributed node ticktime ## Distributed node ticktime
node.dist_net_ticktime = 60 node.dist_net_ticktime = 60
## Distributed node port range ## Distributed node port range
## node.dist_listen_min = 6000 ## node.dist_listen_min = 6369
## node.dist_listen_max = 6999 ## node.dist_listen_max = 6369
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Log ## Log
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Set the log dir
log.dir = {{ platform_log_dir }}
## Console log. Enum: off, file, console, both ## Console log. Enum: off, file, console, both
log.console = console log.console = console
## Syslog. Enum: on, off
log.syslog = on
## syslog level. Enum: debug, info, notice, warning, error, critical, alert, emergency
log.syslog.level = error
## Console log level. Enum: debug, info, notice, warning, error, critical, alert, emergency ## Console log level. Enum: debug, info, notice, warning, error, critical, alert, emergency
log.console.level = error log.console.level = error
## Console log file ## Console log file
## log.console.file = log/console.log ## log.console.file = {{ platform_log_dir }}/console.log
## Error log file ## Error log file
log.error.file = log/error.log log.error.file = {{ platform_log_dir }}/error.log
## Enable the crash log. Enum: on, off ## Enable the crash log. Enum: on, off
log.crash = on log.crash = on
log.crash.file = log/crash.log log.crash.file = {{ platform_log_dir }}/crash.log
##--------------------------------------------------------------------
## Allow Anonymous and Default ACL
##--------------------------------------------------------------------
## Allow Anonymous authentication
mqtt.allow_anonymous = true
## Default ACL File
mqtt.acl_file = {{ platform_etc_dir }}/acl.conf
## Cache ACL for PUBLISH
mqtt.cache_acl = true
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Protocol ## MQTT Protocol
@ -79,37 +106,45 @@ mqtt.max_clientid_len = 1024
## Max Packet Size Allowed, 64K by default. ## Max Packet Size Allowed, 64K by default.
mqtt.max_packet_size = 64KB mqtt.max_packet_size = 64KB
##--------------------------------------------------------------------
## MQTT Connection
##--------------------------------------------------------------------
## Force GC: integer. Value 0 disabled the Force GC.
mqtt.conn.force_gc_count = 100
##--------------------------------------------------------------------
## MQTT Client
##--------------------------------------------------------------------
## Client Idle Timeout (Second) ## Client Idle Timeout (Second)
mqtt.client_idle_timeout = 30 mqtt.client.idle_timeout = 30s
## Allow Anonymous authentication ## Enable client Stats: on | off
mqtt.allow_anonymous = true mqtt.client.enable_stats = off
## Default ACL File
mqtt.acl_file = etc/acl.conf
## Cache ACL for PUBLISH
mqtt.cache_acl = true
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Session ## MQTT Session
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Max number of QoS 1 and 2 messages that can be “inflight” at one time. ## Upgrade QoS?
## 0 means no limit mqtt.session.upgrade_qos = off
mqtt.session.max_inflight = 100
## Retry interval for redelivering QoS1/2 messages. ## Max Size of the Inflight Window for QoS1 and QoS2 messages
mqtt.session.retry_interval = 60 ## 0 means no limit
mqtt.session.max_inflight = 32
## Retry Interval for redelivering QoS1/2 messages.
mqtt.session.retry_interval = 20s
## Client -> Broker: Max Packets Awaiting PUBREL, 0 means no limit
mqtt.session.max_awaiting_rel = 100
## Awaiting PUBREL Timeout ## Awaiting PUBREL Timeout
mqtt.session.await_rel_timeout = 20 mqtt.session.await_rel_timeout = 20s
## Max Packets that Awaiting PUBREL, 0 means no limit ## Enable Statistics: on | off
mqtt.session.max_awaiting_rel = 0 mqtt.session.enable_stats = off
## Statistics Collection Interval(seconds)
mqtt.session.collect_interval = 0
## Expired after 1 day: ## Expired after 1 day:
## w - week ## w - week
@ -117,7 +152,7 @@ mqtt.session.collect_interval = 0
## h - hour ## h - hour
## m - minute ## m - minute
## s - second ## s - second
mqtt.session.expired_after = 1d mqtt.session.expiry_interval = 2h
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Queue ## MQTT Queue
@ -172,10 +207,10 @@ mqtt.bridge.ping_down_interval = 1
##------------------------------------------------------------------- ##-------------------------------------------------------------------
## Dir of plugins' config ## Dir of plugins' config
mqtt.plugins.etc_dir = etc/plugins/ mqtt.plugins.etc_dir ={{ platform_etc_dir }}/plugins/
## File to store loaded plugin names. ## File to store loaded plugin names.
mqtt.plugins.loaded_file = data/loaded_plugins mqtt.plugins.loaded_file = {{ platform_data_dir }}/loaded_plugins
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Listeners ## MQTT Listeners
@ -218,12 +253,13 @@ mqtt.listener.ssl.max_clients = 512
mqtt.listener.ssl.proxy_protocol = 1 mqtt.listener.ssl.proxy_protocol = 1
mqtt.listener.ssl.proxy_protocol_timeout = 10 mqtt.listener.ssl.proxy_protocol_timeout = 10
## Configuring SSL Options ## Configuring SSL Options. See http://erlang.org/doc/man/ssl.html
## See http://erlang.org/doc/man/ssl.html ### TLS only for POODLE attack
mqtt.listener.ssl.handshake_timeout = 15 mqtt.listener.ssl.tls_versions = tlsv1.2,tlsv1.1,tlsv1
mqtt.listener.ssl.keyfile = etc/certs/key.pem mqtt.listener.ssl.handshake_timeout = 15s
mqtt.listener.ssl.certfile = etc/certs/cert.pem mqtt.listener.ssl.keyfile = {{ platform_etc_dir }}/certs/key.pem
## mqtt.listener.ssl.cacertfile = etc/certs/cacert.pem mqtt.listener.ssl.certfile = {{ platform_etc_dir }}/certs/cert.pem
## mqtt.listener.ssl.cacertfile = {{ platform_etc_dir }}/certs/cacert.pem
## mqtt.listener.ssl.verify = verify_peer ## mqtt.listener.ssl.verify = verify_peer
## mqtt.listener.ssl.fail_if_no_peer_cert = true ## mqtt.listener.ssl.fail_if_no_peer_cert = true
@ -241,9 +277,10 @@ mqtt.listener.https = 8084
mqtt.listener.https.acceptors = 4 mqtt.listener.https.acceptors = 4
mqtt.listener.https.max_clients = 64 mqtt.listener.https.max_clients = 64
mqtt.listener.https.handshake_timeout = 15 mqtt.listener.https.handshake_timeout = 15
mqtt.listener.https.certfile = etc/certs/cert.pem mqtt.listener.https.keyfile = {{ platform_etc_dir }}/certs/key.pem
mqtt.listener.https.keyfile = etc/certs/key.pem mqtt.listener.https.certfile = {{ platform_etc_dir }}/certs/cert.pem
## mqtt.listener.https.cacertfile = etc/certs/cacert.pem ## mqtt.listener.https.cacertfile = {{ platform_etc_dir }}/certs/cacert.pem
## mqtt.listener.https.verify = verify_peer ## mqtt.listener.https.verify = verify_peer
## mqtt.listener.https.fail_if_no_peer_cert = true ## mqtt.listener.https.fail_if_no_peer_cert = true

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,13 +18,13 @@
%% Banner %% Banner
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(COPYRIGHT, "Copyright (C) 2012-2017, Feng Lee <feng@emqtt.io>"). -define(COPYRIGHT, "Copyright (c) 2013-2017 EMQ Enterprise, Inc.").
-define(LICENSE_MESSAGE, "Licensed under the Apache License, Version 2.0"). -define(LICENSE_MESSAGE, "Licensed under the Apache License, Version 2.0").
-define(PROTOCOL_VERSION, "MQTT/3.1.1"). -define(PROTOCOL_VERSION, "MQTT/5.0").
-define(ERTS_MINIMUM, "7.0"). -define(ERTS_MINIMUM, "8.0").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Sys/Queue/Share Topics' Prefix %% Sys/Queue/Share Topics' Prefix
@ -42,27 +42,28 @@
-type(pubsub() :: publish | subscribe). -type(pubsub() :: publish | subscribe).
-define(PUBSUB(PS), (PS =:= publish orelse PS =:= subscribe)). -define(PS(PS), (PS =:= publish orelse PS =:= subscribe)).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Topic %% MQTT Topic
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_topic, { -record(mqtt_topic,
topic :: binary(), { topic :: binary(),
flags = [] :: [retained | static] flags = [] :: [retained | static]
}). }).
-type(mqtt_topic() :: #mqtt_topic{}). -type(mqtt_topic() :: #mqtt_topic{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Subscription %% MQTT Subscription
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_subscription, {
subid :: binary() | atom(), -record(mqtt_subscription,
topic :: binary(), { subid :: binary() | atom(),
qos :: 0 | 1 | 2 topic :: binary(),
}). qos :: 0 | 1 | 2
}).
-type(mqtt_subscription() :: #mqtt_subscription{}). -type(mqtt_subscription() :: #mqtt_subscription{}).
@ -73,18 +74,18 @@
-type(ws_header_key() :: atom() | binary() | string()). -type(ws_header_key() :: atom() | binary() | string()).
-type(ws_header_val() :: atom() | binary() | string() | integer()). -type(ws_header_val() :: atom() | binary() | string() | integer()).
-record(mqtt_client, { -record(mqtt_client,
client_id :: binary() | undefined, { client_id :: binary() | undefined,
client_pid :: pid(), client_pid :: pid(),
username :: binary() | undefined, username :: binary() | undefined,
peername :: {inet:ip_address(), integer()}, peername :: {inet:ip_address(), inet:port_number()},
clean_sess :: boolean(), clean_sess :: boolean(),
proto_ver :: 3 | 4, proto_ver :: 3 | 4,
keepalive = 0, keepalive = 0,
will_topic :: undefined | binary(), will_topic :: undefined | binary(),
ws_initial_headers :: list({ws_header_key(), ws_header_val()}), ws_initial_headers :: list({ws_header_key(), ws_header_val()}),
connected_at :: erlang:timestamp() connected_at :: erlang:timestamp()
}). }).
-type(mqtt_client() :: #mqtt_client{}). -type(mqtt_client() :: #mqtt_client{}).
@ -92,95 +93,103 @@
%% MQTT Session %% MQTT Session
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_session, { -record(mqtt_session,
client_id :: binary(), { client_id :: binary(),
sess_pid :: pid(), sess_pid :: pid(),
persistent :: boolean() clean_sess :: boolean()
}). }).
-type(mqtt_session() :: #mqtt_session{}). -type(mqtt_session() :: #mqtt_session{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Message %% MQTT Message
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-type(mqtt_msgid() :: binary() | undefined).
-type(mqtt_msg_id() :: binary() | undefined).
-type(mqtt_pktid() :: 1..16#ffff | undefined). -type(mqtt_pktid() :: 1..16#ffff | undefined).
-record(mqtt_message, { -type(mqtt_msg_from() :: atom() | {binary(), undefined | binary()}).
id :: mqtt_msgid(), %% Global unique message ID
pktid :: mqtt_pktid(), %% PacketId -record(mqtt_message,
from :: {binary(), undefined | binary()}, %% ClientId and Username { %% Global unique message ID
topic :: binary(), %% Topic that the message is published to id :: mqtt_msg_id(),
qos = 0 :: 0 | 1 | 2, %% Message QoS %% PacketId
flags = [] :: [retain | dup | sys], %% Message Flags pktid :: mqtt_pktid(),
retain = false :: boolean(), %% Retain flag %% ClientId and Username
dup = false :: boolean(), %% Dup flag from :: mqtt_msg_from(),
sys = false :: boolean(), %% $SYS flag %% Topic that the message is published to
headers = [] :: list(), topic :: binary(),
payload :: binary(), %% Payload %% Message QoS
timestamp :: pos_integer() %% os:timestamp to seconds qos = 0 :: 0 | 1 | 2,
}). %% Message Flags
flags = [] :: [retain | dup | sys],
%% Retain flag
retain = false :: boolean(),
%% Dup flag
dup = false :: boolean(),
%% $SYS flag
sys = false :: boolean(),
%% Headers
headers = [] :: list(),
%% Payload
payload :: binary(),
%% Timestamp
timestamp :: erlang:timestamp()
}).
-type(mqtt_message() :: #mqtt_message{}). -type(mqtt_message() :: #mqtt_message{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Delivery %% MQTT Delivery
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_delivery, {
sender :: pid(), %% Pid of the sender/publisher -record(mqtt_delivery,
message :: mqtt_message(), %% Message { sender :: pid(), %% Pid of the sender/publisher
flows :: list() message :: mqtt_message(), %% Message
}). flows :: list()
}).
-type(mqtt_delivery() :: #mqtt_delivery{}). -type(mqtt_delivery() :: #mqtt_delivery{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Route %% MQTT Route
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_route, {
topic :: binary(), -record(mqtt_route,
node :: node() { topic :: binary(),
}). node :: node()
}).
-type(mqtt_route() :: #mqtt_route{}). -type(mqtt_route() :: #mqtt_route{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Alarm %% MQTT Alarm
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_alarm, {
id :: binary(), -record(mqtt_alarm,
severity :: warning | error | critical, { id :: binary(),
title :: iolist() | binary(), severity :: warning | error | critical,
summary :: iolist() | binary(), title :: iolist() | binary(),
timestamp :: erlang:timestamp() %% Timestamp summary :: iolist() | binary(),
}). timestamp :: erlang:timestamp()
}).
-type(mqtt_alarm() :: #mqtt_alarm{}). -type(mqtt_alarm() :: #mqtt_alarm{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Plugin %% MQTT Plugin
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_plugin, {
name, -record(mqtt_plugin, { name, version, descr, active = false }).
version,
descr,
active = false
}).
-type(mqtt_plugin() :: #mqtt_plugin{}). -type(mqtt_plugin() :: #mqtt_plugin{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT CLI Command %% MQTT CLI Command. For example: 'broker metrics'
%% For example: 'broker metrics'
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_cli, {
name, -record(mqtt_cli, { name, action, args = [], opts = [], usage, descr }).
action,
args = [],
opts = [],
usage,
descr
}).
-type(mqtt_cli() :: #mqtt_cli{}). -type(mqtt_cli() :: #mqtt_cli{}).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,19 +16,18 @@
%% Internal Header File %% Internal Header File
-define(GPROC_POOL(JoinOrLeave, Pool, I), -define(GPROC_POOL(JoinOrLeave, Pool, Id),
(begin (begin
case JoinOrLeave of case JoinOrLeave of
join -> gproc_pool:connect_worker(Pool, {Pool, Id}); join -> gproc_pool:connect_worker(Pool, {Pool, Id});
leave -> gproc_pool:disconnect_worker(Pool, {Pool, I}) leave -> gproc_pool:disconnect_worker(Pool, {Pool, Id})
end end
end)). end)).
-define(PROC_NAME(M, I), (list_to_atom(lists:concat([M, "_", I])))). -define(PROC_NAME(M, I), (list_to_atom(lists:concat([M, "_", I])))).
-define(record_to_proplist(Def, Rec), -define(record_to_proplist(Def, Rec),
lists:zip(record_info(fields, Def), lists:zip(record_info(fields, Def), tl(tuple_to_list(Rec)))).
tl(tuple_to_list(Rec)))).
-define(record_to_proplist(Def, Rec, Fields), -define(record_to_proplist(Def, Rec, Fields),
[{K, V} || {K, V} <- ?record_to_proplist(Def, Rec), [{K, V} || {K, V} <- ?record_to_proplist(Def, Rec),
@ -58,3 +57,5 @@
false-> (FalseFun) false-> (FalseFun)
end)). end)).
-define(FULLSWEEP_OPTS, [{fullsweep_after, 10}]).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,23 +14,32 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Protocol Header %%--------------------------------------------------------------------
%% MQTT SockOpts
%%--------------------------------------------------------------------
-define(MQTT_SOCKOPTS, [binary, {packet, raw}, {reuseaddr, true},
{backlog, 512}, {nodelay, true}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Protocol Version and Levels %% MQTT Protocol Version and Levels
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(MQTT_PROTO_V31, 3).
-define(MQTT_PROTO_V311, 4). -define(MQTT_PROTO_V3, 3).
-define(MQTT_PROTO_V4, 4).
-define(MQTT_PROTO_V5, 5).
-define(PROTOCOL_NAMES, [ -define(PROTOCOL_NAMES, [
{?MQTT_PROTO_V31, <<"MQIsdp">>}, {?MQTT_PROTO_V3, <<"MQIsdp">>},
{?MQTT_PROTO_V311, <<"MQTT">>}]). {?MQTT_PROTO_V4, <<"MQTT">>},
{?MQTT_PROTO_V5, <<"MQTT">>}]).
-type(mqtt_vsn() :: ?MQTT_PROTO_V31 | ?MQTT_PROTO_V311). -type(mqtt_vsn() :: ?MQTT_PROTO_V3 | ?MQTT_PROTO_V4 | ?MQTT_PROTO_V5).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT QoS %% MQTT QoS Level
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(QOS_0, 0). %% At most once -define(QOS_0, 0). %% At most once
-define(QOS_1, 1). %% At least once -define(QOS_1, 1). %% At least once
-define(QOS_2, 2). %% Exactly once -define(QOS_2, 2). %% Exactly once
@ -63,28 +72,31 @@
end). end).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Max ClientId Length. Why 1024? NiDongDe... %% Max ClientId Length. Why 1024?
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(MAX_CLIENTID_LEN, 1024). -define(MAX_CLIENTID_LEN, 1024).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Control Packet Types %% MQTT Control Packet Types
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(RESERVED, 0). %% Reserved
-define(CONNECT, 1). %% Client request to connect to Server -define(RESERVED, 0). %% Reserved
-define(CONNACK, 2). %% Server to Client: Connect acknowledgment -define(CONNECT, 1). %% Client request to connect to Server
-define(PUBLISH, 3). %% Publish message -define(CONNACK, 2). %% Server to Client: Connect acknowledgment
-define(PUBACK, 4). %% Publish acknowledgment -define(PUBLISH, 3). %% Publish message
-define(PUBREC, 5). %% Publish received (assured delivery part 1) -define(PUBACK, 4). %% Publish acknowledgment
-define(PUBREL, 6). %% Publish release (assured delivery part 2) -define(PUBREC, 5). %% Publish received (assured delivery part 1)
-define(PUBCOMP, 7). %% Publish complete (assured delivery part 3) -define(PUBREL, 6). %% Publish release (assured delivery part 2)
-define(SUBSCRIBE, 8). %% Client subscribe request -define(PUBCOMP, 7). %% Publish complete (assured delivery part 3)
-define(SUBACK, 9). %% Server Subscribe acknowledgment -define(SUBSCRIBE, 8). %% Client subscribe request
-define(UNSUBSCRIBE, 10). %% Unsubscribe request -define(SUBACK, 9). %% Server Subscribe acknowledgment
-define(UNSUBACK, 11). %% Unsubscribe acknowledgment -define(UNSUBSCRIBE, 10). %% Unsubscribe request
-define(PINGREQ, 12). %% PING request -define(UNSUBACK, 11). %% Unsubscribe acknowledgment
-define(PINGRESP, 13). %% PING response -define(PINGREQ, 12). %% PING request
-define(DISCONNECT, 14). %% Client is disconnecting -define(PINGRESP, 13). %% PING response
-define(DISCONNECT, 14). %% Client or Server is disconnecting
-define(AUTH, 15). %% Authentication exchange
-define(TYPE_NAMES, [ -define(TYPE_NAMES, [
'CONNECT', 'CONNECT',
@ -100,102 +112,122 @@
'UNSUBACK', 'UNSUBACK',
'PINGREQ', 'PINGREQ',
'PINGRESP', 'PINGRESP',
'DISCONNECT']). 'DISCONNECT',
'AUTH']).
-type(mqtt_packet_type() :: ?RESERVED..?DISCONNECT). -type(mqtt_packet_type() :: ?RESERVED..?DISCONNECT).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Connect Return Codes %% MQTT Connect Return Codes
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(CONNACK_ACCEPT, 0). %% Connection accepted
-define(CONNACK_PROTO_VER, 1). %% Unacceptable protocol version -define(CONNACK_ACCEPT, 0). %% Connection accepted
-define(CONNACK_INVALID_ID, 2). %% Client Identifier is correct UTF-8 but not allowed by the Server -define(CONNACK_PROTO_VER, 1). %% Unacceptable protocol version
-define(CONNACK_SERVER, 3). %% Server unavailable -define(CONNACK_INVALID_ID, 2). %% Client Identifier is correct UTF-8 but not allowed by the Server
-define(CONNACK_CREDENTIALS, 4). %% Username or password is malformed -define(CONNACK_SERVER, 3). %% Server unavailable
-define(CONNACK_AUTH, 5). %% Client is not authorized to connect -define(CONNACK_CREDENTIALS, 4). %% Username or password is malformed
-define(CONNACK_AUTH, 5). %% Client is not authorized to connect
-type(mqtt_connack() :: ?CONNACK_ACCEPT..?CONNACK_AUTH). -type(mqtt_connack() :: ?CONNACK_ACCEPT..?CONNACK_AUTH).
%%--------------------------------------------------------------------
%% Max MQTT Packet Length
%%--------------------------------------------------------------------
-define(MAX_PACKET_SIZE, 16#fffffff).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Parser and Serializer %% MQTT Parser and Serializer
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(MAX_LEN, 16#fffffff).
-define(HIGHBIT, 2#10000000). -define(HIGHBIT, 2#10000000).
-define(LOWBITS, 2#01111111). -define(LOWBITS, 2#01111111).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Packet Fixed Header %% MQTT Packet Fixed Header
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_packet_header, {
type = ?RESERVED :: mqtt_packet_type(), -record(mqtt_packet_header,
dup = false :: boolean(), { type = ?RESERVED :: mqtt_packet_type(),
qos = ?QOS_0 :: mqtt_qos(), dup = false :: boolean(),
retain = false :: boolean()}). qos = ?QOS_0 :: mqtt_qos(),
retain = false :: boolean()
}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Packets %% MQTT Packets
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-type(mqtt_client_id() :: binary()).
-type(mqtt_username() :: binary() | undefined).
-type(mqtt_packet_id() :: 1..16#ffff | undefined).
-record(mqtt_packet_connect, { -type(mqtt_client_id() :: binary()).
client_id = <<>> :: mqtt_client_id(), -type(mqtt_username() :: binary() | undefined).
proto_ver = ?MQTT_PROTO_V311 :: mqtt_vsn(), -type(mqtt_packet_id() :: 1..16#ffff | undefined).
proto_name = <<"MQTT">> :: binary(),
will_retain = false :: boolean(),
will_qos = ?QOS_0 :: mqtt_qos(),
will_flag = false :: boolean(),
clean_sess = false :: boolean(),
keep_alive = 60 :: non_neg_integer(),
will_topic = undefined :: undefined | binary(),
will_msg = undefined :: undefined | binary(),
username = undefined :: undefined | binary(),
password = undefined :: undefined | binary()}).
-record(mqtt_packet_connack, { -record(mqtt_packet_connect,
ack_flags = ?RESERVED :: 0 | 1, { client_id = <<>> :: mqtt_client_id(),
return_code :: mqtt_connack() }). proto_ver = ?MQTT_PROTO_V4 :: mqtt_vsn(),
proto_name = <<"MQTT">> :: binary(),
will_retain = false :: boolean(),
will_qos = ?QOS_1 :: mqtt_qos(),
will_flag = false :: boolean(),
clean_sess = false :: boolean(),
keep_alive = 60 :: non_neg_integer(),
will_topic = undefined :: undefined | binary(),
will_msg = undefined :: undefined | binary(),
username = undefined :: undefined | binary(),
password = undefined :: undefined | binary()
}).
-record(mqtt_packet_publish, { -record(mqtt_packet_connack,
topic_name :: binary(), { ack_flags = ?RESERVED :: 0 | 1,
packet_id :: mqtt_packet_id() }). return_code :: mqtt_connack()
}).
-record(mqtt_packet_puback, { -record(mqtt_packet_publish,
packet_id :: mqtt_packet_id() }). { topic_name :: binary(),
packet_id :: mqtt_packet_id()
}).
-record(mqtt_packet_subscribe, { -record(mqtt_packet_puback,
packet_id :: mqtt_packet_id(), { packet_id :: mqtt_packet_id() }).
topic_table :: list({binary(), mqtt_qos()}) }).
-record(mqtt_packet_unsubscribe, { -record(mqtt_packet_subscribe,
packet_id :: mqtt_packet_id(), { packet_id :: mqtt_packet_id(),
topics :: list(binary()) }). topic_table :: list({binary(), mqtt_qos()})
}).
-record(mqtt_packet_suback, { -record(mqtt_packet_unsubscribe,
packet_id :: mqtt_packet_id(), { packet_id :: mqtt_packet_id(),
qos_table :: list(mqtt_qos() | 128) }). topics :: list(binary())
}).
-record(mqtt_packet_unsuback, { -record(mqtt_packet_suback,
packet_id :: mqtt_packet_id() }). { packet_id :: mqtt_packet_id(),
qos_table :: list(mqtt_qos() | 128)
}).
-record(mqtt_packet_unsuback,
{ packet_id :: mqtt_packet_id() }).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Control Packet %% MQTT Control Packet
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-record(mqtt_packet, {
header :: #mqtt_packet_header{},
variable :: #mqtt_packet_connect{} | #mqtt_packet_connack{}
| #mqtt_packet_publish{} | #mqtt_packet_puback{}
| #mqtt_packet_subscribe{} | #mqtt_packet_suback{}
| #mqtt_packet_unsubscribe{} | #mqtt_packet_unsuback{}
| mqtt_packet_id() | undefined,
payload :: binary() | undefined }).
-type mqtt_packet() :: #mqtt_packet{}. -record(mqtt_packet,
{ header :: #mqtt_packet_header{},
variable :: #mqtt_packet_connect{} | #mqtt_packet_connack{}
| #mqtt_packet_publish{} | #mqtt_packet_puback{}
| #mqtt_packet_subscribe{} | #mqtt_packet_suback{}
| #mqtt_packet_unsubscribe{} | #mqtt_packet_unsuback{}
| mqtt_packet_id() | undefined,
payload :: binary() | undefined
}).
-type(mqtt_packet() :: #mqtt_packet{}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Packet Match %% MQTT Packet Match
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-define(CONNECT_PACKET(Var), -define(CONNECT_PACKET(Var),
#mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, variable = Var}). #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT}, variable = Var}).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,20 +16,20 @@
-type(trie_node_id() :: binary() | atom()). -type(trie_node_id() :: binary() | atom()).
-record(trie_node, { -record(trie_node,
node_id :: trie_node_id(), { node_id :: trie_node_id(),
edge_count = 0 :: non_neg_integer(), edge_count = 0 :: non_neg_integer(),
topic :: binary() | undefined, topic :: binary() | undefined,
flags :: [retained | static] flags :: [retained | static]
}). }).
-record(trie_edge, { -record(trie_edge,
node_id :: trie_node_id(), { node_id :: trie_node_id(),
word :: binary() | atom() word :: binary() | atom()
}). }).
-record(trie, { -record(trie,
edge :: #trie_edge{}, { edge :: #trie_edge{},
node_id :: trie_node_id() node_id :: trie_node_id()
}). }).

View File

@ -132,14 +132,14 @@ end}.
%% @doc http://www.erlang.org/doc/man/kernel_app.html %% @doc http://www.erlang.org/doc/man/kernel_app.html
{mapping, "node.dist_listen_min", "kernel.inet_dist_listen_min", [ {mapping, "node.dist_listen_min", "kernel.inet_dist_listen_min", [
{commented, 6000}, {commented, 6369},
{datatype, integer}, {datatype, integer},
hidden hidden
]}. ]}.
%% @see node.dist_listen_min %% @see node.dist_listen_min
{mapping, "node.dist_listen_max", "kernel.inet_dist_listen_max", [ {mapping, "node.dist_listen_max", "kernel.inet_dist_listen_max", [
{commented, 6999}, {commented, 6369},
{datatype, integer}, {datatype, integer},
hidden hidden
]}. ]}.
@ -148,8 +148,13 @@ end}.
%% Log %% Log
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
{mapping, "log.dir", "lager.log_dir", [
{default, "log"},
{datatype, string}
]}.
{mapping, "log.console", "lager.handlers", [ {mapping, "log.console", "lager.handlers", [
{default, file }, {default, file},
{datatype, {enum, [off, file, console, both]}} {datatype, {enum, [off, file, console, both]}}
]}. ]}.
@ -168,6 +173,26 @@ end}.
{datatype, file} {datatype, file}
]}. ]}.
{mapping, "log.syslog", "lager.handlers", [
{default, off},
{datatype, flag}
]}.
{mapping, "log.syslog.identity", "lager.handlers", [
{default, "emq"},
{datatype, string}
]}.
{mapping, "log.syslog.facility", "lager.handlers", [
{default, local0},
{datatype, {enum, [daemon, local0, local1, local2, local3, local4, local5, local6, local7]}}
]}.
{mapping, "log.syslog.level", "lager.handlers", [
{default, err},
{datatype, {enum, [debug, info, notice, warning, error, critical, alert, emergency]}}
]}.
{mapping, "log.error.redirect", "lager.error_logger_redirect", [ {mapping, "log.error.redirect", "lager.error_logger_redirect", [
{default, on}, {default, on},
{datatype, flag}, {datatype, flag},
@ -209,7 +234,16 @@ end}.
both -> [ConsoleHandler, ConsoleFileHandler]; both -> [ConsoleHandler, ConsoleFileHandler];
_ -> [] _ -> []
end, end,
ConsoleHandlers ++ ErrorHandler
SyslogHandler = case cuttlefish:conf_get("log.syslog", Conf) of
false -> [];
true -> [{lager_syslog_backend,
[cuttlefish:conf_get("log.syslog.identity", Conf),
cuttlefish:conf_get("log.syslog.facility", Conf),
cuttlefish:conf_get("log.syslog.level", Conf)]}]
end,
ConsoleHandlers ++ ErrorHandler ++ SyslogHandler
end end
}. }.
@ -240,33 +274,9 @@ end}.
]}. ]}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Protocol %% Allow Anonymous and Default ACL
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Set the Max ClientId Length Allowed.
{mapping, "mqtt.max_clientid_len", "emqttd.protocol", [
{default, 1024},
{datatype, integer}
]}.
%% @doc Max Packet Size Allowed, 64K by default.
{mapping, "mqtt.max_packet_size", "emqttd.protocol", [
{default, "64KB"},
{datatype, bytesize}
]}.
%% @doc Client Idle Timeout.
{mapping, "mqtt.client_idle_timeout", "emqttd.protocol", [
{default, 30},
{datatype, integer}
]}.
{translation, "emqttd.protocol", fun(Conf) ->
[{max_clientid_len, cuttlefish:conf_get("mqtt.max_clientid_len", Conf)},
{max_packet_size, cuttlefish:conf_get("mqtt.max_packet_size", Conf)},
{client_idle_timeout, cuttlefish:conf_get("mqtt.client_idle_timeout", Conf)}]
end}.
%% @doc Allow Anonymous %% @doc Allow Anonymous
{mapping, "mqtt.allow_anonymous", "emqttd.allow_anonymous", [ {mapping, "mqtt.allow_anonymous", "emqttd.allow_anonymous", [
{default, false}, {default, false},
@ -285,10 +295,68 @@ end}.
{datatype, {enum, [true, false]}} {datatype, {enum, [true, false]}}
]}. ]}.
%%--------------------------------------------------------------------
%% MQTT Protocol
%%--------------------------------------------------------------------
%% @doc Set the Max ClientId Length Allowed.
{mapping, "mqtt.max_clientid_len", "emqttd.protocol", [
{default, 1024},
{datatype, integer}
]}.
%% @doc Max Packet Size Allowed, 64K by default.
{mapping, "mqtt.max_packet_size", "emqttd.protocol", [
{default, "64KB"},
{datatype, bytesize}
]}.
{translation, "emqttd.protocol", fun(Conf) ->
[{max_clientid_len, cuttlefish:conf_get("mqtt.max_clientid_len", Conf)},
{max_packet_size, cuttlefish:conf_get("mqtt.max_packet_size", Conf)}]
end}.
%%--------------------------------------------------------------------
%% MQTT Connection
%%--------------------------------------------------------------------
%% @doc Force the client to GC: integer
{mapping, "mqtt.conn.force_gc_count", "emqttd.conn_force_gc_count", [
{datatype, integer}
]}.
%%--------------------------------------------------------------------
%% MQTT Client
%%--------------------------------------------------------------------
%% @doc Client Idle Timeout.
{mapping, "mqtt.client.idle_timeout", "emqttd.client", [
{default, "30s"},
{datatype, {duration, ms}}
]}.
%% @doc Enable Stats of Client.
{mapping, "mqtt.client.enable_stats", "emqttd.client", [
{default, off},
{datatype, flag}
]}.
%% @doc Client
{translation, "emqttd.client", fun(Conf) ->
[{client_idle_timeout, cuttlefish:conf_get("mqtt.client.idle_timeout", Conf)},
{client_enable_stats, cuttlefish:conf_get("mqtt.client.enable_stats", Conf)}]
end}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Session %% MQTT Session
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Upgrade QoS?
{mapping, "mqtt.session.upgrade_qos", "emqttd.session", [
{default, off},
{datatype, flag}
]}.
%% @doc Max number of QoS 1 and 2 messages that can be “inflight” at one time. %% @doc Max number of QoS 1 and 2 messages that can be “inflight” at one time.
%% 0 means no limit %% 0 means no limit
{mapping, "mqtt.session.max_inflight", "emqttd.session", [ {mapping, "mqtt.session.max_inflight", "emqttd.session", [
@ -296,17 +364,10 @@ end}.
{datatype, integer} {datatype, integer}
]}. ]}.
%% @doc Retry interval for redelivering QoS1/2 messages. %% @doc Retry interval for redelivering QoS1/2 messages.
{mapping, "mqtt.session.retry_interval", "emqttd.session", [ {mapping, "mqtt.session.retry_interval", "emqttd.session", [
{default, 60}, {default, "20s"},
{datatype, integer} {datatype, {duration, ms}}
]}.
%% @doc Awaiting PUBREL Timeout
{mapping, "mqtt.session.await_rel_timeout", "emqttd.session", [
{default, 30},
{datatype, integer}
]}. ]}.
%% @doc Max Packets that Awaiting PUBREL, 0 means no limit %% @doc Max Packets that Awaiting PUBREL, 0 means no limit
@ -315,25 +376,32 @@ end}.
{datatype, integer} {datatype, integer}
]}. ]}.
%% @doc Statistics Collection Interval(seconds) %% @doc Awaiting PUBREL Timeout
{mapping, "mqtt.session.collect_interval", "emqttd.session", [ {mapping, "mqtt.session.await_rel_timeout", "emqttd.session", [
{default, 0}, {default, "20s"},
{datatype, integer} {datatype, {duration, ms}}
]}. ]}.
%% @doc Session expired after... %% @doc Enable Stats
{mapping, "mqtt.session.expired_after", "emqttd.session", [ {mapping, "mqtt.session.enable_stats", "emqttd.session", [
{default, "2d"}, {default, off},
{datatype, {duration, s}} {datatype, flag}
]}.
%% @doc Session Expiry Interval
{mapping, "mqtt.session.expiry_interval", "emqttd.session", [
{default, "2h"},
{datatype, {duration, ms}}
]}. ]}.
{translation, "emqttd.session", fun(Conf) -> {translation, "emqttd.session", fun(Conf) ->
[{max_inflight, cuttlefish:conf_get("mqtt.session.max_inflight", Conf)}, [{upgrade_qos, cuttlefish:conf_get("mqtt.session.upgrade_qos", Conf)},
{retry_interval, cuttlefish:conf_get("mqtt.session.retry_interval", Conf)}, {max_inflight, cuttlefish:conf_get("mqtt.session.max_inflight", Conf)},
{retry_interval, cuttlefish:conf_get("mqtt.session.retry_interval", Conf)},
{max_awaiting_rel, cuttlefish:conf_get("mqtt.session.max_awaiting_rel", Conf)},
{await_rel_timeout, cuttlefish:conf_get("mqtt.session.await_rel_timeout", Conf)}, {await_rel_timeout, cuttlefish:conf_get("mqtt.session.await_rel_timeout", Conf)},
{max_awaiting_rel, cuttlefish:conf_get("mqtt.session.max_awaiting_rel", Conf)}, {enable_stats, cuttlefish:conf_get("mqtt.session.enable_stats", Conf)},
{collect_interval, cuttlefish:conf_get("mqtt.session.collect_interval", Conf)}, {expiry_interval, cuttlefish:conf_get("mqtt.session.expiry_interval", Conf)}]
{expired_after, cuttlefish:conf_get("mqtt.session.expired_after", Conf)}]
end}. end}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -517,6 +585,11 @@ end}.
hidden hidden
]}. ]}.
{mapping, "mqtt.listener.tcp.tune_buffer", "emqttd.listeners", [
{default, off},
{datatype, flag}
]}.
{mapping, "mqtt.listener.tcp.nodelay", "emqttd.listeners", [ {mapping, "mqtt.listener.tcp.nodelay", "emqttd.listeners", [
{datatype, {enum, [true, false]}}, {datatype, {enum, [true, false]}},
hidden hidden
@ -542,21 +615,22 @@ end}.
]}. ]}.
{mapping, "mqtt.listener.ssl.proxy_protocol", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl.proxy_protocol", "emqttd.listeners", [
{default, 1}, {default, off},
{datatype, integer}, {datatype, flag}
{validators, ["range:1-2"]},
hidden
]}. ]}.
{mapping, "mqtt.listener.ssl.proxy_protocol_timeout", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl.proxy_protocol_timeout", "emqttd.listeners", [
{default, 10}, {default, 5s},
{datatype, integer}, {datatype, {duration, ms}}
hidden ]}.
{mapping, "mqtt.listener.ssl.tls_versions", "emqttd.listeners", [
{datatype, string}
]}. ]}.
{mapping, "mqtt.listener.ssl.handshake_timeout", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl.handshake_timeout", "emqttd.listeners", [
{default, 15}, {default, "15s"},
{datatype, integer} {datatype, {duration, ms}}
]}. ]}.
{mapping, "mqtt.listener.ssl.keyfile", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl.keyfile", "emqttd.listeners", [
@ -663,7 +737,8 @@ end}.
Filter = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end, Filter = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end,
LisOpts = fun(Prefix) -> LisOpts = fun(Prefix) ->
Filter([{acceptors, cuttlefish:conf_get(Prefix ++ ".acceptors", Conf)}, Filter([{acceptors, cuttlefish:conf_get(Prefix ++ ".acceptors", Conf)},
{max_clients, cuttlefish:conf_get(Prefix ++ ".max_clients", Conf)}]) {max_clients, cuttlefish:conf_get(Prefix ++ ".max_clients", Conf)},
{tune_buffer, cuttlefish:conf_get(Prefix ++ ".tune_buffer", Conf, undefined)}])
end, end,
TcpOpts = fun(Prefix) -> TcpOpts = fun(Prefix) ->
Filter([{backlog, cuttlefish:conf_get(Prefix ++ ".backlog", Conf, undefined)}, Filter([{backlog, cuttlefish:conf_get(Prefix ++ ".backlog", Conf, undefined)},
@ -672,8 +747,16 @@ end}.
{buffer, cuttlefish:conf_get(Prefix ++ ".buffer", Conf, undefined)}, {buffer, cuttlefish:conf_get(Prefix ++ ".buffer", Conf, undefined)},
{nodelay, cuttlefish:conf_get(Prefix ++ ".nodelay", Conf, true)}]) {nodelay, cuttlefish:conf_get(Prefix ++ ".nodelay", Conf, true)}])
end, end,
SplitFun = fun(undefined) -> undefined; (S) -> string:tokens(S, ",") end,
SslOpts = fun(Prefix) -> SslOpts = fun(Prefix) ->
Filter([{handshake_timeout, cuttlefish:conf_get(Prefix ++ ".handshake_timeout", Conf) * 1000}, Versions = case SplitFun(cuttlefish:conf_get(Prefix ++ ".tls_versions", Conf, undefined)) of
undefined -> undefined;
L -> [list_to_atom(V) || V <- L]
end,
Filter([{versions, Versions},
{handshake_timeout, cuttlefish:conf_get(Prefix ++ ".handshake_timeout", Conf), undefined},
{keyfile, cuttlefish:conf_get(Prefix ++ ".keyfile", Conf, undefined)}, {keyfile, cuttlefish:conf_get(Prefix ++ ".keyfile", Conf, undefined)},
{certfile, cuttlefish:conf_get(Prefix ++ ".certfile", Conf, undefined)}, {certfile, cuttlefish:conf_get(Prefix ++ ".certfile", Conf, undefined)},
{cacertfile, cuttlefish:conf_get(Prefix ++ ".cacertfile", Conf, undefined)}, {cacertfile, cuttlefish:conf_get(Prefix ++ ".cacertfile", Conf, undefined)},
@ -689,13 +772,10 @@ end}.
Port -> Port ->
ConnOpts = Filter([{rate_limit, cuttlefish:conf_get(Key ++ ".rate_limit", Conf, undefined)}, ConnOpts = Filter([{rate_limit, cuttlefish:conf_get(Key ++ ".rate_limit", Conf, undefined)},
{proxy_protocol, cuttlefish:conf_get(Key ++ ".proxy_protocol", Conf, undefined)}, {proxy_protocol, cuttlefish:conf_get(Key ++ ".proxy_protocol", Conf, undefined)},
{proxy_protocol_timeout, case cuttlefish:conf_get(Key ++ ".proxy_protocol_timeout", Conf, undefined) of {proxy_protocol_timeout, cuttlefish:conf_get(Key ++ ".proxy_protocol_timeout", Conf, undefined)}]),
undefined -> undefined;
I -> I * 1000
end}]),
Opts = [{connopts, ConnOpts}, {sockopts, TcpOpts(Key)} | LisOpts(Key)], Opts = [{connopts, ConnOpts}, {sockopts, TcpOpts(Key)} | LisOpts(Key)],
[{Name, Port, case Name =:= ssl orelse Name =:= https of [{Name, Port, case Name =:= ssl orelse Name =:= https of
true -> [{ssl, SslOpts(Key)} | Opts]; true -> [{sslopts, SslOpts(Key)} | Opts];
false -> Opts false -> Opts
end}] end}]
end end

View File

@ -1,4 +1,4 @@
{deps, [ {deps, [
{gproc,".*",{git,"https://github.com/uwiger/gproc",""}},{lager,".*",{git,"https://github.com/basho/lager","master"}},{gen_logger,".*",{git,"https://github.com/emqtt/gen_logger",""}},{esockd,".*",{git,"https://github.com/emqtt/esockd","master"}},{mochiweb,".*",{git,"https://github.com/emqtt/mochiweb",""}} {gproc,".*",{git,"https://github.com/uwiger/gproc",""}},{lager,".*",{git,"https://github.com/basho/lager","master"}},{esockd,".*",{git,"https://github.com/emqtt/esockd","v4.2"}},{mochiweb,".*",{git,"https://github.com/emqtt/mochiweb",""}},{lager_syslog,".*",{git,"https://github.com/basho/lager_syslog",""}},{pbkdf2,".*",{git,"https://github.com/comtihon/erlang-pbkdf2.git","2.0.0"}}
]}. ]}.
{erl_opts, [{parse_transform,lager_transform}]}. {erl_opts, [{parse_transform,lager_transform}]}.

32
rebar.lock Normal file
View File

@ -0,0 +1,32 @@
[{<<"esockd">>,
{git,"https://github.com/emqtt/esockd",
{ref,"6ef597f16ce242fe37ae019d6ff5214f7a784c80"}},
0},
{<<"gen_logger">>,
{git,"https://github.com/emqtt/gen_logger.git",
{ref,"f6e9f2f373d99f41ffe0579ab5a5f3b19472c9c5"}},
1},
{<<"goldrush">>,
{git,"https://github.com/basho/goldrush.git",
{ref,"8f1b715d36b650ec1e1f5612c00e28af6ab0de82"}},
1},
{<<"gproc">>,
{git,"https://github.com/uwiger/gproc",
{ref,"01c8fbfdd5e4701e8e4b57b0c8279872f9574b0b"}},
0},
{<<"lager">>,
{git,"https://github.com/basho/lager",
{ref,"81eaef0ce98fdbf64ab95665e3bc2ec4b24c7dac"}},
0},
{<<"lager_syslog">>,
{git,"https://github.com/basho/lager_syslog",
{ref,"126dd0284fcac9b01613189a82facf8d803411a2"}},
0},
{<<"mochiweb">>,
{git,"https://github.com/emqtt/mochiweb",
{ref,"af27c0c90bf4c1bfeae0290e4c541264b69f7168"}},
0},
{<<"syslog">>,
{git,"git://github.com/Vagabond/erlang-syslog",
{ref,"0e4f0e95c361af298c5d1d17ceccfa831efc036d"}},
1}].

12
src/emqttd.app.src Normal file
View File

@ -0,0 +1,12 @@
{application, emqttd, [
{description, "Erlang MQTT Broker"},
{vsn, "2.1.0"},
{modules, []},
{registered, [emqttd_sup]},
{applications, [kernel,stdlib,gproc,lager,esockd,mochiweb,lager_syslog,pbkdf2]},
{env, []},
{mod, {emqttd_app, []}},
{maintainers, ["Feng Lee <feng@emqtt.io>"]},
{licenses, ["MIT"]},
{links, [{"Github", "https://github.com/emqtt/emqttd"}]}
]}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,10 +14,12 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Facade Module for The EMQ Broker %% @doc EMQ Main Module.
-module(emqttd). -module(emqttd).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -136,25 +138,28 @@ subscriber_down(Subscriber) ->
%% Hooks API %% Hooks API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(hook(atom(), function(), list(any())) -> ok | {error, any()}). -spec(hook(atom(), function() | {emqttd_hooks:hooktag(), function()}, list(any()))
hook(Hook, Function, InitArgs) -> -> ok | {error, any()}).
emqttd_hook:add(Hook, Function, InitArgs). hook(Hook, TagFunction, InitArgs) ->
emqttd_hooks:add(Hook, TagFunction, InitArgs).
-spec(hook(atom(), function(), list(any()), integer()) -> ok | {error, any()}). -spec(hook(atom(), function() | {emqttd_hooks:hooktag(), function()}, list(any()), integer())
hook(Hook, Function, InitArgs, Priority) -> -> ok | {error, any()}).
emqttd_hook:add(Hook, Function, InitArgs, Priority). hook(Hook, TagFunction, InitArgs, Priority) ->
emqttd_hooks:add(Hook, TagFunction, InitArgs, Priority).
-spec(unhook(atom(), function()) -> ok | {error, any()}). -spec(unhook(atom(), function() | {emqttd_hooks:hooktag(), function()})
unhook(Hook, Function) -> -> ok | {error, any()}).
emqttd_hook:delete(Hook, Function). unhook(Hook, TagFunction) ->
emqttd_hooks:delete(Hook, TagFunction).
-spec(run_hooks(atom(), list(any())) -> ok | stop). -spec(run_hooks(atom(), list(any())) -> ok | stop).
run_hooks(Hook, Args) -> run_hooks(Hook, Args) ->
emqttd_hook:run(Hook, Args). emqttd_hooks:run(Hook, Args).
-spec(run_hooks(atom(), list(any()), any()) -> {ok | stop, any()}). -spec(run_hooks(atom(), list(any()), any()) -> {ok | stop, any()}).
run_hooks(Hook, Args, Acc) -> run_hooks(Hook, Args, Acc) ->
emqttd_hook:run(Hook, Args, Acc). emqttd_hooks:run(Hook, Args, Acc).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Debug %% Debug

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,29 +16,25 @@
-module(emqttd_access_control). -module(emqttd_access_control).
-include("emqttd.hrl").
-behaviour(gen_server). -behaviour(gen_server).
-define(SERVER, ?MODULE). -author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl").
%% API Function Exports %% API Function Exports
-export([start_link/0, -export([start_link/0, auth/2, check_acl/3, reload_acl/0, lookup_mods/1,
auth/2, % authentication register_mod/3, register_mod/4, unregister_mod/2, stop/0]).
check_acl/3, % acl check
reload_acl/0, % reload acl
lookup_mods/1,
register_mod/3, register_mod/4,
unregister_mod/2,
stop/0]).
%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-define(ACCESS_CONTROL_TAB, mqtt_access_control). -define(ACCESS_CONTROL_TAB, mqtt_access_control).
-type password() :: undefined | binary(). -type(password() :: undefined | binary()).
-record(state, {}). -record(state, {}).
@ -74,7 +70,7 @@ auth(Client, Password, [{Mod, State, _Seq} | Mods]) ->
Client :: mqtt_client(), Client :: mqtt_client(),
PubSub :: pubsub(), PubSub :: pubsub(),
Topic :: binary()). Topic :: binary()).
check_acl(Client, PubSub, Topic) when ?PUBSUB(PubSub) -> check_acl(Client, PubSub, Topic) when ?PS(PubSub) ->
case lookup_mods(acl) of case lookup_mods(acl) of
[] -> case emqttd:env(allow_anonymous, false) of [] -> case emqttd:env(allow_anonymous, false) of
true -> allow; true -> allow;

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,21 +16,24 @@
-module(emqttd_access_rule). -module(emqttd_access_rule).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-type who() :: all | binary() |
-type(who() :: all | binary() |
{ipaddr, esockd_cidr:cidr_string()} | {ipaddr, esockd_cidr:cidr_string()} |
{client, binary()} | {client, binary()} |
{user, binary()}. {user, binary()}).
-type access() :: subscribe | publish | pubsub. -type(access() :: subscribe | publish | pubsub).
-type topic() :: binary(). -type(topic() :: binary()).
-type rule() :: {allow, all} | -type(rule() :: {allow, all} |
{allow, who(), access(), list(topic())} | {allow, who(), access(), list(topic())} |
{deny, all} | {deny, all} |
{deny, who(), access(), list(topic())}. {deny, who(), access(), list(topic())}).
-export_type([rule/0]). -export_type([rule/0]).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,7 +18,10 @@
-behaviour(emqttd_acl_mod). -behaviour(emqttd_acl_mod).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_cli.hrl").
-export([all_rules/0]). -export([all_rules/0]).
@ -112,7 +115,7 @@ reload_acl(#state{config = undefined}) ->
reload_acl(State) -> reload_acl(State) ->
case catch load_rules_from_file(State) of case catch load_rules_from_file(State) of
{'EXIT', Error} -> {error, Error}; {'EXIT', Error} -> {error, Error};
true -> ok true -> ?PRINT("~s~n", ["reload acl_internal successfully"]), ok
end. end.
%% @doc ACL Module Description %% @doc ACL Module Description

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_acl_mod). -module(emqttd_acl_mod).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -26,10 +28,9 @@
-callback(init(AclOpts :: list()) -> {ok, State :: any()}). -callback(init(AclOpts :: list()) -> {ok, State :: any()}).
-callback(check_acl({Client, PubSub, Topic}, State :: any()) -> allow | deny | ignore when -callback(check_acl({Client :: mqtt_client(),
Client :: mqtt_client(), PubSub :: pubsub(),
PubSub :: pubsub(), Topic :: binary()}, State :: any()) -> allow | deny | ignore).
Topic :: binary()).
-callback(reload_acl(State :: any()) -> ok | {error, any()}). -callback(reload_acl(State :: any()) -> ok | {error, any()}).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_alarm). -module(emqttd_alarm).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(gen_event). -behaviour(gen_event).
-include("emqttd.hrl"). -include("emqttd.hrl").
@ -85,17 +87,17 @@ handle_event({set_alarm, Alarm = #mqtt_alarm{id = AlarmId,
severity = Severity, severity = Severity,
title = Title, title = Title,
summary = Summary}}, Alarms)-> summary = Summary}}, Alarms)->
Timestamp = os:timestamp(), TS = os:timestamp(),
Json = mochijson2:encode([{id, AlarmId}, Json = mochijson2:encode([{id, AlarmId},
{severity, Severity}, {severity, Severity},
{title, iolist_to_binary(Title)}, {title, iolist_to_binary(Title)},
{summary, iolist_to_binary(Summary)}, {summary, iolist_to_binary(Summary)},
{ts, emqttd_time:now_to_secs(Timestamp)}]), {ts, emqttd_time:now_secs(TS)}]),
emqttd:publish(alarm_msg(alert, AlarmId, Json)), emqttd:publish(alarm_msg(alert, AlarmId, Json)),
{ok, [Alarm#mqtt_alarm{timestamp = Timestamp} | Alarms]}; {ok, [Alarm#mqtt_alarm{timestamp = TS} | Alarms]};
handle_event({clear_alarm, AlarmId}, Alarms) -> handle_event({clear_alarm, AlarmId}, Alarms) ->
Json = mochijson2:encode([{id, AlarmId}, {ts, emqttd_time:now_to_secs()}]), Json = mochijson2:encode([{id, AlarmId}, {ts, emqttd_time:now_secs()}]),
emqttd:publish(alarm_msg(clear, AlarmId, Json)), emqttd:publish(alarm_msg(clear, AlarmId, Json)),
{ok, lists:keydelete(AlarmId, 2, Alarms), hibernate}; {ok, lists:keydelete(AlarmId, 2, Alarms), hibernate};

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,29 +18,26 @@
-behaviour(application). -behaviour(application).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd_cli.hrl"). -include("emqttd_cli.hrl").
-include("emqttd_protocol.hrl").
%% Application callbacks %% Application callbacks
-export([start/2, stop/1]). -export([start/2, stop/1]).
-export([start_listener/1, stop_listener/1]). -export([start_listener/1, stop_listener/1]).
%% MQTT SockOpts -type(listener() :: {atom(), esockd:listen_on(), [esockd:option()]}).
-define(MQTT_SOCKOPTS, [binary, {packet, raw}, {reuseaddr, true},
{backlog, 512}, {nodelay, true}]).
-type listener() :: {atom(), esockd:listen_on(), [esockd:option()]}. -define(APP, emqttd).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Application callbacks %% Application callbacks
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(start(StartType, StartArgs) -> {ok, pid()} | {ok, pid(), State} | {error, Reason} when start(_Type, _Args) ->
StartType :: normal | {takeover, node()} | {failover, node()},
StartArgs :: term(),
State :: term(),
Reason :: term()).
start(_StartType, _StartArgs) ->
print_banner(), print_banner(),
emqttd_mnesia:start(), emqttd_mnesia:start(),
{ok, Sup} = emqttd_sup:start_link(), {ok, Sup} = emqttd_sup:start_link(),
@ -63,12 +60,11 @@ stop(_State) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
print_banner() -> print_banner() ->
?PRINT("starting emqttd on node '~s'~n", [node()]). ?PRINT("starting ~s on node '~s'~n", [?APP, node()]).
print_vsn() -> print_vsn() ->
{ok, Vsn} = application:get_key(vsn), {ok, Vsn} = application:get_key(vsn),
{ok, Desc} = application:get_key(description), ?PRINT("~s ~s is running now~n", [?APP, Vsn]).
?PRINT("~s ~s is running now~n", [Desc, Vsn]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Start Servers %% Start Servers
@ -76,7 +72,7 @@ print_vsn() ->
start_servers(Sup) -> start_servers(Sup) ->
Servers = [{"emqttd ctl", emqttd_ctl}, Servers = [{"emqttd ctl", emqttd_ctl},
{"emqttd hook", emqttd_hook}, {"emqttd hook", emqttd_hooks},
{"emqttd router", emqttd_router}, {"emqttd router", emqttd_router},
{"emqttd pubsub", {supervisor, emqttd_pubsub_sup}}, {"emqttd pubsub", {supervisor, emqttd_pubsub_sup}},
{"emqttd stats", emqttd_stats}, {"emqttd stats", emqttd_stats},
@ -176,14 +172,14 @@ start_listener({Proto, ListenOn, Opts}) when Proto == https; Proto == wss ->
mochiweb:start_http('mqtt:wss', ListenOn, Opts, {emqttd_http, handle_request, []}). mochiweb:start_http('mqtt:wss', ListenOn, Opts, {emqttd_http, handle_request, []}).
start_listener(Proto, ListenOn, Opts) -> start_listener(Proto, ListenOn, Opts) ->
{ok, Env} = emqttd:env(protocol), Env = lists:append(emqttd:env(client, []), emqttd:env(protocol, [])),
MFArgs = {emqttd_client, start_link, [Env]}, MFArgs = {emqttd_client, start_link, [Env]},
{ok, _} = esockd:open(Proto, ListenOn, merge_sockopts(Opts), MFArgs). {ok, _} = esockd:open(Proto, ListenOn, merge_sockopts(Opts), MFArgs).
merge_sockopts(Options) -> merge_sockopts(Options) ->
SockOpts = emqttd_opts:merge(?MQTT_SOCKOPTS, SockOpts = emqttd_misc:merge_opts(
proplists:get_value(sockopts, Options, [])), ?MQTT_SOCKOPTS, proplists:get_value(sockopts, Options, [])),
emqttd_opts:merge(Options, [{sockopts, SockOpts}]). emqttd_misc:merge_opts(Options, [{sockopts, SockOpts}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Stop Listeners %% Stop Listeners
@ -211,3 +207,4 @@ merge_sockopts_test_() ->
?_assert(merge_sockopts(Opts) == [{sockopts, ?MQTT_SOCKOPTS} | Opts]). ?_assert(merge_sockopts(Opts) == [{sockopts, ?MQTT_SOCKOPTS} | Opts]).
-endif. -endif.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,11 +16,13 @@
-module(emqttd_auth_mod). -module(emqttd_auth_mod).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-export([passwd_hash/2]). -export([passwd_hash/2]).
-type(hash_type() :: plain | md5 | sha | sha256). -type(hash_type() :: plain | md5 | sha | sha256 | pbkdf2).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Authentication behavihour %% Authentication behavihour
@ -30,10 +32,10 @@
-callback(init(AuthOpts :: list()) -> {ok, State :: any()}). -callback(init(AuthOpts :: list()) -> {ok, State :: any()}).
-callback(check(Client, Password, State) -> ok | | {ok, boolean()} | ignore | {error, string()} when -callback(check(Client :: mqtt_client(),
Client :: mqtt_client(), Password :: binary(),
Password :: binary(), State :: any())
State :: any()). -> ok | | {ok, boolean()} | ignore | {error, string()}).
-callback(description() -> string()). -callback(description() -> string()).
@ -49,7 +51,7 @@ behaviour_info(_Other) ->
-endif. -endif.
%% @doc Password Hash %% @doc Password Hash
-spec(passwd_hash(hash_type(), binary()) -> binary()). -spec(passwd_hash(hash_type(), binary() | tuple()) -> binary()).
passwd_hash(plain, Password) -> passwd_hash(plain, Password) ->
Password; Password;
passwd_hash(md5, Password) -> passwd_hash(md5, Password) ->
@ -57,7 +59,10 @@ passwd_hash(md5, Password) ->
passwd_hash(sha, Password) -> passwd_hash(sha, Password) ->
hexstring(crypto:hash(sha, Password)); hexstring(crypto:hash(sha, Password));
passwd_hash(sha256, Password) -> passwd_hash(sha256, Password) ->
hexstring(crypto:hash(sha256, Password)). hexstring(crypto:hash(sha256, Password));
passwd_hash(pbkdf2,{Salt, Password, Macfun, Iterations, Dklen}) ->
{ok,Hexstring} = pbkdf2:pbkdf2(Macfun, Password, Salt, Iterations, Dklen),
pbkdf2:to_hex(Hexstring).
hexstring(<<X:128/big-unsigned-integer>>) -> hexstring(<<X:128/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~32.16.0b", [X])); iolist_to_binary(io_lib:format("~32.16.0b", [X]));

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_base62). -module(emqttd_base62).
-author("Feng Lee <feng@emqtt.io>").
-export([encode/1, decode/1]). -export([encode/1, decode/1]).
%% @doc Encode an integer to base62 string %% @doc Encode an integer to base62 string

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_boot). -module(emqttd_boot).
-author("Feng Lee <feng@emqtt.io>").
-export([apply_module_attributes/1, all_module_attributes/1]). -export([apply_module_attributes/1, all_module_attributes/1]).
%% only {F, Args}... %% only {F, Args}...
@ -58,6 +60,6 @@ ignore_lib_apps(Apps) ->
snmp, otp_mibs, public_key, asn1, ssh, hipe, snmp, otp_mibs, public_key, asn1, ssh, hipe,
common_test, observer, webtool, xmerl, tools, common_test, observer, webtool, xmerl, tools,
test_server, compiler, debugger, eunit, et, test_server, compiler, debugger, eunit, et,
gen_logger, wx], wx],
[App || App = {Name, _, _} <- Apps, not lists:member(Name, LibApps)]. [App || App = {Name, _, _} <- Apps, not lists:member(Name, LibApps)].

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@
-behaviour(gen_server2). -behaviour(gen_server2).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -38,16 +40,16 @@
qos = ?QOS_2, qos = ?QOS_2,
topic_suffix = <<>>, topic_suffix = <<>>,
topic_prefix = <<>>, topic_prefix = <<>>,
mqueue :: emqttd_mqueue:mqueue(), mqueue :: emqttd_mqueue:mqueue(),
max_queue_len = 10000, max_queue_len = 10000,
ping_down_interval = ?PING_DOWN_INTERVAL, ping_down_interval = ?PING_DOWN_INTERVAL,
status = up}). status = up}).
-type option() :: {qos, mqtt_qos()} | -type(option() :: {qos, mqtt_qos()} |
{topic_suffix, binary()} | {topic_suffix, binary()} |
{topic_prefix, binary()} | {topic_prefix, binary()} |
{max_queue_len, pos_integer()} | {max_queue_len, pos_integer()} |
{ping_down_interval, pos_integer()}. {ping_down_interval, pos_integer()}).
-export_type([option/0]). -export_type([option/0]).
@ -77,9 +79,10 @@ init([Pool, Id, Node, Topic, Options]) ->
MQueue = emqttd_mqueue:new(qname(Node, Topic), MQueue = emqttd_mqueue:new(qname(Node, Topic),
[{max_len, State#state.max_queue_len}], [{max_len, State#state.max_queue_len}],
emqttd_alarm:alarm_fun()), emqttd_alarm:alarm_fun()),
{ok, State#state{pool = Pool, id = Id, mqueue = MQueue}}; {ok, State#state{pool = Pool, id = Id, mqueue = MQueue},
hibernate, {backoff, 1000, 1000, 10000}};
false -> false ->
{stop, {cannot_connect, Node}} {stop, {cannot_connect_node, Node}}
end. end.
parse_opts([], State) -> parse_opts([], State) ->

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@
-behavior(supervisor). -behavior(supervisor).
-author("Feng Lee <feng@emqtt.io>").
-export([start_link/0, bridges/0, start_bridge/2, start_bridge/3, stop_bridge/2]). -export([start_link/0, bridges/0, start_bridge/2, start_bridge/3, stop_bridge/2]).
-export([init/1]). -export([init/1]).
@ -47,7 +49,7 @@ start_bridge(Node, _Topic, _Options) when Node =:= node() ->
{error, bridge_to_self}; {error, bridge_to_self};
start_bridge(Node, Topic, Options) when is_atom(Node) andalso is_binary(Topic) -> start_bridge(Node, Topic, Options) when is_atom(Node) andalso is_binary(Topic) ->
{ok, BridgeEnv} = emqttd:env(bridge), {ok, BridgeEnv} = emqttd:env(bridge),
Options1 = emqttd_opts:merge(BridgeEnv, Options), Options1 = emqttd_misc:merge_opts(BridgeEnv, Options),
supervisor:start_child(?MODULE, bridge_spec(Node, Topic, Options1)). supervisor:start_child(?MODULE, bridge_spec(Node, Topic, Options1)).
%% @doc Stop a bridge %% @doc Stop a bridge

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@
-behaviour(gen_server). -behaviour(gen_server).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").
@ -40,16 +42,18 @@
-record(state, {started_at, sys_interval, heartbeat, tick_tref, version, sysdescr}). -record(state, {started_at, sys_interval, heartbeat, tick_tref, version, sysdescr}).
-define(APP, emqttd).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
-define(BROKER_TAB, mqtt_broker). -define(BROKER_TAB, mqtt_broker).
%% $SYS Topics of Broker %% $SYS Topics of Broker
-define(SYSTOP_BROKERS, [ -define(SYSTOP_BROKERS, [
version, % Broker version version, % Broker version
uptime, % Broker uptime uptime, % Broker uptime
datetime, % Broker local datetime datetime, % Broker local datetime
sysdescr % Broker description sysdescr % Broker description
]). ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -74,12 +78,12 @@ notify(EventType, Event) ->
%% @doc Get broker version %% @doc Get broker version
-spec(version() -> string()). -spec(version() -> string()).
version() -> version() ->
{ok, Version} = application:get_key(emqttd, vsn), Version. {ok, Version} = application:get_key(?APP, vsn), Version.
%% @doc Get broker description %% @doc Get broker description
-spec(sysdescr() -> string()). -spec(sysdescr() -> string()).
sysdescr() -> sysdescr() ->
{ok, Descr} = application:get_key(emqttd, description), Descr. {ok, Descr} = application:get_key(?APP, description), Descr.
%% @doc Get broker uptime %% @doc Get broker uptime
-spec(uptime() -> string()). -spec(uptime() -> string()).
@ -102,14 +106,14 @@ start_tick(0, _Msg) ->
start_tick(Interval, Msg) when Interval > 0 -> start_tick(Interval, Msg) when Interval > 0 ->
{ok, TRef} = timer:send_interval(Interval, Msg), TRef. {ok, TRef} = timer:send_interval(Interval, Msg), TRef.
%% @doc Start tick timer %% @doc Stop tick timer
stop_tick(undefined) -> stop_tick(undefined) ->
ok; ok;
stop_tick(TRef) -> stop_tick(TRef) ->
timer:cancel(TRef). timer:cancel(TRef).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_server callbacks %% gen_server Callbacks
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
@ -161,13 +165,11 @@ retain(brokers) ->
Payload = list_to_binary(string:join([atom_to_list(N) || Payload = list_to_binary(string:join([atom_to_list(N) ||
N <- emqttd_mnesia:running_nodes()], ",")), N <- emqttd_mnesia:running_nodes()], ",")),
Msg = emqttd_message:make(broker, <<"$SYS/brokers">>, Payload), Msg = emqttd_message:make(broker, <<"$SYS/brokers">>, Payload),
Msg1 = emqttd_message:set_flag(sys, emqttd_message:set_flag(retain, Msg)), emqttd:publish(emqttd_message:set_flag(sys, emqttd_message:set_flag(retain, Msg))).
emqttd:publish(Msg1).
retain(Topic, Payload) when is_binary(Payload) -> retain(Topic, Payload) when is_binary(Payload) ->
Msg = emqttd_message:make(broker, emqttd_topic:systop(Topic), Payload), Msg = emqttd_message:make(broker, emqttd_topic:systop(Topic), Payload),
Msg1 = emqttd_message:set_flag(sys, emqttd_message:set_flag(retain, Msg)), emqttd:publish(emqttd_message:set_flag(sys, emqttd_message:set_flag(retain, Msg))).
emqttd:publish(Msg1).
publish(Topic, Payload) when is_binary(Payload) -> publish(Topic, Payload) when is_binary(Payload) ->
Msg = emqttd_message:make(broker, emqttd_topic:systop(Topic), Payload), Msg = emqttd_message:make(broker, emqttd_topic:systop(Topic), Payload),

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_cli). -module(emqttd_cli).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_cli.hrl"). -include("emqttd_cli.hrl").
@ -30,7 +32,7 @@
-export([status/1, broker/1, cluster/1, users/1, clients/1, sessions/1, -export([status/1, broker/1, cluster/1, users/1, clients/1, sessions/1,
routes/1, topics/1, subscriptions/1, plugins/1, bridges/1, routes/1, topics/1, subscriptions/1, plugins/1, bridges/1,
listeners/1, vm/1, mnesia/1, trace/1]). listeners/1, vm/1, mnesia/1, trace/1, acl/1]).
-define(PROC_INFOKEYS, [status, -define(PROC_INFOKEYS, [status,
memory, memory,
@ -57,6 +59,7 @@ is_cmd(Fun) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Node status %% @doc Node status
status([]) -> status([]) ->
{InternalStatus, _ProvidedStatus} = init:get_status(), {InternalStatus, _ProvidedStatus} = init:get_status(),
?PRINT("Node ~p is ~p~n", [node(), InternalStatus]), ?PRINT("Node ~p is ~p~n", [node(), InternalStatus]),
@ -71,6 +74,7 @@ status(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Query broker %% @doc Query broker
broker([]) -> broker([]) ->
Funs = [sysdescr, version, uptime, datetime], Funs = [sysdescr, version, uptime, datetime],
foreach(fun(Fun) -> foreach(fun(Fun) ->
@ -105,6 +109,7 @@ broker(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Cluster with other nodes %% @doc Cluster with other nodes
cluster(["join", SNode]) -> cluster(["join", SNode]) ->
case emqttd_cluster:join(emqttd_node:parse_name(SNode)) of case emqttd_cluster:join(emqttd_node:parse_name(SNode)) of
ok -> ok ->
@ -143,10 +148,15 @@ cluster(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Users usage %% @doc Users usage
users(Args) -> emq_auth_username:cli(Args). users(Args) -> emq_auth_username:cli(Args).
acl(["reload"]) -> emqttd_access_control:reload_acl();
acl(_) -> ?USAGE([{"acl reload", "reload etc/acl.conf"}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Query clients %% @doc Query clients
clients(["list"]) -> clients(["list"]) ->
dump(mqtt_client); dump(mqtt_client);
@ -169,14 +179,17 @@ if_client(ClientId, Fun) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Sessions Command %% @doc Sessions Command
sessions(["list"]) -> sessions(["list"]) ->
dump(mqtt_local_session); dump(mqtt_local_session);
%% performance issue? %% performance issue?
sessions(["list", "persistent"]) -> sessions(["list", "persistent"]) ->
lists:foreach(fun print/1, ets:match_object(mqtt_local_session, {'_', '_', false, '_'})); lists:foreach(fun print/1, ets:match_object(mqtt_local_session, {'_', '_', false, '_'}));
%% performance issue? %% performance issue?
sessions(["list", "transient"]) -> sessions(["list", "transient"]) ->
lists:foreach(fun print/1, ets:match_object(mqtt_local_session, {'_', '_', true, '_'})); lists:foreach(fun print/1, ets:match_object(mqtt_local_session, {'_', '_', true, '_'}));
@ -194,6 +207,7 @@ sessions(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Routes Command %% @doc Routes Command
routes(["list"]) -> routes(["list"]) ->
Routes = emqttd_router:dump(), Routes = emqttd_router:dump(),
foreach(fun print/1, Routes); foreach(fun print/1, Routes);
@ -207,6 +221,7 @@ routes(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Topics Command %% @doc Topics Command
topics(["list"]) -> topics(["list"]) ->
lists:foreach(fun(Topic) -> ?PRINT("~s~n", [Topic]) end, emqttd:topics()); lists:foreach(fun(Topic) -> ?PRINT("~s~n", [Topic]) end, emqttd:topics());
@ -260,14 +275,14 @@ subscriptions(_) ->
{"subscriptions del <ClientId>", "Delete static subscriptions manually"}, {"subscriptions del <ClientId>", "Delete static subscriptions manually"},
{"subscriptions del <ClientId> <Topic>", "Delete a static subscription manually"}]). {"subscriptions del <ClientId> <Topic>", "Delete a static subscription manually"}]).
if_could_print(Tab, Fun) -> % if_could_print(Tab, Fun) ->
case mnesia:table_info(Tab, size) of % case mnesia:table_info(Tab, size) of
Size when Size >= ?MAX_LIMIT -> % Size when Size >= ?MAX_LIMIT ->
?PRINT("Could not list, too many ~ss: ~p~n", [Tab, Size]); % ?PRINT("Could not list, too many ~ss: ~p~n", [Tab, Size]);
_Size -> % _Size ->
Keys = mnesia:dirty_all_keys(Tab), % Keys = mnesia:dirty_all_keys(Tab),
foreach(fun(Key) -> Fun(ets:lookup(Tab, Key)) end, Keys) % foreach(fun(Key) -> Fun(ets:lookup(Tab, Key)) end, Keys)
end. % end.
if_valid_qos(QoS, Fun) -> if_valid_qos(QoS, Fun) ->
try list_to_integer(QoS) of try list_to_integer(QoS) of
@ -303,6 +318,7 @@ plugins(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Bridges command %% @doc Bridges command
bridges(["list"]) -> bridges(["list"]) ->
foreach(fun({Node, Topic, _Pid}) -> foreach(fun({Node, Topic, _Pid}) ->
?PRINT("bridge: ~s--~s-->~s~n", [node(), Topic, Node]) ?PRINT("bridge: ~s--~s-->~s~n", [node(), Topic, Node])
@ -360,6 +376,7 @@ parse_opt(_Cmd, Opt, _Val) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc vm command %% @doc vm command
vm([]) -> vm([]) ->
vm(["all"]); vm(["all"]);
@ -398,6 +415,7 @@ vm(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc mnesia Command %% @doc mnesia Command
mnesia([]) -> mnesia([]) ->
mnesia:system_info(); mnesia:system_info();
@ -406,6 +424,7 @@ mnesia(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Trace Command %% @doc Trace Command
trace(["list"]) -> trace(["list"]) ->
foreach(fun({{Who, Name}, LogFile}) -> foreach(fun({{Who, Name}, LogFile}) ->
?PRINT("trace ~s ~s -> ~s~n", [Who, Name, LogFile]) ?PRINT("trace ~s ~s -> ~s~n", [Who, Name, LogFile])
@ -448,6 +467,7 @@ trace_off(Who, Name) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Listeners Command %% @doc Listeners Command
listeners([]) -> listeners([]) ->
foreach(fun({{Protocol, ListenOn}, Pid}) -> foreach(fun({{Protocol, ListenOn}, Pid}) ->
Info = [{acceptors, esockd:get_acceptors(Pid)}, Info = [{acceptors, esockd:get_acceptors(Pid)},
@ -503,7 +523,7 @@ print(#mqtt_client{client_id = ClientId, clean_sess = CleanSess, username = User
peername = Peername, connected_at = ConnectedAt}) -> peername = Peername, connected_at = ConnectedAt}) ->
?PRINT("Client(~s, clean_sess=~s, username=~s, peername=~s, connected_at=~p)~n", ?PRINT("Client(~s, clean_sess=~s, username=~s, peername=~s, connected_at=~p)~n",
[ClientId, CleanSess, Username, emqttd_net:format(Peername), [ClientId, CleanSess, Username, emqttd_net:format(Peername),
emqttd_time:now_to_secs(ConnectedAt)]); emqttd_time:now_secs(ConnectedAt)]);
%% print(#mqtt_topic{topic = Topic, flags = Flags}) -> %% print(#mqtt_topic{topic = Topic, flags = Flags}) ->
%% ?PRINT("~s: ~s~n", [Topic, string:join([atom_to_list(F) || F <- Flags], ",")]); %% ?PRINT("~s: ~s~n", [Topic, string:join([atom_to_list(F) || F <- Flags], ",")]);
@ -517,20 +537,21 @@ print({Topic, Node}) ->
?PRINT("~s -> ~s~n", [Topic, Node]); ?PRINT("~s -> ~s~n", [Topic, Node]);
print({ClientId, _ClientPid, _Persistent, SessInfo}) -> print({ClientId, _ClientPid, _Persistent, SessInfo}) ->
Data = lists:append(SessInfo, emqttd_stats:get_session_stats(ClientId)),
InfoKeys = [clean_sess, InfoKeys = [clean_sess,
subscriptions,
max_inflight, max_inflight,
inflight_queue, inflight_len,
message_queue, mqueue_len,
message_dropped, mqueue_dropped,
awaiting_rel, awaiting_rel_len,
awaiting_ack, deliver_msg,
awaiting_comp, enqueue_msg,
created_at], created_at],
?PRINT("Session(~s, clean_sess=~s, max_inflight=~w, inflight_queue=~w, " ?PRINT("Session(~s, clean_sess=~s, max_inflight=~w, inflight=~w, "
"message_queue=~w, message_dropped=~w, " "mqueue_len=~w, mqueue_dropped=~w, awaiting_rel=~w, "
"awaiting_rel=~w, awaiting_ack=~w, awaiting_comp=~w, " "deliver_msg=~w, enqueue_msg=~w, created_at=~w)~n",
"created_at=~w)~n", [ClientId | [format(Key, get_value(Key, Data)) || Key <- InfoKeys]]).
[ClientId | [format(Key, get_value(Key, SessInfo)) || Key <- InfoKeys]]).
print(subscription, {Sub, Topic}) when is_pid(Sub) -> print(subscription, {Sub, Topic}) when is_pid(Sub) ->
?PRINT("~p -> ~s~n", [Sub, Topic]); ?PRINT("~p -> ~s~n", [Sub, Topic]);
@ -542,7 +563,7 @@ print(subscription, {Sub, Topic}) ->
?PRINT("~s -> ~s~n", [Sub, Topic]). ?PRINT("~s -> ~s~n", [Sub, Topic]).
format(created_at, Val) -> format(created_at, Val) ->
emqttd_time:now_to_secs(Val); emqttd_time:now_secs(Val);
format(_, Val) -> format(_, Val) ->
Val. Val.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,10 +14,13 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc MQTT Client Connection %% @doc MQTT/TCP Connection.
-module(emqttd_client). -module(emqttd_client).
-behaviour(gen_server). -behaviour(gen_server2).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
@ -25,163 +28,206 @@
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").
-import(proplists, [get_value/2, get_value/3]).
%% API Function Exports %% API Function Exports
-export([start_link/2, session/1, info/1, kick/1, -export([start_link/2]).
set_rate_limit/2, get_rate_limit/1]).
%% Management and Monitor API
-export([info/1, stats/1, kick/1]).
-export([set_rate_limit/2, get_rate_limit/1]).
%% SUB/UNSUB Asynchronously. Called by plugins. %% SUB/UNSUB Asynchronously. Called by plugins.
-export([subscribe/2, unsubscribe/2]). -export([subscribe/2, unsubscribe/2]).
%% Get the session proc?
-export([session/1]).
%% gen_server Function Exports %% gen_server Function Exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]). code_change/3, terminate/2]).
%% gen_server2 Callbacks
-export([prioritise_call/4, prioritise_info/3, handle_pre_hibernate/1]).
%% Client State %% Client State
-record(client_state, {connection, connname, peername, peerhost, peerport, %% Unused fields: connname, peerhost, peerport
await_recv, conn_state, rate_limit, parser_fun, -record(client_state, {connection, peername, conn_state, await_recv,
proto_state, packet_opts, keepalive, mountpoint}). rate_limit, packet_size, parser, proto_state,
keepalive, enable_stats, force_gc_count}).
-define(INFO_KEYS, [peername, peerhost, peerport, await_recv, conn_state]). -define(INFO_KEYS, [peername, conn_state, await_recv]).
-define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt]). -define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt, send_pend]).
-define(LOG(Level, Format, Args, State), -define(LOG(Level, Format, Args, State),
lager:Level("Client(~s): " ++ Format, [State#client_state.connname | Args])). lager:Level("Client(~s): " ++ Format,
[esockd_net:format(State#client_state.peername) | Args])).
start_link(Connection, MqttEnv) -> start_link(Conn, Env) ->
{ok, proc_lib:spawn_link(?MODULE, init, [[Connection, MqttEnv]])}. {ok, proc_lib:spawn_opt(?MODULE, init, [[Conn, Env]], [link | ?FULLSWEEP_OPTS])}.
session(CPid) ->
gen_server:call(CPid, session, infinity).
info(CPid) -> info(CPid) ->
gen_server:call(CPid, info, infinity). gen_server2:call(CPid, info).
stats(CPid) ->
gen_server2:call(CPid, stats).
kick(CPid) -> kick(CPid) ->
gen_server:call(CPid, kick). gen_server2:call(CPid, kick).
set_rate_limit(Cpid, Rl) -> set_rate_limit(Cpid, Rl) ->
gen_server:call(Cpid, {set_rate_limit, Rl}). gen_server2:call(Cpid, {set_rate_limit, Rl}).
get_rate_limit(Cpid) -> get_rate_limit(Cpid) ->
gen_server:call(Cpid, get_rate_limit). gen_server2:call(Cpid, get_rate_limit).
subscribe(CPid, TopicTable) -> subscribe(CPid, TopicTable) ->
gen_server:cast(CPid, {subscribe, TopicTable}). CPid ! {subscribe, TopicTable}.
unsubscribe(CPid, Topics) -> unsubscribe(CPid, Topics) ->
gen_server:cast(CPid, {unsubscribe, Topics}). CPid ! {unsubscribe, Topics}.
init([OriginConn, MqttEnv]) -> session(CPid) ->
{ok, Connection} = OriginConn:wait(), gen_server2:call(CPid, session, infinity).
{PeerHost, PeerPort, PeerName} =
case Connection:peername() of %%--------------------------------------------------------------------
{ok, Peer = {Host, Port}} -> %% gen_server Callbacks
{Host, Port, Peer}; %%--------------------------------------------------------------------
{error, enotconn} ->
Connection:fast_close(), init([Conn0, Env]) ->
exit(normal); {ok, Conn} = Conn0:wait(),
{error, Reason} -> case Conn:peername() of
Connection:fast_close(), {ok, Peername} -> do_init(Conn, Env, Peername);
exit({shutdown, Reason}) {error, enotconn} -> Conn:fast_close(),
end, exit(normal);
ConnName = esockd_net:format(PeerName), {error, Reason} -> Conn:fast_close(),
exit({shutdown, Reason})
end.
do_init(Conn, Env, Peername) ->
%% Send Fun
SendFun = send_fun(Conn, Peername),
RateLimit = get_value(rate_limit, Conn:opts()),
PacketSize = get_value(max_packet_size, Env, ?MAX_PACKET_SIZE),
Parser = emqttd_parser:initial_state(PacketSize),
ProtoState = emqttd_protocol:init(Peername, SendFun, Env),
EnableStats = get_value(client_enable_stats, Env, false),
ForceGcCount = emqttd_gc:conn_max_gc_count(),
State = run_socket(#client_state{connection = Conn,
peername = Peername,
await_recv = false,
conn_state = running,
rate_limit = RateLimit,
packet_size = PacketSize,
parser = Parser,
proto_state = ProtoState,
enable_stats = EnableStats,
force_gc_count = ForceGcCount}),
IdleTimout = get_value(client_idle_timeout, Env, 30000),
gen_server2:enter_loop(?MODULE, [], State, self(), IdleTimout,
{backoff, 2000, 2000, 20000}).
send_fun(Conn, Peername) ->
Self = self(), Self = self(),
fun(Packet) ->
%% Send Packet...
SendFun = fun(Packet) ->
Data = emqttd_serializer:serialize(Packet), Data = emqttd_serializer:serialize(Packet),
?LOG(debug, "SEND ~p", [Data], #client_state{connname = ConnName}), ?LOG(debug, "SEND ~p", [Data], #client_state{peername = Peername}),
emqttd_metrics:inc('bytes/sent', iolist_size(Data)), emqttd_metrics:inc('bytes/sent', iolist_size(Data)),
try Connection:async_send(Data) of try Conn:async_send(Data) of
true -> ok true -> ok
catch catch
error:Error -> Self ! {shutdown, Error} error:Error -> Self ! {shutdown, Error}
end end
end, end.
ParserFun = emqttd_parser:new(MqttEnv),
ProtoState = emqttd_protocol:init(PeerName, SendFun, MqttEnv),
RateLimit = proplists:get_value(rate_limit, Connection:opts()),
State = run_socket(#client_state{connection = Connection,
connname = ConnName,
peername = PeerName,
peerhost = PeerHost,
peerport = PeerPort,
await_recv = false,
conn_state = running,
rate_limit = RateLimit,
parser_fun = ParserFun,
proto_state = ProtoState,
packet_opts = MqttEnv}),
IdleTimout = proplists:get_value(client_idle_timeout, MqttEnv, 30),
gen_server:enter_loop(?MODULE, [], State, timer:seconds(IdleTimout)).
handle_call(session, _From, State = #client_state{proto_state = ProtoState}) -> prioritise_call(Msg, _From, _Len, _State) ->
{reply, emqttd_protocol:session(ProtoState), State}; case Msg of info -> 10; stats -> 10; state -> 10; _ -> 5 end.
handle_call(info, _From, State = #client_state{connection = Connection, prioritise_info(Msg, _Len, _State) ->
proto_state = ProtoState}) -> case Msg of {redeliver, _} -> 5; _ -> 0 end.
ClientInfo = ?record_to_proplist(client_state, State, ?INFO_KEYS),
handle_pre_hibernate(State) ->
{hibernate, emqttd_gc:reset_conn_gc_count(#client_state.force_gc_count, emit_stats(State))}.
handle_call(info, From, State = #client_state{proto_state = ProtoState}) ->
ProtoInfo = emqttd_protocol:info(ProtoState), ProtoInfo = emqttd_protocol:info(ProtoState),
{ok, SockStats} = Connection:getstat(?SOCK_STATS), ClientInfo = ?record_to_proplist(client_state, State, ?INFO_KEYS),
{reply, lists:append([ClientInfo, [{proto_info, ProtoInfo}, {reply, Stats, _, _} = handle_call(stats, From, State),
{sock_stats, SockStats}]]), State}; reply(lists:append([ClientInfo, ProtoInfo, Stats]), State);
handle_call(stats, _From, State = #client_state{proto_state = ProtoState}) ->
reply(lists:append([emqttd_misc:proc_stats(),
emqttd_protocol:stats(ProtoState),
sock_stats(State)]), State);
handle_call(kick, _From, State) -> handle_call(kick, _From, State) ->
{stop, {shutdown, kick}, ok, State}; {stop, {shutdown, kick}, ok, State};
handle_call({set_rate_limit, Rl}, _From, State) -> handle_call({set_rate_limit, Rl}, _From, State) ->
{reply, ok, State#client_state{rate_limit = Rl}}; reply(ok, State#client_state{rate_limit = Rl});
handle_call(get_rate_limit, _From, State = #client_state{rate_limit = Rl}) -> handle_call(get_rate_limit, _From, State = #client_state{rate_limit = Rl}) ->
{reply, Rl, State}; reply(Rl, State);
handle_call(session, _From, State = #client_state{proto_state = ProtoState}) ->
reply(emqttd_protocol:session(ProtoState), State);
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->
?UNEXPECTED_REQ(Req, State). ?UNEXPECTED_REQ(Req, State).
handle_cast({subscribe, TopicTable}, State) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:handle({subscribe, TopicTable}, ProtoState)
end, State);
handle_cast({unsubscribe, Topics}, State) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:handle({unsubscribe, Topics}, ProtoState)
end, State);
handle_cast(Msg, State) -> handle_cast(Msg, State) ->
?UNEXPECTED_MSG(Msg, State). ?UNEXPECTED_MSG(Msg, State).
handle_info({subscribe, TopicTable}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:subscribe(TopicTable, ProtoState)
end, State);
handle_info({unsubscribe, Topics}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:unsubscribe(Topics, ProtoState)
end, State);
%% Asynchronous SUBACK
handle_info({suback, PacketId, GrantedQos}, State) ->
with_proto(
fun(ProtoState) ->
Packet = ?SUBACK_PACKET(PacketId, GrantedQos),
emqttd_protocol:send(Packet, ProtoState)
end, State);
handle_info({deliver, Message}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:send(Message, ProtoState)
end, State);
handle_info({redeliver, {?PUBREL, PacketId}}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:pubrel(PacketId, ProtoState)
end, State);
handle_info(emit_stats, State) ->
{noreply, emit_stats(State), hibernate};
handle_info(timeout, State) -> handle_info(timeout, State) ->
shutdown(idle_timeout, State); shutdown(idle_timeout, State);
%% fix issue #535 %% Fix issue #535
handle_info({shutdown, Error}, State) -> handle_info({shutdown, Error}, State) ->
shutdown(Error, State); shutdown(Error, State);
%% Asynchronous SUBACK
handle_info({suback, PacketId, GrantedQos}, State) ->
with_proto_state(fun(ProtoState) ->
Packet = ?SUBACK_PACKET(PacketId, GrantedQos),
emqttd_protocol:send(Packet, ProtoState)
end, State);
handle_info({deliver, Message}, State) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:send(Message, ProtoState)
end, State);
handle_info({redeliver, {?PUBREL, PacketId}}, State) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:redeliver({?PUBREL, PacketId}, ProtoState)
end, State);
handle_info({shutdown, conflict, {ClientId, NewPid}}, State) -> handle_info({shutdown, conflict, {ClientId, NewPid}}, State) ->
?LOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid], State), ?LOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid], State),
shutdown(conflict, State); shutdown(conflict, State);
handle_info(activate_sock, State) -> handle_info(activate_sock, State) ->
hibernate(run_socket(State#client_state{conn_state = running})); {noreply, run_socket(State#client_state{conn_state = running}), hibernate};
handle_info({inet_async, _Sock, _Ref, {ok, Data}}, State) -> handle_info({inet_async, _Sock, _Ref, {ok, Data}}, State) ->
Size = iolist_size(Data), Size = iolist_size(Data),
@ -193,26 +239,31 @@ handle_info({inet_async, _Sock, _Ref, {error, Reason}}, State) ->
shutdown(Reason, State); shutdown(Reason, State);
handle_info({inet_reply, _Sock, ok}, State) -> handle_info({inet_reply, _Sock, ok}, State) ->
hibernate(State); {noreply, gc(State), hibernate}; %% Tune GC
handle_info({inet_reply, _Sock, {error, Reason}}, State) -> handle_info({inet_reply, _Sock, {error, Reason}}, State) ->
shutdown(Reason, State); shutdown(Reason, State);
handle_info({keepalive, start, Interval}, State = #client_state{connection = Connection}) -> handle_info({keepalive, start, Interval}, State = #client_state{connection = Conn}) ->
?LOG(debug, "Keepalive at the interval of ~p", [Interval], State), ?LOG(debug, "Keepalive at the interval of ~p", [Interval], State),
StatFun = fun() -> StatFun = fun() ->
case Connection:getstat([recv_oct]) of case Conn:getstat([recv_oct]) of
{ok, [{recv_oct, RecvOct}]} -> {ok, RecvOct}; {ok, [{recv_oct, RecvOct}]} -> {ok, RecvOct};
{error, Error} -> {error, Error} {error, Error} -> {error, Error}
end end
end, end,
KeepAlive = emqttd_keepalive:start(StatFun, Interval, {keepalive, check}), case emqttd_keepalive:start(StatFun, Interval, {keepalive, check}) of
hibernate(State#client_state{keepalive = KeepAlive}); {ok, KeepAlive} ->
{noreply, State#client_state{keepalive = KeepAlive}, hibernate};
{error, Error} ->
?LOG(warning, "Keepalive error - ~p", [Error], State),
shutdown(Error, State)
end;
handle_info({keepalive, check}, State = #client_state{keepalive = KeepAlive}) -> handle_info({keepalive, check}, State = #client_state{keepalive = KeepAlive}) ->
case emqttd_keepalive:check(KeepAlive) of case emqttd_keepalive:check(KeepAlive) of
{ok, KeepAlive1} -> {ok, KeepAlive1} ->
hibernate(State#client_state{keepalive = KeepAlive1}); {noreply, State#client_state{keepalive = KeepAlive1}, hibernate};
{error, timeout} -> {error, timeout} ->
?LOG(debug, "Keepalive timeout", [], State), ?LOG(debug, "Keepalive timeout", [], State),
shutdown(keepalive_timeout, State); shutdown(keepalive_timeout, State);
@ -224,10 +275,10 @@ handle_info({keepalive, check}, State = #client_state{keepalive = KeepAlive}) ->
handle_info(Info, State) -> handle_info(Info, State) ->
?UNEXPECTED_INFO(Info, State). ?UNEXPECTED_INFO(Info, State).
terminate(Reason, #client_state{connection = Connection, terminate(Reason, #client_state{connection = Conn,
keepalive = KeepAlive, keepalive = KeepAlive,
proto_state = ProtoState}) -> proto_state = ProtoState}) ->
Connection:fast_close(), Conn:fast_close(),
emqttd_keepalive:cancel(KeepAlive), emqttd_keepalive:cancel(KeepAlive),
case {ProtoState, Reason} of case {ProtoState, Reason} of
{undefined, _} -> {undefined, _} ->
@ -245,25 +296,21 @@ code_change(_OldVsn, State, _Extra) ->
%% Internal functions %% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
with_proto_state(Fun, State = #client_state{proto_state = ProtoState}) -> %% Receive and Parse TCP Data
{ok, ProtoState1} = Fun(ProtoState),
hibernate(State#client_state{proto_state = ProtoState1}).
%% Receive and parse tcp data
received(<<>>, State) -> received(<<>>, State) ->
hibernate(State); {noreply, gc(State), hibernate};
received(Bytes, State = #client_state{parser_fun = ParserFun, received(Bytes, State = #client_state{parser = Parser,
packet_opts = PacketOpts, packet_size = PacketSize,
proto_state = ProtoState}) -> proto_state = ProtoState}) ->
case catch ParserFun(Bytes) of case catch emqttd_parser:parse(Bytes, Parser) of
{more, NewParser} -> {more, NewParser} ->
noreply(run_socket(State#client_state{parser_fun = NewParser})); {noreply, run_socket(State#client_state{parser = NewParser}), hibernate};
{ok, Packet, Rest} -> {ok, Packet, Rest} ->
emqttd_metrics:received(Packet), emqttd_metrics:received(Packet),
case emqttd_protocol:received(Packet, ProtoState) of case emqttd_protocol:received(Packet, ProtoState) of
{ok, ProtoState1} -> {ok, ProtoState1} ->
received(Rest, State#client_state{parser_fun = emqttd_parser:new(PacketOpts), received(Rest, State#client_state{parser = emqttd_parser:initial_state(PacketSize),
proto_state = ProtoState1}); proto_state = ProtoState1});
{error, Error} -> {error, Error} ->
?LOG(error, "Protocol error - ~p", [Error], State), ?LOG(error, "Protocol error - ~p", [Error], State),
@ -289,7 +336,7 @@ rate_limit(Size, State = #client_state{rate_limit = Rl}) ->
{0, Rl1} -> {0, Rl1} ->
run_socket(State#client_state{conn_state = running, rate_limit = Rl1}); run_socket(State#client_state{conn_state = running, rate_limit = Rl1});
{Pause, Rl1} -> {Pause, Rl1} ->
?LOG(error, "Rate limiter pause for ~p", [Pause], State), ?LOG(warning, "Rate limiter pause for ~p", [Pause], State),
erlang:send_after(Pause, self(), activate_sock), erlang:send_after(Pause, self(), activate_sock),
State#client_state{conn_state = blocked, rate_limit = Rl1} State#client_state{conn_state = blocked, rate_limit = Rl1}
end. end.
@ -298,15 +345,31 @@ run_socket(State = #client_state{conn_state = blocked}) ->
State; State;
run_socket(State = #client_state{await_recv = true}) -> run_socket(State = #client_state{await_recv = true}) ->
State; State;
run_socket(State = #client_state{connection = Connection}) -> run_socket(State = #client_state{connection = Conn}) ->
Connection:async_recv(0, infinity), Conn:async_recv(0, infinity),
State#client_state{await_recv = true}. State#client_state{await_recv = true}.
noreply(State) -> with_proto(Fun, State = #client_state{proto_state = ProtoState}) ->
{noreply, State}. {ok, ProtoState1} = Fun(ProtoState),
{noreply, State#client_state{proto_state = ProtoState1}, hibernate}.
hibernate(State) -> emit_stats(State = #client_state{proto_state = ProtoState}) ->
{noreply, State, hibernate}. emit_stats(emqttd_protocol:clientid(ProtoState), State).
emit_stats(_ClientId, State = #client_state{enable_stats = false}) ->
State;
emit_stats(undefined, State) ->
State;
emit_stats(ClientId, State) ->
{reply, Stats, _, _} = handle_call(stats, undefined, State),
emqttd_stats:set_client_stats(ClientId, Stats),
State.
sock_stats(#client_state{connection = Conn}) ->
case Conn:getstat(?SOCK_STATS) of {ok, Ss} -> Ss; {error, _} -> [] end.
reply(Reply, State) ->
{reply, Reply, State, hibernate}.
shutdown(Reason, State) -> shutdown(Reason, State) ->
stop({shutdown, Reason}, State). stop({shutdown, Reason}, State).
@ -314,3 +377,7 @@ shutdown(Reason, State) ->
stop(Reason, State) -> stop(Reason, State) ->
{stop, Reason, State}. {stop, Reason, State}.
gc(State = #client_state{connection = Conn}) ->
Cb = fun() -> Conn:gc() end,
emqttd_gc:maybe_force_gc(#client_state.force_gc_count, State, Cb).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_cluster). -module(emqttd_cluster).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
%% Cluster API %% Cluster API

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -15,10 +15,13 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc MQTT Client Manager %% @doc MQTT Client Manager
-module(emqttd_cm). -module(emqttd_cm).
-behaviour(gen_server2). -behaviour(gen_server2).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").
@ -120,6 +123,7 @@ handle_info({'DOWN', MRef, process, DownPid, _Reason}, State) ->
{ok, {ClientId, DownPid}} -> {ok, {ClientId, DownPid}} ->
case lookup_proc(ClientId) of case lookup_proc(ClientId) of
DownPid -> DownPid ->
emqttd_stats:del_client_stats(ClientId),
ets:delete(mqtt_client, ClientId); ets:delete(mqtt_client, ClientId);
_ -> _ ->
ignore ignore

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -15,10 +15,13 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Client Manager Supervisor. %% @doc Client Manager Supervisor.
-module(emqttd_cm_sup). -module(emqttd_cm_sup).
-behaviour(supervisor). -behaviour(supervisor).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
%% API %% API

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@
-behaviour(gen_server). -behaviour(gen_server).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_cli.hrl"). -include("emqttd_cli.hrl").
@ -134,7 +136,9 @@ next_seq(State = #state{seq = Seq}) ->
State#state{seq = Seq + 1}. State#state{seq = Seq + 1}.
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
register_cmd_test_() -> register_cmd_test_() ->
{setup, {setup,
fun() -> fun() ->
@ -149,4 +153,5 @@ register_cmd_test_() ->
[?_assertMatch([{{0,test0},{?MODULE, test0}, []}], ets:lookup(?CMD_TAB, {Seq,test0}))] [?_assertMatch([{{0,test0},{?MODULE, test0}, []}], ets:lookup(?CMD_TAB, {Seq,test0}))]
end end
}. }.
-endif. -endif.

50
src/emqttd_gc.erl Normal file
View File

@ -0,0 +1,50 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
%% GC Utility functions.
-module(emqttd_gc).
-author("Feng Lee <feng@emqtt.io>").
-export([conn_max_gc_count/0, reset_conn_gc_count/2, maybe_force_gc/2,
maybe_force_gc/3]).
-spec(conn_max_gc_count() -> integer()).
conn_max_gc_count() ->
case emqttd:env(conn_force_gc_count) of
{ok, I} when I > 0 -> I + rand:uniform(I);
{ok, I} when I =< 0 -> undefined;
undefined -> undefined
end.
-spec(reset_conn_gc_count(pos_integer(), tuple()) -> tuple()).
reset_conn_gc_count(Pos, State) ->
case element(Pos, State) of
undefined -> State;
_I -> setelement(Pos, State, conn_max_gc_count())
end.
maybe_force_gc(Pos, State) ->
maybe_force_gc(Pos, State, fun() -> ok end).
maybe_force_gc(Pos, State, Cb) ->
case element(Pos, State) of
undefined -> State;
I when I =< 0 -> Cb(), garbage_collect(),
reset_conn_gc_count(Pos, State);
I -> setelement(Pos, State, I - 1)
end.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -15,15 +15,18 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd gen_mod behaviour %% @doc emqttd gen_mod behaviour
-module(emqttd_gen_mod). -module(emqttd_gen_mod).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-ifdef(use_specs). -ifdef(use_specs).
-callback load(Opts :: any()) -> ok | {error, any()}. -callback(load(Opts :: any()) -> ok | {error, any()}).
-callback unload(State :: any()) -> any(). -callback(unload(State :: any()) -> any()).
-else. -else.
@ -35,4 +38,3 @@ behaviour_info(_Other) ->
undefined. undefined.
-endif. -endif.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -27,13 +27,14 @@
%% 4. Sequence: 2 bytes sequence in one process %% 4. Sequence: 2 bytes sequence in one process
%% %%
%% @end %% @end
-module(emqttd_guid). -module(emqttd_guid).
-export([gen/0, new/0, timestamp/1, to_hexstr/1, from_hexstr/1, to_base62/1, from_base62/1]). -export([gen/0, new/0, timestamp/1, to_hexstr/1, from_hexstr/1, to_base62/1, from_base62/1]).
-define(MAX_SEQ, 16#FFFF). -define(MAX_SEQ, 16#FFFF).
-type guid() :: <<_:128>>. -type(guid() :: <<_:128>>).
%% @doc Generate a global unique id. %% @doc Generate a global unique id.
-spec(gen() -> guid()). -spec(gen() -> guid()).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,12 +14,12 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-module(emqttd_hook). -module(emqttd_hooks).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(gen_server). -behaviour(gen_server).
-author("Feng Lee <feng@emqtt.io>").
%% Start %% Start
-export([start_link/0]). -export([start_link/0]).
@ -32,7 +32,12 @@
-record(state, {}). -record(state, {}).
-record(callback, {function :: function(), -type(hooktag() :: atom() | string() | binary()).
-export_type([hooktag/0]).
-record(callback, {tag :: hooktag(),
function :: function(),
init_args = [] :: list(any()), init_args = [] :: list(any()),
priority = 0 :: integer()}). priority = 0 :: integer()}).
@ -40,10 +45,6 @@
-define(HOOK_TAB, mqtt_hook). -define(HOOK_TAB, mqtt_hook).
%%--------------------------------------------------------------------
%% Start API
%%--------------------------------------------------------------------
start_link() -> start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
@ -51,17 +52,24 @@ start_link() ->
%% Hooks API %% Hooks API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(add(atom(), function(), list(any())) -> ok). -spec(add(atom(), function() | {hooktag(), function()}, list(any())) -> ok).
add(HookPoint, Function, InitArgs) -> add(HookPoint, Function, InitArgs) when is_function(Function) ->
add(HookPoint, Function, InitArgs, 0). add(HookPoint, {undefined, Function}, InitArgs, 0);
-spec(add(atom(), function(), list(any()), integer()) -> ok). add(HookPoint, {Tag, Function}, InitArgs) when is_function(Function) ->
add(HookPoint, Function, InitArgs, Priority) -> add(HookPoint, {Tag, Function}, InitArgs, 0).
gen_server:call(?MODULE, {add, HookPoint, Function, InitArgs, Priority}).
-spec(delete(atom(), function()) -> ok). -spec(add(atom(), function() | {hooktag(), function()}, list(any()), integer()) -> ok).
delete(HookPoint, Function) -> add(HookPoint, Function, InitArgs, Priority) when is_function(Function) ->
gen_server:call(?MODULE, {delete, HookPoint, Function}). add(HookPoint, {undefined, Function}, InitArgs, Priority);
add(HookPoint, {Tag, Function}, InitArgs, Priority) when is_function(Function) ->
gen_server:call(?MODULE, {add, HookPoint, {Tag, Function}, InitArgs, Priority}).
-spec(delete(atom(), function() | {hooktag(), function()}) -> ok).
delete(HookPoint, Function) when is_function(Function) ->
delete(HookPoint, {undefined, Function});
delete(HookPoint, {Tag, Function}) when is_function(Function) ->
gen_server:call(?MODULE, {delete, HookPoint, {Tag, Function}}).
%% @doc Run hooks without Acc. %% @doc Run hooks without Acc.
-spec(run(atom(), list(Arg :: any())) -> ok | stop). -spec(run(atom(), list(Arg :: any())) -> ok | stop).
@ -89,7 +97,8 @@ run_([#callback{function = Fun, init_args = InitArgs} | Callbacks], Args, Acc) -
ok -> run_(Callbacks, Args, Acc); ok -> run_(Callbacks, Args, Acc);
{ok, NewAcc} -> run_(Callbacks, Args, NewAcc); {ok, NewAcc} -> run_(Callbacks, Args, NewAcc);
stop -> {stop, Acc}; stop -> {stop, Acc};
{stop, NewAcc} -> {stop, NewAcc} {stop, NewAcc} -> {stop, NewAcc};
_Any -> run_(Callbacks, Args, Acc)
end; end;
run_([], _Args, Acc) -> run_([], _Args, Acc) ->
@ -98,8 +107,8 @@ run_([], _Args, Acc) ->
-spec(lookup(atom()) -> [#callback{}]). -spec(lookup(atom()) -> [#callback{}]).
lookup(HookPoint) -> lookup(HookPoint) ->
case ets:lookup(?HOOK_TAB, HookPoint) of case ets:lookup(?HOOK_TAB, HookPoint) of
[] -> []; [#hook{callbacks = Callbacks}] -> Callbacks;
[#hook{callbacks = Callbacks}] -> Callbacks [] -> []
end. end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -110,39 +119,38 @@ init([]) ->
ets:new(?HOOK_TAB, [set, protected, named_table, {keypos, #hook.name}]), ets:new(?HOOK_TAB, [set, protected, named_table, {keypos, #hook.name}]),
{ok, #state{}}. {ok, #state{}}.
handle_call({add, HookPoint, Function, InitArgs, Priority}, _From, State) -> handle_call({add, HookPoint, {Tag, Function}, InitArgs, Priority}, _From, State) ->
Reply = Callback = #callback{tag = Tag, function = Function,
case ets:lookup(?HOOK_TAB, HookPoint) of init_args = InitArgs, priority = Priority},
[#hook{callbacks = Callbacks}] -> {reply,
case lists:keyfind(Function, #callback.function, Callbacks) of case ets:lookup(?HOOK_TAB, HookPoint) of
false -> [#hook{callbacks = Callbacks}] ->
Callback = #callback{function = Function, case contain_(Tag, Function, Callbacks) of
init_args = InitArgs, false ->
priority = Priority}, insert_hook_(HookPoint, add_callback_(Callback, Callbacks));
insert_hook_(HookPoint, add_callback_(Callback, Callbacks)); true ->
_Callback -> {error, already_hooked}
{error, already_hooked} end;
end; [] ->
[] -> insert_hook_(HookPoint, [Callback])
Callback = #callback{function = Function, end, State};
init_args = InitArgs,
priority = Priority},
insert_hook_(HookPoint, [Callback])
end,
{reply, Reply, State};
handle_call({delete, HookPoint, Function}, _From, State) -> handle_call({delete, HookPoint, {Tag, Function}}, _From, State) ->
Reply = {reply,
case ets:lookup(?HOOK_TAB, HookPoint) of case ets:lookup(?HOOK_TAB, HookPoint) of
[#hook{callbacks = Callbacks}] -> [#hook{callbacks = Callbacks}] ->
insert_hook_(HookPoint, del_callback_(Function, Callbacks)); case contain_(Tag, Function, Callbacks) of
[] -> true ->
{error, not_found} insert_hook_(HookPoint, del_callback_(Tag, Function, Callbacks));
end, false ->
{reply, Reply, State}; {error, not_found}
end;
[] ->
{error, not_found}
end, State};
handle_call(_Req, _From, State) -> handle_call(Req, _From, State) ->
{reply, ignore, State}. {reply, {error, {unexpected_request, Req}}, State}.
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State}.
@ -166,6 +174,16 @@ insert_hook_(HookPoint, Callbacks) ->
add_callback_(Callback, Callbacks) -> add_callback_(Callback, Callbacks) ->
lists:keymerge(#callback.priority, Callbacks, [Callback]). lists:keymerge(#callback.priority, Callbacks, [Callback]).
del_callback_(Function, Callbacks) -> del_callback_(Tag, Function, Callbacks) ->
lists:keydelete(Function, #callback.function, Callbacks). lists:filter(
fun(#callback{tag = Tag1, function = Func1}) ->
not ((Tag =:= Tag1) andalso (Function =:= Func1))
end, Callbacks).
contain_(_Tag, _Function, []) ->
false;
contain_(Tag, Function, [#callback{tag = Tag, function = Function}|_Callbacks]) ->
true;
contain_(Tag, Function, [_Callback | Callbacks]) ->
contain_(Tag, Function, Callbacks).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,9 +14,12 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd http publish API and websocket client. %% @doc HTTP publish API and websocket client.
-module(emqttd_http). -module(emqttd_http).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -98,7 +101,7 @@ http_publish(Req) ->
Msg = emqttd_message:make(ClientId, Qos, Topic, Payload), Msg = emqttd_message:make(ClientId, Qos, Topic, Payload),
emqttd:publish(Msg#mqtt_message{retain = Retain}) emqttd:publish(Msg#mqtt_message{retain = Retain})
end, Topics), end, Topics),
Req:ok({"text/plain", <<"ok">>}); Req:ok({"text/plain", <<"OK">>});
{false, _} -> {false, _} ->
Req:respond({400, [], <<"Bad QoS">>}); Req:respond({400, [], <<"Bad QoS">>});
{_, false} -> {_, false} ->

94
src/emqttd_inflight.erl Normal file
View File

@ -0,0 +1,94 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
%% @doc Inflight Window that wraps the gb_trees.
-module(emqttd_inflight).
-author("Feng Lee <feng@emqtt.io>").
-export([new/1, contain/2, lookup/2, insert/3, update/3, delete/2, values/1,
to_list/1, size/1, max_size/1, is_full/1, is_empty/1, window/1]).
-type(inflight() :: {?MODULE, list()}).
-export_type([inflight/0]).
-spec(new(non_neg_integer()) -> inflight()).
new(MaxSize) when MaxSize >= 0 ->
{?MODULE, [MaxSize, gb_trees:empty()]}.
-spec(contain(Key :: any(), inflight()) -> boolean()).
contain(Key, {?MODULE, [_MaxSize, Tree]}) ->
gb_trees:is_defined(Key, Tree).
-spec(lookup(Key :: any(), inflight()) -> any()).
lookup(Key, {?MODULE, [_MaxSize, Tree]}) ->
gb_trees:get(Key, Tree).
-spec(insert(Key :: any(), Value :: any(), inflight()) -> inflight()).
insert(Key, Value, {?MODULE, [MaxSize, Tree]}) ->
{?MODULE, [MaxSize, gb_trees:insert(Key, Value, Tree)]}.
-spec(delete(Key :: any(), inflight()) -> inflight()).
delete(Key, {?MODULE, [MaxSize, Tree]}) ->
{?MODULE, [MaxSize, gb_trees:delete(Key, Tree)]}.
-spec(update(Key :: any(), Val :: any(), inflight()) -> inflight()).
update(Key, Val, {?MODULE, [MaxSize, Tree]}) ->
{?MODULE, [MaxSize, gb_trees:update(Key, Val, Tree)]}.
-spec(is_full(inflight()) -> boolean()).
is_full({?MODULE, [0, _Tree]}) ->
false;
is_full({?MODULE, [MaxSize, Tree]}) ->
MaxSize =< gb_trees:size(Tree).
-spec(is_empty(inflight()) -> boolean()).
is_empty({?MODULE, [_MaxSize, Tree]}) ->
gb_trees:is_empty(Tree).
-spec(smallest(inflight()) -> {K :: any(), V :: any()}).
smallest({?MODULE, [_MaxSize, Tree]}) ->
gb_trees:smallest(Tree).
-spec(largest(inflight()) -> {K :: any(), V :: any()}).
largest({?MODULE, [_MaxSize, Tree]}) ->
gb_trees:largest(Tree).
-spec(values(inflight()) -> list()).
values({?MODULE, [_MaxSize, Tree]}) ->
gb_trees:values(Tree).
-spec(to_list(inflight()) -> list({K :: any(), V :: any()})).
to_list({?MODULE, [_MaxSize, Tree]}) ->
gb_trees:to_list(Tree).
-spec(window(inflight()) -> list()).
window(Inflight = {?MODULE, [_MaxSize, Tree]}) ->
case gb_trees:is_empty(Tree) of
true -> [];
false -> [Key || {Key, _Val} <- [smallest(Inflight), largest(Inflight)]]
end.
-spec(size(inflight()) -> non_neg_integer()).
size({?MODULE, [_MaxSize, Tree]}) ->
gb_trees:size(Tree).
-spec(max_size(inflight()) -> non_neg_integer()).
max_size({?MODULE, [MaxSize, _Tree]}) ->
MaxSize.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -15,25 +15,32 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Client Keepalive %% @doc Client Keepalive
-module(emqttd_keepalive). -module(emqttd_keepalive).
-author("Feng Lee <feng@emqtt.io>").
-export([start/3, check/1, cancel/1]). -export([start/3, check/1, cancel/1]).
-record(keepalive, {statfun, statval, -record(keepalive, {statfun, statval, tsec, tmsg, tref, repeat = 0}).
tsec, tmsg, tref,
repeat = 0}).
-type keepalive() :: #keepalive{}. -type(keepalive() :: #keepalive{}).
-export_type([keepalive/0]).
%% @doc Start a keepalive %% @doc Start a keepalive
-spec(start(fun(), integer(), any()) -> undefined | keepalive()). -spec(start(fun(), integer(), any()) -> {ok, keepalive()} | {error, any()}).
start(_, 0, _) -> start(_, 0, _) ->
undefined; {ok, #keepalive{}};
start(StatFun, TimeoutSec, TimeoutMsg) -> start(StatFun, TimeoutSec, TimeoutMsg) ->
{ok, StatVal} = StatFun(), case StatFun() of
#keepalive{statfun = StatFun, statval = StatVal, {ok, StatVal} ->
tsec = TimeoutSec, tmsg = TimeoutMsg, {ok, #keepalive{statfun = StatFun, statval = StatVal,
tref = timer(TimeoutSec, TimeoutMsg)}. tsec = TimeoutSec, tmsg = TimeoutMsg,
tref = timer(TimeoutSec, TimeoutMsg)}};
{error, Error} ->
{error, Error}
end.
%% @doc Check keepalive, called when timeout. %% @doc Check keepalive, called when timeout.
-spec(check(keepalive()) -> {ok, keepalive()} | {error, any()}). -spec(check(keepalive()) -> {ok, keepalive()} | {error, any()}).
@ -56,12 +63,10 @@ resume(KeepAlive = #keepalive{tsec = TimeoutSec, tmsg = TimeoutMsg}) ->
%% @doc Cancel Keepalive %% @doc Cancel Keepalive
-spec(cancel(keepalive()) -> ok). -spec(cancel(keepalive()) -> ok).
cancel(#keepalive{tref = TRef}) -> cancel(#keepalive{tref = TRef}) when is_reference(TRef) ->
cancel(TRef); catch erlang:cancel_timer(TRef), ok;
cancel(undefined) -> cancel(_) ->
ok; ok.
cancel(TRef) ->
catch erlang:cancel_timer(TRef).
timer(Sec, Msg) -> timer(Sec, Msg) ->
erlang:send_after(timer:seconds(Sec), self(), Msg). erlang:send_after(timer:seconds(Sec), self(), Msg).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -15,8 +15,11 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc MQTT Message Functions %% @doc MQTT Message Functions
-module(emqttd_message). -module(emqttd_message).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -42,7 +45,7 @@ make(From, Qos, Topic, Payload) ->
qos = ?QOS_I(Qos), qos = ?QOS_I(Qos),
topic = Topic, topic = Topic,
payload = Payload, payload = Payload,
timestamp = emqttd_time:now_to_secs()}. timestamp = os:timestamp()}.
%% @doc Message from Packet %% @doc Message from Packet
-spec(from_packet(mqtt_packet()) -> mqtt_message()). -spec(from_packet(mqtt_packet()) -> mqtt_message()).
@ -60,7 +63,7 @@ from_packet(#mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
dup = Dup, dup = Dup,
topic = Topic, topic = Topic,
payload = Payload, payload = Payload,
timestamp = emqttd_time:now_to_secs()}; timestamp = os:timestamp()};
from_packet(#mqtt_packet_connect{will_flag = false}) -> from_packet(#mqtt_packet_connect{will_flag = false}) ->
undefined; undefined;
@ -78,7 +81,7 @@ from_packet(#mqtt_packet_connect{client_id = ClientId,
qos = Qos, qos = Qos,
dup = false, dup = false,
payload = Msg, payload = Msg,
timestamp = emqttd_time:now_to_secs()}. timestamp = os:timestamp()}.
from_packet(ClientId, Packet) -> from_packet(ClientId, Packet) ->
Msg = from_packet(Packet), Msg = from_packet(Packet),

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,11 +14,12 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd metrics. responsible for collecting broker metrics.
-module(emqttd_metrics). -module(emqttd_metrics).
-behaviour(gen_server). -behaviour(gen_server).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -37,7 +38,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
-record(state, {tick_tref}). -record(state, {tick}).
-define(METRIC_TAB, mqtt_metric). -define(METRIC_TAB, mqtt_metric).
@ -57,12 +58,16 @@
{counter, 'packets/publish/sent'}, % PUBLISH packets sent {counter, 'packets/publish/sent'}, % PUBLISH packets sent
{counter, 'packets/puback/received'}, % PUBACK packets received {counter, 'packets/puback/received'}, % PUBACK packets received
{counter, 'packets/puback/sent'}, % PUBACK packets sent {counter, 'packets/puback/sent'}, % PUBACK packets sent
{counter, 'packets/puback/missed'}, % PUBACK packets missed
{counter, 'packets/pubrec/received'}, % PUBREC packets received {counter, 'packets/pubrec/received'}, % PUBREC packets received
{counter, 'packets/pubrec/sent'}, % PUBREC packets sent {counter, 'packets/pubrec/sent'}, % PUBREC packets sent
{counter, 'packets/pubrec/missed'}, % PUBREC packets missed
{counter, 'packets/pubrel/received'}, % PUBREL packets received {counter, 'packets/pubrel/received'}, % PUBREL packets received
{counter, 'packets/pubrel/sent'}, % PUBREL packets sent {counter, 'packets/pubrel/sent'}, % PUBREL packets sent
{counter, 'packets/pubrel/missed'}, % PUBREL packets missed
{counter, 'packets/pubcomp/received'}, % PUBCOMP packets received {counter, 'packets/pubcomp/received'}, % PUBCOMP packets received
{counter, 'packets/pubcomp/sent'}, % PUBCOMP packets sent {counter, 'packets/pubcomp/sent'}, % PUBCOMP packets sent
{counter, 'packets/pubcomp/missed'}, % PUBCOMP packets missed
{counter, 'packets/subscribe'}, % SUBSCRIBE Packets received {counter, 'packets/subscribe'}, % SUBSCRIBE Packets received
{counter, 'packets/suback'}, % SUBACK packets sent {counter, 'packets/suback'}, % SUBACK packets sent
{counter, 'packets/unsubscribe'}, % UNSUBSCRIBE Packets received {counter, 'packets/unsubscribe'}, % UNSUBSCRIBE Packets received
@ -74,14 +79,15 @@
%% Messages sent and received of broker %% Messages sent and received of broker
-define(SYSTOP_MESSAGES, [ -define(SYSTOP_MESSAGES, [
{counter, 'messages/received'}, % Messages received {counter, 'messages/received'}, % All Messages received
{counter, 'messages/sent'}, % Messages sent {counter, 'messages/sent'}, % All Messages sent
{counter, 'messages/qos0/received'}, % Messages received {counter, 'messages/qos0/received'}, % QoS0 Messages received
{counter, 'messages/qos0/sent'}, % Messages sent {counter, 'messages/qos0/sent'}, % QoS0 Messages sent
{counter, 'messages/qos1/received'}, % Messages received {counter, 'messages/qos1/received'}, % QoS1 Messages received
{counter, 'messages/qos1/sent'}, % Messages sent {counter, 'messages/qos1/sent'}, % QoS1 Messages sent
{counter, 'messages/qos2/received'}, % Messages received {counter, 'messages/qos2/received'}, % QoS2 Messages received
{counter, 'messages/qos2/sent'}, % Messages sent {counter, 'messages/qos2/sent'}, % QoS2 Messages sent
{counter, 'messages/qos2/dropped'}, % QoS2 Messages dropped
{gauge, 'messages/retained'}, % Messagea retained {gauge, 'messages/retained'}, % Messagea retained
{counter, 'messages/dropped'} % Messages dropped {counter, 'messages/dropped'} % Messages dropped
]). ]).
@ -138,7 +144,7 @@ qos_received(?QOS_2) ->
sent(?PUBLISH_PACKET(_Qos, <<"$SYS/", _/binary>>, _, _)) -> sent(?PUBLISH_PACKET(_Qos, <<"$SYS/", _/binary>>, _, _)) ->
ignore; ignore;
sent(Packet) -> sent(Packet) ->
emqttd_metrics:inc('packets/sent'), inc('packets/sent'),
sent1(Packet). sent1(Packet).
sent1(?PUBLISH_PACKET(Qos, _PktId)) -> sent1(?PUBLISH_PACKET(Qos, _PktId)) ->
inc('packets/publish/sent'), inc('packets/publish/sent'),
@ -245,7 +251,7 @@ init([]) ->
% $SYS Topics for metrics % $SYS Topics for metrics
% [ok = emqttd:create(topic, metric_topic(Topic)) || {_, Topic} <- Metrics], % [ok = emqttd:create(topic, metric_topic(Topic)) || {_, Topic} <- Metrics],
% Tick to publish metrics % Tick to publish metrics
{ok, #state{tick_tref = emqttd_broker:start_tick(tick)}, hibernate}. {ok, #state{tick = emqttd_broker:start_tick(tick)}, hibernate}.
handle_call(_Req, _From, State) -> handle_call(_Req, _From, State) ->
{reply, error, State}. {reply, error, State}.
@ -261,7 +267,7 @@ handle_info(tick, State) ->
handle_info(_Info, State) -> handle_info(_Info, State) ->
{noreply, State}. {noreply, State}.
terminate(_Reason, #state{tick_tref = TRef}) -> terminate(_Reason, #state{tick = TRef}) ->
emqttd_broker:stop_tick(TRef). emqttd_broker:stop_tick(TRef).
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,12 +14,15 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-module(emqttd_opts). -module(emqttd_misc).
-export([merge/2]). -author("Feng Lee <feng@emqtt.io>").
-export([merge_opts/2, start_timer/2, start_timer/3, cancel_timer/1,
proc_stats/0, proc_stats/1]).
%% @doc Merge Options %% @doc Merge Options
merge(Defaults, Options) -> merge_opts(Defaults, Options) ->
lists:foldl( lists:foldl(
fun({Opt, Val}, Acc) -> fun({Opt, Val}, Acc) ->
case lists:keymember(Opt, 1, Acc) of case lists:keymember(Opt, 1, Acc) of
@ -33,3 +36,30 @@ merge(Defaults, Options) ->
end end
end, Defaults, Options). end, Defaults, Options).
-spec(start_timer(integer(), term()) -> reference()).
start_timer(Interval, Msg) ->
start_timer(Interval, self(), Msg).
-spec(start_timer(integer(), pid() | atom(), term()) -> reference()).
start_timer(Interval, Dest, Msg) ->
erlang:start_timer(Interval, Dest, Msg).
-spec(cancel_timer(undefined | reference()) -> ok).
cancel_timer(undefined) ->
ok;
cancel_timer(Timer) ->
case catch erlang:cancel_timer(Timer) of
false -> receive {timeout, Timer, _} -> ok after 0 -> ok end;
_ -> ok
end.
-spec(proc_stats() -> list()).
proc_stats() ->
proc_stats(self()).
-spec(proc_stats(pid()) -> list()).
proc_stats(Pid) ->
Stats = process_info(Pid, [message_queue_len, heap_size, reductions]),
{value, {_, V}, Stats1} = lists:keytake(message_queue_len, 1, Stats),
[{mailbox_len, V} | Stats1].

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_mnesia). -module(emqttd_mnesia).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -39,11 +39,7 @@ start_link() ->
start_child(ChildSpec) when is_tuple(ChildSpec) -> start_child(ChildSpec) when is_tuple(ChildSpec) ->
supervisor:start_child(?MODULE, ChildSpec). supervisor:start_child(?MODULE, ChildSpec).
%% start_child(Mod, Type) when is_atom(Mod) andalso is_atom(Type) ->
%% start_child(Mod::atom(), Type::type()) -> {ok, pid()}
%% @type type() = worker | supervisor
%%
start_child(Mod, Type) when is_atom(Mod) and is_atom(Type) ->
supervisor:start_child(?MODULE, ?CHILD(Mod, Type)). supervisor:start_child(?MODULE, ?CHILD(Mod, Type)).
-spec(stop_child(any()) -> ok | {error, any()}). -spec(stop_child(any()) -> ok | {error, any()}).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -43,52 +43,50 @@
-module(emqttd_mqueue). -module(emqttd_mqueue).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
-import(proplists, [get_value/3]). -import(proplists, [get_value/3]).
-export([new/3, type/1, name/1, is_empty/1, len/1, max_len/1, in/2, out/1, stats/1]). -export([new/3, type/1, name/1, is_empty/1, len/1, max_len/1, in/2, out/1,
dropped/1, stats/1]).
-define(LOW_WM, 0.2). -define(LOW_WM, 0.2).
-define(HIGH_WM, 0.6). -define(HIGH_WM, 0.6).
-type priority() :: {iolist(), pos_integer()}. -type(priority() :: {iolist(), pos_integer()}).
-type option() :: {type, simple | priority} -type(option() :: {type, simple | priority}
| {max_length, pos_integer() | infinity} | {max_length, pos_integer() | infinity}
| {priority, list(priority())} | {priority, list(priority())}
| {low_watermark, float()} %% Low watermark | {low_watermark, float()} %% Low watermark
| {high_watermark, float()} %% High watermark | {high_watermark, float()} %% High watermark
| {queue_qos0, boolean()}. %% Queue Qos0? | {queue_qos0, boolean()}). %% Queue Qos0?
-type mqueue_option() :: {max_length, pos_integer()} %% Max queue length -type(stat() :: {max_len, infinity | pos_integer()}
| {low_watermark, float()} %% Low watermark
| {high_watermark, float()} %% High watermark
| {queue_qos0, boolean()}. %% Queue Qos0
-type stat() :: {max_len, infinity | pos_integer()}
| {len, non_neg_integer()} | {len, non_neg_integer()}
| {dropped, non_neg_integer()}. | {dropped, non_neg_integer()}).
-record(mqueue, {type :: simple | priority, -record(mqueue, {type :: simple | priority,
name, q :: queue:queue() | priority_queue:q(), name, q :: queue:queue() | priority_queue:q(),
%% priority table %% priority table
pseq = 0, priorities = [], pseq = 0, priorities = [],
%% len of simple queue %% len of simple queue
len = 0, max_len = ?MAX_LEN, len = 0, max_len = infinity,
low_wm = ?LOW_WM, high_wm = ?HIGH_WM, low_wm = ?LOW_WM, high_wm = ?HIGH_WM,
qos0 = false, dropped = 0, qos0 = false, dropped = 0,
alarm_fun}). alarm_fun}).
-type mqueue() :: #mqueue{}. -type(mqueue() :: #mqueue{}).
-export_type([mqueue/0, priority/0, option/0]). -export_type([mqueue/0, priority/0, option/0]).
%% @doc New Queue. %% @doc New Queue.
-spec(new(iolist(), list(mqueue_option()), fun()) -> mqueue()). -spec(new(iolist(), list(option()), fun()) -> mqueue()).
new(Name, Opts, AlarmFun) -> new(Name, Opts, AlarmFun) ->
Type = get_value(type, Opts, simple), Type = get_value(type, Opts, simple),
MaxLen = get_value(max_length, Opts, infinity), MaxLen = get_value(max_length, Opts, infinity),
@ -141,6 +139,10 @@ len(#mqueue{type = priority, q = Q}) -> priority_queue:len(Q).
max_len(#mqueue{max_len= MaxLen}) -> MaxLen. max_len(#mqueue{max_len= MaxLen}) -> MaxLen.
%% @doc Dropped of the mqueue
-spec(dropped(mqueue()) -> non_neg_integer()).
dropped(#mqueue{dropped = Dropped}) -> Dropped.
%% @doc Stats of the mqueue %% @doc Stats of the mqueue
-spec(stats(mqueue()) -> [stat()]). -spec(stats(mqueue()) -> [stat()]).
stats(#mqueue{type = Type, q = Q, max_len = MaxLen, len = Len, dropped = Dropped}) -> stats(#mqueue{type = Type, q = Q, max_len = MaxLen, len = Len, dropped = Dropped}) ->
@ -208,14 +210,12 @@ maybe_set_alarm(MQ = #mqueue{name = Name, len = Len, high_wm = HighWM, alarm_fun
title = io_lib:format("Queue ~s high-water mark", [Name]), title = io_lib:format("Queue ~s high-water mark", [Name]),
summary = io_lib:format("queue len ~p > high_watermark ~p", [Len, HighWM])}, summary = io_lib:format("queue len ~p > high_watermark ~p", [Len, HighWM])},
MQ#mqueue{alarm_fun = AlarmFun(alert, Alarm)}; MQ#mqueue{alarm_fun = AlarmFun(alert, Alarm)};
maybe_set_alarm(MQ) -> maybe_set_alarm(MQ) ->
MQ. MQ.
maybe_clear_alarm(MQ = #mqueue{name = Name, len = Len, low_wm = LowWM, alarm_fun = AlarmFun}) maybe_clear_alarm(MQ = #mqueue{name = Name, len = Len, low_wm = LowWM, alarm_fun = AlarmFun})
when Len < LowWM -> when Len < LowWM ->
MQ#mqueue{alarm_fun = AlarmFun(clear, list_to_binary(["queue_high_watermark.", Name]))}; MQ#mqueue{alarm_fun = AlarmFun(clear, list_to_binary(["queue_high_watermark.", Name]))};
maybe_clear_alarm(MQ) -> maybe_clear_alarm(MQ) ->
MQ. MQ.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,7 +14,6 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd net utilities.
-module(emqttd_net). -module(emqttd_net).
-include_lib("kernel/include/inet.hrl"). -include_lib("kernel/include/inet.hrl").

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_node). -module(emqttd_node).
-author("Feng Lee <feng@emqtt.io>").
-import(lists, [concat/1]). -import(lists, [concat/1]).
-export([is_aliving/1, parse_name/1]). -export([is_aliving/1, parse_name/1]).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,9 +14,10 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc MQTT Packet Functions
-module(emqttd_packet). -module(emqttd_packet).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -28,12 +29,13 @@
%% @doc Protocol name of version %% @doc Protocol name of version
-spec(protocol_name(mqtt_vsn()) -> binary()). -spec(protocol_name(mqtt_vsn()) -> binary()).
protocol_name(Ver) when Ver =:= ?MQTT_PROTO_V31; Ver =:= ?MQTT_PROTO_V311 -> protocol_name(?MQTT_PROTO_V3) -> <<"MQIsdp">>;
proplists:get_value(Ver, ?PROTOCOL_NAMES). protocol_name(?MQTT_PROTO_V4) -> <<"MQTT">>;
protocol_name(?MQTT_PROTO_V5) -> <<"MQTT">>.
%% @doc Name of MQTT packet type %% @doc Name of MQTT packet type
-spec(type_name(mqtt_packet_type()) -> atom()). -spec(type_name(mqtt_packet_type()) -> atom()).
type_name(Type) when Type > ?RESERVED andalso Type =< ?DISCONNECT -> type_name(Type) when Type > ?RESERVED andalso Type =< ?AUTH ->
lists:nth(Type, ?TYPE_NAMES). lists:nth(Type, ?TYPE_NAMES).
%% @doc Connack Name %% @doc Connack Name

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,32 +17,31 @@
%% @doc MQTT Packet Parser %% @doc MQTT Packet Parser
-module(emqttd_parser). -module(emqttd_parser).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
%% API %% API
-export([new/1, parse/2]). -export([initial_state/0, initial_state/1, parse/2]).
-record(mqtt_packet_limit, {max_packet_size}). -type(max_packet_size() :: 1..?MAX_PACKET_SIZE).
-type option() :: {atom(), any()}. -spec(initial_state() -> {none, max_packet_size()}).
initial_state() ->
-type parser() :: fun( (binary()) -> any() ). initial_state(?MAX_PACKET_SIZE).
%% @doc Initialize a parser %% @doc Initialize a parser
-spec(new(Opts :: [option()]) -> parser()). -spec(initial_state(max_packet_size()) -> {none, max_packet_size()}).
new(Opts) -> initial_state(MaxSize) ->
fun(Bin) -> parse(Bin, {none, limit(Opts)}) end. {none, MaxSize}.
limit(Opts) ->
#mqtt_packet_limit{max_packet_size = proplists:get_value(max_packet_size, Opts, ?MAX_LEN)}.
%% @doc Parse MQTT Packet %% @doc Parse MQTT Packet
-spec(parse(binary(), {none, [option()]} | fun()) -spec(parse(binary(), {none, pos_integer()} | fun())
-> {ok, mqtt_packet()} | {error, any()} | {more, fun()}). -> {ok, mqtt_packet()} | {error, any()} | {more, fun()}).
parse(<<>>, {none, Limit}) -> parse(<<>>, {none, MaxLen}) ->
{more, fun(Bin) -> parse(Bin, {none, Limit}) end}; {more, fun(Bin) -> parse(Bin, {none, MaxLen}) end};
parse(<<Type:4, Dup:1, QoS:2, Retain:1, Rest/binary>>, {none, Limit}) -> parse(<<Type:4, Dup:1, QoS:2, Retain:1, Rest/binary>>, {none, Limit}) ->
parse_remaining_len(Rest, #mqtt_packet_header{type = Type, parse_remaining_len(Rest, #mqtt_packet_header{type = Type,
dup = bool(Dup), dup = bool(Dup),
@ -55,7 +54,7 @@ parse_remaining_len(<<>>, Header, Limit) ->
parse_remaining_len(Rest, Header, Limit) -> parse_remaining_len(Rest, Header, Limit) ->
parse_remaining_len(Rest, Header, 1, 0, Limit). parse_remaining_len(Rest, Header, 1, 0, Limit).
parse_remaining_len(_Bin, _Header, _Multiplier, Length, #mqtt_packet_limit{max_packet_size = MaxLen}) parse_remaining_len(_Bin, _Header, _Multiplier, Length, MaxLen)
when Length > MaxLen -> when Length > MaxLen ->
{error, invalid_mqtt_frame_len}; {error, invalid_mqtt_frame_len};
parse_remaining_len(<<>>, Header, Multiplier, Length, Limit) -> parse_remaining_len(<<>>, Header, Multiplier, Length, Limit) ->
@ -68,7 +67,7 @@ parse_remaining_len(<<0:8, Rest/binary>>, Header, 1, 0, _Limit) ->
parse_frame(Rest, Header, 0); parse_frame(Rest, Header, 0);
parse_remaining_len(<<1:1, Len:7, Rest/binary>>, Header, Multiplier, Value, Limit) -> parse_remaining_len(<<1:1, Len:7, Rest/binary>>, Header, Multiplier, Value, Limit) ->
parse_remaining_len(Rest, Header, Multiplier * ?HIGHBIT, Value + Len * Multiplier, Limit); parse_remaining_len(Rest, Header, Multiplier * ?HIGHBIT, Value + Len * Multiplier, Limit);
parse_remaining_len(<<0:1, Len:7, Rest/binary>>, Header, Multiplier, Value, #mqtt_packet_limit{max_packet_size = MaxLen}) -> parse_remaining_len(<<0:1, Len:7, Rest/binary>>, Header, Multiplier, Value, MaxLen) ->
FrameLen = Value + Len * Multiplier, FrameLen = Value + Len * Multiplier,
if if
FrameLen > MaxLen -> {error, invalid_mqtt_frame_len}; FrameLen > MaxLen -> {error, invalid_mqtt_frame_len};
@ -86,7 +85,7 @@ parse_frame(Bin, #mqtt_packet_header{type = Type, qos = Qos} = Header, Length)
WillRetain : 1, WillRetain : 1,
WillQos : 2, WillQos : 2,
WillFlag : 1, WillFlag : 1,
CleanSession : 1, CleanSess : 1,
_Reserved : 1, _Reserved : 1,
KeepAlive : 16/big, KeepAlive : 16/big,
Rest3/binary>> = Rest2, Rest3/binary>> = Rest2,
@ -104,7 +103,7 @@ parse_frame(Bin, #mqtt_packet_header{type = Type, qos = Qos} = Header, Length)
will_retain = bool(WillRetain), will_retain = bool(WillRetain),
will_qos = WillQos, will_qos = WillQos,
will_flag = bool(WillFlag), will_flag = bool(WillFlag),
clean_sess = bool(CleanSession), clean_sess = bool(CleanSess),
keep_alive = KeepAlive, keep_alive = KeepAlive,
client_id = ClientId, client_id = ClientId,
will_topic = WillTopic, will_topic = WillTopic,

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_plugins). -module(emqttd_plugins).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-export([init/0]). -export([init/0]).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016 Feng Lee <feng@emqtt.io>. All Rights Reserved. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,11 +18,14 @@
-author("Feng Lee <feng@emqtt.io>"). -author("Feng Lee <feng@emqtt.io>").
-type(pmon() :: {?MODULE, map()}).
-export([new/0, monitor/2, demonitor/2, erase/2]). -export([new/0, monitor/2, demonitor/2, erase/2]).
new() -> {?MODULE, [maps:new()]}. -type(pmon() :: {?MODULE, map()}).
-export_type([pmon/0]).
new() ->
{?MODULE, [maps:new()]}.
-spec(monitor(pid(), pmon()) -> pmon()). -spec(monitor(pid(), pmon()) -> pmon()).
monitor(Pid, PM = {?MODULE, [M]}) -> monitor(Pid, PM = {?MODULE, [M]}) ->

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
%% @doc Common Pool Supervisor %% @doc Common Pool Supervisor
-module(emqttd_pool_sup). -module(emqttd_pool_sup).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(supervisor). -behaviour(supervisor).
%% API %% API

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc.
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ start_link() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% API %% API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(start_link(atom(), pos_integer()) -> {ok, pid()} | ignore | {error, any()}). -spec(start_link(atom(), pos_integer()) -> {ok, pid()} | ignore | {error, any()}).
start_link(Pool, Id) -> start_link(Pool, Id) ->
gen_server:start_link({local, ?PROC_NAME(?MODULE, Id)}, ?MODULE, [Pool, Id], []). gen_server:start_link({local, ?PROC_NAME(?MODULE, Id)}, ?MODULE, [Pool, Id], []).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_protocol). -module(emqttd_protocol).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -25,42 +27,54 @@
-import(proplists, [get_value/2, get_value/3]). -import(proplists, [get_value/2, get_value/3]).
%% API %% API
-export([init/3, info/1, clientid/1, client/1, session/1]). -export([init/3, info/1, stats/1, clientid/1, client/1, session/1]).
-export([received/2, handle/2, send/2, redeliver/2, shutdown/2]). -export([subscribe/2, unsubscribe/2, pubrel/2, shutdown/2]).
-export([received/2, send/2]).
-export([process/2]). -export([process/2]).
%% Protocol State -record(proto_stats, {enable_stats = false, recv_pkt = 0, recv_msg = 0,
-record(proto_state, {peername, sendfun, connected = false, send_pkt = 0, send_msg = 0}).
client_id, client_pid, clean_sess,
proto_ver, proto_name, username, is_superuser = false,
will_msg, keepalive, max_clientid_len = ?MAX_CLIENTID_LEN,
session, ws_initial_headers, %% Headers from first HTTP request for websocket client
connected_at}).
-type proto_state() :: #proto_state{}. %% Protocol State
%% ws_initial_headers: Headers from first HTTP request for WebSocket Client.
-record(proto_state, {peername, sendfun, connected = false, client_id, client_pid,
clean_sess, proto_ver, proto_name, username, is_superuser,
will_msg, keepalive, max_clientid_len, session, stats_data,
ws_initial_headers, connected_at}).
-type(proto_state() :: #proto_state{}).
-define(INFO_KEYS, [client_id, username, clean_sess, proto_ver, proto_name, -define(INFO_KEYS, [client_id, username, clean_sess, proto_ver, proto_name,
keepalive, will_msg, ws_initial_headers, connected_at]). keepalive, will_msg, ws_initial_headers, connected_at]).
-define(STATS_KEYS, [recv_pkt, recv_msg, send_pkt, send_msg]).
-define(LOG(Level, Format, Args, State), -define(LOG(Level, Format, Args, State),
lager:Level([{client, State#proto_state.client_id}], "Client(~s@~s): " ++ Format, lager:Level([{client, State#proto_state.client_id}], "Client(~s@~s): " ++ Format,
[State#proto_state.client_id, esockd_net:format(State#proto_state.peername) | Args])). [State#proto_state.client_id, esockd_net:format(State#proto_state.peername) | Args])).
%% @doc Init protocol %% @doc Init protocol
init(Peername, SendFun, Opts) -> init(Peername, SendFun, Opts) ->
EnableStats = get_value(client_enable_stats, Opts, false),
MaxLen = get_value(max_clientid_len, Opts, ?MAX_CLIENTID_LEN), MaxLen = get_value(max_clientid_len, Opts, ?MAX_CLIENTID_LEN),
WsInitialHeaders = get_value(ws_initial_headers, Opts), WsInitialHeaders = get_value(ws_initial_headers, Opts),
#proto_state{peername = Peername, #proto_state{peername = Peername,
sendfun = SendFun, sendfun = SendFun,
max_clientid_len = MaxLen,
client_pid = self(), client_pid = self(),
ws_initial_headers = WsInitialHeaders}. max_clientid_len = MaxLen,
is_superuser = false,
ws_initial_headers = WsInitialHeaders,
stats_data = #proto_stats{enable_stats = EnableStats}}.
info(ProtoState) -> info(ProtoState) ->
?record_to_proplist(proto_state, ProtoState, ?INFO_KEYS). ?record_to_proplist(proto_state, ProtoState, ?INFO_KEYS).
stats(#proto_state{stats_data = Stats}) ->
tl(?record_to_proplist(proto_stats, Stats)).
clientid(#proto_state{client_id = ClientId}) -> clientid(#proto_state{client_id = ClientId}) ->
ClientId. ClientId.
@ -96,8 +110,10 @@ session(#proto_state{session = Session}) ->
%% A Client can only send the CONNECT Packet once over a Network Connection. %% A Client can only send the CONNECT Packet once over a Network Connection.
-spec(received(mqtt_packet(), proto_state()) -> {ok, proto_state()} | {error, any()}). -spec(received(mqtt_packet(), proto_state()) -> {ok, proto_state()} | {error, any()}).
received(Packet = ?PACKET(?CONNECT), State = #proto_state{connected = false}) -> received(Packet = ?PACKET(?CONNECT),
process(Packet, State#proto_state{connected = true}); State = #proto_state{connected = false, stats_data = Stats}) ->
trace(recv, Packet, State), Stats1 = inc_stats(recv, ?CONNECT, Stats),
process(Packet, State#proto_state{connected = true, stats_data = Stats1});
received(?PACKET(?CONNECT), State = #proto_state{connected = true}) -> received(?PACKET(?CONNECT), State = #proto_state{connected = true}) ->
{error, protocol_bad_connect, State}; {error, protocol_bad_connect, State};
@ -106,36 +122,42 @@ received(?PACKET(?CONNECT), State = #proto_state{connected = true}) ->
received(_Packet, State = #proto_state{connected = false}) -> received(_Packet, State = #proto_state{connected = false}) ->
{error, protocol_not_connected, State}; {error, protocol_not_connected, State};
received(Packet = ?PACKET(_Type), State) -> received(Packet = ?PACKET(Type), State = #proto_state{stats_data = Stats}) ->
trace(recv, Packet, State), trace(recv, Packet, State), Stats1 = inc_stats(recv, Type, Stats),
case validate_packet(Packet) of case validate_packet(Packet) of
ok -> ok ->
process(Packet, State); process(Packet, State#proto_state{stats_data = Stats1});
{error, Reason} -> {error, Reason} ->
{error, Reason, State} {error, Reason, State}
end. end.
handle({subscribe, RawTopicTable}, ProtoState = #proto_state{client_id = ClientId, subscribe(RawTopicTable, ProtoState = #proto_state{client_id = ClientId,
username = Username, username = Username,
session = Session}) -> session = Session}) ->
TopicTable = parse_topic_table(RawTopicTable), TopicTable = parse_topic_table(RawTopicTable),
case emqttd:run_hooks('client.subscribe', [ClientId, Username], TopicTable) of case emqttd_hooks:run('client.subscribe', [ClientId, Username], TopicTable) of
{ok, TopicTable1} -> {ok, TopicTable1} ->
emqttd_session:subscribe(Session, TopicTable1); emqttd_session:subscribe(Session, TopicTable1);
{stop, _} -> {stop, _} ->
ok ok
end, end,
{ok, ProtoState};
handle({unsubscribe, RawTopics}, ProtoState = #proto_state{client_id = ClientId,
username = Username,
session = Session}) ->
{ok, TopicTable} = emqttd:run_hooks('client.unsubscribe',
[ClientId, Username], parse_topics(RawTopics)),
emqttd_session:unsubscribe(Session, TopicTable),
{ok, ProtoState}. {ok, ProtoState}.
process(Packet = ?CONNECT_PACKET(Var), State0) -> unsubscribe(RawTopics, ProtoState = #proto_state{client_id = ClientId,
username = Username,
session = Session}) ->
case emqttd_hooks:run('client.unsubscribe', [ClientId, Username], parse_topics(RawTopics)) of
{ok, TopicTable} ->
emqttd_session:unsubscribe(Session, TopicTable);
{stop, _} ->
ok
end,
{ok, ProtoState}.
%% @doc Send PUBREL
pubrel(PacketId, State) -> send(?PUBREL_PACKET(PacketId), State).
process(?CONNECT_PACKET(Var), State0) ->
#mqtt_packet_connect{proto_ver = ProtoVer, #mqtt_packet_connect{proto_ver = ProtoVer,
proto_name = ProtoName, proto_name = ProtoName,
@ -154,8 +176,6 @@ process(Packet = ?CONNECT_PACKET(Var), State0) ->
will_msg = willmsg(Var), will_msg = willmsg(Var),
connected_at = os:timestamp()}, connected_at = os:timestamp()},
trace(recv, Packet, State1),
{ReturnCode1, SessPresent, State3} = {ReturnCode1, SessPresent, State3} =
case validate_connect(Var, State1) of case validate_connect(Var, State1) of
?CONNACK_ACCEPT -> ?CONNACK_ACCEPT ->
@ -171,6 +191,8 @@ process(Packet = ?CONNECT_PACKET(Var), State0) ->
emqttd_cm:reg(client(State2)), emqttd_cm:reg(client(State2)),
%% Start keepalive %% Start keepalive
start_keepalive(KeepAlive), start_keepalive(KeepAlive),
%% Emit Stats
self() ! emit_stats,
%% ACCEPT %% ACCEPT
{?CONNACK_ACCEPT, SP, State2#proto_state{session = Session, is_superuser = IsSuperuser}}; {?CONNACK_ACCEPT, SP, State2#proto_state{session = Session, is_superuser = IsSuperuser}};
{error, Error} -> {error, Error} ->
@ -184,7 +206,7 @@ process(Packet = ?CONNECT_PACKET(Var), State0) ->
{ReturnCode, false, State1} {ReturnCode, false, State1}
end, end,
%% Run hooks %% Run hooks
emqttd:run_hooks('client.connected', [ReturnCode1], client(State3)), emqttd_hooks:run('client.connected', [ReturnCode1], client(State3)),
%% Send connack %% Send connack
send(?CONNACK_PACKET(ReturnCode1, sp(SessPresent)), State3), send(?CONNACK_PACKET(ReturnCode1, sp(SessPresent)), State3),
%% stop if authentication failure %% stop if authentication failure
@ -217,8 +239,11 @@ process(?SUBSCRIBE_PACKET(PacketId, []), State) ->
send(?SUBACK_PACKET(PacketId, []), State); send(?SUBACK_PACKET(PacketId, []), State);
%% TODO: refactor later... %% TODO: refactor later...
process(?SUBSCRIBE_PACKET(PacketId, RawTopicTable), State = #proto_state{session = Session, process(?SUBSCRIBE_PACKET(PacketId, RawTopicTable),
client_id = ClientId, username = Username, is_superuser = IsSuperuser}) -> State = #proto_state{session = Session,
client_id = ClientId,
username = Username,
is_superuser = IsSuperuser}) ->
Client = client(State), TopicTable = parse_topic_table(RawTopicTable), Client = client(State), TopicTable = parse_topic_table(RawTopicTable),
AllowDenies = if AllowDenies = if
IsSuperuser -> []; IsSuperuser -> [];
@ -229,7 +254,7 @@ process(?SUBSCRIBE_PACKET(PacketId, RawTopicTable), State = #proto_state{session
?LOG(error, "Cannot SUBSCRIBE ~p for ACL Deny", [TopicTable], State), ?LOG(error, "Cannot SUBSCRIBE ~p for ACL Deny", [TopicTable], State),
send(?SUBACK_PACKET(PacketId, [16#80 || _ <- TopicTable]), State); send(?SUBACK_PACKET(PacketId, [16#80 || _ <- TopicTable]), State);
false -> false ->
case emqttd:run_hooks('client.subscribe', [ClientId, Username], TopicTable) of case emqttd_hooks:run('client.subscribe', [ClientId, Username], TopicTable) of
{ok, TopicTable1} -> {ok, TopicTable1} ->
emqttd_session:subscribe(Session, PacketId, TopicTable1), {ok, State}; emqttd_session:subscribe(Session, PacketId, TopicTable1), {ok, State};
{stop, _} -> {stop, _} ->
@ -241,10 +266,16 @@ process(?SUBSCRIBE_PACKET(PacketId, RawTopicTable), State = #proto_state{session
process(?UNSUBSCRIBE_PACKET(PacketId, []), State) -> process(?UNSUBSCRIBE_PACKET(PacketId, []), State) ->
send(?UNSUBACK_PACKET(PacketId), State); send(?UNSUBACK_PACKET(PacketId), State);
process(?UNSUBSCRIBE_PACKET(PacketId, RawTopics), State = #proto_state{ process(?UNSUBSCRIBE_PACKET(PacketId, RawTopics),
client_id = ClientId, username = Username, session = Session}) -> State = #proto_state{client_id = ClientId,
{ok, TopicTable} = emqttd:run_hooks('client.unsubscribe', [ClientId, Username], parse_topics(RawTopics)), username = Username,
emqttd_session:unsubscribe(Session, TopicTable), session = Session}) ->
case emqttd_hooks:run('client.unsubscribe', [ClientId, Username], parse_topics(RawTopics)) of
{ok, TopicTable} ->
emqttd_session:unsubscribe(Session, TopicTable);
{stop, _} ->
ok
end,
send(?UNSUBACK_PACKET(PacketId), State); send(?UNSUBACK_PACKET(PacketId), State);
process(?PACKET(?PINGREQ), State) -> process(?PACKET(?PINGREQ), State) ->
@ -255,7 +286,9 @@ process(?PACKET(?DISCONNECT), State) ->
{stop, normal, State#proto_state{will_msg = undefined}}. {stop, normal, State#proto_state{will_msg = undefined}}.
publish(Packet = ?PUBLISH_PACKET(?QOS_0, _PacketId), publish(Packet = ?PUBLISH_PACKET(?QOS_0, _PacketId),
#proto_state{client_id = ClientId, username = Username, session = Session}) -> #proto_state{client_id = ClientId,
username = Username,
session = Session}) ->
Msg = emqttd_message:from_packet(Username, ClientId, Packet), Msg = emqttd_message:from_packet(Username, ClientId, Packet),
emqttd_session:publish(Session, Msg); emqttd_session:publish(Session, Msg);
@ -280,15 +313,16 @@ with_puback(Type, Packet = ?PUBLISH_PACKET(_Qos, PacketId),
-spec(send(mqtt_message() | mqtt_packet(), proto_state()) -> {ok, proto_state()}). -spec(send(mqtt_message() | mqtt_packet(), proto_state()) -> {ok, proto_state()}).
send(Msg, State = #proto_state{client_id = ClientId, username = Username}) send(Msg, State = #proto_state{client_id = ClientId, username = Username})
when is_record(Msg, mqtt_message) -> when is_record(Msg, mqtt_message) ->
emqttd:run_hooks('message.delivered', [ClientId, Username], Msg), emqttd_hooks:run('message.delivered', [ClientId, Username], Msg),
send(emqttd_message:to_packet(Msg), State); send(emqttd_message:to_packet(Msg), State);
send(Packet, State = #proto_state{sendfun = SendFun}) send(Packet = ?PACKET(Type),
when is_record(Packet, mqtt_packet) -> State = #proto_state{sendfun = SendFun, stats_data = Stats}) ->
trace(send, Packet, State), trace(send, Packet, State),
emqttd_metrics:sent(Packet), emqttd_metrics:sent(Packet),
SendFun(Packet), SendFun(Packet),
{ok, State}. Stats1 = inc_stats(send, Type, Stats),
{ok, State#proto_state{stats_data = Stats1}}.
trace(recv, Packet, ProtoState) -> trace(recv, Packet, ProtoState) ->
?LOG(info, "RECV ~s", [emqttd_packet:format(Packet)], ProtoState); ?LOG(info, "RECV ~s", [emqttd_packet:format(Packet)], ProtoState);
@ -296,9 +330,23 @@ trace(recv, Packet, ProtoState) ->
trace(send, Packet, ProtoState) -> trace(send, Packet, ProtoState) ->
?LOG(info, "SEND ~s", [emqttd_packet:format(Packet)], ProtoState). ?LOG(info, "SEND ~s", [emqttd_packet:format(Packet)], ProtoState).
%% @doc redeliver PUBREL PacketId inc_stats(_Direct, _Type, Stats = #proto_stats{enable_stats = false}) ->
redeliver({?PUBREL, PacketId}, State) -> Stats;
send(?PUBREL_PACKET(PacketId), State).
inc_stats(recv, Type, Stats) ->
#proto_stats{recv_pkt = PktCnt, recv_msg = MsgCnt} = Stats,
inc_stats(Type, #proto_stats.recv_pkt, PktCnt, #proto_stats.recv_msg, MsgCnt, Stats);
inc_stats(send, Type, Stats) ->
#proto_stats{send_pkt = PktCnt, send_msg = MsgCnt} = Stats,
inc_stats(Type, #proto_stats.send_pkt, PktCnt, #proto_stats.send_msg, MsgCnt, Stats).
inc_stats(Type, PktPos, PktCnt, MsgPos, MsgCnt, Stats) ->
Stats1 = setelement(PktPos, Stats, PktCnt + 1),
case Type =:= ?PUBLISH of
true -> setelement(MsgPos, Stats1, MsgCnt + 1);
false -> Stats1
end.
stop_if_auth_failure(RC, State) when RC == ?CONNACK_CREDENTIALS; RC == ?CONNACK_AUTH -> stop_if_auth_failure(RC, State) when RC == ?CONNACK_CREDENTIALS; RC == ?CONNACK_AUTH ->
{stop, {shutdown, auth_failure}, State}; {stop, {shutdown, auth_failure}, State};
@ -318,7 +366,7 @@ shutdown(Error, State = #proto_state{will_msg = WillMsg}) ->
?LOG(info, "Shutdown for ~p", [Error], State), ?LOG(info, "Shutdown for ~p", [Error], State),
Client = client(State), Client = client(State),
send_willmsg(Client, WillMsg), send_willmsg(Client, WillMsg),
emqttd:run_hooks('client.disconnected', [Error], Client), emqttd_hooks:run('client.disconnected', [Error], Client),
%% let it down %% let it down
%% emqttd_cm:unreg(ClientId). %% emqttd_cm:unreg(ClientId).
ok. ok.
@ -368,19 +416,19 @@ validate_protocol(#mqtt_packet_connect{proto_ver = Ver, proto_name = Name}) ->
validate_clientid(#mqtt_packet_connect{client_id = ClientId}, validate_clientid(#mqtt_packet_connect{client_id = ClientId},
#proto_state{max_clientid_len = MaxLen}) #proto_state{max_clientid_len = MaxLen})
when (size(ClientId) >= 1) andalso (size(ClientId) =< MaxLen) -> when (byte_size(ClientId) >= 1) andalso (byte_size(ClientId) =< MaxLen) ->
true; true;
%% Issue#599: Null clientId and clean_sess = false %% Issue#599: Null clientId and clean_sess = false
validate_clientid(#mqtt_packet_connect{client_id = ClientId, validate_clientid(#mqtt_packet_connect{client_id = ClientId,
clean_sess = CleanSess}, _ProtoState) clean_sess = CleanSess}, _ProtoState)
when size(ClientId) == 0 andalso (not CleanSess) -> when byte_size(ClientId) == 0 andalso (not CleanSess) ->
false; false;
%% MQTT3.1.1 allow null clientId. %% MQTT3.1.1 allow null clientId.
validate_clientid(#mqtt_packet_connect{proto_ver =?MQTT_PROTO_V311, validate_clientid(#mqtt_packet_connect{proto_ver =?MQTT_PROTO_V4,
client_id = ClientId}, _ProtoState) client_id = ClientId}, _ProtoState)
when size(ClientId) =:= 0 -> when byte_size(ClientId) =:= 0 ->
true; true;
validate_clientid(#mqtt_packet_connect{proto_ver = ProtoVer, validate_clientid(#mqtt_packet_connect{proto_ver = ProtoVer,

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -18,11 +18,12 @@
-behaviour(gen_server2). -behaviour(gen_server2).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").
%% Start API.
-export([start_link/3]). -export([start_link/3]).
%% PubSub API. %% PubSub API.
@ -31,7 +32,7 @@
-export([dispatch/2]). -export([dispatch/2]).
%% gen_server. %% gen_server Callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
@ -45,7 +46,6 @@
%% Start PubSub %% Start PubSub
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Start one Pubsub
-spec(start_link(atom(), pos_integer(), list()) -> {ok, pid()} | ignore | {error, any()}). -spec(start_link(atom(), pos_integer(), list()) -> {ok, pid()} | ignore | {error, any()}).
start_link(Pool, Id, Env) -> start_link(Pool, Id, Env) ->
gen_server2:start_link({local, ?PROC_NAME(?MODULE, Id)}, ?MODULE, [Pool, Id, Env], []). gen_server2:start_link({local, ?PROC_NAME(?MODULE, Id)}, ?MODULE, [Pool, Id, Env], []).
@ -115,6 +115,7 @@ dispatch({_Share, [Sub]}, Topic, Msg) ->
dispatch(Sub, Topic, Msg); dispatch(Sub, Topic, Msg);
dispatch({_Share, []}, _Topic, _Msg) -> dispatch({_Share, []}, _Topic, _Msg) ->
ok; ok;
%%TODO: round-robbin
dispatch({_Share, Subs}, Topic, Msg) -> dispatch({_Share, Subs}, Topic, Msg) ->
dispatch(lists:nth(rand:uniform(length(Subs)), Subs), Topic, Msg). dispatch(lists:nth(rand:uniform(length(Subs)), Subs), Topic, Msg).
@ -163,11 +164,12 @@ pick(Subscriber) ->
init([Pool, Id, Env]) -> init([Pool, Id, Env]) ->
?GPROC_POOL(join, Pool, Id), ?GPROC_POOL(join, Pool, Id),
{ok, #state{pool = Pool, id = Id, env = Env}}. {ok, #state{pool = Pool, id = Id, env = Env},
hibernate, {backoff, 2000, 2000, 20000}}.
handle_call({subscribe, Topic, Subscriber, Options}, _From, State) -> handle_call({subscribe, Topic, Subscriber, Options}, _From, State) ->
add_subscriber(Topic, Subscriber, Options), add_subscriber(Topic, Subscriber, Options),
{reply, ok, setstats(State)}; {reply, ok, setstats(State), hibernate};
handle_call({unsubscribe, Topic, Subscriber, Options}, _From, State) -> handle_call({unsubscribe, Topic, Subscriber, Options}, _From, State) ->
del_subscriber(Topic, Subscriber, Options), del_subscriber(Topic, Subscriber, Options),
@ -178,7 +180,7 @@ handle_call(Req, _From, State) ->
handle_cast({subscribe, Topic, Subscriber, Options}, State) -> handle_cast({subscribe, Topic, Subscriber, Options}, State) ->
add_subscriber(Topic, Subscriber, Options), add_subscriber(Topic, Subscriber, Options),
{noreply, setstats(State)}; {noreply, setstats(State), hibernate};
handle_cast({unsubscribe, Topic, Subscriber, Options}, State) -> handle_cast({unsubscribe, Topic, Subscriber, Options}, State) ->
del_subscriber(Topic, Subscriber, Options), del_subscriber(Topic, Subscriber, Options),

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
%% @doc PubSub Supervisor. %% @doc PubSub Supervisor.
-module(emqttd_pubsub_sup). -module(emqttd_pubsub_sup).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(supervisor). -behaviour(supervisor).
%% API %% API

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
%% @doc MQTT Packet Serializer %% @doc MQTT Packet Serializer
-module(emqttd_serializer). -module(emqttd_serializer).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -25,8 +27,8 @@
-export([serialize/1]). -export([serialize/1]).
%% @doc Serialise MQTT Packet %% @doc Serialise MQTT Packet
-spec(serialize(mqtt_packet()) -> binary()). -spec(serialize(mqtt_packet()) -> iolist()).
serialize(#mqtt_packet{header = Header = #mqtt_packet_header{type = Type}, serialize(#mqtt_packet{header = Header = #mqtt_packet_header{type = Type},
variable = Variable, variable = Variable,
payload = Payload}) -> payload = Payload}) ->
serialize_header(Header, serialize_header(Header,
@ -37,14 +39,12 @@ serialize_header(#mqtt_packet_header{type = Type,
dup = Dup, dup = Dup,
qos = Qos, qos = Qos,
retain = Retain}, retain = Retain},
{VariableBin, PayloadBin}) when ?CONNECT =< Type andalso Type =< ?DISCONNECT -> {VariableBin, PayloadBin})
Len = size(VariableBin) + size(PayloadBin), when ?CONNECT =< Type andalso Type =< ?DISCONNECT ->
true = (Len =< ?MAX_LEN), Len = byte_size(VariableBin) + byte_size(PayloadBin),
LenBin = serialize_len(Len), true = (Len =< ?MAX_PACKET_SIZE),
<<Type:4, (opt(Dup)):1, (opt(Qos)):2, (opt(Retain)):1, [<<Type:4, (opt(Dup)):1, (opt(Qos)):2, (opt(Retain)):1>>,
LenBin/binary, serialize_len(Len), VariableBin, PayloadBin].
VariableBin/binary,
PayloadBin/binary>>.
serialize_variable(?CONNECT, #mqtt_packet_connect{client_id = ClientId, serialize_variable(?CONNECT, #mqtt_packet_connect{client_id = ClientId,
proto_ver = ProtoVer, proto_ver = ProtoVer,
@ -58,7 +58,7 @@ serialize_variable(?CONNECT, #mqtt_packet_connect{client_id = ClientId,
will_msg = WillMsg, will_msg = WillMsg,
username = Username, username = Username,
password = Password}, undefined) -> password = Password}, undefined) ->
VariableBin = <<(size(ProtoName)):16/big-unsigned-integer, VariableBin = <<(byte_size(ProtoName)):16/big-unsigned-integer,
ProtoName/binary, ProtoName/binary,
ProtoVer:8, ProtoVer:8,
(opt(Username)):1, (opt(Username)):1,
@ -73,7 +73,7 @@ serialize_variable(?CONNECT, #mqtt_packet_connect{client_id = ClientId,
PayloadBin1 = case WillFlag of PayloadBin1 = case WillFlag of
true -> <<PayloadBin/binary, true -> <<PayloadBin/binary,
(serialize_utf(WillTopic))/binary, (serialize_utf(WillTopic))/binary,
(size(WillMsg)):16/big-unsigned-integer, (byte_size(WillMsg)):16/big-unsigned-integer,
WillMsg/binary>>; WillMsg/binary>>;
false -> PayloadBin false -> PayloadBin
end, end,
@ -93,7 +93,7 @@ serialize_variable(?SUBACK, #mqtt_packet_suback{packet_id = PacketId,
{<<PacketId:16/big>>, << <<Q:8>> || Q <- QosTable >>}; {<<PacketId:16/big>>, << <<Q:8>> || Q <- QosTable >>};
serialize_variable(?UNSUBSCRIBE, #mqtt_packet_unsubscribe{packet_id = PacketId, serialize_variable(?UNSUBSCRIBE, #mqtt_packet_unsubscribe{packet_id = PacketId,
topics = Topics }, undefined) -> topics = Topics }, undefined) ->
{<<PacketId:16/big>>, serialize_topics(Topics)}; {<<PacketId:16/big>>, serialize_topics(Topics)};
serialize_variable(?UNSUBACK, #mqtt_packet_unsuback{packet_id = PacketId}, undefined) -> serialize_variable(?UNSUBACK, #mqtt_packet_unsuback{packet_id = PacketId}, undefined) ->
@ -134,7 +134,7 @@ serialize_topics([H|_] = Topics) when is_binary(H) ->
serialize_utf(String) -> serialize_utf(String) ->
StringBin = unicode:characters_to_binary(String), StringBin = unicode:characters_to_binary(String),
Len = size(StringBin), Len = byte_size(StringBin),
true = (Len =< 16#ffff), true = (Len =< 16#ffff),
<<Len:16/big, StringBin/binary>>. <<Len:16/big, StringBin/binary>>.
@ -148,4 +148,3 @@ opt(false) -> 0;
opt(true) -> 1; opt(true) -> 1;
opt(X) when is_integer(X) -> X; opt(X) when is_integer(X) -> X;
opt(B) when is_binary(B) -> 1. opt(B) when is_binary(B) -> 1.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,10 +16,10 @@
-module(emqttd_server). -module(emqttd_server).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(gen_server2). -behaviour(gen_server2).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
@ -43,7 +43,7 @@
%% Debug API %% Debug API
-export([dump/0]). -export([dump/0]).
%% gen_server. %% gen_server Callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
@ -89,7 +89,7 @@ async_subscribe(Topic, Subscriber, Options) when is_binary(Topic) ->
-spec(publish(mqtt_message()) -> {ok, mqtt_delivery()} | ignore). -spec(publish(mqtt_message()) -> {ok, mqtt_delivery()} | ignore).
publish(Msg = #mqtt_message{from = From}) -> publish(Msg = #mqtt_message{from = From}) ->
trace(publish, From, Msg), trace(publish, From, Msg),
case emqttd_hook:run('message.publish', [], Msg) of case emqttd_hooks:run('message.publish', [], Msg) of
{ok, Msg1 = #mqtt_message{topic = Topic}} -> {ok, Msg1 = #mqtt_message{topic = Topic}} ->
emqttd_pubsub:publish(Topic, Msg1); emqttd_pubsub:publish(Topic, Msg1);
{stop, Msg1} -> {stop, Msg1} ->
@ -97,14 +97,13 @@ publish(Msg = #mqtt_message{from = From}) ->
ignore ignore
end. end.
%% @private
trace(publish, From, _Msg) when is_atom(From) -> trace(publish, From, _Msg) when is_atom(From) ->
%% Dont' trace '$SYS' publish %% Dont' trace '$SYS' publish
ignore; ignore;
trace(publish, {ClientId, Username}, #mqtt_message{topic = Topic, payload = Payload}) -> trace(publish, {ClientId, Username}, #mqtt_message{topic = Topic, payload = Payload}) ->
lager:info([{client, ClientId}, {topic, Topic}], lager:info([{client, ClientId}, {topic, Topic}],
"~s/~s PUBLISH to ~s: ~p", [ClientId, Username, Topic, Payload]); "~s/~s PUBLISH to ~s: ~p", [ClientId, Username, Topic, Payload]);
trace(publish, From, #mqtt_message{topic = Topic, payload = Payload}) when is_binary(From); is_list(From) -> trace(publish, From, #mqtt_message{topic = Topic, payload = Payload}) when is_binary(From); is_list(From) ->
lager:info([{client, From}, {topic, Topic}], lager:info([{client, From}, {topic, Topic}],
"~s PUBLISH to ~s: ~p", [From, Topic, Payload]). "~s PUBLISH to ~s: ~p", [From, Topic, Payload]).
@ -142,7 +141,8 @@ subscriptions(Subscriber) ->
subscription(Topic, Subscriber) -> subscription(Topic, Subscriber) ->
{Topic, Subscriber, ets:lookup_element(mqtt_subproperty, {Topic, Subscriber}, 2)}. {Topic, Subscriber, ets:lookup_element(mqtt_subproperty, {Topic, Subscriber}, 2)}.
subscribers(Topic) -> emqttd_pubsub:subscribers(Topic). subscribers(Topic) ->
emqttd_pubsub:subscribers(Topic).
-spec(is_subscribed(binary(), emqttd:subscriber()) -> boolean()). -spec(is_subscribed(binary(), emqttd:subscriber()) -> boolean()).
is_subscribed(Topic, Subscriber) when is_binary(Topic) -> is_subscribed(Topic, Subscriber) when is_binary(Topic) ->

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,9 +14,11 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd session supervisor. %% @doc Session Supervisor.
-module(emqttd_session_sup). -module(emqttd_session_sup).
-author("Feng Lee <feng@emqtt.io>").
-behavior(supervisor). -behavior(supervisor).
-export([start_link/0, start_session/3]). -export([start_link/0, start_session/3]).
@ -41,4 +43,3 @@ init([]) ->
{ok, {{simple_one_for_one, 0, 1}, {ok, {{simple_one_for_one, 0, 1},
[{session, {emqttd_session, start_link, []}, [{session, {emqttd_session, start_link, []},
temporary, 5000, worker, [emqttd_session]}]}}. temporary, 5000, worker, [emqttd_session]}]}}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,9 +14,10 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Session Manager
-module(emqttd_sm). -module(emqttd_sm).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(gen_server2). -behaviour(gen_server2).
-include("emqttd.hrl"). -include("emqttd.hrl").
@ -32,10 +33,12 @@
%% API Function Exports %% API Function Exports
-export([start_link/2]). -export([start_link/2]).
-export([start_session/2, lookup_session/1, reg_session/3, unreg_session/1]). -export([start_session/2, lookup_session/1, register_session/3, unregister_session/1]).
-export([dispatch/3]). -export([dispatch/3]).
-export([local_sessions/0]).
%% gen_server Function Exports %% gen_server Function Exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
@ -91,27 +94,31 @@ lookup_session(ClientId) ->
end. end.
%% @doc Register a session with info. %% @doc Register a session with info.
-spec(reg_session(binary(), boolean(), [tuple()]) -> true). -spec(register_session(binary(), boolean(), [tuple()]) -> true).
reg_session(ClientId, CleanSess, Properties) -> register_session(ClientId, CleanSess, Properties) ->
ets:insert(mqtt_local_session, {ClientId, self(), CleanSess, Properties}). ets:insert(mqtt_local_session, {ClientId, self(), CleanSess, Properties}).
%% @doc Unregister a session. %% @doc Unregister a session.
-spec(unreg_session(binary()) -> true). -spec(unregister_session(binary()) -> true).
unreg_session(ClientId) -> unregister_session(ClientId) ->
ets:delete(mqtt_local_session, ClientId). ets:delete(mqtt_local_session, ClientId).
dispatch(ClientId, Topic, Msg) -> dispatch(ClientId, Topic, Msg) ->
try ets:lookup_element(mqtt_local_session, ClientId, 2) of try ets:lookup_element(mqtt_local_session, ClientId, 2) of
Pid -> Pid ! {dispatch, Topic, Msg} Pid -> Pid ! {dispatch, Topic, Msg}
catch catch
error:badarg -> io:format("Session Not Found: ~p~n", [ClientId]), ok %%TODO: How?? error:badarg -> ok %%FIXME Later.
end. end.
call(SM, Req) -> call(SM, Req) ->
gen_server2:call(SM, Req, ?TIMEOUT). %%infinity). gen_server2:call(SM, Req, ?TIMEOUT). %%infinity).
%% @doc for debug.
local_sessions() ->
ets:tab2list(mqtt_local_session).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_server callbacks %% gen_server Callbacks
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([Pool, Id]) -> init([Pool, Id]) ->
@ -171,6 +178,7 @@ handle_info({'DOWN', MRef, process, DownPid, _Reason}, State) ->
[] -> [] ->
ok; ok;
[Sess = #mqtt_session{sess_pid = DownPid}] -> [Sess = #mqtt_session{sess_pid = DownPid}] ->
emqttd_stats:del_session_stats(ClientId),
mnesia:delete_object(mqtt_session, Sess, write); mnesia:delete_object(mqtt_session, Sess, write);
[_Sess] -> [_Sess] ->
ok ok
@ -208,7 +216,7 @@ create_session({CleanSess, {ClientId, Username}, ClientPid}, State) ->
create_session(CleanSess, {ClientId, Username}, ClientPid) -> create_session(CleanSess, {ClientId, Username}, ClientPid) ->
case emqttd_session_sup:start_session(CleanSess, {ClientId, Username}, ClientPid) of case emqttd_session_sup:start_session(CleanSess, {ClientId, Username}, ClientPid) of
{ok, SessPid} -> {ok, SessPid} ->
Session = #mqtt_session{client_id = ClientId, sess_pid = SessPid, persistent = not CleanSess}, Session = #mqtt_session{client_id = ClientId, sess_pid = SessPid, clean_sess = CleanSess},
case insert_session(Session) of case insert_session(Session) of
{aborted, {conflict, ConflictPid}} -> {aborted, {conflict, ConflictPid}} ->
%% Conflict with othe node? %% Conflict with othe node?

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
%% @doc Session Helper. %% @doc Session Helper.
-module(emqttd_sm_helper). -module(emqttd_sm_helper).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(gen_server). -behaviour(gen_server).
-include("emqttd.hrl"). -include("emqttd.hrl").

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -15,10 +15,13 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Session Manager Supervisor. %% @doc Session Manager Supervisor.
-module(emqttd_sm_sup). -module(emqttd_sm_sup).
-behaviour(supervisor). -behaviour(supervisor).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-define(SM, emqttd_sm). -define(SM, emqttd_sm).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,34 +14,39 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd statistics
-module(emqttd_stats). -module(emqttd_stats).
-include("emqttd.hrl").
-behaviour(gen_server). -behaviour(gen_server).
-define(SERVER, ?MODULE). -author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl").
-export([start_link/0, stop/0]). -export([start_link/0, stop/0]).
%% statistics API. %% Client and Session Stats
-export([statsfun/1, statsfun/2, -export([set_client_stats/2, get_client_stats/1, del_client_stats/1,
getstats/0, getstat/1, set_session_stats/2, get_session_stats/1, del_session_stats/1]).
setstat/2, setstats/3]).
%% Statistics API.
-export([statsfun/1, statsfun/2, getstats/0, getstat/1, setstat/2, setstats/3]).
%% gen_server Function Exports %% gen_server Function Exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
-record(state, {tick_tref}). -record(state, {tick}).
-type(stats() :: list({atom(), non_neg_integer()})).
-define(STATS_TAB, mqtt_stats). -define(STATS_TAB, mqtt_stats).
-define(CLIENT_STATS_TAB, mqtt_client_stats).
-define(SESSION_STATS_TAB, mqtt_session_stats).
%% $SYS Topics for Clients %% $SYS Topics for Clients
-define(SYSTOP_CLIENTS, [ -define(SYSTOP_CLIENTS, [
'clients/count', % clients connected current 'clients/count', % clients connected current
'clients/max' % max clients connected 'clients/max' % max clients connected
]). ]).
%% $SYS Topics for Sessions %% $SYS Topics for Sessions
@ -75,10 +80,40 @@
%% @doc Start stats server %% @doc Start stats server
-spec(start_link() -> {ok, pid()} | ignore | {error, term()}). -spec(start_link() -> {ok, pid()} | ignore | {error, term()}).
start_link() -> start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() -> stop() ->
gen_server:call(?SERVER, stop). gen_server:call(?MODULE, stop).
-spec(set_client_stats(binary(), stats()) -> true).
set_client_stats(ClientId, Stats) ->
ets:insert(?CLIENT_STATS_TAB, {ClientId, [{'$ts', emqttd_time:now_secs()}|Stats]}).
-spec(get_client_stats(binary()) -> stats()).
get_client_stats(ClientId) ->
case ets:lookup(?CLIENT_STATS_TAB, ClientId) of
[{_, Stats}] -> Stats;
[] -> []
end.
-spec(del_client_stats(binary()) -> true).
del_client_stats(ClientId) ->
ets:delete(?CLIENT_STATS_TAB, ClientId).
-spec(set_session_stats(binary(), stats()) -> true).
set_session_stats(ClientId, Stats) ->
ets:insert(?SESSION_STATS_TAB, {ClientId, [{'$ts', emqttd_time:now_secs()}|Stats]}).
-spec(get_session_stats(binary()) -> stats()).
get_session_stats(ClientId) ->
case ets:lookup(?SESSION_STATS_TAB, ClientId) of
[{_, Stats}] -> Stats;
[] -> []
end.
-spec(del_session_stats(binary()) -> true).
del_session_stats(ClientId) ->
ets:delete(?SESSION_STATS_TAB, ClientId).
%% @doc Generate stats fun %% @doc Generate stats fun
-spec(statsfun(Stat :: atom()) -> fun()). -spec(statsfun(Stat :: atom()) -> fun()).
@ -118,13 +153,14 @@ setstats(Stat, MaxStat, Val) ->
init([]) -> init([]) ->
emqttd_time:seed(), emqttd_time:seed(),
ets:new(?STATS_TAB, [set, public, named_table, {write_concurrency, true}]), lists:foreach(
fun(Tab) ->
Tab = ets:new(Tab, [set, public, named_table, {write_concurrency, true}])
end, [?STATS_TAB, ?CLIENT_STATS_TAB, ?SESSION_STATS_TAB]),
Topics = ?SYSTOP_CLIENTS ++ ?SYSTOP_SESSIONS ++ ?SYSTOP_PUBSUB ++ ?SYSTOP_RETAINED, Topics = ?SYSTOP_CLIENTS ++ ?SYSTOP_SESSIONS ++ ?SYSTOP_PUBSUB ++ ?SYSTOP_RETAINED,
ets:insert(?STATS_TAB, [{Topic, 0} || Topic <- Topics]), ets:insert(?STATS_TAB, [{Topic, 0} || Topic <- Topics]),
% Create $SYS Topics
% [ok = emqttd:create(topic, stats_topic(Topic)) || Topic <- Topics],
% Tick to publish stats % Tick to publish stats
{ok, #state{tick_tref = emqttd_broker:start_tick(tick)}, hibernate}. {ok, #state{tick = emqttd_broker:start_tick(tick)}, hibernate}.
handle_call(stop, _From, State) -> handle_call(stop, _From, State) ->
{stop, normal, ok, State}; {stop, normal, ok, State};
@ -154,7 +190,7 @@ handle_info(tick, State) ->
handle_info(_Info, State) -> handle_info(_Info, State) ->
{noreply, State}. {noreply, State}.
terminate(_Reason, #state{tick_tref = TRef}) -> terminate(_Reason, #state{tick = TRef}) ->
emqttd_broker:stop_tick(TRef). emqttd_broker:stop_tick(TRef).
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,11 +14,12 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc emqttd top supervisor.
-module(emqttd_sup). -module(emqttd_sup).
-behaviour(supervisor). -behaviour(supervisor).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
%% API %% API
@ -50,5 +51,5 @@ start_child(Mod, Type) when is_atom(Mod) and is_atom(Type) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
{ok, {{one_for_all, 10, 3600}, []}}. {ok, {{one_for_all, 0, 1}, []}}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
%% @doc VM System Monitor %% @doc VM System Monitor
-module(emqttd_sysmon). -module(emqttd_sysmon).
-author("Feng Lee <feng@emqtt.io>").
-behavior(gen_server). -behavior(gen_server).
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,21 +16,25 @@
-module(emqttd_time). -module(emqttd_time).
-export([seed/0, now_to_secs/0, now_to_secs/1, now_to_ms/0, now_to_ms/1]). -author("Feng Lee <feng@emqtt.io>").
-export([seed/0, now_secs/0, now_secs/1, now_ms/0, now_ms/1, ts_from_ms/1]).
seed() -> seed() ->
case erlang:function_exported(erlang, timestamp, 0) of rand:seed(exsplus, erlang:timestamp()).
true -> random:seed(erlang:timestamp()); %% R18
false -> random:seed(os:timestamp()) %% Compress now() deprecated warning...
end.
now_to_secs() -> now_to_secs(os:timestamp()). now_ms() ->
now_ms(os:timestamp()).
now_to_secs({MegaSecs, Secs, _MicroSecs}) -> now_ms({MegaSecs, Secs, MicroSecs}) ->
MegaSecs * 1000000 + Secs.
now_to_ms() -> now_to_ms(os:timestamp()).
now_to_ms({MegaSecs, Secs, MicroSecs}) ->
(MegaSecs * 1000000 + Secs) * 1000 + round(MicroSecs/1000). (MegaSecs * 1000000 + Secs) * 1000 + round(MicroSecs/1000).
now_secs() ->
now_secs(os:timestamp()).
now_secs({MegaSecs, Secs, _MicroSecs}) ->
MegaSecs * 1000000 + Secs.
ts_from_ms(Ms) ->
{Ms div 1000000, Ms rem 1000000, 0}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,8 +16,12 @@
-module(emqttd_topic). -module(emqttd_topic).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
-include("emqttd_internal.hrl").
-import(lists, [reverse/1]). -import(lists, [reverse/1]).
-export([match/2, validate/1, triples/1, words/1, wildcard/1]). -export([match/2, validate/1, triples/1, words/1, wildcard/1]).
@ -55,8 +59,8 @@ wildcard([_H|T]) ->
%% @doc Match Topic name with filter %% @doc Match Topic name with filter
-spec(match(Name, Filter) -> boolean() when -spec(match(Name, Filter) -> boolean() when
Name :: topic() | words(), Name :: topic() | words(),
Filter :: topic() | words()). Filter :: topic() | words()).
match(Name, Filter) when is_binary(Name) and is_binary(Filter) -> match(Name, Filter) when is_binary(Name) and is_binary(Filter) ->
match(words(Name), words(Filter)); match(words(Name), words(Filter));
match([], []) -> match([], []) ->
@ -101,10 +105,7 @@ validate2([''|Words]) ->
validate2(['+'|Words]) -> validate2(['+'|Words]) ->
validate2(Words); validate2(Words);
validate2([W|Words]) -> validate2([W|Words]) ->
case validate3(W) of case validate3(W) of true -> validate2(Words); false -> false end.
true -> validate2(Words);
false -> false
end.
validate3(<<>>) -> validate3(<<>>) ->
true; true;
@ -180,24 +181,28 @@ join(Words) ->
parse(Topic) when is_binary(Topic) -> parse(Topic) when is_binary(Topic) ->
parse(Topic, []). parse(Topic, []).
parse(Topic = <<"$local/", Topic1/binary>>, Options) -> parse(<<"$local/", Topic1/binary>>, Options) ->
case lists:member(local, Options) of if_not_contain(local, Options, fun() ->
true -> error({invalid_topic, Topic}); parse(Topic1, [local | Options])
false -> parse(Topic1, [local | Options]) end);
end;
parse(Topic = <<"$queue/", Topic1/binary>>, Options) -> parse(<<"$queue/", Topic1/binary>>, Options) ->
case lists:keyfind(share, 1, Options) of if_not_contain(share, Options,fun() ->
{share, _} -> error({invalid_topic, Topic}); parse(Topic1, [{share, '$queue'} | Options])
false -> parse(Topic1, [{share, '$queue'} | Options]) end);
end;
parse(Topic = <<"$share/", Topic1/binary>>, Options) -> parse(<<"$share/", Topic1/binary>>, Options) ->
case lists:keyfind(share, 1, Options) of if_not_contain(share, Options, fun() ->
{share, _} -> error({invalid_topic, Topic}); [Share, Topic2] = binary:split(Topic1, <<"/">>),
false -> [Share, Topic2] = binary:split(Topic1, <<"/">>), {Topic2, [{share, Share} | Options]}
{Topic2, [{share, Share} | Options]} end);
end;
parse(Topic, Options) -> {Topic, Options}. parse(Topic, Options) ->
{Topic, Options}.
if_not_contain(local, Options, Fun) ->
?IF(lists:member(local, Options), error(invalid_topic), Fun());
if_not_contain(share, Options, Fun) ->
?IF(lists:keyfind(share, 1, Options), error(invalid_topic), Fun()).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,13 +14,13 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc %% @docTrace MQTT packets/messages by ClientID or Topic.
%% Trace MQTT packets/messages by ClientID or Topic.
%% @end
-module(emqttd_trace). -module(emqttd_trace).
-behaviour(gen_server). -behaviour(gen_server).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd_internal.hrl"). -include("emqttd_internal.hrl").
%% API Function Exports %% API Function Exports
@ -34,7 +34,7 @@
-record(state, {level, traces}). -record(state, {level, traces}).
-type trace_who() :: {client | topic, binary()}. -type(trace_who() :: {client | topic, binary()}).
-define(TRACE_OPTIONS, [{formatter_config, [time, " [",severity,"] ", message, "\n"]}]). -define(TRACE_OPTIONS, [{formatter_config, [time, " [",severity,"] ", message, "\n"]}]).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(emqttd_trace_sup). -module(emqttd_trace_sup).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(supervisor). -behaviour(supervisor).
%% API %% API
@ -29,6 +31,6 @@ start_link() ->
init([]) -> init([]) ->
Trace = {trace, {emqttd_trace, start_link, []}, Trace = {trace, {emqttd_trace, start_link, []},
permanent, 5000, worker, [emqttd_trace]}, permanent, 5000, worker, [emqttd_trace]},
{ok, {{one_for_one, 10, 100}, [Trace]}}. {ok, {{one_for_one, 10, 3600}, [Trace]}}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,8 +17,11 @@
%% @doc MQTT Topic Trie: %% @doc MQTT Topic Trie:
%% [Trie](http://en.wikipedia.org/wiki/Trie) %% [Trie](http://en.wikipedia.org/wiki/Trie)
%% @end %% @end
-module(emqttd_trie). -module(emqttd_trie).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd_trie.hrl"). -include("emqttd_trie.hrl").
%% Mnesia Callbacks %% Mnesia Callbacks

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,13 +16,20 @@
-module(emqttd_ws). -module(emqttd_ws).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd_protocol.hrl").
-import(proplists, [get_value/3]).
-export([handle_request/1, ws_loop/3]). -export([handle_request/1, ws_loop/3]).
%% WebSocket Loop State %% WebSocket Loop State
-record(wsocket_state, {peer, client_pid, packet_opts, parser_fun}). -record(wsocket_state, {peername, client_pid, max_packet_size, parser}).
-define(WSLOG(Level, Peer, Format, Args), -define(WSLOG(Level, Format, Args, State),
lager:Level("WsClient(~s): " ++ Format, [Peer | Args])). lager:Level("WsClient(~s): " ++ Format,
[esockd_net:format(State#wsocket_state.peername) | Args])).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Handle WebSocket Request %% Handle WebSocket Request
@ -30,18 +37,14 @@
%% @doc Handle WebSocket Request. %% @doc Handle WebSocket Request.
handle_request(Req) -> handle_request(Req) ->
Peer = Req:get(peer), {ok, ProtoEnv} = emqttd:env(protocol),
{ok, PktOpts} = emqttd:env(protocol), PacketSize = get_value(max_packet_size, ProtoEnv, ?MAX_PACKET_SIZE),
ParserFun = emqttd_parser:new(PktOpts), Parser = emqttd_parser:initial_state(PacketSize),
{ReentryWs, ReplyChannel} = upgrade(Req), %% Upgrade WebSocket.
{ReentryWs, ReplyChannel} = mochiweb_websocket:upgrade_connection(Req, fun ?MODULE:ws_loop/3),
{ok, ClientPid} = emqttd_ws_client_sup:start_client(self(), Req, ReplyChannel), {ok, ClientPid} = emqttd_ws_client_sup:start_client(self(), Req, ReplyChannel),
ReentryWs(#wsocket_state{peer = Peer, client_pid = ClientPid, ReentryWs(#wsocket_state{peername = Req:get(peername), parser = Parser,
packet_opts = PktOpts, parser_fun = ParserFun}). max_packet_size = PacketSize, client_pid = ClientPid}).
%% @doc Upgrade WebSocket.
%% @private
upgrade(Req) ->
mochiweb_websocket:upgrade_connection(Req, fun ?MODULE:ws_loop/3).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Receive Loop %% Receive Loop
@ -52,25 +55,24 @@ ws_loop(<<>>, State, _ReplyChannel) ->
State; State;
ws_loop([<<>>], State, _ReplyChannel) -> ws_loop([<<>>], State, _ReplyChannel) ->
State; State;
ws_loop(Data, State = #wsocket_state{peer = Peer, client_pid = ClientPid, ws_loop(Data, State = #wsocket_state{client_pid = ClientPid, parser = Parser}, ReplyChannel) ->
parser_fun = ParserFun}, ReplyChannel) -> ?WSLOG(debug, "RECV ~p", [Data], State),
?WSLOG(debug, Peer, "RECV ~p", [Data]),
emqttd_metrics:inc('bytes/received', iolist_size(Data)), emqttd_metrics:inc('bytes/received', iolist_size(Data)),
case catch ParserFun(iolist_to_binary(Data)) of case catch emqttd_parser:parse(iolist_to_binary(Data), Parser) of
{more, NewParser} -> {more, NewParser} ->
State#wsocket_state{parser_fun = NewParser}; State#wsocket_state{parser = NewParser};
{ok, Packet, Rest} -> {ok, Packet, Rest} ->
gen_server:cast(ClientPid, {received, Packet}), gen_server:cast(ClientPid, {received, Packet}),
ws_loop(Rest, reset_parser(State), ReplyChannel); ws_loop(Rest, reset_parser(State), ReplyChannel);
{error, Error} -> {error, Error} ->
?WSLOG(error, Peer, "Frame error: ~p", [Error]), ?WSLOG(error, "Frame error: ~p", [Error], State),
exit({shutdown, Error}); exit({shutdown, Error});
{'EXIT', Reason} -> {'EXIT', Reason} ->
?WSLOG(error, Peer, "Frame error: ~p", [Reason]), ?WSLOG(error, "Frame error: ~p", [Reason], State),
?WSLOG(error, Peer, "Error data: ~p", [Data]), ?WSLOG(error, "Error data: ~p", [Data], State),
exit({shutdown, parser_error}) exit({shutdown, parser_error})
end. end.
reset_parser(State = #wsocket_state{packet_opts = PktOpts}) -> reset_parser(State = #wsocket_state{max_packet_size = PacketSize}) ->
State#wsocket_state{parser_fun = emqttd_parser:new(PktOpts)}. State#wsocket_state{parser = emqttd_parser:initial_state(PacketSize)}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -14,106 +14,136 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc MQTT WebSocket Connection.
-module(emqttd_ws_client). -module(emqttd_ws_client).
-behaviour(gen_server). -behaviour(gen_server2).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd.hrl"). -include("emqttd.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
-include("emqttd_internal.hrl").
-import(proplists, [get_value/3]).
%% API Exports %% API Exports
-export([start_link/4, session/1, info/1, kick/1]). -export([start_link/4]).
%% Management and Monitor API
-export([info/1, stats/1, kick/1]).
%% SUB/UNSUB Asynchronously %% SUB/UNSUB Asynchronously
-export([subscribe/2, unsubscribe/2]). -export([subscribe/2, unsubscribe/2]).
%% Get the session proc?
-export([session/1]).
%% gen_server Function Exports %% gen_server Function Exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
%% WebSocket Client State %% gen_server2 Callbacks
-record(wsclient_state, {ws_pid, peer, connection, proto_state, keepalive}). -export([prioritise_call/4, prioritise_info/3, handle_pre_hibernate/1]).
-define(WSLOG(Level, Peer, Format, Args), %% WebSocket Client State
lager:Level("WsClient(~s): " ++ Format, [Peer | Args])). -record(wsclient_state, {ws_pid, peername, connection, proto_state, keepalive,
enable_stats, force_gc_count}).
-define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt, send_pend]).
-define(WSLOG(Level, Format, Args, State),
lager:Level("WsClient(~s): " ++ Format,
[esockd_net:format(State#wsclient_state.peername) | Args])).
%% @doc Start WebSocket Client. %% @doc Start WebSocket Client.
start_link(MqttEnv, WsPid, Req, ReplyChannel) -> start_link(Env, WsPid, Req, ReplyChannel) ->
gen_server:start_link(?MODULE, [MqttEnv, WsPid, Req, ReplyChannel], []). gen_server2:start_link(?MODULE, [Env, WsPid, Req, ReplyChannel],
[{spawn_opt, ?FULLSWEEP_OPTS}]). %% Tune GC.
session(CPid) ->
gen_server:call(CPid, session, infinity).
info(CPid) -> info(CPid) ->
gen_server:call(CPid, info, infinity). gen_server2:call(CPid, info).
stats(CPid) ->
gen_server2:call(CPid, stats).
kick(CPid) -> kick(CPid) ->
gen_server:call(CPid, kick). gen_server2:call(CPid, kick).
subscribe(CPid, TopicTable) -> subscribe(CPid, TopicTable) ->
gen_server:cast(CPid, {subscribe, TopicTable}). CPid ! {subscribe, TopicTable}.
unsubscribe(CPid, Topics) -> unsubscribe(CPid, Topics) ->
gen_server:cast(CPid, {unsubscribe, Topics}). CPid ! {unsubscribe, Topics}.
session(CPid) ->
gen_server2:call(CPid, session).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_server Callbacks %% gen_server Callbacks
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([MqttEnv, WsPid, Req, ReplyChannel]) -> init([Env, WsPid, Req, ReplyChannel]) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
true = link(WsPid), true = link(WsPid),
{ok, Peername} = Req:get(peername), {ok, Peername} = Req:get(peername),
Headers = mochiweb_headers:to_list( Headers = mochiweb_headers:to_list(
mochiweb_request:get(headers, Req)), mochiweb_request:get(headers, Req)),
%% SendFun = fun(Payload) -> ReplyChannel({binary, Payload}) end, Conn = Req:get(connection),
SendFun = fun(Packet) -> ProtoState = emqttd_protocol:init(Peername, send_fun(ReplyChannel),
Data = emqttd_serializer:serialize(Packet), [{ws_initial_headers, Headers} | Env]),
emqttd_metrics:inc('bytes/sent', iolist_size(Data)), IdleTimeout = get_value(client_idle_timeout, Env, 30000),
ReplyChannel({binary, Data}) EnableStats = get_value(client_enable_stats, Env, false),
end, ForceGcCount = emqttd_gc:conn_max_gc_count(),
ProtoState = emqttd_protocol:init(Peername, SendFun, {ok, #wsclient_state{connection = Conn,
[{ws_initial_headers, Headers} | MqttEnv]), ws_pid = WsPid,
{ok, #wsclient_state{ws_pid = WsPid, peer = Req:get(peer), peername = Peername,
connection = Req:get(connection), proto_state = ProtoState,
proto_state = ProtoState}, idle_timeout(MqttEnv)}. enable_stats = EnableStats,
force_gc_count = ForceGcCount},
IdleTimeout, {backoff, 2000, 2000, 20000}, ?MODULE}.
idle_timeout(MqttEnv) -> prioritise_call(Msg, _From, _Len, _State) ->
timer:seconds(proplists:get_value(client_idle_timeout, MqttEnv, 10)). case Msg of info -> 10; stats -> 10; state -> 10; _ -> 5 end.
handle_call(session, _From, State = #wsclient_state{proto_state = ProtoState}) -> prioritise_info(Msg, _Len, _State) ->
{reply, emqttd_protocol:session(ProtoState), State}; case Msg of {redeliver, _} -> 5; _ -> 0 end.
handle_call(info, _From, State = #wsclient_state{peer = Peer, handle_pre_hibernate(State = #wsclient_state{ws_pid = WsPid}) ->
proto_state = ProtoState}) -> erlang:garbage_collect(WsPid),
ProtoInfo = emqttd_protocol:info(ProtoState), {hibernate, emqttd_gc:reset_conn_gc_count(#wsclient_state.force_gc_count, emit_stats(State))}.
{reply, [{websocket, true}, {peer, Peer}| ProtoInfo], State};
handle_call(info, From, State = #wsclient_state{peername = Peername,
proto_state = ProtoState}) ->
Info = [{websocket, true}, {peername, Peername} | emqttd_protocol:info(ProtoState)],
{reply, Stats, _, _} = handle_call(stats, From, State),
reply(lists:append(Info, Stats), State);
handle_call(stats, _From, State = #wsclient_state{proto_state = ProtoState}) ->
reply(lists:append([emqttd_misc:proc_stats(),
wsock_stats(State),
emqttd_protocol:stats(ProtoState)]), State);
handle_call(kick, _From, State) -> handle_call(kick, _From, State) ->
{stop, {shutdown, kick}, ok, State}; {stop, {shutdown, kick}, ok, State};
handle_call(Req, _From, State = #wsclient_state{peer = Peer}) -> handle_call(session, _From, State = #wsclient_state{proto_state = ProtoState}) ->
?WSLOG(critical, Peer, "Unexpected request: ~p", [Req]), reply(emqttd_protocol:session(ProtoState), State);
{reply, {error, unsupported_request}, State}.
handle_cast({subscribe, TopicTable}, State) -> handle_call(Req, _From, State) ->
with_proto_state(fun(ProtoState) -> ?WSLOG(error, "Unexpected request: ~p", [Req], State),
emqttd_protocol:handle({subscribe, TopicTable}, ProtoState) reply({error, unexpected_request}, State).
end, State);
handle_cast({unsubscribe, Topics}, State) -> handle_cast({received, Packet}, State = #wsclient_state{proto_state = ProtoState}) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:handle({unsubscribe, Topics}, ProtoState)
end, State);
handle_cast({received, Packet}, State = #wsclient_state{peer = Peer, proto_state = ProtoState}) ->
emqttd_metrics:received(Packet), emqttd_metrics:received(Packet),
case emqttd_protocol:received(Packet, ProtoState) of case emqttd_protocol:received(Packet, ProtoState) of
{ok, ProtoState1} -> {ok, ProtoState1} ->
noreply(State#wsclient_state{proto_state = ProtoState1}); {noreply, gc(State#wsclient_state{proto_state = ProtoState1}), hibernate};
{error, Error} -> {error, Error} ->
?WSLOG(error, Peer, "Protocol error - ~p", [Error]), ?WSLOG(error, "Protocol error - ~p", [Error], State),
shutdown(Error, State); shutdown(Error, State);
{error, Error, ProtoState1} -> {error, Error, ProtoState1} ->
shutdown(Error, State#wsclient_state{proto_state = ProtoState1}); shutdown(Error, State#wsclient_state{proto_state = ProtoState1});
@ -121,67 +151,83 @@ handle_cast({received, Packet}, State = #wsclient_state{peer = Peer, proto_state
stop(Reason, State#wsclient_state{proto_state = ProtoState1}) stop(Reason, State#wsclient_state{proto_state = ProtoState1})
end; end;
handle_cast(Msg, State = #wsclient_state{peer = Peer}) -> handle_cast(Msg, State) ->
?WSLOG(critical, Peer, "Unexpected msg: ~p", [Msg]), ?WSLOG(error, "Unexpected Msg: ~p", [Msg], State),
noreply(State). {noreply, State, hibernate}.
handle_info({subscribe, TopicTable}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:subscribe(TopicTable, ProtoState)
end, State);
handle_info({unsubscribe, Topics}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:unsubscribe(Topics, ProtoState)
end, State);
handle_info({suback, PacketId, GrantedQos}, State) ->
with_proto(
fun(ProtoState) ->
Packet = ?SUBACK_PACKET(PacketId, GrantedQos),
emqttd_protocol:send(Packet, ProtoState)
end, State);
handle_info({deliver, Message}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:send(Message, ProtoState)
end, gc(State));
handle_info({redeliver, {?PUBREL, PacketId}}, State) ->
with_proto(
fun(ProtoState) ->
emqttd_protocol:pubrel(PacketId, ProtoState)
end, State);
handle_info(emit_stats, State) ->
{noreply, emit_stats(State), hibernate};
handle_info(timeout, State) -> handle_info(timeout, State) ->
shutdown(idle_timeout, State); shutdown(idle_timeout, State);
handle_info({suback, PacketId, GrantedQos}, State) -> handle_info({shutdown, conflict, {ClientId, NewPid}}, State) ->
with_proto_state(fun(ProtoState) -> ?WSLOG(warning, "clientid '~s' conflict with ~p", [ClientId, NewPid], State),
Packet = ?SUBACK_PACKET(PacketId, GrantedQos),
emqttd_protocol:send(Packet, ProtoState)
end, State);
handle_info({deliver, Message}, State) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:send(Message, ProtoState)
end, State);
handle_info({redeliver, {?PUBREL, PacketId}}, State) ->
with_proto_state(fun(ProtoState) ->
emqttd_protocol:redeliver({?PUBREL, PacketId}, ProtoState)
end, State);
handle_info({shutdown, conflict, {ClientId, NewPid}}, State = #wsclient_state{peer = Peer}) ->
?WSLOG(warning, Peer, "clientid '~s' conflict with ~p", [ClientId, NewPid]),
shutdown(conflict, State); shutdown(conflict, State);
handle_info({keepalive, start, Interval}, State = #wsclient_state{peer = Peer, connection = Conn}) -> handle_info({keepalive, start, Interval}, State = #wsclient_state{connection = Conn}) ->
?WSLOG(debug, Peer, "Keepalive at the interval of ~p", [Interval]), ?WSLOG(debug, "Keepalive at the interval of ~p", [Interval], State),
StatFun = fun() -> case emqttd_keepalive:start(stat_fun(Conn), Interval, {keepalive, check}) of
case Conn:getstat([recv_oct]) of {ok, KeepAlive} ->
{ok, [{recv_oct, RecvOct}]} -> {ok, RecvOct}; {noreply, State#wsclient_state{keepalive = KeepAlive}, hibernate};
{error, Error} -> {error, Error} {error, Error} ->
end ?WSLOG(warning, "Keepalive error - ~p", [Error], State),
end, shutdown(Error, State)
KeepAlive = emqttd_keepalive:start(StatFun, Interval, {keepalive, check}), end;
noreply(State#wsclient_state{keepalive = KeepAlive});
handle_info({keepalive, check}, State = #wsclient_state{peer = Peer, handle_info({keepalive, check}, State = #wsclient_state{keepalive = KeepAlive}) ->
keepalive = KeepAlive}) ->
case emqttd_keepalive:check(KeepAlive) of case emqttd_keepalive:check(KeepAlive) of
{ok, KeepAlive1} -> {ok, KeepAlive1} ->
noreply(State#wsclient_state{keepalive = KeepAlive1}); {noreply, emit_stats(State#wsclient_state{keepalive = KeepAlive1}), hibernate};
{error, timeout} -> {error, timeout} ->
?WSLOG(debug, Peer, "Keepalive Timeout!", []), ?WSLOG(debug, "Keepalive Timeout!", [], State),
shutdown(keepalive_timeout, State); shutdown(keepalive_timeout, State);
{error, Error} -> {error, Error} ->
?WSLOG(warning, Peer, "Keepalive error - ~p", [Error]), ?WSLOG(warning, "Keepalive error - ~p", [Error], State),
shutdown(keepalive_error, State) shutdown(keepalive_error, State)
end; end;
handle_info({'EXIT', WsPid, normal}, State = #wsclient_state{ws_pid = WsPid}) -> handle_info({'EXIT', WsPid, normal}, State = #wsclient_state{ws_pid = WsPid}) ->
stop(normal, State); stop(normal, State);
handle_info({'EXIT', WsPid, Reason}, State = #wsclient_state{peer = Peer, ws_pid = WsPid}) -> handle_info({'EXIT', WsPid, Reason}, State = #wsclient_state{ws_pid = WsPid}) ->
?WSLOG(error, Peer, "shutdown: ~p",[Reason]), ?WSLOG(error, "shutdown: ~p",[Reason], State),
shutdown(Reason, State); shutdown(Reason, State);
handle_info(Info, State = #wsclient_state{peer = Peer}) -> handle_info(Info, State) ->
?WSLOG(critical, Peer, "Unexpected Info: ~p", [Info]), ?WSLOG(error, "Unexpected Info: ~p", [Info], State),
noreply(State). {noreply, State, hibernate}.
terminate(Reason, #wsclient_state{proto_state = ProtoState, keepalive = KeepAlive}) -> terminate(Reason, #wsclient_state{proto_state = ProtoState, keepalive = KeepAlive}) ->
emqttd_keepalive:cancel(KeepAlive), emqttd_keepalive:cancel(KeepAlive),
@ -199,16 +245,52 @@ code_change(_OldVsn, State, _Extra) ->
%% Internal functions %% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
with_proto_state(Fun, State = #wsclient_state{proto_state = ProtoState}) -> send_fun(ReplyChannel) ->
{ok, ProtoState1} = Fun(ProtoState), fun(Packet) ->
noreply(State#wsclient_state{proto_state = ProtoState1}). Data = emqttd_serializer:serialize(Packet),
emqttd_metrics:inc('bytes/sent', iolist_size(Data)),
ReplyChannel({binary, Data})
end.
noreply(State) -> stat_fun(Conn) ->
{noreply, State, hibernate}. fun() ->
case Conn:getstat([recv_oct]) of
{ok, [{recv_oct, RecvOct}]} -> {ok, RecvOct};
{error, Error} -> {error, Error}
end
end.
emit_stats(State = #wsclient_state{proto_state = ProtoState}) ->
emit_stats(emqttd_protocol:clientid(ProtoState), State).
emit_stats(_ClientId, State = #wsclient_state{enable_stats = false}) ->
State;
emit_stats(undefined, State) ->
State;
emit_stats(ClientId, State) ->
{reply, Stats, _, _} = handle_call(stats, undefined, State),
emqttd_stats:set_client_stats(ClientId, Stats),
State.
wsock_stats(#wsclient_state{connection = Conn}) ->
case Conn:getstat(?SOCK_STATS) of
{ok, Ss} -> Ss;
{error, _} -> []
end.
with_proto(Fun, State = #wsclient_state{proto_state = ProtoState}) ->
{ok, ProtoState1} = Fun(ProtoState),
{noreply, State#wsclient_state{proto_state = ProtoState1}, hibernate}.
reply(Reply, State) ->
{reply, Reply, State, hibernate}.
shutdown(Reason, State) -> shutdown(Reason, State) ->
stop({shutdown, Reason}, State). stop({shutdown, Reason}, State).
stop(Reason, State ) -> stop(Reason, State) ->
{stop, Reason, State}. {stop, Reason, State}.
gc(State) ->
emqttd_gc:maybe_force_gc(#wsclient_state.force_gc_count, State).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -29,7 +29,7 @@
start_link() -> start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []). supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% @doc Start a WebSocket Client %% @doc Start a WebSocket Connection.
-spec(start_client(pid(), mochiweb_request:request(), fun()) -> {ok, pid()}). -spec(start_client(pid(), mochiweb_request:request(), fun()) -> {ok, pid()}).
start_client(WsPid, Req, ReplyChannel) -> start_client(WsPid, Req, ReplyChannel) ->
supervisor:start_child(?MODULE, [WsPid, Req, ReplyChannel]). supervisor:start_child(?MODULE, [WsPid, Req, ReplyChannel]).
@ -37,8 +37,9 @@ start_client(WsPid, Req, ReplyChannel) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Supervisor callbacks %% Supervisor callbacks
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
{ok, Env} = emqttd:env(protocol), Env = lists:append(emqttd:env(client, []), emqttd:env(protocol, [])),
{ok, {{simple_one_for_one, 0, 1}, {ok, {{simple_one_for_one, 0, 1},
[{ws_client, {emqttd_ws_client, start_link, [Env]}, [{ws_client, {emqttd_ws_client, start_link, [Env]},
temporary, 5000, worker, [emqttd_ws_client]}]}}. temporary, 5000, worker, [emqttd_ws_client]}]}}.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
-module(lager_emqtt_backend). -module(lager_emqtt_backend).
-author("Feng Lee <feng@emqtt.io>").
-behaviour(gen_event). -behaviour(gen_event).
-include_lib("lager/include/lager.hrl"). -include_lib("lager/include/lager.hrl").

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -47,7 +47,8 @@ all() ->
{group, http}, {group, http},
{group, cluster}, {group, cluster},
{group, alarms}, {group, alarms},
{group, cli}]. {group, cli},
{group, cleanSession}].
groups() -> groups() ->
[{protocol, [sequence], [{protocol, [sequence],
@ -103,7 +104,11 @@ groups() ->
cli_bridges, cli_bridges,
cli_plugins, cli_plugins,
cli_listeners, cli_listeners,
cli_vm]}]. cli_vm]},
{cleanSession, [sequence],
[cleanSession_validate,
cleanSession_validate1,
cleanSession_validate2]}].
init_per_suite(Config) -> init_per_suite(Config) ->
application:start(lager), application:start(lager),
@ -366,37 +371,39 @@ set_get_stat(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
add_delete_hook(_) -> add_delete_hook(_) ->
emqttd:hook(test_hook, fun ?MODULE:hook_fun1/1, []), ok = emqttd:hook(test_hook, fun ?MODULE:hook_fun1/1, []),
emqttd:hook(test_hook, fun ?MODULE:hook_fun2/1, []), ok = emqttd:hook(test_hook, {tag, fun ?MODULE:hook_fun2/1}, []),
{error, already_hooked} = emqttd:hook(test_hook, fun ?MODULE:hook_fun2/1, []), {error, already_hooked} = emqttd:hook(test_hook, {tag, fun ?MODULE:hook_fun2/1}, []),
Callbacks = [{callback, fun ?MODULE:hook_fun1/1, [], 0}, Callbacks = [{callback, undefined, fun ?MODULE:hook_fun1/1, [], 0},
{callback, fun ?MODULE:hook_fun2/1, [], 0}], {callback, tag, fun ?MODULE:hook_fun2/1, [], 0}],
Callbacks = emqttd_hook:lookup(test_hook), Callbacks = emqttd_hooks:lookup(test_hook),
emqttd:unhook(test_hook, fun ?MODULE:hook_fun1/1), ok = emqttd:unhook(test_hook, fun ?MODULE:hook_fun1/1),
emqttd:unhook(test_hook, fun ?MODULE:hook_fun2/1), ct:print("Callbacks: ~p~n", [emqttd_hooks:lookup(test_hook)]),
ok = emqttd:unhook(test_hook, fun ?MODULE:hook_fun2/1), ok = emqttd:unhook(test_hook, {tag, fun ?MODULE:hook_fun2/1}),
{error, not_found} = emqttd:unhook(test_hook1, fun ?MODULE:hook_fun2/1), {error, not_found} = emqttd:unhook(test_hook1, {tag, fun ?MODULE:hook_fun2/1}),
[] = emqttd_hook:lookup(test_hook), [] = emqttd_hooks:lookup(test_hook),
emqttd:hook(emqttd_hook, fun ?MODULE:hook_fun1/1, [], 9), ok = emqttd:hook(emqttd_hook, fun ?MODULE:hook_fun1/1, [], 9),
emqttd:hook(emqttd_hook, fun ?MODULE:hook_fun2/1, [], 8), ok = emqttd:hook(emqttd_hook, {"tag", fun ?MODULE:hook_fun2/1}, [], 8),
Callbacks2 = [{callback, fun ?MODULE:hook_fun2/1, [], 8}, Callbacks2 = [{callback, "tag", fun ?MODULE:hook_fun2/1, [], 8},
{callback, fun ?MODULE:hook_fun1/1, [], 9}], {callback, undefined, fun ?MODULE:hook_fun1/1, [], 9}],
Callbacks2 = emqttd_hook:lookup(emqttd_hook), Callbacks2 = emqttd_hooks:lookup(emqttd_hook),
emqttd:unhook(emqttd_hook, fun ?MODULE:hook_fun1/1), ok = emqttd:unhook(emqttd_hook, fun ?MODULE:hook_fun1/1),
emqttd:unhook(emqttd_hook, fun ?MODULE:hook_fun2/1), ok = emqttd:unhook(emqttd_hook, {"tag", fun ?MODULE:hook_fun2/1}),
[] = emqttd_hook:lookup(emqttd_hook). [] = emqttd_hooks:lookup(emqttd_hook).
run_hooks(_) -> run_hooks(_) ->
emqttd:hook(foldl_hook, fun ?MODULE:hook_fun3/4, [init]), ok = emqttd:hook(foldl_hook, fun ?MODULE:hook_fun3/4, [init]),
emqttd:hook(foldl_hook, fun ?MODULE:hook_fun4/4, [init]), ok = emqttd:hook(foldl_hook, {tag, fun ?MODULE:hook_fun3/4}, [init]),
emqttd:hook(foldl_hook, fun ?MODULE:hook_fun5/4, [init]), ok = emqttd:hook(foldl_hook, fun ?MODULE:hook_fun4/4, [init]),
ok = emqttd:hook(foldl_hook, fun ?MODULE:hook_fun5/4, [init]),
{stop, [r3, r2]} = emqttd:run_hooks(foldl_hook, [arg1, arg2], []), {stop, [r3, r2]} = emqttd:run_hooks(foldl_hook, [arg1, arg2], []),
{ok, []} = emqttd:run_hooks(unknown_hook, [], []), {ok, []} = emqttd:run_hooks(unknown_hook, [], []),
emqttd:hook(foreach_hook, fun ?MODULE:hook_fun6/2, [initArg]), ok = emqttd:hook(foreach_hook, fun ?MODULE:hook_fun6/2, [initArg]),
emqttd:hook(foreach_hook, fun ?MODULE:hook_fun7/2, [initArg]), ok = emqttd:hook(foreach_hook, {tag, fun ?MODULE:hook_fun6/2}, [initArg]),
emqttd:hook(foreach_hook, fun ?MODULE:hook_fun8/2, [initArg]), ok = emqttd:hook(foreach_hook, fun ?MODULE:hook_fun7/2, [initArg]),
ok = emqttd:hook(foreach_hook, fun ?MODULE:hook_fun8/2, [initArg]),
stop = emqttd:run_hooks(foreach_hook, [arg]). stop = emqttd:run_hooks(foreach_hook, [arg]).
hook_fun1([]) -> ok. hook_fun1([]) -> ok.
@ -514,8 +521,8 @@ cluster_remove2(_) ->
ok = emqttd_cluster:join(Z), ok = emqttd_cluster:join(Z),
Node = node(), Node = node(),
[Z, Node] = emqttd_mnesia:running_nodes(), [Z, Node] = emqttd_mnesia:running_nodes(),
ok = rpc:call(Z, emqttd_mnesia, ensure_stopped, []),
ok = emqttd_cluster:remove(Z), ok = emqttd_cluster:remove(Z),
ok = rpc:call(Z, emqttd_mnesia, ensure_stopped, []),
[Node] = emqttd_mnesia:running_nodes(), [Node] = emqttd_mnesia:running_nodes(),
slave:stop(Z). slave:stop(Z).
@ -616,6 +623,59 @@ cli_vm(_) ->
emqttd_cli:vm([]), emqttd_cli:vm([]),
emqttd_cli:vm(["ports"]). emqttd_cli:vm(["ports"]).
cleanSession_validate(_) ->
{ok, C1} = emqttc:start_link([{host, "localhost"},
{port, 1883},
{client_id, <<"c1">>},
{clean_sess, false}]),
timer:sleep(10),
emqttc:subscribe(C1, <<"topic">>, qos0),
ok = emqttd_cli:sessions(["list", "persistent"]),
emqttc:disconnect(C1),
{ok, Pub} = emqttc:start_link([{host, "localhost"},
{port, 1883},
{client_id, <<"pub">>}]),
emqttc:publish(Pub, <<"topic">>, <<"m1">>, [{qos, 0}]),
timer:sleep(10),
{ok, C11} = emqttc:start_link([{host, "localhost"},
{port, 1883},
{client_id, <<"c1">>},
{clean_sess, false}]),
timer:sleep(100),
Metrics = emqttd_metrics:all(),
ct:log("Metrics:~p~n", [Metrics]),
?assertEqual(1, proplists:get_value('messages/qos0/sent', Metrics)),
?assertEqual(1, proplists:get_value('messages/qos0/received', Metrics)),
emqttc:disconnect(Pub),
emqttc:disconnect(C11).
cleanSession_validate1(_) ->
{ok, C1} = emqttc:start_link([{host, "localhost"},
{port, 1883},
{client_id, <<"c1">>},
{clean_sess, true}]),
timer:sleep(10),
emqttc:subscribe(C1, <<"topic">>, qos1),
ok = emqttd_cli:sessions(["list", "transient"]),
emqttc:disconnect(C1),
{ok, Pub} = emqttc:start_link([{host, "localhost"},
{port, 1883},
{client_id, <<"pub">>}]),
emqttc:publish(Pub, <<"topic">>, <<"m1">>, [{qos, 1}]),
timer:sleep(10),
{ok, C11} = emqttc:start_link([{host, "localhost"},
{port, 1883},
{client_id, <<"c1">>},
{clean_sess, false}]),
timer:sleep(100),
Metrics = emqttd_metrics:all(),
?assertEqual(0, proplists:get_value('messages/qos1/sent', Metrics)),
?assertEqual(1, proplists:get_value('messages/qos1/received', Metrics)),
emqttc:disconnect(Pub),
emqttc:disconnect(C11).
ensure_ok(ok) -> ok; ensure_ok(ok) -> ok;
ensure_ok({error, {already_started, _}}) -> ok. ensure_ok({error, {already_started, _}}) -> ok.

View File

@ -1,3 +1,8 @@
##===================================================================
## EMQ Configuration R2.1
##===================================================================
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Node Args ## Node Args
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
@ -11,6 +16,11 @@ node.cookie = emq_dist_cookie
## SMP support: enable, auto, disable ## SMP support: enable, auto, disable
node.smp = auto node.smp = auto
## vm.args: -heart
## Heartbeat monitoring of an Erlang runtime system
## Value should be 'on' or comment the line
## node.heartbeat = on
## Enable kernel poll ## Enable kernel poll
node.kernel_poll = on node.kernel_poll = on
@ -40,21 +50,30 @@ node.crash_dump = log/crash.dump
node.dist_net_ticktime = 60 node.dist_net_ticktime = 60
## Distributed node port range ## Distributed node port range
## node.dist_listen_min = 6000 ## node.dist_listen_min = 6369
## node.dist_listen_max = 6999 ## node.dist_listen_max = 6369
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Log ## Log
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Set the log dir
log.dir = {{ platform_log_dir }}
## Console log. Enum: off, file, console, both ## Console log. Enum: off, file, console, both
log.console = console log.console = console
## Syslog. Enum: on, off
log.syslog = on
## syslog level. Enum: debug, info, notice, warning, error, critical, alert, emergency
log.syslog.level = error
## Console log level. Enum: debug, info, notice, warning, error, critical, alert, emergency ## Console log level. Enum: debug, info, notice, warning, error, critical, alert, emergency
log.console.level = error log.console.level = error
## Console log file ## Console log file
## log.console.file = log/console.log ## log.console.file = {{ platform_log_dir }}/console.log
## Error log file ## Error log file
log.error.file = log/error.log log.error.file = log/error.log
@ -64,6 +83,19 @@ log.crash = on
log.crash.file = log/crash.log log.crash.file = log/crash.log
##--------------------------------------------------------------------
## Allow Anonymous and Default ACL
##--------------------------------------------------------------------
## Allow Anonymous authentication
mqtt.allow_anonymous = true
## Default ACL File
mqtt.acl_file = etc/acl.conf
## Cache ACL for PUBLISH
mqtt.cache_acl = true
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Protocol ## MQTT Protocol
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
@ -74,34 +106,38 @@ mqtt.max_clientid_len = 1024
## Max Packet Size Allowed, 64K by default. ## Max Packet Size Allowed, 64K by default.
mqtt.max_packet_size = 64KB mqtt.max_packet_size = 64KB
##--------------------------------------------------------------------
## MQTT Client
##--------------------------------------------------------------------
## Client Idle Timeout (Second) ## Client Idle Timeout (Second)
mqtt.client_idle_timeout = 30 mqtt.client.idle_timeout = 30s
## Allow Anonymous authentication ## Enable client Stats: seconds or off
mqtt.allow_anonymous = true mqtt.client.enable_stats = off
## Default ACL File
mqtt.acl_file = etc/acl.conf
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Session ## MQTT Session
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## Upgrade QoS?
mqtt.session.upgrade_qos = off
## Max number of QoS 1 and 2 messages that can be “inflight” at one time. ## Max number of QoS 1 and 2 messages that can be “inflight” at one time.
## 0 means no limit ## 0 means no limit
mqtt.session.max_inflight = 100 mqtt.session.max_inflight = 32
## Retry interval for redelivering QoS1/2 messages. ## Retry Interval for redelivering QoS1/2 messages.
mqtt.session.retry_interval = 60 mqtt.session.retry_interval = 20s
## Awaiting PUBREL Timeout
mqtt.session.await_rel_timeout = 20
## Max Packets that Awaiting PUBREL, 0 means no limit ## Max Packets that Awaiting PUBREL, 0 means no limit
mqtt.session.max_awaiting_rel = 0 mqtt.session.max_awaiting_rel = 100
## Statistics Collection Interval(seconds) ## Awaiting PUBREL Timeout
mqtt.session.collect_interval = 0 mqtt.session.await_rel_timeout = 20s
## Enable Statistics at the Interval(seconds)
mqtt.session.enable_stats = off
## Expired after 1 day: ## Expired after 1 day:
## w - week ## w - week
@ -109,7 +145,7 @@ mqtt.session.collect_interval = 0
## h - hour ## h - hour
## m - minute ## m - minute
## s - second ## s - second
mqtt.session.expired_after = 1d mqtt.session.expiry_interval = 2h
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Queue ## MQTT Queue
@ -204,12 +240,13 @@ mqtt.listener.ssl.max_clients = 512
## Rate Limit. Format is 'burst,rate', Unit is KB/Sec ## Rate Limit. Format is 'burst,rate', Unit is KB/Sec
## mqtt.listener.ssl.rate_limit = 100,10 ## mqtt.listener.ssl.rate_limit = 100,10
## Configuring SSL Options ## Configuring SSL Options. See http://erlang.org/doc/man/ssl.html
## See http://erlang.org/doc/man/ssl.html ### TLS only for POODLE attack
mqtt.listener.ssl.handshake_timeout = 15 mqtt.listener.ssl.tls_versions = tlsv1.2,tlsv1.1,tlsv1
mqtt.listener.ssl.handshake_timeout = 15s
mqtt.listener.ssl.keyfile = certs/key.pem mqtt.listener.ssl.keyfile = certs/key.pem
mqtt.listener.ssl.certfile = certs/cert.pem mqtt.listener.ssl.certfile = certs/cert.pem
## mqtt.listener.ssl.cacertfile = etc/certs/cacert.pem ## mqtt.listener.ssl.cacertfile = {{ platform_etc_dir }}/certs/cacert.pem
## mqtt.listener.ssl.verify = verify_peer ## mqtt.listener.ssl.verify = verify_peer
## mqtt.listener.ssl.fail_if_no_peer_cert = true ## mqtt.listener.ssl.fail_if_no_peer_cert = true
@ -219,13 +256,14 @@ mqtt.listener.http.acceptors = 4
mqtt.listener.http.max_clients = 64 mqtt.listener.http.max_clients = 64
## HTTP(SSL) Listener ## HTTP(SSL) Listener
## mqtt.listener.https = 8084 mqtt.listener.https = 8084
## mqtt.listener.https.acceptors = 4 mqtt.listener.https.acceptors = 4
## mqtt.listener.https.max_clients = 64 mqtt.listener.https.max_clients = 64
## mqtt.listener.https.handshake_timeout = 15 mqtt.listener.https.handshake_timeout = 15
## mqtt.listener.https.certfile = etc/certs/cert.pem mqtt.listener.https.keyfile = certs/key.pem
## mqtt.listener.https.keyfile = etc/certs/key.pem mqtt.listener.https.certfile = certs/cert.pem
## mqtt.listener.https.cacertfile = etc/certs/cacert.pem ## mqtt.listener.https.cacertfile = {{ platform_etc_dir }}/certs/cacert.pem
## mqtt.listener.https.verify = verify_peer ## mqtt.listener.https.verify = verify_peer
## mqtt.listener.https.fail_if_no_peer_cert = true ## mqtt.listener.https.fail_if_no_peer_cert = true

View File

@ -22,6 +22,19 @@
hidden hidden
]}. ]}.
%% @doc http://erlang.org/doc/man/heart.html
{mapping, "node.heartbeat", "vm_args.-heart", [
{datatype, flag},
hidden
]}.
{translation, "vm_args.-heart", fun(Conf) ->
case cuttlefish:conf_get("node.heartbeat", Conf) of
true -> "";
false -> cuttlefish:invalid("should be 'on' or comment the line!")
end
end}.
%% @doc Enable Kernel Poll %% @doc Enable Kernel Poll
{mapping, "node.kernel_poll", "vm_args.+K", [ {mapping, "node.kernel_poll", "vm_args.+K", [
{default, on}, {default, on},
@ -135,8 +148,13 @@
%% Log %% Log
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
{mapping, "log.dir", "lager.log_dir", [
{default, "log"},
{datatype, string}
]}.
{mapping, "log.console", "lager.handlers", [ {mapping, "log.console", "lager.handlers", [
{default, file }, {default, file},
{datatype, {enum, [off, file, console, both]}} {datatype, {enum, [off, file, console, both]}}
]}. ]}.
@ -155,6 +173,26 @@
{datatype, file} {datatype, file}
]}. ]}.
{mapping, "log.syslog", "lager.handlers", [
{default, off},
{datatype, flag}
]}.
{mapping, "log.syslog.identity", "lager.handlers", [
{default, "emq"},
{datatype, string}
]}.
{mapping, "log.syslog.facility", "lager.handlers", [
{default, local0},
{datatype, {enum, [daemon, local0, local1, local2, local3, local4, local5, local6, local7]}}
]}.
{mapping, "log.syslog.level", "lager.handlers", [
{default, err},
{datatype, {enum, [debug, info, notice, warning, error, critical, alert, emergency]}}
]}.
{mapping, "log.error.redirect", "lager.error_logger_redirect", [ {mapping, "log.error.redirect", "lager.error_logger_redirect", [
{default, on}, {default, on},
{datatype, flag}, {datatype, flag},
@ -196,7 +234,16 @@
both -> [ConsoleHandler, ConsoleFileHandler]; both -> [ConsoleHandler, ConsoleFileHandler];
_ -> [] _ -> []
end, end,
ConsoleHandlers ++ ErrorHandler
SyslogHandler = case cuttlefish:conf_get("log.syslog", Conf) of
false -> [];
true -> [{lager_syslog_backend,
[cuttlefish:conf_get("log.syslog.identity", Conf),
cuttlefish:conf_get("log.syslog.facility", Conf),
cuttlefish:conf_get("log.syslog.level", Conf)]}]
end,
ConsoleHandlers ++ ErrorHandler ++ SyslogHandler
end end
}. }.
@ -226,6 +273,28 @@
hidden hidden
]}. ]}.
%%--------------------------------------------------------------------
%% Allow Anonymous and Default ACL
%%--------------------------------------------------------------------
%% @doc Allow Anonymous
{mapping, "mqtt.allow_anonymous", "emqttd.allow_anonymous", [
{default, false},
{datatype, {enum, [true, false]}}
]}.
%% @doc Default ACL File
{mapping, "mqtt.acl_file", "emqttd.acl_file", [
{datatype, string},
hidden
]}.
%% @doc Cache ACL for PUBLISH
{mapping, "mqtt.cache_acl", "emqttd.cache_acl", [
{default, true},
{datatype, {enum, [true, false]}}
]}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Protocol %% MQTT Protocol
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -242,35 +311,42 @@
{datatype, bytesize} {datatype, bytesize}
]}. ]}.
%% @doc Client Idle Timeout.
{mapping, "mqtt.client_idle_timeout", "emqttd.protocol", [
{default, 30},
{datatype, integer}
]}.
{translation, "emqttd.protocol", fun(Conf) -> {translation, "emqttd.protocol", fun(Conf) ->
[{max_clientid_len, cuttlefish:conf_get("mqtt.max_clientid_len", Conf)}, [{max_clientid_len, cuttlefish:conf_get("mqtt.max_clientid_len", Conf)},
{max_packet_size, cuttlefish:conf_get("mqtt.max_packet_size", Conf)}, {max_packet_size, cuttlefish:conf_get("mqtt.max_packet_size", Conf)}]
{client_idle_timeout, cuttlefish:conf_get("mqtt.client_idle_timeout", Conf)}]
end}. end}.
%% @doc Allow Anonymous %%--------------------------------------------------------------------
{mapping, "mqtt.allow_anonymous", "emqttd.allow_anonymous", [ %% MQTT Client
{default, false}, %%--------------------------------------------------------------------
{datatype, {enum, [true, false]}},
hidden %% @doc Client Idle Timeout.
{mapping, "mqtt.client.idle_timeout", "emqttd.client", [
{default, "30s"},
{datatype, {duration, ms}}
]}. ]}.
%% @doc Default ACL File %% @doc Enable Stats of Client.
{mapping, "mqtt.acl_file", "emqttd.acl_file", [ {mapping, "mqtt.client.enable_stats", "emqttd.client", [
{datatype, string}, {default, off},
hidden {datatype, [{duration, ms}, flag]}
]}. ]}.
%% @doc Client
{translation, "emqttd.client", fun(Conf) ->
[{client_idle_timeout, cuttlefish:conf_get("mqtt.client.idle_timeout", Conf)},
{client_enable_stats, cuttlefish:conf_get("mqtt.client.enable_stats", Conf)}]
end}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Session %% MQTT Session
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Upgrade QoS?
{mapping, "mqtt.session.upgrade_qos", "emqttd.session", [
{default, off},
{datatype, flag}
]}.
%% @doc Max number of QoS 1 and 2 messages that can be “inflight” at one time. %% @doc Max number of QoS 1 and 2 messages that can be “inflight” at one time.
%% 0 means no limit %% 0 means no limit
{mapping, "mqtt.session.max_inflight", "emqttd.session", [ {mapping, "mqtt.session.max_inflight", "emqttd.session", [
@ -278,17 +354,10 @@ end}.
{datatype, integer} {datatype, integer}
]}. ]}.
%% @doc Retry interval for redelivering QoS1/2 messages. %% @doc Retry interval for redelivering QoS1/2 messages.
{mapping, "mqtt.session.retry_interval", "emqttd.session", [ {mapping, "mqtt.session.retry_interval", "emqttd.session", [
{default, 60}, {default, "20s"},
{datatype, integer} {datatype, {duration, ms}}
]}.
%% @doc Awaiting PUBREL Timeout
{mapping, "mqtt.session.await_rel_timeout", "emqttd.session", [
{default, 30},
{datatype, integer}
]}. ]}.
%% @doc Max Packets that Awaiting PUBREL, 0 means no limit %% @doc Max Packets that Awaiting PUBREL, 0 means no limit
@ -297,25 +366,32 @@ end}.
{datatype, integer} {datatype, integer}
]}. ]}.
%% @doc Statistics Collection Interval(seconds) %% @doc Awaiting PUBREL Timeout
{mapping, "mqtt.session.collect_interval", "emqttd.session", [ {mapping, "mqtt.session.await_rel_timeout", "emqttd.session", [
{default, 0}, {default, "20s"},
{datatype, integer} {datatype, {duration, ms}}
]}. ]}.
%% @doc Session expired after... %% @doc Enable Stats
{mapping, "mqtt.session.expired_after", "emqttd.session", [ {mapping, "mqtt.session.enable_stats", "emqttd.session", [
{default, "2d"}, {default, off},
{datatype, {duration, s}} {datatype, [{duration, ms}, flag]}
]}.
%% @doc Session Expiry Interval
{mapping, "mqtt.session.expiry_interval", "emqttd.session", [
{default, "2h"},
{datatype, {duration, ms}}
]}. ]}.
{translation, "emqttd.session", fun(Conf) -> {translation, "emqttd.session", fun(Conf) ->
[{max_inflight, cuttlefish:conf_get("mqtt.session.max_inflight", Conf)}, [{upgrade_qos, cuttlefish:conf_get("mqtt.session.upgrade_qos", Conf)},
{retry_interval, cuttlefish:conf_get("mqtt.session.retry_interval", Conf)}, {max_inflight, cuttlefish:conf_get("mqtt.session.max_inflight", Conf)},
{retry_interval, cuttlefish:conf_get("mqtt.session.retry_interval", Conf)},
{max_awaiting_rel, cuttlefish:conf_get("mqtt.session.max_awaiting_rel", Conf)},
{await_rel_timeout, cuttlefish:conf_get("mqtt.session.await_rel_timeout", Conf)}, {await_rel_timeout, cuttlefish:conf_get("mqtt.session.await_rel_timeout", Conf)},
{max_awaiting_rel, cuttlefish:conf_get("mqtt.session.max_awaiting_rel", Conf)}, {enable_stats, cuttlefish:conf_get("mqtt.session.enable_stats", Conf)},
{collect_interval, cuttlefish:conf_get("mqtt.session.collect_interval", Conf)}, {expiry_interval, cuttlefish:conf_get("mqtt.session.expiry_interval", Conf)}]
{expired_after, cuttlefish:conf_get("mqtt.session.expired_after", Conf)}]
end}. end}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -331,28 +407,25 @@ end}.
%% @doc Topic Priority: 0~255, Default is 0 %% @doc Topic Priority: 0~255, Default is 0
{mapping, "mqtt.queue.priority", "emqttd.queue", [ {mapping, "mqtt.queue.priority", "emqttd.queue", [
{default, ""}, {default, ""},
{datatype, string}, {datatype, string}
hidden
]}. ]}.
%% @doc Max queue length. Enqueued messages when persistent client disconnected, or inflight window is full. %% @doc Max queue length. Enqueued messages when persistent client disconnected, or inflight window is full.
{mapping, "mqtt.queue.max_length", "emqttd.queue", [ {mapping, "mqtt.queue.max_length", "emqttd.queue", [
{default, infinity}, {default, infinity},
{datatype, [atom, integer]} {datatype, [integer, {atom, infinity}]}
]}. ]}.
%% @doc Low-water mark of queued messages %% @doc Low-water mark of queued messages
{mapping, "mqtt.queue.low_watermark", "emqttd.queue", [ {mapping, "mqtt.queue.low_watermark", "emqttd.queue", [
{default, "20%"}, {default, "20%"},
{datatype, string}, {datatype, string}
hidden
]}. ]}.
%% @doc High-water mark of queued messages %% @doc High-water mark of queued messages
{mapping, "mqtt.queue.high_watermark", "emqttd.queue", [ {mapping, "mqtt.queue.high_watermark", "emqttd.queue", [
{default, "60%"}, {default, "60%"},
{datatype, string}, {datatype, string}
hidden
]}. ]}.
%% @doc Queue Qos0 messages? %% @doc Queue Qos0 messages?
@ -405,8 +478,7 @@ end}.
{mapping, "mqtt.pubsub.async", "emqttd.pubsub", [ {mapping, "mqtt.pubsub.async", "emqttd.pubsub", [
{default, true}, {default, true},
{datatype, {enum, [true, false]}}, {datatype, {enum, [true, false]}}
hidden
]}. ]}.
{translation, "emqttd.pubsub", fun(Conf) -> {translation, "emqttd.pubsub", fun(Conf) ->
@ -451,7 +523,7 @@ end}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
{mapping, "mqtt.listener.tcp", "emqttd.listeners", [ {mapping, "mqtt.listener.tcp", "emqttd.listeners", [
{default, 1883}, %% {default, 1883},
{datatype, [integer, ip]} {datatype, [integer, ip]}
]}. ]}.
@ -467,8 +539,7 @@ end}.
{mapping, "mqtt.listener.tcp.rate_limit", "emqttd.listeners", [ {mapping, "mqtt.listener.tcp.rate_limit", "emqttd.listeners", [
{default, undefined}, {default, undefined},
{datatype, string}, {datatype, string}
hidden
]}. ]}.
{mapping, "mqtt.listener.tcp.backlog", "emqttd.listeners", [ {mapping, "mqtt.listener.tcp.backlog", "emqttd.listeners", [
@ -497,7 +568,7 @@ end}.
]}. ]}.
{mapping, "mqtt.listener.ssl", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl", "emqttd.listeners", [
{default, 8883}, %% {default, 8883},
{datatype, [integer, ip]} {datatype, [integer, ip]}
]}. ]}.
@ -515,9 +586,13 @@ end}.
{datatype, string} {datatype, string}
]}. ]}.
{mapping, "mqtt.listener.ssl.tls_versions", "emqttd.listeners", [
{datatype, string}
]}.
{mapping, "mqtt.listener.ssl.handshake_timeout", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl.handshake_timeout", "emqttd.listeners", [
{default, 15}, {default, "15s"},
{datatype, integer} {datatype, {duration, ms}}
]}. ]}.
{mapping, "mqtt.listener.ssl.keyfile", "emqttd.listeners", [ {mapping, "mqtt.listener.ssl.keyfile", "emqttd.listeners", [
@ -541,7 +616,7 @@ end}.
]}. ]}.
{mapping, "mqtt.listener.http", "emqttd.listeners", [ {mapping, "mqtt.listener.http", "emqttd.listeners", [
{default, 8883}, %% {default, 8083},
{datatype, [integer, ip]} {datatype, [integer, ip]}
]}. ]}.
@ -556,9 +631,8 @@ end}.
]}. ]}.
{mapping, "mqtt.listener.https", "emqttd.listeners", [ {mapping, "mqtt.listener.https", "emqttd.listeners", [
{default, undefined}, %%{default, 8084},
{datatype, [integer, ip]}, {datatype, [integer, ip]}
hidden
]}. ]}.
{mapping, "mqtt.listener.https.acceptors", "emqttd.listeners", [ {mapping, "mqtt.listener.https.acceptors", "emqttd.listeners", [
@ -610,8 +684,16 @@ end}.
{buffer, cuttlefish:conf_get(Prefix ++ ".buffer", Conf, undefined)}, {buffer, cuttlefish:conf_get(Prefix ++ ".buffer", Conf, undefined)},
{nodelay, cuttlefish:conf_get(Prefix ++ ".nodelay", Conf, true)}]) {nodelay, cuttlefish:conf_get(Prefix ++ ".nodelay", Conf, true)}])
end, end,
SplitFun = fun(undefined) -> undefined; (S) -> string:tokens(S, ",") end,
SslOpts = fun(Prefix) -> SslOpts = fun(Prefix) ->
Filter([{handshake_timeout, cuttlefish:conf_get(Prefix ++ ".handshake_timeout", Conf) * 1000}, Versions = case SplitFun(cuttlefish:conf_get(Prefix ++ ".tls_versions", Conf, undefined)) of
undefined -> undefined;
L -> [list_to_atom(V) || V <- L]
end,
Filter([{versions, Versions},
{handshake_timeout, cuttlefish:conf_get(Prefix ++ ".handshake_timeout", Conf), undefined},
{keyfile, cuttlefish:conf_get(Prefix ++ ".keyfile", Conf, undefined)}, {keyfile, cuttlefish:conf_get(Prefix ++ ".keyfile", Conf, undefined)},
{certfile, cuttlefish:conf_get(Prefix ++ ".certfile", Conf, undefined)}, {certfile, cuttlefish:conf_get(Prefix ++ ".certfile", Conf, undefined)},
{cacertfile, cuttlefish:conf_get(Prefix ++ ".cacertfile", Conf, undefined)}, {cacertfile, cuttlefish:conf_get(Prefix ++ ".cacertfile", Conf, undefined)},
@ -628,7 +710,7 @@ end}.
ConnOpts = Filter([{rate_limit, cuttlefish:conf_get(Key ++ ".rate_limit", Conf, undefined)}]), ConnOpts = Filter([{rate_limit, cuttlefish:conf_get(Key ++ ".rate_limit", Conf, undefined)}]),
Opts = [{connopts, ConnOpts}, {sockopts, TcpOpts(Key)} | LisOpts(Key)], Opts = [{connopts, ConnOpts}, {sockopts, TcpOpts(Key)} | LisOpts(Key)],
[{Name, Port, case Name =:= ssl orelse Name =:= https of [{Name, Port, case Name =:= ssl orelse Name =:= https of
true -> [{ssl, SslOpts(Key)} | Opts]; true -> [{sslopts, SslOpts(Key)} | Opts];
false -> Opts false -> Opts
end}] end}]
end end

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -0,0 +1,51 @@
%%
%% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%%
-module(emqttd_inflight_SUITE).
-author("Feng Lee <feng@emqtt.io>").
-include_lib("eunit/include/eunit.hrl").
%% CT
-compile(export_all).
all() -> [t_contain, t_lookup, t_insert, t_update, t_delete, t_window,
t_is_full, t_is_empty].
t_contain(_) ->
Inflight = emqttd_inflight:new(0),
?assertNot(Inflight:contain(k)),
Inflight1 = Inflight:insert(k, v),
?assert(Inflight1:contain(k)).
t_lookup(_) ->
Inflight = (emqttd_inflight:new(0)):insert(k, v),
?assertEqual(v, Inflight:lookup(k)).
t_insert(_) ->
Inflight = ((emqttd_inflight:new(0)):insert(k1, v1)):insert(k2, v2),
?assertEqual(v2, Inflight:lookup(k2)).
t_update(_) ->
Inflight = ((emqttd_inflight:new(0)):insert(k, v1)):update(k, v2),
?assertEqual(v2, Inflight:lookup(k)).
t_delete(_) ->
Inflight = ((emqttd_inflight:new(0)):insert(k, v1)):delete(k),
?assert(Inflight:is_empty()).
t_window(_) ->
?assertEqual([], (emqttd_inflight:new(10)):window()),
Inflight = ((emqttd_inflight:new(0)):insert(1, 1)):insert(2, 2),
?assertEqual([1, 2], Inflight:window()).
t_is_full(_) ->
Inflight = ((emqttd_inflight:new(1)):insert(k, v1)),
?assert(Inflight:is_full()).
t_is_empty(_) ->
Inflight = ((emqttd_inflight:new(1)):insert(k, v1)),
?assertNot(Inflight:is_empty()).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -71,7 +71,7 @@ guid_base62(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
opts_merge(_) -> opts_merge(_) ->
Opts = emqttd_opts:merge(?SOCKOPTS, [raw, Opts = emqttd_misc:merge_opts(?SOCKOPTS, [raw,
binary, binary,
{backlog, 1024}, {backlog, 1024},
{nodelay, false}, {nodelay, false},
@ -141,8 +141,8 @@ priority_queue_out2(_) ->
time_now_to_(_) -> time_now_to_(_) ->
emqttd_time:seed(), emqttd_time:seed(),
emqttd_time:now_to_secs(), emqttd_time:now_secs(),
emqttd_time:now_to_ms(). emqttd_time:now_ms().
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% emqttd_node %% emqttd_node

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ groups() -> [{keepalive, [], [t_keepalive]}].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
t_keepalive(_) -> t_keepalive(_) ->
KA = emqttd_keepalive:start(fun() -> {ok, 1} end, 1, {keepalive, timeout}), {ok, KA} = emqttd_keepalive:start(fun() -> {ok, 1} end, 1, {keepalive, timeout}),
[resumed, timeout] = lists:reverse(keepalive_recv(KA, [])). [resumed, timeout] = lists:reverse(keepalive_recv(KA, [])).
keepalive_recv(KA, Acc) -> keepalive_recv(KA, Acc) ->

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -22,6 +22,8 @@
-include("emqttd.hrl"). -include("emqttd.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
all() -> all() ->
@ -71,7 +73,7 @@ groups() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
parse_connect(_) -> parse_connect(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined) %% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
V31ConnBin = <<16,37,0,6,77,81,73,115,100,112,3,2,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,49,45,105,77,97,99,46,108,111,99,97>>, V31ConnBin = <<16,37,0,6,77,81,73,115,100,112,3,2,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,49,45,105,77,97,99,46,108,111,99,97>>,
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
@ -82,7 +84,7 @@ parse_connect(_) ->
proto_name = <<"MQIsdp">>, proto_name = <<"MQIsdp">>,
client_id = <<"mosqpub/10451-iMac.loca">>, client_id = <<"mosqpub/10451-iMac.loca">>,
clean_sess = true, clean_sess = true,
keep_alive = 60}}, <<>>} = Parser(V31ConnBin), keep_alive = 60}}, <<>>} = emqttd_parser:parse(V31ConnBin, Parser),
%% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined) %% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
V311ConnBin = <<16,35,0,4,77,81,84,84,4,2,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,49,45,105,77,97,99,46,108,111,99,97>>, V311ConnBin = <<16,35,0,4,77,81,84,84,4,2,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,49,45,105,77,97,99,46,108,111,99,97>>,
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
@ -93,7 +95,7 @@ parse_connect(_) ->
proto_name = <<"MQTT">>, proto_name = <<"MQTT">>,
client_id = <<"mosqpub/10451-iMac.loca">>, client_id = <<"mosqpub/10451-iMac.loca">>,
clean_sess = true, clean_sess = true,
keep_alive = 60 } }, <<>>} = Parser(V311ConnBin), keep_alive = 60 } }, <<>>} = emqttd_parser:parse(V311ConnBin, Parser),
%% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60) %% 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>>, V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
@ -105,7 +107,7 @@ parse_connect(_) ->
proto_name = <<"MQTT">>, proto_name = <<"MQTT">>,
client_id = <<>>, client_id = <<>>,
clean_sess = true, clean_sess = true,
keep_alive = 60 } }, <<>>} = Parser(V311ConnWithoutClientId), keep_alive = 60 } }, <<>>} = emqttd_parser:parse(V311ConnWithoutClientId, Parser),
%%CONNECT(Q0, R0, D0, ClientId=mosqpub/10452-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, %%CONNECT(Q0, R0, D0, 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)) %% Username=test, Password=******, Will(Qos=1, Retain=false, Topic=/will, Msg=willmsg))
ConnBinWithWill = <<16,67,0,6,77,81,73,115,100,112,3,206,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,50,45,105,77,97,99,46,108,111,99,97,0,5,47,119,105,108,108,0,7,119,105,108,108,109,115,103,0,4,116,101,115,116,0,6,112,117,98,108,105,99>>, ConnBinWithWill = <<16,67,0,6,77,81,73,115,100,112,3,206,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,50,45,105,77,97,99,46,108,111,99,97,0,5,47,119,105,108,108,0,7,119,105,108,108,109,115,103,0,4,116,101,115,116,0,6,112,117,98,108,105,99>>,
@ -124,18 +126,18 @@ parse_connect(_) ->
will_topic = <<"/will">>, will_topic = <<"/will">>,
will_msg = <<"willmsg">>, will_msg = <<"willmsg">>,
username = <<"test">>, username = <<"test">>,
password = <<"public">>}}, <<>>} = Parser(ConnBinWithWill), password = <<"public">>}}, <<>>} = emqttd_parser:parse(ConnBinWithWill, Parser),
ok. ok.
parse_bridge(_) -> parse_bridge(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
Data = <<16,86,0,6,77,81,73,115,100,112,131,44,0,60,0,19,67,95,48,48,58,48,67,58,50,57,58,50,66,58,55,55,58,53,50, Data = <<16,86,0,6,77,81,73,115,100,112,131,44,0,60,0,19,67,95,48,48,58,48,67,58,50,57,58,50,66,58,55,55,58,53,50,
0,48,36,83,89,83,47,98,114,111,107,101,114,47,99,111,110,110,101,99,116,105,111,110,47,67,95,48,48,58,48, 0,48,36,83,89,83,47,98,114,111,107,101,114,47,99,111,110,110,101,99,116,105,111,110,47,67,95,48,48,58,48,
67,58,50,57,58,50,66,58,55,55,58,53,50,47,115,116,97,116,101,0,1,48>>, 67,58,50,57,58,50,66,58,55,55,58,53,50,47,115,116,97,116,101,0,1,48>>,
%% CONNECT(Q0, R0, D0, ClientId=C_00:0C:29:2B:77:52, ProtoName=MQIsdp, ProtoVsn=131, CleanSess=false, KeepAlive=60, %% CONNECT(Q0, R0, D0, ClientId=C_00:0C:29:2B:77:52, ProtoName=MQIsdp, ProtoVsn=131, CleanSess=false, KeepAlive=60,
%% Username=undefined, Password=undefined, Will(Q1, R1, Topic=$SYS/broker/connection/C_00:0C:29:2B:77:52/state, Msg=0)) %% Username=undefined, Password=undefined, Will(Q1, R1, Topic=$SYS/broker/connection/C_00:0C:29:2B:77:52/state, Msg=0))
{ok, #mqtt_packet{variable = Variable}, <<>>} = Parser(Data), {ok, #mqtt_packet{variable = Variable}, <<>>} = emqttd_parser:parse(Data, Parser),
#mqtt_packet_connect{client_id = <<"C_00:0C:29:2B:77:52">>, #mqtt_packet_connect{client_id = <<"C_00:0C:29:2B:77:52">>,
proto_ver = 16#03, proto_ver = 16#03,
proto_name = <<"MQIsdp">>, proto_name = <<"MQIsdp">>,
@ -148,7 +150,7 @@ parse_bridge(_) ->
will_msg = <<"0">>} = Variable. will_msg = <<"0">>} = Variable.
parse_publish(_) -> parse_publish(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%%PUBLISH(Qos=1, Retain=false, Dup=false, TopicName=a/b/c, PacketId=1, Payload=<<"hahah">>) %%PUBLISH(Qos=1, Retain=false, Dup=false, TopicName=a/b/c, PacketId=1, Payload=<<"hahah">>)
PubBin = <<50,14,0,5,97,47,98,47,99,0,1,104,97,104,97,104>>, PubBin = <<50,14,0,5,97,47,98,47,99,0,1,104,97,104,97,104>>,
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
@ -157,7 +159,7 @@ parse_publish(_) ->
retain = false}, retain = false},
variable = #mqtt_packet_publish{topic_name = <<"a/b/c">>, variable = #mqtt_packet_publish{topic_name = <<"a/b/c">>,
packet_id = 1}, packet_id = 1},
payload = <<"hahah">> }, <<>>} = Parser(PubBin), payload = <<"hahah">> }, <<>>} = emqttd_parser:parse(PubBin, Parser),
%PUBLISH(Qos=0, Retain=false, Dup=false, TopicName=xxx/yyy, PacketId=undefined, Payload=<<"hello">>) %PUBLISH(Qos=0, Retain=false, Dup=false, TopicName=xxx/yyy, PacketId=undefined, Payload=<<"hello">>)
%DISCONNECT(Qos=0, Retain=false, Dup=false) %DISCONNECT(Qos=0, Retain=false, Dup=false)
@ -168,43 +170,43 @@ parse_publish(_) ->
retain = false}, retain = false},
variable = #mqtt_packet_publish{topic_name = <<"xxx/yyy">>, variable = #mqtt_packet_publish{topic_name = <<"xxx/yyy">>,
packet_id = undefined}, packet_id = undefined},
payload = <<"hello">> }, <<224,0>>} = Parser(PubBin1), payload = <<"hello">> }, <<224,0>>} = emqttd_parser:parse(PubBin1, Parser),
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT,
dup = false, dup = false,
qos = 0, qos = 0,
retain = false}}, <<>>} = Parser(<<224, 0>>). retain = false}}, <<>>} = emqttd_parser:parse(<<224, 0>>, Parser).
parse_puback(_) -> parse_puback(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1) %%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1)
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBACK, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBACK,
dup = false, dup = false,
qos = 0, qos = 0,
retain = false}}, <<>>} = Parser(<<64,2,0,1>>). retain = false}}, <<>>} = emqttd_parser:parse(<<64,2,0,1>>, Parser).
parse_pubrec(_) -> parse_pubrec(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%%PUBREC(Qos=0, Retain=false, Dup=false, PacketId=1) %%PUBREC(Qos=0, Retain=false, Dup=false, PacketId=1)
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREC, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREC,
dup = false, dup = false,
qos = 0, qos = 0,
retain = false}}, <<>>} = Parser(<<5:4,0:4,2,0,1>>). retain = false}}, <<>>} = emqttd_parser:parse(<<5:4,0:4,2,0,1>>, Parser).
parse_pubrel(_) -> parse_pubrel(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREL, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREL,
dup = false, dup = false,
qos = 1, qos = 1,
retain = false}}, <<>>} = Parser(<<6:4,2:4,2,0,1>>). retain = false}}, <<>>} = emqttd_parser:parse(<<6:4,2:4,2,0,1>>, Parser).
parse_pubcomp(_) -> parse_pubcomp(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBCOMP, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBCOMP,
dup = false, dup = false,
qos = 0, qos = 0,
retain = false}}, <<>>} = Parser(<<7:4,0:4,2,0,1>>). retain = false}}, <<>>} = emqttd_parser:parse(<<7:4,0:4,2,0,1>>, Parser).
parse_subscribe(_) -> parse_subscribe(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%% SUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[{<<"TopicA">>,2}]) %% SUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[{<<"TopicA">>,2}])
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?SUBSCRIBE, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?SUBSCRIBE,
dup = false, dup = false,
@ -212,10 +214,10 @@ parse_subscribe(_) ->
retain = false}, retain = false},
variable = #mqtt_packet_subscribe{packet_id = 2, variable = #mqtt_packet_subscribe{packet_id = 2,
topic_table = [{<<"TopicA">>,2}]} }, <<>>} topic_table = [{<<"TopicA">>,2}]} }, <<>>}
= Parser(<<130,11,0,2,0,6,84,111,112,105,99,65,2>>). = emqttd_parser:parse(<<130,11,0,2,0,6,84,111,112,105,99,65,2>>, Parser).
parse_unsubscribe(_) -> parse_unsubscribe(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%% UNSUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[<<"TopicA">>]) %% UNSUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[<<"TopicA">>])
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBSCRIBE, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBSCRIBE,
dup = false, dup = false,
@ -223,24 +225,24 @@ parse_unsubscribe(_) ->
retain = false}, retain = false},
variable = #mqtt_packet_unsubscribe{packet_id = 2, variable = #mqtt_packet_unsubscribe{packet_id = 2,
topics = [<<"TopicA">>]}}, <<>>} topics = [<<"TopicA">>]}}, <<>>}
= Parser(<<162,10,0,2,0,6,84,111,112,105,99,65>>). = emqttd_parser:parse(<<162,10,0,2,0,6,84,111,112,105,99,65>>, Parser).
parse_pingreq(_) -> parse_pingreq(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PINGREQ, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PINGREQ,
dup = false, dup = false,
qos = 0, qos = 0,
retain = false}}, <<>>} retain = false}}, <<>>}
= Parser(<<?PINGREQ:4, 0:4, 0:8>>). = emqttd_parser:parse(<<?PINGREQ:4, 0:4, 0:8>>, Parser).
parse_disconnect(_) -> parse_disconnect(_) ->
Parser = emqttd_parser:new([]), Parser = emqttd_parser:initial_state(),
%DISCONNECT(Qos=0, Retain=false, Dup=false) %DISCONNECT(Qos=0, Retain=false, Dup=false)
Bin = <<224, 0>>, Bin = <<224, 0>>,
{ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT,
dup = false, dup = false,
qos = 0, qos = 0,
retain = false}}, <<>>} = Parser(Bin). retain = false}}, <<>>} = emqttd_parser:parse(Bin, Parser).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Serialize Cases %% Serialize Cases
@ -260,7 +262,7 @@ serialize_connect(_) ->
serialize_connack(_) -> serialize_connack(_) ->
ConnAck = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK}, ConnAck = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
variable = #mqtt_packet_connack{ack_flags = 0, return_code = 0}}, variable = #mqtt_packet_connack{ack_flags = 0, return_code = 0}},
<<32,2,0,0>> = serialize(ConnAck). ?assertEqual(<<32,2,0,0>>, iolist_to_binary(serialize(ConnAck))).
serialize_publish(_) -> serialize_publish(_) ->
serialize(?PUBLISH_PACKET(?QOS_0, <<"Topic">>, undefined, <<"Payload">>)), serialize(?PUBLISH_PACKET(?QOS_0, <<"Topic">>, undefined, <<"Payload">>)),
@ -303,20 +305,20 @@ long_payload() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
packet_proto_name(_) -> packet_proto_name(_) ->
<<"MQIsdp">> = emqttd_packet:protocol_name(3), ?assertEqual(<<"MQIsdp">>, emqttd_packet:protocol_name(3)),
<<"MQTT">> = emqttd_packet:protocol_name(4). ?assertEqual(<<"MQTT">>, emqttd_packet:protocol_name(4)).
packet_type_name(_) -> packet_type_name(_) ->
'CONNECT' = emqttd_packet:type_name(?CONNECT), ?assertEqual('CONNECT', emqttd_packet:type_name(?CONNECT)),
'UNSUBSCRIBE' = emqttd_packet:type_name(?UNSUBSCRIBE). ?assertEqual('UNSUBSCRIBE', emqttd_packet:type_name(?UNSUBSCRIBE)).
packet_connack_name(_) -> packet_connack_name(_) ->
'CONNACK_ACCEPT' = emqttd_packet:connack_name(?CONNACK_ACCEPT), ?assertEqual('CONNACK_ACCEPT', emqttd_packet:connack_name(?CONNACK_ACCEPT)),
'CONNACK_PROTO_VER' = emqttd_packet:connack_name(?CONNACK_PROTO_VER), ?assertEqual('CONNACK_PROTO_VER', emqttd_packet:connack_name(?CONNACK_PROTO_VER)),
'CONNACK_INVALID_ID' = emqttd_packet:connack_name(?CONNACK_INVALID_ID), ?assertEqual('CONNACK_INVALID_ID', emqttd_packet:connack_name(?CONNACK_INVALID_ID)),
'CONNACK_SERVER' = emqttd_packet:connack_name(?CONNACK_SERVER), ?assertEqual('CONNACK_SERVER', emqttd_packet:connack_name(?CONNACK_SERVER)),
'CONNACK_CREDENTIALS' = emqttd_packet:connack_name(?CONNACK_CREDENTIALS), ?assertEqual('CONNACK_CREDENTIALS', emqttd_packet:connack_name(?CONNACK_CREDENTIALS)),
'CONNACK_AUTH' = emqttd_packet:connack_name(?CONNACK_AUTH). ?assertEqual('CONNACK_AUTH', emqttd_packet:connack_name(?CONNACK_AUTH)).
packet_format(_) -> packet_format(_) ->
io:format("~s", [emqttd_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{}))]), io:format("~s", [emqttd_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{}))]),
@ -336,26 +338,25 @@ packet_format(_) ->
message_make(_) -> message_make(_) ->
Msg = emqttd_message:make(<<"clientid">>, <<"topic">>, <<"payload">>), Msg = emqttd_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
0 = Msg#mqtt_message.qos, ?assertEqual(0, Msg#mqtt_message.qos),
Msg1 = emqttd_message:make(<<"clientid">>, qos2, <<"topic">>, <<"payload">>), Msg1 = emqttd_message:make(<<"clientid">>, qos2, <<"topic">>, <<"payload">>),
true = is_binary(Msg1#mqtt_message.id), ?assert(is_binary(Msg1#mqtt_message.id)),
2 = Msg1#mqtt_message.qos. ?assertEqual(2, Msg1#mqtt_message.qos).
message_from_packet(_) -> message_from_packet(_) ->
Msg = emqttd_message:from_packet(?PUBLISH_PACKET(1, <<"topic">>, 10, <<"payload">>)), Msg = emqttd_message:from_packet(?PUBLISH_PACKET(1, <<"topic">>, 10, <<"payload">>)),
1 = Msg#mqtt_message.qos, ?assertEqual(1, Msg#mqtt_message.qos),
10 = Msg#mqtt_message.pktid, ?assertEqual(10, Msg#mqtt_message.pktid),
<<"topic">> = Msg#mqtt_message.topic, ?assertEqual(<<"topic">>, Msg#mqtt_message.topic),
WillMsg = emqttd_message:from_packet(#mqtt_packet_connect{will_flag = true, WillMsg = emqttd_message:from_packet(#mqtt_packet_connect{will_flag = true,
will_topic = <<"WillTopic">>, will_topic = <<"WillTopic">>,
will_msg = <<"WillMsg">>}), will_msg = <<"WillMsg">>}),
<<"WillTopic">> = WillMsg#mqtt_message.topic, ?assertEqual(<<"WillTopic">>, WillMsg#mqtt_message.topic),
<<"WillMsg">> = WillMsg#mqtt_message.payload, ?assertEqual(<<"WillMsg">>, WillMsg#mqtt_message.payload),
Msg2 = emqttd_message:from_packet(<<"username">>, <<"clientid">>, Msg2 = emqttd_message:from_packet(<<"username">>, <<"clientid">>,
?PUBLISH_PACKET(1, <<"topic">>, 20, <<"payload">>)), ?PUBLISH_PACKET(1, <<"topic">>, 20, <<"payload">>)),
{<<"clientid">>, <<"username">>} = Msg2#mqtt_message.from, ?assertEqual({<<"clientid">>, <<"username">>}, Msg2#mqtt_message.from),
io:format("~s", [emqttd_message:format(Msg2)]). io:format("~s", [emqttd_message:format(Msg2)]).
message_flag(_) -> message_flag(_) ->
@ -363,13 +364,13 @@ message_flag(_) ->
Msg2 = emqttd_message:from_packet(<<"clientid">>, Pkt), Msg2 = emqttd_message:from_packet(<<"clientid">>, Pkt),
Msg3 = emqttd_message:set_flag(retain, Msg2), Msg3 = emqttd_message:set_flag(retain, Msg2),
Msg4 = emqttd_message:set_flag(dup, Msg3), Msg4 = emqttd_message:set_flag(dup, Msg3),
true = Msg4#mqtt_message.dup, ?assert(Msg4#mqtt_message.dup),
true = Msg4#mqtt_message.retain, ?assert(Msg4#mqtt_message.retain),
Msg5 = emqttd_message:set_flag(Msg4), Msg5 = emqttd_message:set_flag(Msg4),
Msg6 = emqttd_message:unset_flag(dup, Msg5), Msg6 = emqttd_message:unset_flag(dup, Msg5),
Msg7 = emqttd_message:unset_flag(retain, Msg6), Msg7 = emqttd_message:unset_flag(retain, Msg6),
false = Msg7#mqtt_message.dup, ?assertNot(Msg7#mqtt_message.dup),
false = Msg7#mqtt_message.retain, ?assertNot(Msg7#mqtt_message.retain),
emqttd_message:unset_flag(Msg7), emqttd_message:unset_flag(Msg7),
emqttd_message:to_packet(Msg7). emqttd_message:to_packet(Msg7).

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2016-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.