Merge branch 'emq20' of https://github.com/emqtt/emqttd into emq20

This commit is contained in:
huangdan 2016-09-28 11:21:38 +08:00
commit 188dcb2645
3 changed files with 37 additions and 8 deletions

View File

@ -33,7 +33,7 @@
is_subscribed/2, subscriber_down/1]). is_subscribed/2, subscriber_down/1]).
%% Hooks API %% Hooks API
-export([hook/4, hook/3, unhook/2, run_hooks/3]). -export([hook/4, hook/3, unhook/2, run_hooks/2, run_hooks/3]).
%% Debug API %% Debug API
-export([dump/0]). -export([dump/0]).
@ -151,6 +151,10 @@ hook(Hook, Function, InitArgs, Priority) ->
unhook(Hook, Function) -> unhook(Hook, Function) ->
emqttd_hook:delete(Hook, Function). emqttd_hook:delete(Hook, Function).
-spec(run_hooks(atom(), list(any())) -> ok | stop).
run_hooks(Hook, Args) ->
emqttd_hook: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_hook:run(Hook, Args, Acc).

View File

@ -24,7 +24,7 @@
-export([start_link/0]). -export([start_link/0]).
%% Hooks API %% Hooks API
-export([add/3, add/4, delete/2, run/3, lookup/1]). -export([add/3, add/4, delete/2, run/2, run/3, lookup/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,
@ -63,10 +63,26 @@ add(HookPoint, Function, InitArgs, Priority) ->
delete(HookPoint, Function) -> delete(HookPoint, Function) ->
gen_server:call(?MODULE, {delete, HookPoint, Function}). gen_server:call(?MODULE, {delete, HookPoint, Function}).
-spec(run(atom(), list(any()), any()) -> any()). %% @doc Run hooks without Acc.
-spec(run(atom(), list(Arg :: any())) -> ok | stop).
run(HookPoint, Args) ->
run_(lookup(HookPoint), Args).
-spec(run(atom(), list(Arg :: any()), any()) -> any()).
run(HookPoint, Args, Acc) -> run(HookPoint, Args, Acc) ->
run_(lookup(HookPoint), Args, Acc). run_(lookup(HookPoint), Args, Acc).
%% @private
run_([#callback{function = Fun, init_args = InitArgs} | Callbacks], Args) ->
case apply(Fun, lists:append([Args, InitArgs])) of
ok -> run_(Callbacks, Args);
stop -> stop;
_Any -> run_(Callbacks, Args)
end;
run_([], _Args) ->
ok.
%% @private %% @private
run_([#callback{function = Fun, init_args = InitArgs} | Callbacks], Args, Acc) -> run_([#callback{function = Fun, init_args = InitArgs} | Callbacks], Args, Acc) ->
case apply(Fun, lists:append([Args, [Acc], InitArgs])) of case apply(Fun, lists:append([Args, [Acc], InitArgs])) of

View File

@ -337,11 +337,16 @@ add_delete_hook(_) ->
[] = emqttd_hook:lookup(emqttd_hook). [] = emqttd_hook:lookup(emqttd_hook).
run_hooks(_) -> run_hooks(_) ->
emqttd:hook(test_hook, fun ?MODULE:hook_fun3/4, [init]), emqttd:hook(foldl_hook, fun ?MODULE:hook_fun3/4, [init]),
emqttd:hook(test_hook, fun ?MODULE:hook_fun4/4, [init]), emqttd:hook(foldl_hook, fun ?MODULE:hook_fun4/4, [init]),
emqttd:hook(test_hook, fun ?MODULE:hook_fun5/4, [init]), emqttd:hook(foldl_hook, fun ?MODULE:hook_fun5/4, [init]),
{stop, [r3, r2]} = emqttd:run_hooks(test_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]),
emqttd:hook(foreach_hook, fun ?MODULE:hook_fun7/2, [initArg]),
emqttd:hook(foreach_hook, fun ?MODULE:hook_fun8/2, [initArg]),
stop = emqttd:run_hooks(foreach_hook, [arg]).
hook_fun1([]) -> ok. hook_fun1([]) -> ok.
hook_fun2([]) -> {ok, []}. hook_fun2([]) -> {ok, []}.
@ -350,6 +355,10 @@ hook_fun3(arg1, arg2, _Acc, init) -> ok.
hook_fun4(arg1, arg2, Acc, init) -> {ok, [r2 | Acc]}. hook_fun4(arg1, arg2, Acc, init) -> {ok, [r2 | Acc]}.
hook_fun5(arg1, arg2, Acc, init) -> {stop, [r3 | Acc]}. hook_fun5(arg1, arg2, Acc, init) -> {stop, [r3 | Acc]}.
hook_fun6(arg, initArg) -> ok.
hook_fun7(arg, initArg) -> any.
hook_fun8(arg, initArg) -> stop.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% HTTP Request Test %% HTTP Request Test
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------