emqttd_mock_client.erl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. -module(emqttd_mock_client).
  2. -behaviour(gen_server).
  3. %% ------------------------------------------------------------------
  4. %% API Function Exports
  5. %% ------------------------------------------------------------------
  6. -export([start_link/1, start_session/1, stop/1]).
  7. %% ------------------------------------------------------------------
  8. %% gen_server Function Exports
  9. %% ------------------------------------------------------------------
  10. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  11. terminate/2, code_change/3]).
  12. -record(state, {clientid, session}).
  13. %% ------------------------------------------------------------------
  14. %% API Function Definitions
  15. %% ------------------------------------------------------------------
  16. start_link(ClientId) ->
  17. gen_server:start_link(?MODULE, [ClientId], []).
  18. start_session(CPid) ->
  19. gen_server:call(CPid, start_session).
  20. stop(CPid) ->
  21. gen_server:call(CPid, stop).
  22. %% ------------------------------------------------------------------
  23. %% gen_server Function Definitions
  24. %% ------------------------------------------------------------------
  25. init([ClientId]) ->
  26. {ok, #state{clientid = ClientId}}.
  27. handle_call(start_session, _From, State = #state{clientid = ClientId}) ->
  28. {ok, SessPid, _} = emqttd_sm:start_session(true, {ClientId, undefined}),
  29. {reply, {ok, SessPid}, State#state{session = SessPid}};
  30. handle_call(stop, _From, State) ->
  31. {stop, normal, ok, State};
  32. handle_call(_Request, _From, State) ->
  33. {reply, ok, State}.
  34. handle_cast(_Msg, State) ->
  35. {noreply, State}.
  36. handle_info(_Info, State) ->
  37. {noreply, State}.
  38. terminate(_Reason, _State) ->
  39. ok.
  40. code_change(_OldVsn, State, _Extra) ->
  41. {ok, State}.