emqttd_node.erl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2012-2016 Feng Lee <feng@emqtt.io>.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqttd_node).
  17. -import(lists, [concat/1]).
  18. -export([is_aliving/1, parse_name/1]).
  19. %% @doc Is node aliving
  20. -spec is_aliving(node()) -> boolean().
  21. is_aliving(Node) ->
  22. case net_adm:ping(Node) of
  23. pong -> true;
  24. pang -> false
  25. end.
  26. %% @doc Parse node name
  27. -spec parse_name(string()) -> atom().
  28. parse_name(Name) when is_list(Name) ->
  29. case string:tokens(Name, "@") of
  30. [_Node, _Host] -> list_to_atom(Name);
  31. _ -> with_host(Name)
  32. end.
  33. with_host(Name) ->
  34. [_, Host] = string:tokens(atom_to_list(node()), "@"),
  35. list_to_atom(concat([Name, "@", Host])).